Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import static com.application.DB.Constants.PROJECT_ID;
public class HelpingFunctions {
static Map<String,String> manMoist = new HashMap<>();
/**
* Creates a simple date format to use for converting millis in numbers to a usefull date format
*
* @return returns the date format
*/
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;
}
/**
* 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 for the database
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
*
* @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
*/
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();
}
/**
* Stores data about manual measurements for moisture level in the lumber
*
* @return a list containing the outdate for the lumber and the corresponding diff value
*/
public static Map<String, String> getManMoist() {
return manMoist;
}
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Function to iterate through all the Kwh values and storing them in a map
*
* @param data a map to store all the data
* @param baseline a baseline to base all the next values of to get a zero point
* @param result TableResult to iterate through
* @param kwhNameParameter Name of the Kwh name parameter in the database
* @param kwhTimestampNameParameter Name of the timestamp parameter in the database
*/
static void iterateKwhValues(Map<String, Number> data, int baseline, TableResult result, String kwhNameParameter, String kwhTimestampNameParameter) {
for (FieldValueList row : result.iterateAll()) {
// Sets the baseline in order to reset the kWh counter
if (baseline == 0) {
baseline = row.get("" + kwhNameParameter + "").getNumericValue().intValue();
}
// kWh value
int variantValue = row.get("" + kwhNameParameter + "").getNumericValue().intValue() - baseline; //-baseline
// Retrieving the wanted data
long timeStamp = row.get("" + kwhTimestampNameParameter + "").getTimestampValue() / 1000;
// Riktig format, men i string
String formatedTimeStamp = getDateFormat().format(timeStamp);
// Checks for negative values
if (variantValue > 0) {
// Adding the data to a list in order to sort through later
data.put(formatedTimeStamp, variantValue);
}
}
}