Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Bacheloroppgave_2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Eilert Tunheim
Bacheloroppgave_2022
Commits
96d0c351
Commit
96d0c351
authored
3 years ago
by
Eilert Tunheim
Browse files
Options
Downloads
Patches
Plain Diff
Added functional functionality to compare dates and finds the date that will reset kwh to 0!
parent
824e2b1e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/com/application/DB/DB.java
+102
-36
102 additions, 36 deletions
src/main/java/com/application/DB/DB.java
target/classes/com/application/DB/DB.class
+0
-0
0 additions, 0 deletions
target/classes/com/application/DB/DB.class
with
102 additions
and
36 deletions
src/main/java/com/application/DB/DB.java
+
102
−
36
View file @
96d0c351
...
...
@@ -24,7 +24,12 @@ import java.util.*;
public
class
DB
{
private
static
Job
queryJob
;
private
static
SimpleDateFormat
dateFormat
;
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
...
...
@@ -46,7 +51,9 @@ public class DB {
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
getKwh
();
//getKwh();
//getName();
getZeroPointDate
();
}
...
...
@@ -106,15 +113,11 @@ public class DB {
// Initializing the data map to store the results
Map
<
String
,
Number
>
data
=
new
HashMap
<>();
// Initializing the date format
DateTimeFormatter
format
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm:ss"
);
DateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
// 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-0
1-28
11:10:09\" ORDER BY TimeStamp ASC"
;
"WHERE TimeStamp BETWEEN \"2021-01-
30
08:51:03\" "
+
"AND \"2021-0
2-15
11:10:09\" ORDER BY TimeStamp ASC"
;
// Creates a job configuration
queryJob
=
getJob
(
QueryJobConfiguration
.
newBuilder
(
sqlStatement
).
build
());
...
...
@@ -130,35 +133,19 @@ public class DB {
// Retrieving the wanted data
long
timeStamp
=
row
.
get
(
"TimeStamp"
).
getTimestampValue
()/
1000
;
// Riktig format, men i string
String
formatedTimeStamp
=
dateFormat
.
format
(
timeStamp
);
// Format the date to LocalDateTime datatype
LocalDateTime
date
=
LocalDateTime
.
parse
(
formatedTimeStamp
,
format
);
// Creates a future date to compare against
LocalDateTime
dateFuture
=
date
.
plusDays
(
1
);
//System.out.println(date);
String
formatedTimeStamp
=
getDateFormat
().
format
(
timeStamp
);
// Adding the data to a list in order to sort through later
data
.
put
(
formatedTimeStamp
,
variantValue
);
}
/*
NavigableMap<String, Number> sortedData = new TreeMap<>(data);
for (Map.Entry<String, Number> entry : sortedData.entrySet()) {
String key = entry.getKey();
String next = sortedData.higherEntry(entry.getKey()).getKey(); // next
//System.out.println("Nå: "+key);
//System.out.printf("Neste: %s\n\n",next);
}
*/
//NavigableMap<String, Number> sortedData = new TreeMap<>(data);
return
data
;
}
/**
* Retrieves information about kwh and the corresponding date
*
* @return the results
* @throws Exception returns potential error
*/
public
static
void
getName
()
throws
Exception
{
...
...
@@ -167,12 +154,6 @@ public class DB {
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`"
+
...
...
@@ -193,15 +174,100 @@ public class DB {
long
calculatedStart
=
row
.
get
(
"CalculatedStart"
).
getTimestampValue
()/
1000
;
long
calculatedStop
=
row
.
get
(
"CalculatedStop"
).
getTimestampValue
()/
1000
;
// Formatting the dates
String
formattedCalculatedStart
=
d
ateFormat
.
format
(
calculatedStart
);
String
formattedCalculatedStop
=
d
ateFormat
.
format
(
calculatedStop
);
String
formattedCalculatedStart
=
getD
ateFormat
()
.
format
(
calculatedStart
);
String
formattedCalculatedStop
=
getD
ateFormat
()
.
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);
System
.
out
.
printf
(
"%s\t\t\t%s\t\t\t%s\n"
,
name
,
formattedCalculatedStart
,
formattedCalculatedStop
);
}
//return data;
}
private
static
void
getZeroPointDate
()
throws
Exception
{
// Initializing a date format in the data type DateTimeFormatter
// Required for iterating through the dates.
DateTimeFormatter
format
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm:ss"
);
// Initializing the data map to store the results
Map
<
String
,
String
>
dates
=
new
HashMap
<>();
// Preparing a query statement
final
String
sqlStatement
=
"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\" "
+
"AND \"2022-03-15 11:10:09\" ORDER BY InTidTork ASC"
;
// Creates a job configuration
queryJob
=
getJob
(
QueryJobConfiguration
.
newBuilder
(
sqlStatement
).
build
());
// Iterating through the results
TableResult
result
=
queryJob
.
getQueryResults
();
System
.
out
.
println
(
"InTidTork\t\t\tUtTidTork"
);
for
(
FieldValueList
row
:
result
.
iterateAll
())
{
// Defining variables
String
formatedInTidTork
;
String
formatedUtTidTork
;
// Retrieving the wanted data
// InTidTork:
if
(!
row
.
get
(
"InTidTork"
).
isNull
()){
long
InTidTorkLong
=
row
.
get
(
"InTidTork"
).
getTimestampValue
()/
1000
;
// Formating the data from long to a string in the correct date format
formatedInTidTork
=
getDateFormat
().
format
(
InTidTorkLong
);
}
else
{
formatedInTidTork
=
""
;
}
// UtTidTork:
if
(!
row
.
get
(
"UtTidTork"
).
isNull
()){
long
utTidTorkLong
=
row
.
get
(
"UtTidTork"
).
getTimestampValue
()/
1000
;
// Formating the data from long to a string in the correct date format
formatedUtTidTork
=
getDateFormat
().
format
(
utTidTorkLong
);
}
else
{
formatedUtTidTork
=
""
;
}
dates
.
put
(
formatedInTidTork
,
formatedUtTidTork
);
//System.out.printf("%s\t\t\t%s\n",formatedInTidTork,formatedUtTidTork);
}
NavigableMap
<
String
,
String
>
dataSet
=
new
TreeMap
<>(
dates
);
/*
for (Map.Entry<String, String> entry : dataSet.entrySet()) {
System.out.printf("Key: %s\t\t\tValue: %s\n",entry.getKey(),entry.getValue());
}
*/
// Iterating through the data in order to find and set a zeropoint for the dates.
for
(
Map
.
Entry
<
String
,
String
>
entry
:
dataSet
.
entrySet
())
{
// Retrieving the entry key
String
key
=
entry
.
getKey
();
// Format the date to LocalDateTime datatype
LocalDateTime
date
=
LocalDateTime
.
parse
(
key
,
format
);
// Creates a future date to compare against
LocalDateTime
dateNowPlus
=
date
.
plusDays
(
1
);
String
formatedDateNowPlus
=
format
.
format
(
dateNowPlus
);
try
{
String
next
=
dataSet
.
higherEntry
(
entry
.
getKey
()).
getKey
();
// next
System
.
out
.
printf
(
"Current date: %s\t\t\tPluss 1: %s\t\t\tNext date: %s\n"
,
key
,
formatedDateNowPlus
,
next
);
//System.out.printf("Neste: %s\n\n",next);
if
(
next
.
compareTo
(
formatedDateNowPlus
)
>
0
){
System
.
out
.
printf
(
"Inni if!!@@, Er next mindre enn formated?\nNext, skal være større: %s\nFormated, skal være mindre: %s\n\n"
,
next
,
formatedDateNowPlus
);
}
}
catch
(
NullPointerException
e
){
System
.
out
.
println
(
e
.
getMessage
());
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
target/classes/com/application/DB/DB.class
+
0
−
0
View file @
96d0c351
No preview for this file type
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment