forked from community/device-mgt-core
parent
d0dbceebd3
commit
c083fc7b11
@ -1,102 +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: {}");
|
||||
}
|
||||
}
|
||||
}
|
||||
//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: {}");
|
||||
// }
|
||||
// }
|
||||
//}
|
Loading…
Reference in new issue