Fixed period api

This commit is contained in:
stepan323446 2025-12-19 10:09:36 +01:00
parent 6f13e5bf3b
commit 7332509976

View File

@ -116,29 +116,32 @@ class StatsByPeriodAPI(APIView):
elif period == WeatherPeriods.hour:
start_time = now - timedelta(hours=1)
trunc_func = TruncMinute
step = 10
step = 1
elif period == WeatherPeriods.hour_6:
start_time = now - timedelta(hours=6)
trunc_func = TruncHour
step = 1
elif period == WeatherPeriods.hour_12:
start_time = now - timedelta(hours=12)
trunc_func = TruncHour
step = 1
elif period == WeatherPeriods.hour_24:
start_time = now - timedelta(hours=24)
trunc_func = TruncHour
step = 1
qs = (
WeatherStats.objects
.filter(created_at__gte=start_time)
.annotate(time_slot=trunc_func('created_at'))
.values('time_slot')
.annotate(date=trunc_func('created_at'))
.values('date')
.annotate(
humidity_air=Avg('humidity_air'),
humidity_ground=Avg('humidity_ground'),
temperature=Avg('temperature'),
light=Avg('light')
)
.order_by('-time_slot')
.order_by('-date')
)
data_dict = {entry['date']: entry for entry in qs}