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

Merge branch 'Eilerts_branch' into 'master'

Updated DB

See merge request mesji/bacheloroppgave_2022!24
parents e26419c0 79653c51
No related branches found
No related tags found
No related merge requests found
...@@ -2,28 +2,30 @@ package com.application.DB; ...@@ -2,28 +2,30 @@ package com.application.DB;
import com.google.auth.oauth2.GoogleCredentials; import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials; import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.*;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.Job;
import com.google.cloud.bigquery.JobInfo;
import com.google.cloud.bigquery.QueryJobConfiguration;
import com.google.cloud.bigquery.TableResult;
import com.sun.media.jfxmedia.AudioClip;
import org.apache.commons.codec.binary.Hex;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Date;
import java.util.TimeZone;
/**
* This class is responsible for handling database related activities
*
* @author Eilert Tunheim, Karin Pettersen, Mads Arnesen
* @version 1.0
*/
public class DB { public class DB {
private static Map<String, Number> data;
private static QueryJobConfiguration queryConfig;
private static Job queryJob;
/**
* Retrieves the credentials file
* @return
* @throws Exception
*/
private static GoogleCredentials getCredentials() throws Exception { private static GoogleCredentials getCredentials() throws Exception {
File credentialsPath = new File("./src/main/resources/com.application/sf-drying-optimization-1e234ad2b0f4.json"); File credentialsPath = new File("./src/main/resources/com.application/sf-drying-optimization-1e234ad2b0f4.json");
...@@ -37,31 +39,31 @@ public class DB { ...@@ -37,31 +39,31 @@ public class DB {
return credentials; return credentials;
} }
/**
* Creates a builder
* @return a builder
* @throws Exception returns potential error
*/
private static BigQuery getBuilder() throws Exception {
// Step 1: Initialize BigQuery service // Step 1: Initialize BigQuery service
// Here we set our project ID and get the `BigQuery` service object // Here we set our project ID and get the `BigQuery` service object
// this is the interface to our BigQuery instance that // this is the interface to our BigQuery instance that
// we use to execute jobs on // we use to execute jobs on
private static BigQuery getBuilder() throws Exception {
return BigQueryOptions.newBuilder(). return BigQueryOptions.newBuilder().
setCredentials(getCredentials()). setCredentials(getCredentials()).
setProjectId("sf-drying-optimization") setProjectId("sf-drying-optimization")
.build().getService(); .build().getService();
} }
public static void getFromExistingTable() throws Exception {
//ArrayList<String, Integer> results = new ArrayList<>();
// 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 InTidTork, UtTidTork " +
"FROM sf-drying-optimization.124.int_gs_ds_sipalpackages " +
"WHERE Tork LIKE \"%5%\" AND InTidTork BETWEEN \"2020-05-14 12:51:03\" " +
"AND \"2022-03-03 16:10:09\" ORDER BY InTidTork";
QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(GET_WORD_COUNT).build();
/**
* 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 // Step 3: Run the job on BigQuery
// create a `Job` instance from the job configuration using the BigQuery service // create a `Job` instance from the job configuration using the BigQuery service
// the job starts executing once the `create` method executes // the job starts executing once the `create` method executes
...@@ -77,6 +79,31 @@ public class DB { ...@@ -77,6 +79,31 @@ public class DB {
if (queryJob.getStatus().getError() != null) { if (queryJob.getStatus().getError() != null) {
throw new Exception(queryJob.getStatus().getError().toString()); throw new Exception(queryJob.getStatus().getError().toString());
} }
return queryJob;
}
/**
* Retrieves information about kwh and the corresponding date
*
* @return the results
* @throws Exception returns potential error
*/
public static Map<String, Number> getKwh() throws Exception {
// Initializing a data
data = new HashMap<>();
// Preparing a query job
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(queryConfig);
// Step 4: Display results // Step 4: Display results
// Print out a header line, and iterate through the // Print out a header line, and iterate through the
...@@ -89,13 +116,14 @@ public class DB { ...@@ -89,13 +116,14 @@ public class DB {
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
newFormat.setTimeZone(TimeZone.getTimeZone("UTC")); newFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
long inTidTork = row.get("InTidTork").getTimestampValue()/1000; long timeStamp = row.get("TimeStamp").getTimestampValue()/1000;
long utTidTork = row.get("UtTidTork").getTimestampValue()/1000; String formatedTimeStamp = newFormat.format(timeStamp);
int variantValue = row.get("VariantValue").getNumericValue().intValue();
String formatedInTidTork = newFormat.format(inTidTork); data.put(formatedTimeStamp, variantValue);
String formatedUtTidTork = newFormat.format(utTidTork);
System.out.printf("%s\t\t\t\t\t%s\n", formatedInTidTork, formatedUtTidTork);
} }
return data;
} }
} }
\ No newline at end of file
...@@ -24,7 +24,7 @@ import javafx.scene.chart.XYChart; ...@@ -24,7 +24,7 @@ import javafx.scene.chart.XYChart;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.*;
/** /**
* This class launches the application * This class launches the application
...@@ -38,7 +38,7 @@ public class Main extends Application { ...@@ -38,7 +38,7 @@ public class Main extends Application {
private HBox menuBar; private HBox menuBar;
private HBox logoBar; private HBox logoBar;
private VBox sideBar; private VBox sideBar;
private LineChart lineChart; private LineChart<String, Number> lineChart;
private MenuBar menubar2; private MenuBar menubar2;
/** /**
...@@ -99,8 +99,6 @@ public class Main extends Application { ...@@ -99,8 +99,6 @@ public class Main extends Application {
VBox.setVgrow(this.logoBar, Priority.ALWAYS); VBox.setVgrow(this.logoBar, Priority.ALWAYS);
DB.getFromExistingTable();
// Sets the scene and defines boundaries // Sets the scene and defines boundaries
//Scene scene = new Scene(root, 1200, 600); //Scene scene = new Scene(root, 1200, 600);
Scene scene = new Scene(root, 1200, 600); Scene scene = new Scene(root, 1200, 600);
...@@ -187,31 +185,23 @@ public class Main extends Application { ...@@ -187,31 +185,23 @@ public class Main extends Application {
} }
private LineChart<String,Number> createLineChart() { private LineChart<String,Number> createLineChart() throws Exception {
final CategoryAxis xAxis = new CategoryAxis(); final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Month"); xAxis.setLabel("Date");
final LineChart<String, Number> lineChart = final LineChart<String, Number> lineChart =
new LineChart<String, Number>(xAxis, yAxis); new LineChart<>(xAxis, yAxis);
lineChart.setTitle("Drying Process");
lineChart.setTitle("Stock Monitoring, 2010"); Map<String, Number> map = DB.getKwh();
Map<String, Number> treeMap = new TreeMap<>(map);
XYChart.Series series1 = new XYChart.Series(); XYChart.Series series1 = new XYChart.Series();
series1.setName("Portfolio 1"); series1.setName("Drying 1");
treeMap.forEach((key, value) -> series1.getData().add(new XYChart.Data(key,value)));
series1.getData().add(new XYChart.Data("Jan", 23));
series1.getData().add(new XYChart.Data("Feb", 14));
series1.getData().add(new XYChart.Data("Mar", 15));
series1.getData().add(new XYChart.Data("Apr", 24));
series1.getData().add(new XYChart.Data("May", 34));
series1.getData().add(new XYChart.Data("Jun", 36));
series1.getData().add(new XYChart.Data("Jul", 22));
series1.getData().add(new XYChart.Data("Aug", 45));
series1.getData().add(new XYChart.Data("Sep", 43));
series1.getData().add(new XYChart.Data("Oct", 17));
series1.getData().add(new XYChart.Data("Nov", 29));
series1.getData().add(new XYChart.Data("Dec", 25));
lineChart.getData().add(series1); lineChart.getData().add(series1);
return lineChart; return lineChart;
......
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