-
Notifications
You must be signed in to change notification settings - Fork 5
/
SqlWeatherSource.java
218 lines (196 loc) · 8.46 KB
/
SqlWeatherSource.java
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
69
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
112
113
114
115
116
117
118
119
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* © 2021. TU Dortmund University,
* Institute of Energy Systems, Energy Efficiency and Energy Economics,
* Research group Distribution grid planning and operation
*/
package edu.ie3.datamodel.io.source.sql;
import static edu.ie3.datamodel.io.source.sql.SqlDataSource.createBaseQueryString;
import edu.ie3.datamodel.exceptions.SourceException;
import edu.ie3.datamodel.io.connectors.SqlConnector;
import edu.ie3.datamodel.io.factory.timeseries.TimeBasedWeatherValueFactory;
import edu.ie3.datamodel.io.naming.DatabaseNamingStrategy;
import edu.ie3.datamodel.io.source.IdCoordinateSource;
import edu.ie3.datamodel.io.source.WeatherSource;
import edu.ie3.datamodel.models.timeseries.individual.IndividualTimeSeries;
import edu.ie3.datamodel.models.timeseries.individual.TimeBasedValue;
import edu.ie3.datamodel.models.value.WeatherValue;
import edu.ie3.util.interval.ClosedInterval;
import java.sql.*;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.stream.Collectors;
import org.locationtech.jts.geom.Point;
/** SQL source for weather data */
public class SqlWeatherSource extends WeatherSource {
private final SqlDataSource dataSource;
private static final String WHERE = " WHERE ";
private final String factoryCoordinateFieldName;
/**
* Queries that are available within this source. Motivation to have them as field value is to
* avoid creating a new string each time, bc they're always the same.
*/
private final String queryTimeInterval;
private final String queryTimeAndCoordinate;
private final String queryTimeIntervalAndCoordinates;
/**
* Initializes a new SqlWeatherSource
*
* @param connector the connector needed for database connection
* @param idCoordinateSource a coordinate source to map ids to points
* @param schemaName the database schema to use
* @param weatherTableName the name of the table containing weather data
* @param weatherFactory instance of a time based weather value factory
*/
public SqlWeatherSource(
SqlConnector connector,
IdCoordinateSource idCoordinateSource,
String schemaName,
String weatherTableName,
TimeBasedWeatherValueFactory weatherFactory)
throws SQLException {
super(idCoordinateSource, weatherFactory);
this.factoryCoordinateFieldName = weatherFactory.getCoordinateIdFieldString();
this.dataSource = new SqlDataSource(connector, schemaName, new DatabaseNamingStrategy());
dataSource.connector.validateDBTable(weatherTableName, WeatherValue.class, weatherFactory);
String dbTimeColumnName =
dataSource.getDbColumnName(weatherFactory.getTimeFieldString(), weatherTableName);
String dbCoordinateIdColumnName =
dataSource.getDbColumnName(factoryCoordinateFieldName, weatherTableName);
// setup queries
this.queryTimeInterval =
createQueryStringForTimeInterval(schemaName, weatherTableName, dbTimeColumnName);
this.queryTimeAndCoordinate =
createQueryStringForTimeAndCoordinate(
schemaName, weatherTableName, dbTimeColumnName, dbCoordinateIdColumnName);
this.queryTimeIntervalAndCoordinates =
createQueryStringForTimeIntervalAndCoordinates(
schemaName, weatherTableName, dbTimeColumnName, dbCoordinateIdColumnName);
}
@Override
public Map<Point, IndividualTimeSeries<WeatherValue>> getWeather(
ClosedInterval<ZonedDateTime> timeInterval) throws SourceException {
List<TimeBasedValue<WeatherValue>> timeBasedValues =
buildTimeBasedValues(
weatherFactory,
dataSource.executeQuery(
queryTimeInterval,
ps -> {
ps.setTimestamp(1, Timestamp.from(timeInterval.getLower().toInstant()));
ps.setTimestamp(2, Timestamp.from(timeInterval.getUpper().toInstant()));
}));
return mapWeatherValuesToPoints(timeBasedValues);
}
@Override
public Map<Point, IndividualTimeSeries<WeatherValue>> getWeather(
ClosedInterval<ZonedDateTime> timeInterval, Collection<Point> coordinates)
throws SourceException {
Set<Integer> coordinateIds =
coordinates.stream()
.map(idCoordinateSource::getId)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
if (coordinateIds.isEmpty()) {
log.warn("Unable to match coordinates to coordinate ID");
return Collections.emptyMap();
}
List<TimeBasedValue<WeatherValue>> timeBasedValues =
buildTimeBasedValues(
weatherFactory,
dataSource.executeQuery(
queryTimeIntervalAndCoordinates,
ps -> {
Array coordinateIdArr =
ps.getConnection().createArrayOf("integer", coordinateIds.toArray());
ps.setArray(1, coordinateIdArr);
ps.setTimestamp(2, Timestamp.from(timeInterval.getLower().toInstant()));
ps.setTimestamp(3, Timestamp.from(timeInterval.getUpper().toInstant()));
}));
return mapWeatherValuesToPoints(timeBasedValues);
}
@Override
public Optional<TimeBasedValue<WeatherValue>> getWeather(ZonedDateTime date, Point coordinate)
throws SourceException {
Optional<Integer> coordinateId = idCoordinateSource.getId(coordinate);
if (coordinateId.isEmpty()) {
log.warn("Unable to match coordinate {} to a coordinate ID", coordinate);
return Optional.empty();
}
List<TimeBasedValue<WeatherValue>> timeBasedValues =
buildTimeBasedValues(
weatherFactory,
dataSource.executeQuery(
queryTimeAndCoordinate,
ps -> {
ps.setInt(1, coordinateId.get());
ps.setTimestamp(2, Timestamp.from(date.toInstant()));
}));
if (timeBasedValues.isEmpty()) return Optional.empty();
if (timeBasedValues.size() > 1)
log.warn("Retrieved more than one result value, using the first");
return Optional.of(timeBasedValues.get(0));
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
/**
* Creates a base query to retrieve all entities in the given time frame with the following
* pattern: <br>
* {@code <base query> WHERE <time column> BETWEEN ? AND ?;}
*
* @param schemaName the name of the database schema
* @param weatherTableName the name of the database table
* @param timeColumnName the name of the column holding the timestamp info
* @return the query string
*/
private static String createQueryStringForTimeInterval(
String schemaName, String weatherTableName, String timeColumnName) {
return createBaseQueryString(schemaName, weatherTableName)
+ WHERE
+ timeColumnName
+ " BETWEEN ? AND ?;";
}
/**
* Creates a basic query to retrieve an entry for the given time and coordinate with the following
* pattern: <br>
* {@code <base query> WHERE <coordinate column>=? AND <time column>=?;}
*
* @param schemaName the name of the database schema
* @param weatherTableName the name of the database table
* @param timeColumnName the name of the column holding the timestamp info
* @param coordinateColumnName name of the column holding the coordinate id
* @return the query string
*/
private String createQueryStringForTimeAndCoordinate(
String schemaName,
String weatherTableName,
String timeColumnName,
String coordinateColumnName) {
return createBaseQueryString(schemaName, weatherTableName)
+ WHERE
+ coordinateColumnName
+ "=? AND "
+ timeColumnName
+ "=?;";
}
/**
* Creates a basic query to retrieve all entities in the given time frame and coordinates with the
* following pattern: <br>
* {@code <base query> WHERE <coordinate column>= ANY (?) AND <time column> BETWEEN ? AND ?;}
*
* @param schemaName the name of the database schema
* @param weatherTableName the name of the database table
* @param timeColumnName the name of the column holding the timestamp info
* @param coordinateColumnName name of the column holding the coordinate id
* @return the query string
*/
private String createQueryStringForTimeIntervalAndCoordinates(
String schemaName,
String weatherTableName,
String timeColumnName,
String coordinateColumnName) {
return createBaseQueryString(schemaName, weatherTableName)
+ WHERE
+ coordinateColumnName
+ "= ANY (?) AND "
+ timeColumnName
+ " BETWEEN ? AND ?;";
}
}