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 b1903cb269..fd73df5ab8 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 @@ -102,6 +102,26 @@ 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 '" + @@ -114,6 +134,11 @@ 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(); } } @@ -232,6 +257,25 @@ 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 { @@ -242,6 +286,11 @@ 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(); } } @@ -535,6 +584,25 @@ 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 + 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.OK).entity(operation).build(); } catch (OperationManagementException e) { String errorMessage = "Issue in retrieving operation management service instance"; @@ -544,6 +612,11 @@ 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(); } } @@ -616,8 +689,26 @@ 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(); @@ -626,6 +717,11 @@ 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(); } } @@ -638,6 +734,8 @@ 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)) { @@ -650,6 +748,27 @@ 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"; @@ -659,6 +778,11 @@ 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(); } }