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

Merge branch 'Eilerts_branch' into 'master'

Updated DB file

See merge request mesji/bacheloroppgave_2022!27
parents 25e1e467 979e4d5a
Branches
No related tags found
No related merge requests found
......@@ -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
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);
}
}
}
......@@ -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;
......
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.
Please register or to comment