diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml index 95e08b9712..13334db65b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml @@ -480,5 +480,10 @@ google-api-services-sheets v4-rev20230227-2.0.0 + + com.lazerycode.jmeter + jmeter-maven-plugin + 2.8.6 + \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 3d13846272..16bbedb02e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -41,7 +41,6 @@ import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.sheets.v4.Sheets; import com.google.api.services.sheets.v4.SheetsScopes; -import com.google.api.services.sheets.v4.model.AppendValuesResponse; import com.google.api.services.sheets.v4.model.ValueRange; import com.google.gson.Gson; import io.entgra.application.mgt.common.ApplicationInstallResponse; @@ -130,8 +129,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; private static final Log log = LogFactory.getLog(DeviceManagementServiceImpl.class); - private static final String SPREADSHEET_ID = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; - private static final String RANGE = "Sheet1!A:B"; /** * Validate group Id and group Id greater than 0 and exist. @@ -457,7 +454,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } } - @DELETE @Override @Path("/type/{deviceType}/id/{deviceId}") @@ -466,25 +462,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService(); try { - long startTime = System.currentTimeMillis(); // Start measuring response time - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, true); if (persistedDevice == null) { return Response.status(Response.Status.NOT_FOUND).build(); } boolean response = deviceManagementProviderService.disenrollDevice(deviceIdentifier); - - long endTime = System.currentTimeMillis(); // End measuring response time - long responseTime = endTime - startTime; - - saveResponseTimeToGoogleSheet(responseTime); // Save response time to Google Sheet - - if (response) { - String successMessage = "Device of type '" + deviceType + "' with ID '" + deviceId + "' has been deleted."; - System.out.println(successMessage); // Print success message - } - return Response.status(Response.Status.OK).entity(response).build(); } catch (DeviceManagementException e) { String msg = "Error encountered while deleting requested device of type : " + deviceType; @@ -493,41 +476,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } } - private void saveResponseTimeToGoogleSheet(long responseTime) { - try { - Sheets sheetsService = createSheetsService(); - - // Prepare the data to be written - ValueRange requestBody = new ValueRange().setValues(Collections.singletonList( - Arrays.asList(System.currentTimeMillis(), responseTime) - )); - - // Write the data to the spreadsheet - AppendValuesResponse appendResponse = sheetsService.spreadsheets().values() - .append(SPREADSHEET_ID, RANGE, requestBody) - .setValueInputOption("RAW") - .execute(); - - System.out.println("Response Time saved to Google Sheet. Updated rows: " + appendResponse.getUpdates().getUpdatedRows()); - } catch (IOException e) { - e.printStackTrace(); - // Handle any exceptions that occur during the process - } - } - - private Sheets createSheetsService() throws IOException { - GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/apicall-382608-48aa6a62800d.json")) - .createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS)); - try { - return new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), credential) - .setApplicationName("ApiCall") - .build(); - } catch (GeneralSecurityException e) { - throw new RuntimeException(e); - } - } - - @POST @Override @Path("/type/{deviceType}/id/{deviceId}/rename") @@ -535,6 +483,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @PathParam("deviceId") String deviceId) { DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService(); try { + // Start measuring API call response time + long startTime = System.currentTimeMillis(); + Device persistedDevice = deviceManagementProviderService.getDevice(new DeviceIdentifier (deviceId, deviceType), true); persistedDevice.setName(device.getName()); @@ -544,14 +495,53 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { persistedDevice); boolean response = responseOfmodifyEnrollment && responseOfDeviceNameChanged; + // End measuring API call response time + long endTime = System.currentTimeMillis(); + long responseTime = endTime - startTime; + + // Save the response time to Google Spreadsheet + saveResponseTimeToSpreadsheet(responseTime); + return Response.status(Response.Status.CREATED).entity(response).build(); } catch (DeviceManagementException e) { String msg = "Error encountered while updating requested device of type : " + deviceType; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } catch (IOException | GeneralSecurityException e) { + String msg = "Error encountered while saving response time to spreadsheet"; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } + private void saveResponseTimeToSpreadsheet(long responseTime) throws IOException, GeneralSecurityException { + // Load credentials from JSON file + GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/apicall-382608-48aa6a62800d.json")) + .createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS)); + + // Build Google Sheets service + Sheets sheetsService = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), + JacksonFactory.getDefaultInstance(), credential) + .setApplicationName("ApiCall") + .build(); + + // Define the spreadsheet ID and range + String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; + String range = "Sheet1!A:B"; // Assuming column A for timestamps and column B for response times + + // Prepare the values to be written + List> values = Collections.singletonList(Collections.singletonList(responseTime)); + + // Build the value range object + ValueRange body = new ValueRange().setValues(values); + + // Write the response time value to the spreadsheet + sheetsService.spreadsheets().values() + .append(spreadsheetId, range, body) + .setValueInputOption("USER_ENTERED") + .execute(); + } + @GET @Path("/{type}/{id}") @Override