diff --git a/apps/counter/api/views.py b/apps/counter/api/views.py index 89d06f4..afadc54 100644 --- a/apps/counter/api/views.py +++ b/apps/counter/api/views.py @@ -1,6 +1,6 @@ import pytz from datetime import datetime, time -from django.db.models import Sum +from django.db.models import Sum, Max from rest_framework.generics import ( ListAPIView, RetrieveAPIView, @@ -44,9 +44,14 @@ class DeviceLogStatisticAPIView(APIView): tz = pytz.timezone("UTC") today = datetime.now(tz).date() midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None) - total_count = DeviceCount.objects.aggregate(total=Sum('count'))['total'] - daily_count = DeviceCount.objects.filter( - data_time__gt=midnight).aggregate(total=Sum('count'))['total'] + # total_count = DeviceCount.objects.aggregate(total=Sum('count'))['total'] + total_count_queryset = DeviceCount.objects.values('device_id').annotate(count=Max('count')) + total_count = sum(map(lambda x: int(x['count']), total_count_queryset)) + + one_day_ago_queryset = DeviceCount.objects.filter( + data_time__gt=midnight).values('device_id').annotate(count=Max('count')) + one_day_ago_count = sum(map(lambda x: int(x['count']), one_day_ago_queryset)) + daily_count = total_count - one_day_ago_count data = {'total_count': total_count, 'daily_count': daily_count} return Response(data)