From be786e1a78f36f4a7b338abcd2777d4e7e98ff8b Mon Sep 17 00:00:00 2001 From: akeela_azhar Date: Fri, 12 May 2023 11:54:18 +0530 Subject: [PATCH] Update Google sheet response time files --- .../org.wso2.carbon.device.mgt.api/pom.xml | 5 + .../jaxrs/service/impl/ApiResponseTime.java | 216 +++++++++--------- .../service/impl/DeviceAgentServiceImpl.java | 2 +- .../mgt/jaxrs/service/impl/GoogleSheet.java | 68 +++--- ...PendingOperationsResourceResponseTime.java | 102 +++++++++ 5 files changed, 250 insertions(+), 143 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PendingOperationsResourceResponseTime.java 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 fe2e75c0da..b859b99db7 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 @@ -465,5 +465,10 @@ google-api-services-sheets v4-rev539-1.25.0 + + com.google.http-client + google-http-client-jackson2 + 1.43.1 + \ 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/ApiResponseTime.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ApiResponseTime.java index 021bbaf559..65d9a46bb2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ApiResponseTime.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ApiResponseTime.java @@ -1,108 +1,108 @@ -package org.wso2.carbon.device.mgt.jaxrs.service.impl; - - -import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; -import com.google.api.client.googleapis.json.GoogleJsonError; -import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.javanet.NetHttpTransport; -import com.google.api.services.sheets.v4.Sheets; -import com.google.api.services.sheets.v4.model.AppendValuesResponse; -import com.google.api.services.sheets.v4.model.ValueRange; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestTemplate; - -import java.io.IOException; -import java.net.URISyntaxException; -import java.security.GeneralSecurityException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.osgi.service.application.ApplicationDescriptor.APPLICATION_NAME; -import static org.wso2.carbon.device.mgt.jaxrs.service.impl.GoogleSheet.JSON_FACTORY; -import static org.wso2.carbon.device.mgt.jaxrs.service.impl.GoogleSheet.getCredentials; - -@SuppressWarnings("ALL") -@RestController -public class ApiResponseTime { - - public static Object getstartTime() { - return null; - } - - public static Object getendTime() { - return null; - } - - public static Object getelapsedTime() { - return null; - } - - public static void appendDetails(long startTime, long endTime, long elapsedTime) - throws GeneralSecurityException, IOException, URISyntaxException { - final String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; - final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); - - Sheets service = - new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) - .setApplicationName(APPLICATION_NAME) - .build(); - - // Create a list of values to append to the sheet - List newList1 = new ArrayList<>(Arrays.asList(startTime, endTime, elapsedTime)); - List> values = new ArrayList<>(Arrays.asList(newList1)); - - // Append the values to the sheet - AppendValuesResponse result = null; - try { - ValueRange valueRange = new ValueRange().setValues(values); - result = service.spreadsheets().values().append(spreadsheetId, "A2", valueRange) - .setValueInputOption("RAW").execute(); - System.out.printf("%d cells appended.", result.getUpdates().getUpdatedCells()); - } catch (GoogleJsonResponseException e) { - GoogleJsonError error = e.getDetails(); - if (((GoogleJsonError) error).getCode() == 404) { - System.out.printf("Spreadsheet not found with id '%s'.\n", spreadsheetId); - } else { - throw e; - } - } - } - - @GetMapping("/my-api") - public String myApi() { - // Set the URL for the API call - String url = "/next-pending/operation/{type}/{id}"; - - try { - // Record the start time - long startTime = System.currentTimeMillis(); - - // Make the API call using the RestTemplate - RestTemplate restTemplate = new RestTemplate(); - String response = restTemplate.getForObject(url, String.class); - - // Record the end time - long endTime = System.currentTimeMillis(); - - // Calculate the elapsed time - long elapsedTime = endTime - startTime; - - // Print the elapsed time in milliseconds - System.out.println("Start Time: " + startTime + " ms"); - System.out.println("End Time: " + endTime + " ms"); - System.out.println("Elapsed time: " + elapsedTime + " ms"); - - // Append the API call details to a Google Sheet - appendDetails(startTime, endTime, elapsedTime); - - return response; - - } catch (Exception e) { - e.printStackTrace(); - return "Error occurred: " + e.getMessage(); - } - } -} - +//package org.wso2.carbon.device.mgt.jaxrs.service.impl; +// +// +//import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +//import com.google.api.client.googleapis.json.GoogleJsonError; +//import com.google.api.client.googleapis.json.GoogleJsonResponseException; +//import com.google.api.client.http.javanet.NetHttpTransport; +//import com.google.api.services.sheets.v4.Sheets; +//import com.google.api.services.sheets.v4.model.AppendValuesResponse; +//import com.google.api.services.sheets.v4.model.ValueRange; +//import org.springframework.web.bind.annotation.GetMapping; +//import org.springframework.web.bind.annotation.RestController; +//import org.springframework.web.client.RestTemplate; +// +//import java.io.IOException; +//import java.net.URISyntaxException; +//import java.security.GeneralSecurityException; +//import java.util.ArrayList; +//import java.util.Arrays; +//import java.util.List; +// +//import static org.osgi.service.application.ApplicationDescriptor.APPLICATION_NAME; +//import static org.wso2.carbon.device.mgt.jaxrs.service.impl.GoogleSheet.JSON_FACTORY; +//import static org.wso2.carbon.device.mgt.jaxrs.service.impl.GoogleSheet.getCredentials; +// +//@SuppressWarnings("ALL") +//@RestController +//public class ApiResponseTime { +// +// public static Object getstartTime() { +// return null; +// } +// +// public static Object getendTime() { +// return null; +// } +// +// public static Object getelapsedTime() { +// return null; +// } +// +// public static void appendDetails(long startTime, long endTime, long elapsedTime) +// throws GeneralSecurityException, IOException, URISyntaxException { +// final String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; +// final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); +// +// Sheets service = +// new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) +// .setApplicationName(APPLICATION_NAME) +// .build(); +// +// // Create a list of values to append to the sheet +// List newList1 = new ArrayList<>(Arrays.asList(startTime, endTime, elapsedTime)); +// List> values = new ArrayList<>(Arrays.asList(newList1)); +// +// // Append the values to the sheet +// AppendValuesResponse result = null; +// try { +// ValueRange valueRange = new ValueRange().setValues(values); +// result = service.spreadsheets().values().append(spreadsheetId, "A2", valueRange) +// .setValueInputOption("RAW").execute(); +// System.out.printf("%d cells appended.", result.getUpdates().getUpdatedCells()); +// } catch (GoogleJsonResponseException e) { +// GoogleJsonError error = e.getDetails(); +// if (((GoogleJsonError) error).getCode() == 404) { +// System.out.printf("Spreadsheet not found with id '%s'.\n", spreadsheetId); +// } else { +// throw e; +// } +// } +// } +// +// @GetMapping("/my-api") +// public String myApi() { +// // Set the URL for the API call +// String url = "/next-pending/operation/{type}/{id}"; +// +// try { +// // Record the start time +// long startTime = System.currentTimeMillis(); +// +// // Make the API call using the RestTemplate +// RestTemplate restTemplate = new RestTemplate(); +// String response = restTemplate.getForObject(url, String.class); +// +// // Record the end time +// long endTime = System.currentTimeMillis(); +// +// // Calculate the elapsed time +// long elapsedTime = endTime - startTime; +// +// // Print the elapsed time in milliseconds +// System.out.println("Start Time: " + startTime + " ms"); +// System.out.println("End Time: " + endTime + " ms"); +// System.out.println("Elapsed time: " + elapsedTime + " ms"); +// +// // Append the API call details to a Google Sheet +// appendDetails(startTime, endTime, elapsedTime); +// +// return response; +// +// } catch (Exception e) { +// e.printStackTrace(); +// return "Error occurred: " + e.getMessage(); +// } +// } +//} +// 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 ed57498164..774316c0d7 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 @@ -69,7 +69,7 @@ import java.util.Map; @Path("/device/agent") public class DeviceAgentServiceImpl implements DeviceAgentService { - private static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class); + static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class); private static final String POLICY_MONITOR = "POLICY_MONITOR"; @POST @Path("/enroll") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GoogleSheet.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GoogleSheet.java index ce70894d11..c3bb8bb306 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GoogleSheet.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GoogleSheet.java @@ -1,34 +1,34 @@ -package org.wso2.carbon.device.mgt.jaxrs.service.impl; - -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.javanet.NetHttpTransport; -import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.gson.GsonFactory; -import com.google.api.services.sheets.v4.SheetsScopes; - -import java.io.File; -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -public class GoogleSheet { - - private static final String APPLICATION_NAME = "ApiCall"; - static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); - private static final String TOKENS_DIRECTORY_PATH = "tokens/path"; - private static final List SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS,SheetsScopes.DRIVE); - - static GoogleCredential getCredentials(final NetHttpTransport HTTP_TRANSPORT) - throws IOException, GeneralSecurityException { - GoogleCredential credential = new GoogleCredential.Builder() - .setTransport(HTTP_TRANSPORT) - .setJsonFactory(JSON_FACTORY) - .setServiceAccountId("apicall@apicall-382608.iam.gserviceaccount.com") - .setServiceAccountPrivateKeyFromP12File(new File("/home/entgra/MyProject/device-mgt-core/components/device-mgt/org.wso2.carbon.device.mgt.api/target/credentials.p12")).setServiceAccountScopes(Collections.singleton(SheetsScopes.SPREADSHEETS)).build(); - credential.refreshToken(); - return credential; - } - -} +//package org.wso2.carbon.device.mgt.jaxrs.service.impl; +// +//import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +//import com.google.api.client.http.javanet.NetHttpTransport; +//import com.google.api.client.json.JsonFactory; +//import com.google.api.client.json.gson.GsonFactory; +//import com.google.api.services.sheets.v4.SheetsScopes; +// +//import java.io.File; +//import java.io.IOException; +//import java.security.GeneralSecurityException; +//import java.util.Arrays; +//import java.util.Collections; +//import java.util.List; +// +//public class GoogleSheet { +// +// private static final String APPLICATION_NAME = "ApiCall"; +// static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); +// private static final String TOKENS_DIRECTORY_PATH = "tokens/path"; +// private static final List SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS,SheetsScopes.DRIVE); +// +// static GoogleCredential getCredentials(final NetHttpTransport HTTP_TRANSPORT) +// throws IOException, GeneralSecurityException { +// GoogleCredential credential = new GoogleCredential.Builder() +// .setTransport(HTTP_TRANSPORT) +// .setJsonFactory(JSON_FACTORY) +// .setServiceAccountId("apicall@apicall-382608.iam.gserviceaccount.com") +// .setServiceAccountPrivateKeyFromP12File(new File("/home/entgra/MyProject/device-mgt-core/components/device-mgt/org.wso2.carbon.device.mgt.api/target/credentials.p12")).setServiceAccountScopes(Collections.singleton(SheetsScopes.SPREADSHEETS)).build(); +// credential.refreshToken(); +// return credential; +// } +// +//} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PendingOperationsResourceResponseTime.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PendingOperationsResourceResponseTime.java new file mode 100644 index 0000000000..516efe6ed2 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PendingOperationsResourceResponseTime.java @@ -0,0 +1,102 @@ +package org.wso2.carbon.device.mgt.jaxrs.service.impl; + +import com.google.api.client.auth.oauth2.Credential; +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.json.JsonFactory; +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 javax.annotation.PreDestroy; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.Collections; + +import static org.wso2.carbon.device.mgt.jaxrs.service.impl.DeviceAgentServiceImpl.log; + +@Path("/pending/operations/{type}/{id}") +public class PendingOperationsResourceResponseTime { + + private final Client client; + private final Sheets sheetsService; + private final String spreadsheetId; + private final String sheetName; + + public PendingOperationsResourceResponseTime() throws IOException, GeneralSecurityException { + this.client = ClientBuilder.newClient(); + + // Build the credentials object using Google OAuth 2.0 authentication + Credential credential = GoogleCredential.getApplicationDefault().createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS)); + HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); + JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); + + // Create a Sheets service object to interact with the Google Sheets API + this.sheetsService = new Sheets.Builder(httpTransport, jsonFactory, credential) + .setApplicationName("ApiCall") + .build(); + + // Set the spreadsheet ID and sheet name to write the results to + this.spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; + this.sheetName = "Sheet1"; + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + public Response getPendingOperations(@PathParam("type") String type, @PathParam("id") String deviceId) { + long startTime = System.currentTimeMillis(); + Response apiResponse = client.target("http://example.com/api/pending-operations") + .queryParam("type", type) + .queryParam("deviceId", deviceId) + .request(MediaType.APPLICATION_JSON) + .get(); + long endTime = System.currentTimeMillis(); + long responseTime = endTime - startTime; + + // log the response time + log.info("API response time: {} ms"); + + // write the results to Google Sheets + try { + writeResultsToSheet(startTime, endTime, responseTime); + } catch (IOException e) { + log.error("Error writing results to Google Sheets", e); + } + + return apiResponse; + } + + @PreDestroy + public void cleanup() { + client.close(); + } + + private void writeResultsToSheet(long startTime, long endTime, long responseTime) throws IOException { + // create a ValueRange object with the data to write + ValueRange valueRange = new ValueRange().setValues(Collections.singletonList( + Collections.singletonList(startTime + "," + endTime + "," + responseTime))); + + // append the data to the Google Sheet + try { + AppendValuesResponse response = sheetsService.spreadsheets().values() + .append(spreadsheetId, sheetName, valueRange) + .setValueInputOption("RAW") + .execute(); + log.info("Data written to Google Sheets: {}"); + } catch (GoogleJsonResponseException e) { + log.error("Error writing data to Google Sheets: {}"); + } + } +} \ No newline at end of file