diff --git a/.idea/misc.xml b/.idea/misc.xml index d2b574f978d2b5e7c5734f51e7a4c13d10b69e1d..e582eaaa74a2c1a8e0eca1696e0a3f4a0344ae5c 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -10,7 +10,7 @@ <component name="ProjectKey"> <option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" /> </component> - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK"> <output url="file://$PROJECT_DIR$/out" /> </component> </project> \ No newline at end of file diff --git a/src/main/java/com/application/DB/DB.java b/src/main/java/com/application/DB/DB.java index f65225d81da79ffbc8571a86c3dc41f81271cffa..3cb57f0b5c2bf8e0223117a4ad3ae816f4769e29 100644 --- a/src/main/java/com/application/DB/DB.java +++ b/src/main/java/com/application/DB/DB.java @@ -16,12 +16,15 @@ import java.util.*; * This class is responsible for handling database related activities * * @author Eilert Tunheim, Karin Pettersen, Mads Arnesen - * @version 1.0 + * @version 1.1 */ public class DB { - private static Job queryJob; - + /** + * Creates a simple date format to use for converting millis in numbers to a usefull date format + * + * @return returns the date format + */ private static SimpleDateFormat getDateFormat() { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); @@ -47,6 +50,13 @@ public class DB { return credentials; } + + /** + * Launching the functions + * + * @param args default param + * @throws Exception throws exception in case of error + */ public static void main(String[] args) throws Exception { getKwh(); //getName(); @@ -55,7 +65,7 @@ public class DB { /** - * Creates a builder + * Creates a bigquery builder * * @return a builder * @throws Exception returns potential error @@ -98,6 +108,24 @@ public class DB { 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 + */ + private 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(); + + } + /** * Retrieves information about kWh and the corresponding date @@ -126,21 +154,43 @@ public class DB { Map<String, Number> data = new HashMap<>(); // Preparing a query statement + /* final String sqlStatement = "SELECT DISTINCT TimeStamp, VariantValue " + "FROM sf-drying-optimization.124.int_sd_winccsensordata " + "WHERE TimeStamp BETWEEN " + '"'+ entry.getKey() + '"' + " AND " + '"' + entry.getValue() + '"' + - " ORDER BY TimeStamp ASC";// Preparing a query statement + " ORDER BY TimeStamp ASC"; - //System.out.println(sqlStatement); + */ - // Creates a job configuration - queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build()); + // Preparing a query statement + // Query statement 124 Valåsen + final String sqlStatement = "SELECT `TimeStamp`, `VariantValue` " + + "FROM `sf-drying-optimization.124.int_sd_winccsensordata` " + + "WHERE TimeStamp BETWEEN " + '"'+ entry.getKey() + '"' + + " AND " + '"' + entry.getValue() + '"' + + " AND ValueID = 51" + + " ORDER BY TimeStamp ASC"; + + + /* + // Query statement 174 Årjang + final String sqlStatement = + "SELECT Timestamp, RealValue FROM `sf-drying-optimization.174.int_sd_swappconsensordata` " + + "WHERE TimeStamp BETWEEN " + '"'+ entry.getKey() + '"' + + " AND " + '"' + entry.getValue() + '"' + + "AND ValueID = 14 " + + "AND RealValue <> 0 " + + "ORDER BY TimeStamp ASC"; + + */ + + System.out.println(sqlStatement); // Iterating through the results - TableResult result = queryJob.getQueryResults(); - //System.out.println("Timestamp \t kWh"); + TableResult result = createQueryJob(sqlStatement); + //System.out.println("Timestamp \t kWh"); int baseline = 0; for (FieldValueList row : result.iterateAll()) { @@ -151,7 +201,7 @@ public class DB { //System.out.println("baseline: "+baseline); // kWh value - int variantValue = row.get("VariantValue").getNumericValue().intValue()-baseline; + int variantValue = row.get("VariantValue").getNumericValue().intValue(); //-baseline // Retrieving the wanted data long timeStamp = row.get("TimeStamp").getTimestampValue() / 1000; @@ -159,19 +209,25 @@ public class DB { String formatedTimeStamp = getDateFormat().format(timeStamp); // Checks for negative values and unresonable large values - if(variantValue > 0 && variantValue < 5000000){ + if(variantValue > 0){ //&& variantValue < 5000000){ // Adding the data to a list in order to sort through later data.put(formatedTimeStamp, variantValue); } //System.out.printf("Timestamp: \t%s\t\t\tkWh: \t%s\t\t\tBaseline: %s\n",formatedTimeStamp,variantValue,baseline); // Checks if the data is empty } + + System.out.println("Data size: " + data.size()); + NavigableMap<String, Number> sortedData = new TreeMap<>(data); - if(!sortedData.isEmpty()) { + if(!sortedData.isEmpty() && sortedData.size()>50) { finalResults.put(index,sortedData); index += 1; } } + + System.out.println("\nFinal results size: "+finalResults.size()); + // Defining a treemap to sort through the data NavigableMap<Integer, Map> sortedFinalResults = new TreeMap<>(finalResults); @@ -180,53 +236,11 @@ public class DB { System.out.printf("Timestamp: \t%s\t\t\tkWh: \t%s\n",entry.getKey(),entry.getValue()); } + return sortedFinalResults; } - /** - * Retrieves information about kWh and the corresponding date - * - * @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<>(); - - - // 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 = getDateFormat().format(calculatedStart); - String formattedCalculatedStop = getDateFormat().format(calculatedStop); - - java.sql.Timestamp timestamp = new Timestamp(calculatedStart); - //System.out.println(timestamp); - - System.out.printf("%s\t\t\t%s\t\t\t%s\n",name, formattedCalculatedStart, formattedCalculatedStop); - - } - //return data; - } - /** * This function retrieves the intidtork and uttidtork dates from the database. * This variables are not sorted and thus this function iterates through the data in order to @@ -249,19 +263,43 @@ public class DB { Map<String, String> dates = new HashMap<>(); // Preparing a query statement + /* final String sqlStatement = "SELECT InTidTork, UtTidTork FROM `sf-drying-optimization.124.int_gs_ds_sipalpackages`" + "WHERE Tork Like \"%5%\" AND InTidTork BETWEEN \"2021-01-30 08:51:03\" " + "AND \"2022-03-15 11:10:09\" ORDER BY InTidTork ASC"; + */ + + + // Sqlstatement for Valåsen(124) + final String sqlStatement = + "SELECT MAX(Name) as DryingSchedule, MAX(KilnName) as Kiln_name, DryingStarted, Max(CalculatedStop) as CalculatedStop\n" + + "FROM `sf-drying-optimization.124.int_dk_valmaticsdryingbatches` \n" + + "WHERE KilnName = 5 \n" + + "AND DATE(DryingStarted) BETWEEN \"1990-08-17\" AND \"2022-08-17\"\n" + + "AND DATE(CalculatedStop) BETWEEN \"1990-08-17\" AND \"2022-08-17\"\n" + + "AND DATE(CalculatedStart) BETWEEN \"1990-08-17\" AND \"2022-08-17\"\n" + + "Group by DryingStarted\n" + + "Order by DryingStarted ASC"; - // Creates a job configuration - queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build()); + + /* + // Sqlstatement for Årjang(174) + final String sqlStatement = + "SELECT MAX(Name) as DryingSchedule, MAX(KilinId)+1 as KilnName, DryingStarted, Max(DryingCompleted) as DryingCompleted\n" + + "FROM `sf-drying-optimization.174.int_dk_valmaticsdryingbatches` \n" + + "WHERE KilinId = 16 \n" + + "AND DATE(DryingStarted) BETWEEN \"1990-08-17\" AND \"2022-08-17\"\n" + + "AND DATE(DryingCompleted) BETWEEN \"1990-08-17\" AND \"2022-08-17\"\n" + + "Group by DryingStarted\n" + + "Order by DryingStarted desc"; + + */ // Retrieves the results from the queryjob - TableResult result = queryJob.getQueryResults(); + TableResult result = createQueryJob(sqlStatement); //System.out.println("InTidTork\t\t\tUtTidTork"); - // Iterating through the results for (FieldValueList row : result.iterateAll()) { @@ -270,18 +308,19 @@ public class DB { String formatedUtTidTork; // Retrieving the data - // InTidTork: - if(!row.get("InTidTork").isNull()){ - long InTidTorkLong = row.get("InTidTork").getTimestampValue()/1000; + // DryingStarted: + if(!row.get("DryingStarted").isNull()){ + long InTidTorkLong = row.get("DryingStarted").getTimestampValue()/1000; // Formating the data from long to a string in the correct date format formatedInTidTork = getDateFormat().format(InTidTorkLong); } else { formatedInTidTork = ""; } - // UtTidTork: - if(!row.get("UtTidTork").isNull()){ - long utTidTorkLong = row.get("UtTidTork").getTimestampValue()/1000; + // CalculatedStop: + // DryingCompleted: + if(!row.get("CalculatedStop").isNull()){ + long utTidTorkLong = row.get("CalculatedStop").getTimestampValue()/1000; // Formating the data from long to a string in the correct date format formatedUtTidTork = getDateFormat().format(utTidTorkLong); } else { @@ -295,7 +334,10 @@ public class DB { } //System.out.printf("%s\t\t\t%s\n",formatedInTidTork,formatedUtTidTork); } + System.out.printf("Size of dates: %s\n", dates.size()); + return dates; +/* // Defining a treemap to sort through the data NavigableMap<String, String> dataSet = new TreeMap<>(dates); @@ -333,5 +375,7 @@ public class DB { } // Defining a treemap to sort through the data return new TreeMap<>(sortedDates); + + */ } } \ No newline at end of file diff --git a/src/main/java/com/application/Main.java b/src/main/java/com/application/Main.java index 6da5ee776e73e68abe7095e3abb95efb2e0767ef..38ca17b63fdd5e2f0899c012690ddeb37180f705 100644 --- a/src/main/java/com/application/Main.java +++ b/src/main/java/com/application/Main.java @@ -193,7 +193,7 @@ public class Main extends Application { @Override public void run(){ try { - Thread.sleep(10000); + Thread.sleep(100000); } catch (InterruptedException e) { e.printStackTrace(); } @@ -285,13 +285,13 @@ public class Main extends Application { String date = arr[0]; int kwhValue = Integer.parseInt(arr[1]); - System.out.printf("Date: \t%s\t\t\tkWh: \t%s\n",date,kwhValue); + //System.out.printf("Date: \t%s\t\t\tkWh: \t%s\n",date,kwhValue); // Connect the data to a series series.getData().add(new XYChart.Data(date,kwhValue)); } lineChart.getData().add(series); - System.out.println("\n\nNew line\n\n"); + //System.out.println("\n\nNew line\n\n"); } /* for (Map.Entry<Integer, Map> entryKwh : kWh.entrySet()) { diff --git a/target/classes/com/application/DB/DB.class b/target/classes/com/application/DB/DB.class index b043ea6f9c7d141f62c4802fae9187a01698bd52..1da6ea92dcd6fd9b64697518553dd3ad7c737946 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/Main$1WorkerThread.class b/target/classes/com/application/Main$1WorkerThread.class index d4793f5681bdd83671fe08bf926fc358debbea06..fc1cb79295b938a5ba6bc65d216611f6e1d0be64 100644 Binary files a/target/classes/com/application/Main$1WorkerThread.class and b/target/classes/com/application/Main$1WorkerThread.class differ diff --git a/target/classes/com/application/Main.class b/target/classes/com/application/Main.class index b9a0b1c909958ed7e95a60800e7fa3918561a1d6..23a916a35e825fce2f8a9fed85331c992bbed12d 100644 Binary files a/target/classes/com/application/Main.class and b/target/classes/com/application/Main.class differ