package com.application.DB; import com.google.auth.oauth2.GoogleCredentials; import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.cloud.bigquery.*; import java.io.File; import java.io.FileInputStream; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Map; import java.util.TimeZone; import static com.application.DB.Constants.PROJECT_ID; public class HelpingFunctions { static Map<String,String> manMoist = new HashMap<>(); /** * Creates a simple date format to use for converting millis in numbers to a usefull date format * * @return returns the date format */ static SimpleDateFormat getDateFormat() { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); return dateFormat; } /** * Retrieves the credentials file * * @return the credentials * @throws Exception for potential errors */ private static GoogleCredentials getCredentials() throws Exception { File credentialsPath = new File("./src/main/resources/com.application/sf-drying-optimization-1e234ad2b0f4.json"); // Load credentials from JSON key file. If you can't set the GOOGLE_APPLICATION_CREDENTIALS // environment variable, you can explicitly load the credentials file to construct the // credentials. GoogleCredentials credentials; try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) { credentials = ServiceAccountCredentials.fromStream(serviceAccountStream); } return credentials; } /** * Creates a bigquery builder * * @return a builder * @throws Exception returns potential error */ private static BigQuery getBuilder() throws Exception { // Step 1: Initialize BigQuery service // Here we set our project ID and get the `BigQuery` service object // this is the interface to our BigQuery instance that // we use to execute jobs on return BigQueryOptions.newBuilder(). setCredentials(getCredentials()). setProjectId(PROJECT_ID) .build().getService(); } /** * Creates a job for the database * * @param queryConfig query configuration information * @return a job * @throws Exception returns potential error */ private static Job getJob(JobConfiguration queryConfig) throws Exception { // Step 3: Run the job on BigQuery // create a `Job` instance from the job configuration using the BigQuery service // the job starts executing once the `create` method executes Job queryJob = getBuilder().create(JobInfo.newBuilder(queryConfig).build()); queryJob = queryJob.waitFor(); // the waitFor method blocks until the job completes // and returns `null` if the job doesn't exist anymore if (queryJob == null) { throw new Exception("job no longer exists"); } // once the job is done, check if any error occured if (queryJob.getStatus().getError() != null) { throw new Exception(queryJob.getStatus().getError().toString()); } return queryJob; } /** * This function creates a query job that uses the query statement * in order to retrieve information from the database * * @param sqlStatement input for the query statement * @return returns the queryjob with the results * @throws Exception Throws exception in case of error */ static TableResult createQueryJob(String sqlStatement) throws Exception { // Creates a job configuration Job queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build()); // Retrieves the results from the queryjob return queryJob.getQueryResults(); } /** * Stores data about manual measurements for moisture level in the lumber * * @return a list containing the outdate for the lumber and the corresponding diff value */ public static Map<String, String> getManMoist() { return manMoist; } /** * Function to iterate through all the Kwh values and storing them in a map * * @param data a map to store all the data * @param baseline a baseline to base all the next values of to get a zero point * @param result TableResult to iterate through * @param kwhNameParameter Name of the Kwh name parameter in the database * @param kwhTimestampNameParameter Name of the timestamp parameter in the database */ static void iterateKwhValues(Map<String, Number> data, int baseline, TableResult result, String kwhNameParameter, String kwhTimestampNameParameter) { for (FieldValueList row : result.iterateAll()) { // Sets the baseline in order to reset the kWh counter if (baseline == 0) { baseline = row.get("" + kwhNameParameter + "").getNumericValue().intValue(); } // kWh value int variantValue = row.get("" + kwhNameParameter + "").getNumericValue().intValue() - baseline; //-baseline // Retrieving the wanted data long timeStamp = row.get("" + kwhTimestampNameParameter + "").getTimestampValue() / 1000; // Riktig format, men i string String formatedTimeStamp = getDateFormat().format(timeStamp); // Checks for negative values if (variantValue > 0) { // Adding the data to a list in order to sort through later data.put(formatedTimeStamp, variantValue); } } } }