diff --git a/src/main/java/com/application/DB/DB.java b/src/main/java/com/application/DB/DB.java index 04132241274a53465359da5278198fe653a49613..2777ec3da79ca0501ec154bc0133af8bcd0ec12c 100644 --- a/src/main/java/com/application/DB/DB.java +++ b/src/main/java/com/application/DB/DB.java @@ -6,6 +6,7 @@ import com.google.cloud.bigquery.*; import java.io.File; import java.io.FileInputStream; +import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; @@ -17,14 +18,14 @@ import java.util.*; */ public class DB { - private static Map<String, Number> data; - private static QueryJobConfiguration queryConfig; private static Job queryJob; + private static SimpleDateFormat dateFormat; /** * Retrieves the credentials file - * @return - * @throws Exception + * + * @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"); @@ -39,9 +40,14 @@ public class DB { return credentials; } + public static void main(String[] args) throws Exception { + getKwh(); + } + /** * Creates a builder + * * @return a builder * @throws Exception returns potential error */ @@ -59,6 +65,7 @@ public class DB { /** * Creates a job + * * @param queryConfig query configuration information * @return a job * @throws Exception returns potential error @@ -91,39 +98,87 @@ public class DB { */ public static Map<String, Number> getKwh() throws Exception { - // Initializing a data - data = new HashMap<>(); + // Initializing the data map to store the results + Map<String, Number> data = new HashMap<>(); - // Preparing a query job + List<List<Number>> listOfLists = new ArrayList<List<Number>>(); + + // Initializing the date format + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + + // Preparing a query statement final String sqlStatement = "SELECT TimeStamp, VariantValue " + "FROM sf-drying-optimization.124.int_sd_winccsensordata " + "WHERE TimeStamp BETWEEN \"2021-01-25 08:51:03\" " + "AND \"2021-03-04 11:10:09\" ORDER BY TimeStamp"; // Creates a job configuration - queryConfig = QueryJobConfiguration.newBuilder(sqlStatement).build(); + queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build()); - queryJob = getJob(queryConfig); - - // Step 4: Display results - // Print out a header line, and iterate through the - // query results to print each result in a new line - System.out.println("InTidTork \t UtTidTork"); + // Iterating through the results TableResult result = queryJob.getQueryResults(); + System.out.println("InTidTork \t UtTidTork"); for (FieldValueList row : result.iterateAll()) { - // We can use the `get` method along with the column - // name to get the corresponding row entry - SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); - newFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + // Retrieving the wanted data long timeStamp = row.get("TimeStamp").getTimestampValue()/1000; - String formatedTimeStamp = newFormat.format(timeStamp); + String formatedTimeStamp = dateFormat.format(timeStamp); int variantValue = row.get("VariantValue").getNumericValue().intValue(); + System.out.println(timeStamp); + + data.put(formatedTimeStamp, variantValue); + listOfLists.add(new ArrayList<Number>()); } return data; } + + /** + * Retrieves information about kwh and the corresponding date + * + * @return the results + * @throws Exception returns potential error + */ + public static void getName() throws Exception { + + // Initializing the data map to store the results + Map<Object,ArrayList<Object>> data = new HashMap<>(); + + + // Initializing the date format + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + + // Preparing a query statement + final String sqlStatement = + "SELECT Name, CalculatedStart, CalculatedStop FROM `sf-drying-optimization.124.int_dk_valmaticsdryingbatches`" + + "WHERE Name Like \"%Gran%\" AND Name Like \"%3ex%\" AND NAME Like \"%47x150%\" AND DATE(CalculatedStart) " + + "BETWEEN \"2018-08-17\" AND \"2022-08-30\" ORDER BY DATE(CalculatedStart)"; + + // Creates a job configuration + queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build()); + + // Iterating through the results + TableResult result = queryJob.getQueryResults(); + System.out.println("Name\tCalculatedStarted\tCalculatedStop"); + for (FieldValueList row : result.iterateAll()) { + + // Retrieving the wanted data + String name = row.get("Name").getStringValue(); + // The dates are returned as a 16-digit number that needs to be formatted + long calculatedStart = row.get("CalculatedStart").getTimestampValue()/1000; + long calculatedStop = row.get("CalculatedStop").getTimestampValue()/1000; + // Formatting the dates + String formattedCalculatedStart = dateFormat.format(calculatedStart); + String formattedCalculatedStop = dateFormat.format(calculatedStop); + + System.out.printf("%s\t\t\t%s\t\t\t%s\n",name, formattedCalculatedStart, formattedCalculatedStop); + + } + //return data; + } } \ No newline at end of file diff --git a/src/main/java/com/application/DB/Sort.java b/src/main/java/com/application/DB/Sort.java deleted file mode 100644 index 1d8630acc3bb689641486e33a9c16b3928977bcc..0000000000000000000000000000000000000000 --- a/src/main/java/com/application/DB/Sort.java +++ /dev/null @@ -1,95 +0,0 @@ -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.io.IOException; - -public class Sort { - - - 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; - } - - // 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 - private static BigQuery getBuilder() throws Exception { - BigQuery bigquery = BigQueryOptions.newBuilder(). - setCredentials(getCredentials()). - setProjectId("sf-drying-optimization") - .build().getService(); - return bigquery; - } - - - public static void main(String[] args) throws Exception { - Sort.getFromExistingTable(); - } - - - //getFromExistingTable sorts out treespecies, ex, width and height - // - - public static void getFromExistingTable() throws Exception { - - - // Step 2: Prepare query job - // A "QueryJob" is a type of job that executes SQL queries - // we create a new job configuration from our SQL query and - final String GET_WORD_COUNT = - "SELECT Name, CalculatedStart, CalculatedStop FROM `sf-drying-optimization.124.int_dk_valmaticsdryingbatches`" + - "WHERE Name Like \"%Gran%\" AND Name Like \"%3ex%\" AND NAME Like \"%47x150%\" AND DATE(CalculatedStart) " + - "BETWEEN \"2018-08-17\" AND \"2022-08-30\" ORDER BY DATE(CalculatedStart)"; - - QueryJobConfiguration queryConfig = - QueryJobConfiguration.newBuilder(GET_WORD_COUNT).build(); - - // 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()); - } - - // Step 4: Display results - // Print out a header line, and iterate through the - // query results to print each result in a new line - System.out.println("Name\tCalculatedStarted\tCalculatedStop"); - TableResult result = queryJob.getQueryResults(); - for (FieldValueList row : result.iterateAll()) { - // We can use the `get` method along with the column - // name to get the corresponding row entry - - String name = row.get("Name").getStringValue(); - String calculatedStart = row.get("CalculatedStart").getStringValue(); - String calculatedStop = row.get("CalculatedStop").getStringValue(); - System.out.printf("%s\t%s\t%s\n", name,calculatedStart, calculatedStop); - - - } - } -} diff --git a/src/main/java/com/application/Main.java b/src/main/java/com/application/Main.java index eb012b37802325b964ce571f87dff2e273c945bd..55fe15859aaf50de937bbee7368fc47152d51fad 100644 --- a/src/main/java/com/application/Main.java +++ b/src/main/java/com/application/Main.java @@ -69,6 +69,7 @@ public class Main extends Application { /** * Sets the primaryStage and sets the scene for the window. + * * @param primaryStage * @throws Exception */ @@ -99,6 +100,9 @@ public class Main extends Application { VBox.setVgrow(this.logoBar, Priority.ALWAYS); + DB.getName(); + System.out.println(DB.getKwh()); + // Sets the scene and defines boundaries //Scene scene = new Scene(root, 1200, 600); Scene scene = new Scene(root, 1200, 600); @@ -156,6 +160,7 @@ public class Main extends Application { /** * This function imports the logos and defines the alignments + * * @return a logoBar containing the logos in proper alignments * @throws FileNotFoundException */ @@ -196,12 +201,12 @@ public class Main extends Application { lineChart.setTitle("Drying Process"); - Map<String, Number> map = DB.getKwh(); - Map<String, Number> treeMap = new TreeMap<>(map); + Map<String, Number> kwh = DB.getKwh(); + Map<String, Number> sortedKwh = new TreeMap<>(kwh); XYChart.Series series1 = new XYChart.Series(); series1.setName("Drying 1"); - treeMap.forEach((key, value) -> series1.getData().add(new XYChart.Data(key,value))); + sortedKwh.forEach((key, value) -> series1.getData().add(new XYChart.Data(key,value))); lineChart.getData().add(series1); return lineChart; diff --git a/target/classes/com/application/DB/DB.class b/target/classes/com/application/DB/DB.class index 556fc2cc052661cde9e807016e45a7abe5275738..cf5b913fe5b25df78e5f87d4e74206b7819a98bb 100644 Binary files a/target/classes/com/application/DB/DB.class and b/target/classes/com/application/DB/DB.class differ diff --git a/target/classes/com/application/DB/Sort.class b/target/classes/com/application/DB/Sort.class index 1a594c06b3520ce5e5d358f21b0ff1cf017bc63b..af7725eabeed8f31bef3c2fa8720615f22f08490 100644 Binary files a/target/classes/com/application/DB/Sort.class and b/target/classes/com/application/DB/Sort.class differ diff --git a/target/classes/com/application/Main.class b/target/classes/com/application/Main.class index a7169948028a77353b46f51b3ae3f03584f5233d..529aafa67273028ab74fc4440d51c6f3ee9d1512 100644 Binary files a/target/classes/com/application/Main.class and b/target/classes/com/application/Main.class differ