Skip to content

Commit 5afea83

Browse files
Ken LippoldKen Lippold
authored andcommitted
Fix update phenomenon_time
1 parent fe586d2 commit 5afea83

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

sta/services/sensorthings/observation.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from uuid import UUID
33
from datetime import datetime
44
from typing import Optional
5+
from django.db.models import Min, Max, Count
56
from django.db.utils import IntegrityError, DatabaseError, DataError
67
from psycopg.errors import UniqueViolation
78
from ninja.errors import HttpError
@@ -312,9 +313,16 @@ def delete_observations(
312313
@staticmethod
313314
def update_value_count(datastream_id: UUID) -> None:
314315

315-
observation_query = Observation.objects.filter(datastream_id=datastream_id)
316+
aggregate = Observation.objects.filter(datastream_id=datastream_id).aggregate(
317+
min_time=Min("phenomenon_time"),
318+
max_time=Max("phenomenon_time"),
319+
count=Count("id"),
320+
)
316321

317322
datastream = Datastream.objects.get(pk=datastream_id)
318323

319-
datastream.value_count = int(observation_query.count())
324+
datastream.phenomenon_begin_time = aggregate["min_time"]
325+
datastream.phenomenon_end_time = aggregate["max_time"]
326+
datastream.value_count = aggregate["count"]
327+
320328
datastream.save()

0 commit comments

Comments
 (0)