diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java index fc0a74a9c0..193c777560 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java @@ -18,12 +18,6 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -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.ValueRange; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -62,10 +56,8 @@ import org.wso2.carbon.user.api.UserStoreException; import javax.validation.Valid; import javax.ws.rs.*; import javax.ws.rs.core.Response; -import java.io.FileInputStream; -import java.io.IOException; import java.rmi.RemoteException; -import java.security.GeneralSecurityException; + import java.util.*; @Path("/device/agent") @@ -123,26 +115,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceId); pap.publishChanges(); - - // Save the response time to Google Spreadsheet - long startTime = System.currentTimeMillis(); // Record the start time - Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials - String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; - - // Create the values to be written - List> values = Collections.singletonList( - Arrays.asList(device.getDeviceIdentifier(), device.getType(), String.valueOf(System.currentTimeMillis() - startTime)) - ); - - // Create the value range - ValueRange body = new ValueRange().setValues(values); - - // Write the values to the spreadsheet - sheetsService.spreadsheets().values() - .append(spreadsheetId, "Sheet1", body) - .setValueInputOption("USER_ENTERED") - .execute(); - return Response.status(Response.Status.OK).entity(status).build(); } catch (DeviceManagementException e) { String msg = "Error occurred while enrolling the device, which carries the id '" + @@ -155,11 +127,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { } catch (PolicyManagementException e) { log.error("failed to add designated policies against newly enrolled device.", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (IOException e) { - // Handle any errors occurred while writing to the spreadsheet - String errorMessage = "Error writing response time to Google Spreadsheet"; - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } } @@ -169,29 +136,8 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { public Response disEnrollDevice(@PathParam("type") String type, @PathParam("id") String id) { boolean result; DeviceIdentifier deviceIdentifier = new DeviceIdentifier(id, type); - long startTime = System.currentTimeMillis(); // Record the start time try { result = DeviceMgtAPIUtils.getDeviceManagementService().disenrollDevice(deviceIdentifier); - long endTime = System.currentTimeMillis(); // Record the end time - long responseTime = endTime - startTime; // Calculate the response time - - // Save the response time to Google Spreadsheet - Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials - String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; - - // Create the values to be written - List> values = Collections.singletonList( - Arrays.asList(id, type, String.valueOf(responseTime)) - ); - - // Create the value range - ValueRange body = new ValueRange().setValues(values); - - // Write the values to the spreadsheet - sheetsService.spreadsheets().values() - .append(spreadsheetId, "Sheet1", body) - .setValueInputOption("USER_ENTERED") - .execute(); if (result) { return Response.status(Response.Status.OK).build(); } else { @@ -202,11 +148,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { String msg = "Error occurred while enrolling the device, which carries the id '" + id + "'"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (IOException e) { - // Handle any errors occurred while writing to the spreadsheet - String errorMessage = "Error writing response time to Google Spreadsheet"; - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } } @@ -278,25 +219,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { try { device.setType(type); result = DeviceMgtAPIUtils.getDeviceManagementService().modifyEnrollment(device); - - // Save the response time to Google Spreadsheet - long startTime = System.currentTimeMillis(); // Record the start time - Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials - String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; - - // Create the values to be written - List> values = Collections.singletonList( - Arrays.asList(id, type, String.valueOf(System.currentTimeMillis() - startTime)) - ); - - // Create the value range - ValueRange body = new ValueRange().setValues(values); - - // Write the values to the spreadsheet - sheetsService.spreadsheets().values() - .append(spreadsheetId, "Sheet1", body) - .setValueInputOption("USER_ENTERED") - .execute(); if (result) { return Response.status(Response.Status.ACCEPTED).build(); } else { @@ -307,11 +229,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { id + "'"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (IOException e) { - // Handle any errors occurred while writing to the spreadsheet - String errorMessage = "Error writing response time to Google Spreadsheet"; - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } } @@ -538,27 +455,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { OperationList operationsList = new OperationList(); operationsList.setList(operations); operationsList.setCount(operations.size()); - - long endTime = System.currentTimeMillis(); // Record the end time - long responseTime = endTime - startTime; // Calculate the response time - - // Save the response time to Google Spreadsheet - Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials - String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; //Update with Spreadsheet ID - - // Create the values to be written - List> values = Collections.singletonList( - Arrays.asList(deviceId, String.valueOf(responseTime)) - ); - - // Create the value range - ValueRange body = new ValueRange().setValues(values); - - // Write the values to the spreadsheet - sheetsService.spreadsheets().values() - .append(spreadsheetId, "Sheet1", body) - .setValueInputOption("USER_ENTERED") - .execute(); + return Response.status(Response.Status.OK).entity(operationsList).build(); } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; @@ -568,23 +465,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); - } catch (IOException e) { - // Handle any errors occurred while writing to the spreadsheet - String errorMessage = "Error writing response time to Google Spreadsheet"; - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); - } - } - - 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); } } @@ -605,32 +485,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { } Operation operation = DeviceMgtAPIUtils.getDeviceManagementService().getNextPendingOperation( deviceIdentifier); - // Save the response time to Google Spreadsheet - long startTime = System.currentTimeMillis(); // Record the start time - - // Create Sheets service using loaded credentials - Sheets sheetsService = createSheetsService(); - - // Spreadsheet ID - String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; - - // Calculate the response time - long endTime = System.currentTimeMillis(); // Record the end time - long responseTime = endTime - startTime; // Calculate the response time - - // Create the values to be written - List> values = Collections.singletonList( - Arrays.asList(deviceId, type, String.valueOf(responseTime)) - ); - - // Create the value range - ValueRange body = new ValueRange().setValues(values); - - // Write the values to the spreadsheet - sheetsService.spreadsheets().values() - .append(spreadsheetId, "Sheet1", body) - .setValueInputOption("USER_ENTERED") - .execute(); return Response.status(Response.Status.OK).entity(operation).build(); } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; @@ -640,11 +494,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { String errorMessage = "Issue in retrieving deivce management service instance"; log.error(errorMessage, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); - } catch (IOException e) { - // Handle any errors occurred while writing to the spreadsheet - String errorMessage = "Error writing response time to Google Spreadsheet"; - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } } @@ -717,26 +566,8 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { log.error(msg); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } - long startTime = System.currentTimeMillis(); // Record the start time if (DeviceMgtAPIUtils.getDeviceManagementService().updateProperties(deviceIdentifier, properties)) { - // Save the response time to Google Spreadsheet - Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials - String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; - - // Create the values to be written - List> values = Collections.singletonList( - Arrays.asList(deviceId, type, String.valueOf(System.currentTimeMillis() - startTime)) - ); - - // Create the value range - ValueRange body = new ValueRange().setValues(values); - - // Write the values to the spreadsheet - sheetsService.spreadsheets().values() - .append(spreadsheetId, "Sheet1", body) - .setValueInputOption("USER_ENTERED") - .execute(); return Response.status(Response.Status.ACCEPTED).entity("Device properties updated.").build(); } else { return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Device properties not updated.").build(); @@ -745,11 +576,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { String errorMessage = "Issue in retrieving device management service instance"; log.error(errorMessage, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); - } catch (IOException e) { - // Handle any errors occurred while writing to the spreadsheet - String errorMessage = "Error writing response time to Google Spreadsheet"; - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } } @@ -762,8 +588,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { log.error(errorMessage); return Response.status(Response.Status.BAD_REQUEST).build(); } - long startTime = System.currentTimeMillis(); // Record the start time - try { if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) { @@ -776,27 +600,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { OperationList operationsList = new OperationList(); operationsList.setList(operations); operationsList.setCount(operations.size()); - - long endTime = System.currentTimeMillis(); // Record the end time - long responseTime = endTime - startTime; // Calculate the response time - - // Save the response time to Google Spreadsheet - Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials - String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; - - // Create the values to be written - List> values = Collections.singletonList( - Arrays.asList(deviceId, type, status.toString(), String.valueOf(responseTime)) - ); - - // Create the value range - ValueRange body = new ValueRange().setValues(values); - - // Write the values to the spreadsheet - sheetsService.spreadsheets().values() - .append(spreadsheetId, "Sheet1", body) - .setValueInputOption("USER_ENTERED") - .execute(); return Response.status(Response.Status.OK).entity(operationsList).build(); } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; @@ -806,11 +609,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { String errorMessage = "Issue in retrieving device management service"; log.error(errorMessage, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); - } catch (IOException e) { - // Handle any errors occurred while writing to the spreadsheet - String errorMessage = "Error writing response time to Google Spreadsheet"; - log.error(errorMessage, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); } } } 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 804b4b88e0..e203633af5 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 @@ -156,6 +156,35 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } } + private void saveResponseTimeToSpreadsheet(long startTime, long endTime, long responseTime) throws IOException, GeneralSecurityException { + // Load credentials from JSON file + GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("/home/entgra/MyProject/device-mgt-core/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:C"; + + // Prepare the values to be written + List rowValues = Arrays.asList(startTime, endTime, responseTime); + List> values = Collections.singletonList(rowValues); + + // 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}/status") @Override @@ -462,17 +491,29 @@ 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; + + // Save the response time, start time, and end time to Google Spreadsheet + saveResponseTimeToSpreadsheet(startTime, endTime, responseTime); + return Response.status(Response.Status.OK).entity(response).build(); } catch (DeviceManagementException e) { String msg = "Error encountered while deleting 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(); } } @@ -489,7 +530,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { Device persistedDevice = deviceManagementProviderService.getDevice(new DeviceIdentifier (deviceId, deviceType), true); persistedDevice.setName(device.getName()); - System.out.println("This is renamed device"); + System.out.println("This is rename device"); boolean responseOfmodifyEnrollment = deviceManagementProviderService.modifyEnrollment(persistedDevice); boolean responseOfDeviceNameChanged = deviceManagementProviderService.sendDeviceNameChangedNotification( persistedDevice); @@ -499,8 +540,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { long endTime = System.currentTimeMillis(); long responseTime = endTime - startTime; - // Save the response time to Google Spreadsheet - saveResponseTimeToSpreadsheet(responseTime); + // Save the response time, start time, and end time to Google Spreadsheet + saveResponseTimeToSpreadsheet(startTime, endTime, responseTime); return Response.status(Response.Status.CREATED).entity(response).build(); } catch (DeviceManagementException e) { @@ -514,33 +555,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } } - 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:C"; - - // 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}")