Skip to content
Snippets Groups Projects
Commit a6646932 authored by Eilert Tunheim's avatar Eilert Tunheim
Browse files

Merge branch 'Eilerts_branch' into 'master'

Updated DB functions withb new sql statements

See merge request mesji/bacheloroppgave_2022!32
parents 3895ba25 2f57272b
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
<component name="ProjectKey"> <component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" /> <option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component> </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" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>
\ No newline at end of file
...@@ -16,12 +16,15 @@ import java.util.*; ...@@ -16,12 +16,15 @@ import java.util.*;
* This class is responsible for handling database related activities * This class is responsible for handling database related activities
* *
* @author Eilert Tunheim, Karin Pettersen, Mads Arnesen * @author Eilert Tunheim, Karin Pettersen, Mads Arnesen
* @version 1.0 * @version 1.1
*/ */
public class DB { 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() { private static SimpleDateFormat getDateFormat() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
...@@ -47,6 +50,13 @@ public class DB { ...@@ -47,6 +50,13 @@ public class DB {
return credentials; 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 { public static void main(String[] args) throws Exception {
getKwh(); getKwh();
//getName(); //getName();
...@@ -55,7 +65,7 @@ public class DB { ...@@ -55,7 +65,7 @@ public class DB {
/** /**
* Creates a builder * Creates a bigquery builder
* *
* @return a builder * @return a builder
* @throws Exception returns potential error * @throws Exception returns potential error
...@@ -98,6 +108,24 @@ public class DB { ...@@ -98,6 +108,24 @@ public class DB {
return queryJob; 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 * Retrieves information about kWh and the corresponding date
...@@ -126,21 +154,43 @@ public class DB { ...@@ -126,21 +154,43 @@ public class DB {
Map<String, Number> data = new HashMap<>(); Map<String, Number> data = new HashMap<>();
// Preparing a query statement // Preparing a query statement
/*
final String sqlStatement = "SELECT DISTINCT TimeStamp, VariantValue " + final String sqlStatement = "SELECT DISTINCT TimeStamp, VariantValue " +
"FROM sf-drying-optimization.124.int_sd_winccsensordata " + "FROM sf-drying-optimization.124.int_sd_winccsensordata " +
"WHERE TimeStamp BETWEEN " + '"'+ entry.getKey() + '"' + "WHERE TimeStamp BETWEEN " + '"'+ entry.getKey() + '"' +
" AND " + '"' + entry.getValue() + '"' + " AND " + '"' + entry.getValue() + '"' +
" ORDER BY TimeStamp ASC";// Preparing a query statement " ORDER BY TimeStamp ASC";
//System.out.println(sqlStatement); */
// Creates a job configuration // Preparing a query statement
queryJob = getJob(QueryJobConfiguration.newBuilder(sqlStatement).build()); // 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 // Iterating through the results
TableResult result = queryJob.getQueryResults(); TableResult result = createQueryJob(sqlStatement);
//System.out.println("Timestamp \t kWh");
//System.out.println("Timestamp \t kWh");
int baseline = 0; int baseline = 0;
for (FieldValueList row : result.iterateAll()) { for (FieldValueList row : result.iterateAll()) {
...@@ -151,7 +201,7 @@ public class DB { ...@@ -151,7 +201,7 @@ public class DB {
//System.out.println("baseline: "+baseline); //System.out.println("baseline: "+baseline);
// kWh value // kWh value
int variantValue = row.get("VariantValue").getNumericValue().intValue()-baseline; int variantValue = row.get("VariantValue").getNumericValue().intValue(); //-baseline
// Retrieving the wanted data // Retrieving the wanted data
long timeStamp = row.get("TimeStamp").getTimestampValue() / 1000; long timeStamp = row.get("TimeStamp").getTimestampValue() / 1000;
...@@ -159,19 +209,25 @@ public class DB { ...@@ -159,19 +209,25 @@ public class DB {
String formatedTimeStamp = getDateFormat().format(timeStamp); String formatedTimeStamp = getDateFormat().format(timeStamp);
// Checks for negative values and unresonable large values // 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 // Adding the data to a list in order to sort through later
data.put(formatedTimeStamp, variantValue); data.put(formatedTimeStamp, variantValue);
} }
//System.out.printf("Timestamp: \t%s\t\t\tkWh: \t%s\t\t\tBaseline: %s\n",formatedTimeStamp,variantValue,baseline); //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 // Checks if the data is empty
} }
System.out.println("Data size: " + data.size());
NavigableMap<String, Number> sortedData = new TreeMap<>(data); NavigableMap<String, Number> sortedData = new TreeMap<>(data);
if(!sortedData.isEmpty()) { if(!sortedData.isEmpty() && sortedData.size()>50) {
finalResults.put(index,sortedData); finalResults.put(index,sortedData);
index += 1; index += 1;
} }
} }
System.out.println("\nFinal results size: "+finalResults.size());
// Defining a treemap to sort through the data // Defining a treemap to sort through the data
NavigableMap<Integer, Map> sortedFinalResults = new TreeMap<>(finalResults); NavigableMap<Integer, Map> sortedFinalResults = new TreeMap<>(finalResults);
...@@ -180,53 +236,11 @@ public class DB { ...@@ -180,53 +236,11 @@ public class DB {
System.out.printf("Timestamp: \t%s\t\t\tkWh: \t%s\n",entry.getKey(),entry.getValue()); System.out.printf("Timestamp: \t%s\t\t\tkWh: \t%s\n",entry.getKey(),entry.getValue());
} }
return sortedFinalResults; 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 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 * This variables are not sorted and thus this function iterates through the data in order to
...@@ -249,19 +263,43 @@ public class DB { ...@@ -249,19 +263,43 @@ public class DB {
Map<String, String> dates = new HashMap<>(); Map<String, String> dates = new HashMap<>();
// Preparing a query statement // Preparing a query statement
/*
final String sqlStatement = final String sqlStatement =
"SELECT InTidTork, UtTidTork FROM `sf-drying-optimization.124.int_gs_ds_sipalpackages`" + "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\" " + "WHERE Tork Like \"%5%\" AND InTidTork BETWEEN \"2021-01-30 08:51:03\" " +
"AND \"2022-03-15 11:10:09\" ORDER BY InTidTork ASC"; "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 // Retrieves the results from the queryjob
TableResult result = queryJob.getQueryResults(); TableResult result = createQueryJob(sqlStatement);
//System.out.println("InTidTork\t\t\tUtTidTork"); //System.out.println("InTidTork\t\t\tUtTidTork");
// Iterating through the results // Iterating through the results
for (FieldValueList row : result.iterateAll()) { for (FieldValueList row : result.iterateAll()) {
...@@ -270,18 +308,19 @@ public class DB { ...@@ -270,18 +308,19 @@ public class DB {
String formatedUtTidTork; String formatedUtTidTork;
// Retrieving the data // Retrieving the data
// InTidTork: // DryingStarted:
if(!row.get("InTidTork").isNull()){ if(!row.get("DryingStarted").isNull()){
long InTidTorkLong = row.get("InTidTork").getTimestampValue()/1000; long InTidTorkLong = row.get("DryingStarted").getTimestampValue()/1000;
// Formating the data from long to a string in the correct date format // Formating the data from long to a string in the correct date format
formatedInTidTork = getDateFormat().format(InTidTorkLong); formatedInTidTork = getDateFormat().format(InTidTorkLong);
} else { } else {
formatedInTidTork = ""; formatedInTidTork = "";
} }
// UtTidTork: // CalculatedStop:
if(!row.get("UtTidTork").isNull()){ // DryingCompleted:
long utTidTorkLong = row.get("UtTidTork").getTimestampValue()/1000; 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 // Formating the data from long to a string in the correct date format
formatedUtTidTork = getDateFormat().format(utTidTorkLong); formatedUtTidTork = getDateFormat().format(utTidTorkLong);
} else { } else {
...@@ -295,7 +334,10 @@ public class DB { ...@@ -295,7 +334,10 @@ public class DB {
} }
//System.out.printf("%s\t\t\t%s\n",formatedInTidTork,formatedUtTidTork); //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 // Defining a treemap to sort through the data
NavigableMap<String, String> dataSet = new TreeMap<>(dates); NavigableMap<String, String> dataSet = new TreeMap<>(dates);
...@@ -333,5 +375,7 @@ public class DB { ...@@ -333,5 +375,7 @@ public class DB {
} }
// Defining a treemap to sort through the data // Defining a treemap to sort through the data
return new TreeMap<>(sortedDates); return new TreeMap<>(sortedDates);
*/
} }
} }
\ No newline at end of file
...@@ -193,7 +193,7 @@ public class Main extends Application { ...@@ -193,7 +193,7 @@ public class Main extends Application {
@Override @Override
public void run(){ public void run(){
try { try {
Thread.sleep(10000); Thread.sleep(100000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -285,13 +285,13 @@ public class Main extends Application { ...@@ -285,13 +285,13 @@ public class Main extends Application {
String date = arr[0]; String date = arr[0];
int kwhValue = Integer.parseInt(arr[1]); 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 // Connect the data to a series
series.getData().add(new XYChart.Data(date,kwhValue)); series.getData().add(new XYChart.Data(date,kwhValue));
} }
lineChart.getData().add(series); 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()) { for (Map.Entry<Integer, Map> entryKwh : kWh.entrySet()) {
......
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment