From 90be855944233ab874a58957faad4de870ae1da8 Mon Sep 17 00:00:00 2001 From: staudtMarius Date: Wed, 20 Nov 2024 08:57:16 +0100 Subject: [PATCH 1/2] Enhance `TimeSeriesSource` with method to retrieve the previous value before a given key. --- CHANGELOG.md | 1 + .../datamodel/io/source/TimeSeriesSource.java | 4 +- .../io/source/csv/CsvTimeSeriesSource.java | 9 ++++ .../io/source/sql/SqlTimeSeriesSource.java | 47 +++++++++++++++---- .../individual/IndividualTimeSeries.java | 4 +- .../repetitive/LoadProfileInput.java | 4 +- .../source/csv/CsvTimeSeriesSourceIT.groovy | 20 ++++++++ .../source/sql/SqlTimeSeriesSourceIT.groovy | 10 ++++ .../IndividualTimeSeriesTest.groovy | 4 +- 9 files changed, 88 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c6c590e8..e53f08eba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Enhance `TimeSeriesSource` with method to retrieve all time keys after a given key [#543](https://github.com/ie3-institute/PowerSystemDataModel/issues/543) - Enhance `WeatherSource` with method to retrieve all time keys after a given key [#572](https://github.com/ie3-institute/PowerSystemDataModel/issues/572) +- Enhance `TimeSeriesSource` with method to retrieve the previous value before a given key [#1182](https://github.com/ie3-institute/PowerSystemDataModel/issues/1182) ### Fixed diff --git a/src/main/java/edu/ie3/datamodel/io/source/TimeSeriesSource.java b/src/main/java/edu/ie3/datamodel/io/source/TimeSeriesSource.java index a9ade054e..2a80e0a78 100644 --- a/src/main/java/edu/ie3/datamodel/io/source/TimeSeriesSource.java +++ b/src/main/java/edu/ie3/datamodel/io/source/TimeSeriesSource.java @@ -50,7 +50,9 @@ protected Try, FactoryException> createTimeBasedValue( public abstract IndividualTimeSeries getTimeSeries(ClosedInterval timeInterval) throws SourceException; - public abstract Optional getValue(ZonedDateTime time) throws SourceException; + public abstract Optional getValue(ZonedDateTime time); + + public abstract Optional> getPreviousTimeBasedValue(ZonedDateTime time); /** * Method to return all time keys after a given timestamp. diff --git a/src/main/java/edu/ie3/datamodel/io/source/csv/CsvTimeSeriesSource.java b/src/main/java/edu/ie3/datamodel/io/source/csv/CsvTimeSeriesSource.java index 1bbab427a..0210b7dde 100644 --- a/src/main/java/edu/ie3/datamodel/io/source/csv/CsvTimeSeriesSource.java +++ b/src/main/java/edu/ie3/datamodel/io/source/csv/CsvTimeSeriesSource.java @@ -128,6 +128,15 @@ public Optional getValue(ZonedDateTime time) { return timeSeries.getValue(time); } + @Override + public Optional> getPreviousTimeBasedValue(ZonedDateTime time) { + return timeSeries.getPreviousTimeBasedValue(time); + } + + public Optional> getNextTimeBasedValue(ZonedDateTime time) { + return timeSeries.getNextTimeBasedValue(time); + } + @Override public List getTimeKeysAfter(ZonedDateTime time) { return timeSeries.getTimeKeysAfter(time); diff --git a/src/main/java/edu/ie3/datamodel/io/source/sql/SqlTimeSeriesSource.java b/src/main/java/edu/ie3/datamodel/io/source/sql/SqlTimeSeriesSource.java index c75e8b949..8743869b2 100644 --- a/src/main/java/edu/ie3/datamodel/io/source/sql/SqlTimeSeriesSource.java +++ b/src/main/java/edu/ie3/datamodel/io/source/sql/SqlTimeSeriesSource.java @@ -48,6 +48,7 @@ public class SqlTimeSeriesSource extends TimeSeriesSource { private final String queryTimeInterval; private final String queryTimeKeysAfter; + private final String queryForValueBefore; private final String queryTime; public SqlTimeSeriesSource( @@ -63,15 +64,16 @@ public SqlTimeSeriesSource( final ColumnScheme columnScheme = ColumnScheme.parse(valueClass).orElseThrow(); this.tableName = sqlDataSource.databaseNamingStrategy.getTimeSeriesEntityName(columnScheme); + String schemaName = sqlDataSource.schemaName; + String dbTimeColumnName = sqlDataSource.getDbColumnName(factory.getTimeFieldString(), tableName); - this.queryFull = createQueryFull(sqlDataSource.schemaName, tableName); - this.queryTimeInterval = - createQueryForTimeInterval(sqlDataSource.schemaName, tableName, dbTimeColumnName); - this.queryTimeKeysAfter = - createQueryForTimeKeysAfter(sqlDataSource.schemaName, tableName, dbTimeColumnName); - this.queryTime = createQueryForTime(sqlDataSource.schemaName, tableName, dbTimeColumnName); + this.queryFull = createQueryFull(schemaName, tableName); + this.queryTimeInterval = createQueryForTimeInterval(schemaName, tableName, dbTimeColumnName); + this.queryTimeKeysAfter = createQueryForTimeKeysAfter(schemaName, tableName, dbTimeColumnName); + this.queryForValueBefore = createQueryForValueBefore(schemaName, tableName, dbTimeColumnName); + this.queryTime = createQueryForTime(schemaName, tableName, dbTimeColumnName); } /** @@ -179,6 +181,14 @@ public Optional getValue(ZonedDateTime time) { return Optional.of(timeBasedValues.stream().toList().get(0).getValue()); } + @Override + public Optional> getPreviousTimeBasedValue(ZonedDateTime time) { + return getTimeBasedValueSet( + queryForValueBefore, ps -> ps.setTimestamp(1, Timestamp.from(time.toInstant()))) + .stream() + .max(TimeBasedValue::compareTo); + } + @Override public List getTimeKeysAfter(ZonedDateTime time) { return dataSource @@ -278,8 +288,29 @@ private String createQueryForTimeKeysAfter( } /** - * Creates a basic query to retrieve an entry for the given time series uuid and time with the - * following pattern:
+ * Creates a base query to retrieve all time keys after a given time for given time series with + * the following pattern:
+ * {@code WHERE time_series = $timeSeriesUuid AND