diff --git a/src/main/java/com/application/DB/Constants.java b/src/main/java/com/application/DB/Constants.java index 8ca102cb985606484e19ccf4baa3f0a448d58d24..ff18115d3509932a857bad0b30071e3e3b5f1a58 100644 --- a/src/main/java/com/application/DB/Constants.java +++ b/src/main/java/com/application/DB/Constants.java @@ -85,6 +85,10 @@ public final class Constants { //public static final int ARJANG_VALUE_ID = 19; public static final ArrayList<Integer> ARJANG_KWH_CHAMBER_IDS = new ArrayList<>(Arrays.asList(19,19)); + // Moisture Manual measurements + public static final String ARJANG_MANFUKTREG = "int_ds_manfuktreg"; + public static final int ARJANG_NR_ADDED_DAYS = 2; + diff --git a/src/main/java/com/application/DB/DB.java b/src/main/java/com/application/DB/DB.java index 19a5f0b5dc9745181b750caacdfd71c3705544ab..9a72927f0a4de53627b074ed96b5cfda5c96edc3 100644 --- a/src/main/java/com/application/DB/DB.java +++ b/src/main/java/com/application/DB/DB.java @@ -1,13 +1,8 @@ package com.application.DB; -import com.google.auth.oauth2.GoogleCredentials; -import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.cloud.bigquery.*; import org.joda.time.DateTime; -import java.io.File; -import java.io.FileInputStream; -import java.text.SimpleDateFormat; import java.util.*; import static com.application.DB.Constants.*; @@ -20,37 +15,6 @@ import static com.application.DB.Constants.*; */ public class DB { - /** - * 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")); - 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; - } - - /** * Launching the functions * @@ -66,69 +30,6 @@ public class DB { } - /** - * 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 - * - * @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 - */ - 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(); - - } - - public static Map<Integer, Map<String, Number>> setInputParameters() throws Exception { Map<Integer, Map<String, Number>> allDryingPeriods = new HashMap<>(); @@ -320,51 +221,6 @@ public class DB { } - - /** - * - * - * @throws Exception - */ - /* - public static void getManualMeasurements() throws Exception { - - // Sqlstatement - final String sqlStatement = ""; - - System.out.println(sqlStatement); - - // Retrieves the results from the queryjob - TableResult result = createQueryJob(sqlStatement); - - //System.out.println("InTidTork\t\t\tUtTidTork"); - // Iterating through the results - for (FieldValueList row : result.iterateAll()) { - - row.get("DryingStarted").getTimestampValue(); - } - - // Defining a treemap to sort the data incrementally - //NavigableMap<String, String> sortedFinalResults = new TreeMap<>(dates); - - System.out.println("\n"); - - for (Map.Entry<String, String> entry : sortedFinalResults.entrySet()) { - System.out.printf("Intid: \t%s\t\t\tUttid: \t%s\n",entry.getKey(),entry.getValue()); - } - - - - //System.out.printf("Size of dates: %s\n\n", sortedFinalResults.size()); - - - // Returns a treemap that sorts the dates incrementally - //return new TreeMap<>(sortedFinalResults); - } - */ - - - /** * Retrieves information about kWh and the corresponding date * @@ -376,6 +232,8 @@ public class DB { String timestamp, String valueIDName, int valueID, int index) throws Exception { + System.out.println("GetMan size: " + HelpingFunctions.getManMoist().size()); + // Initializing the data map to store the results Map<Integer, Map<String, Number>> finalResults = new HashMap<>(); @@ -399,7 +257,7 @@ public class DB { System.out.println(sqlStatement); // Iterating through the results - TableResult result = createQueryJob(sqlStatement); + TableResult result = HelpingFunctions.createQueryJob(sqlStatement); //System.out.println("Timestamp \t kWh"); int baseline = 0; @@ -417,7 +275,7 @@ public class DB { // Retrieving the wanted data long timeStamp = row.get("" + timestamp + "").getTimestampValue() / 1000; // Riktig format, men i string - String formatedTimeStamp = getDateFormat().format(timeStamp); + String formatedTimeStamp = HelpingFunctions.getDateFormat().format(timeStamp); // Checks for negative values if (variantValue > 0) { @@ -482,8 +340,9 @@ public class DB { String formatedUtTidTork = ""; String formatedUtTidTorkPlussDays = ""; - // Initializing the dates map to store the results + // Initializing maps to store the results Map<String, String> dates = new HashMap<>(); + Map<String, String> manMoistData = new HashMap<>(); // Defining extra parameters if required @@ -513,10 +372,15 @@ public class DB { System.out.println(sqlStatement); + // Increments kilinID back to the correct id nr + if(locationID == 174){ + kilinID += 1; + } + // Retrieves the results from the queryjob - TableResult result = createQueryJob(sqlStatement); + TableResult result = HelpingFunctions.createQueryJob(sqlStatement); //System.out.println("InTidTork\t\t\tUtTidTork"); // Iterating through the results @@ -534,10 +398,10 @@ public class DB { if (nameParameters[i].contains("Gran")) { treeSort = "G"; } - if (nameParameters[i].matches("\\d{2}x\\d{3}")) { + if (nameParameters[i].matches("\\d+x\\d+")) { String[] dimensions = nameParameters[i].split("x"); - width = dimensions[0]; - thickness = dimensions[1]; + thickness = dimensions[0]; + width = dimensions[1]; } if (nameParameters[i].matches("\\d{1}ex")) { sawSet = nameParameters[i]; @@ -559,7 +423,7 @@ public class DB { long doubleValue = row.get("DryingStarted").getTimestampValue(); long InTidTorkLong = doubleValue/1000; // Formating the data from long to a string in the correct date format - formatedInTidTork = getDateFormat().format(InTidTorkLong); + formatedInTidTork = HelpingFunctions.getDateFormat().format(InTidTorkLong); //System.out.println("LONG!!@@@@"); } catch(NumberFormatException e){ //not long value, must be of type string @@ -590,7 +454,7 @@ public class DB { long doubleValue = row.get("DryingCompleted").getTimestampValue(); long utTidTorkLong = doubleValue/1000; // Formating the data from long to a string in the correct date format - formatedUtTidTork = getDateFormat().format(utTidTorkLong); + formatedUtTidTork = HelpingFunctions.getDateFormat().format(utTidTorkLong); } catch(NumberFormatException e) { //not long value, must be of type string @@ -606,61 +470,62 @@ public class DB { } - //System.out.println("Inn formated: "+formatedInTidTork); - //System.out.println("Ut formated: "+formatedUtTidTork+"\n"); - // Checks if intidtork or outtidtork is empty, if so they are ignored and not added to the list - if (!formatedInTidTork.isEmpty() && !formatedUtTidTork.isEmpty()){ - // Adds the data to the dates map - dates.put(formatedInTidTork,formatedUtTidTork); - } - //System.out.printf("%s\t\t\t%s\n",formatedInTidTork,formatedUtTidTork); - } - - - // Adding days to search between - DateTime addedDays = new DateTime(formatedUtTidTork.split(" ")[0]); - formatedUtTidTorkPlussDays = addedDays.plusDays(2).toString(); - formatedUtTidTorkPlussDays = formatedUtTidTorkPlussDays.split("T")[0]; - - - // Getting manual measurement - // Sqlstatement - final String manMeasurement = "SELECT Tork, Dato ,Treslag, Tykkelse, Bredde, Planketype, Malfukt, GjeFukt " + - "FROM `sf-drying-optimization.174.int_ds_manfuktreg` " + - "WHERE Tork LIKE " + kilinID + " " + - "AND Treslag = " + treeSort + " " + - "AND Tykkelse = " + thickness + " " + - "AND Bredde = " + width + " " + - "AND Planketype = " + sawSet + " " + - "AND Dato BETWEEN \""+ formatedUtTidTork.split(" ")[0] +"\" AND \"" + formatedUtTidTorkPlussDays + "\" " + - "ORDER BY Dato"; - - System.out.println(manMeasurement); + // Getting manual measurement: + // Adding days to search between + String date = formatedUtTidTork.split(" ")[0]; + DateTime addedDays = new DateTime(date); + formatedUtTidTorkPlussDays = addedDays.plusDays(ARJANG_NR_ADDED_DAYS).toString(); + formatedUtTidTorkPlussDays = formatedUtTidTorkPlussDays.split("T")[0]; + if (locationID == ARJANG_LOCATION_ID) { - // Retrieves the results from the queryjob - TableResult resultManMeasurement = createQueryJob(manMeasurement); + // Sqlstatement + final String manMeasurementStatement = "SELECT Tork, Dato, Treslag, Tykkelse, Bredde, Planketype, Diff " + + "FROM `" + PROJECT_ID + "." + locationID + "." + ARJANG_MANFUKTREG + "` " + + "WHERE Tork LIKE \"%" + kilinID + "%\" " + + "AND Treslag LIKE \"%" + treeSort + "%\" " + + "AND Tykkelse = " + thickness + " " + + "AND Bredde = " + width + " " + + "AND Planketype = \"" + sawSet + "\" " + + "AND Dato BETWEEN \"" + date + "\" AND \"" + formatedUtTidTorkPlussDays + "\" " + + "ORDER BY Dato"; - //System.out.println("InTidTork\t\t\tUtTidTork"); - // Iterating through the results - for (FieldValueList row : resultManMeasurement.iterateAll()) { + //System.out.println(manMeasurementStatement); - System.out.println(row); - moistureGoal = row.get("Malfukt").getValue().toString(); - moistureMeasured = row.get("GjeFukt").getValue().toString(); + // Retrieves the results from the queryjob + TableResult resultManMeasurement = HelpingFunctions.createQueryJob(manMeasurementStatement); - } + //System.out.println("InTidTork\t\t\tUtTidTork"); + // Iterating through the results + for (FieldValueList manMeasurement : resultManMeasurement.iterateAll()) { + // Checks if value is null + if(!manMeasurement.get("Diff").isNull()){ + moistureMeasured = manMeasurement.get("Diff").getValue().toString(); + } + } + //System.out.println("moistureMeasured: " + moistureMeasured); + if(!moistureMeasured.isEmpty()){ + manMoistData.put(formatedUtTidTork,moistureMeasured); + } + } - System.out.println("moistureGoal: " + moistureGoal); - System.out.println("moistureMeasured: " + moistureMeasured); + //System.out.println("Inn formated: "+formatedInTidTork); + //System.out.println("Ut formated: "+formatedUtTidTork+"\n"); + // Checks if intidtork or outtidtork is empty, if so they are ignored and not added to the list + if (!formatedInTidTork.isEmpty() && !formatedUtTidTork.isEmpty()){ + // Adds the data to the dates map + dates.put(formatedInTidTork,formatedUtTidTork); + } + //System.out.printf("%s\t\t\t%s\n",formatedInTidTork,formatedUtTidTork); + } // Defining a treemap to sort the data incrementally NavigableMap<String, String> sortedFinalResults = new TreeMap<>(dates); @@ -671,10 +536,14 @@ public class DB { System.out.printf("Intid: \t%s\t\t\tUttid: \t%s\n",entry.getKey(),entry.getValue()); } + System.out.printf("Size of dates: %s\n\n", sortedFinalResults.size()); - System.out.printf("Size of dates: %s\n\n", sortedFinalResults.size()); + System.out.println("manMoistData size: " + manMoistData.size()); + for (Map.Entry<String, String> entry : manMoistData.entrySet()) { + HelpingFunctions.getManMoist().put(entry.getKey(), entry.getValue()); + } // Returns a treemap that sorts the dates incrementally return new TreeMap<>(sortedFinalResults); diff --git a/src/main/java/com/application/GUI/LineChartFunctionality.java b/src/main/java/com/application/GUI/LineChartFunctionality.java index 3352b3e2c40527ab10b5e0298eba54ca6f91c3cd..d0f1c25942df05306f4ccf9b4f22347aef285288 100644 --- a/src/main/java/com/application/GUI/LineChartFunctionality.java +++ b/src/main/java/com/application/GUI/LineChartFunctionality.java @@ -49,9 +49,9 @@ public class LineChartFunctionality { private static Map<Integer, ArrayList<Double>> statistics(Map<Integer, ArrayList<Double>> multiMap){ - System.out.println("\n\nMultimap: \n"); + //System.out.println("\n\nMultimap: \n"); for (Map.Entry<Integer, ArrayList<Double>> entry : multiMap.entrySet()) { - System.out.printf("\nIndex: \t%s\t\t\tkWh: \t%s\n", entry.getKey(), entry.getValue()); + //System.out.printf("\nIndex: \t%s\t\t\tkWh: \t%s\n", entry.getKey(), entry.getValue()); if(entry.getValue().size()>1){ SummaryStatistics stats = new SummaryStatistics(); @@ -61,10 +61,10 @@ public class LineChartFunctionality { // Calculate 95% confidence interval double ci = calcMeanCI(stats, CONFIDENCE_INTERVAL); - System.out.println(String.format("Mean: %f", stats.getMean())); + //System.out.println(String.format("Mean: %f", stats.getMean())); double lower = stats.getMean() - ci; double upper = stats.getMean() + ci; - System.out.println(String.format("Confidence Interval 95%%: %f, %f", lower, upper)); + //System.out.println(String.format("Confidence Interval 95%%: %f, %f", lower, upper)); // Deletes entries if they are out of bounds with the confidence interval entry.getValue().removeIf(value -> Double.compare(value, lower) < 0 || Double.compare(value, upper) > 0); @@ -138,7 +138,7 @@ public class LineChartFunctionality { double[][] data = new double[confidenceIntervalData.size()*jMaxSize][2]; int index = 0; - System.out.println(confidenceIntervalData); + //System.out.println(confidenceIntervalData); for (int i = 0; i < confidenceIntervalData.size(); i++) { ArrayList<Double> list = confidenceIntervalData.get(i); @@ -181,7 +181,7 @@ public class LineChartFunctionality { for (double[] datum : data) { for (double v : datum) { - System.out.println("Data: "+v); + //System.out.println("Data: "+v); } } @@ -191,7 +191,7 @@ public class LineChartFunctionality { for (int i = 0; i < confidenceIntervalData.size(); i++) { // Connect the data to a series - System.out.println(simpleRegression.predict(i)); + //System.out.println(simpleRegression.predict(i)); regressionSeries.getData().add(new XYChart.Data<String, Number>(String.valueOf(i), simpleRegression.predict(i))); } updateLineChart(regressionSeries); diff --git a/target/classes/com/application/GUI/LineChartFunctionality.class b/target/classes/com/application/GUI/LineChartFunctionality.class index 5af8d726187d02dc53af7c8a0c5c4932f7360d17..5114429200f1c808b85caad3617510f534fe86cf 100644 Binary files a/target/classes/com/application/GUI/LineChartFunctionality.class and b/target/classes/com/application/GUI/LineChartFunctionality.class differ