Skip to content

Commit

Permalink
Fix error when successive timestamps are within the same minute
Browse files Browse the repository at this point in the history
While having successive timestamps within the same minute, such as
18:42:00 and 18:42:10, is not yet fully supported, sometimes the
database has such cases (primarily because of telemetric system bugs).
This was causing a server error when attempting to get such a time
series.
  • Loading branch information
aptiko committed Jul 17, 2024
1 parent 25639a4 commit 1df3c1c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion enhydris/models/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def _retrieve_and_cache_data(self, start_date, end_date):
cursor.execute(
"""
SELECT STRING_AGG(
TO_CHAR(timestamp AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI')
TO_CHAR(timestamp AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS')
|| ','
|| CASE WHEN value is NULL THEN DOUBLE PRECISION 'NaN'
ELSE value END
Expand Down
27 changes: 27 additions & 0 deletions enhydris/tests/test_models/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,33 @@ def test_data(self):
pd.testing.assert_frame_equal(self.data.data, self.expected_result)


class TimeseriesGetDataWithCloseTimestampsTestCase(DataTestCase):
"""Test get_data when two timestamps are within the same minute.
While having successive timestamps within the same minute, such as 18:42:00 and
18:42:10, is not yet fully supported, sometimes the database has such cases
(primarily because of telemetric system bugs). We test that get_data can handle
them.
"""

def setUp(self):
tzinfo = get_tzinfo("Etc/GMT")
self.data = pd.DataFrame(
data={"value": [1.0, 2.0], "flags": ["", ""]},
columns=["value", "flags"],
index=[
dt.datetime(2024, 7, 17, 18, 42, 0, tzinfo=tzinfo),
dt.datetime(2024, 7, 17, 18, 42, 10, tzinfo=tzinfo),
],
)
self.data.index.name = "date"
self.timeseries.set_data(self.data)

def test_get_data(self):
data = self.timeseries.get_data(timezone="Etc/GMT")
pd.testing.assert_frame_equal(data.data, self.data)


class TimeseriesGetDataInDifferentTimezoneTestCase(DataTestCase):
@classmethod
def setUpClass(cls):
Expand Down

0 comments on commit 1df3c1c

Please sign in to comment.