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: elif period == WeatherPeriods.hour:
start_time = now - timedelta(hours=1) start_time = now - timedelta(hours=1)
trunc_func = TruncMinute trunc_func = TruncMinute
step = 10 step = 1
elif period == WeatherPeriods.hour_6: elif period == WeatherPeriods.hour_6:
start_time = now - timedelta(hours=6) start_time = now - timedelta(hours=6)
trunc_func = TruncHour trunc_func = TruncHour
step = 1
elif period == WeatherPeriods.hour_12: elif period == WeatherPeriods.hour_12:
start_time = now - timedelta(hours=12) start_time = now - timedelta(hours=12)
trunc_func = TruncHour trunc_func = TruncHour
step = 1
elif period == WeatherPeriods.hour_24: elif period == WeatherPeriods.hour_24:
start_time = now - timedelta(hours=24) start_time = now - timedelta(hours=24)
trunc_func = TruncHour trunc_func = TruncHour
step = 1
qs = ( qs = (
WeatherStats.objects WeatherStats.objects
.filter(created_at__gte=start_time) .filter(created_at__gte=start_time)
.annotate(time_slot=trunc_func('created_at')) .annotate(date=trunc_func('created_at'))
.values('time_slot') .values('date')
.annotate( .annotate(
humidity_air=Avg('humidity_air'), humidity_air=Avg('humidity_air'),
humidity_ground=Avg('humidity_ground'), humidity_ground=Avg('humidity_ground'),
temperature=Avg('temperature'), temperature=Avg('temperature'),
light=Avg('light') light=Avg('light')
) )
.order_by('-time_slot') .order_by('-date')
) )
data_dict = {entry['date']: entry for entry in qs} data_dict = {entry['date']: entry for entry in qs}