From 37bef26862a632632bbfe5088ca87378e6a72947 Mon Sep 17 00:00:00 2001 From: akeela_azhar Date: Wed, 10 May 2023 13:17:22 +0530 Subject: [PATCH] Add Google sheet response time files --- .../org.wso2.carbon.device.mgt.api/pom.xml | 10 ++ .../jaxrs/service/impl/ApiResponseTime.java | 108 ++++++++++++++++++ .../mgt/jaxrs/service/impl/GoogleSheet.java | 34 ++++++ 3 files changed, 152 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ApiResponseTime.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GoogleSheet.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 7077d97672..fe2e75c0da 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 @@ -455,5 +455,15 @@ io.entgra.device.mgt.core.apimgt.analytics.extension provided + + com.google.api-client + google-api-client + 2.2.0 + + + com.google.apis + google-api-services-sheets + v4-rev539-1.25.0 + \ 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 new file mode 100644 index 0000000000..021bbaf559 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/ApiResponseTime.java @@ -0,0 +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(); + } + } +} + 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 new file mode 100644 index 0000000000..ce70894d11 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GoogleSheet.java @@ -0,0 +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; + } + +}