From 6ef78351df75f27a9ef7cafa8e7fe6941caf4b74 Mon Sep 17 00:00:00 2001 From: xianfuxing Date: Sat, 11 Aug 2018 14:37:13 +0800 Subject: [PATCH] # bug fixed: statistic using annotate return other filed --- apps/counter/api/views.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/apps/counter/api/views.py b/apps/counter/api/views.py index 5999cc3..ce5242f 100644 --- a/apps/counter/api/views.py +++ b/apps/counter/api/views.py @@ -46,17 +46,19 @@ class DeviceLogStatisticAPIView(APIView): midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None) # calc total count - total_count_queryset = DeviceCount.objects.values('device_id').annotate(count=Max('count')) + total_count_queryset = [DeviceCount.objects.get(device_id=x['device_id'], data_time=x['max_time']) + for x in DeviceCount.objects.values('device_id').annotate(max_time=Max('data_time'))] if total_count_queryset: - total_count = sum(map(lambda x: int(x['count']), total_count_queryset)) + total_count = sum(map(lambda x: int(x.count), total_count_queryset)) else: total_count = 0 # calc daily count - one_day_ago_queryset = DeviceCount.objects.filter( - data_time__gt=midnight).values('device_id').annotate(count=Max('count')) + one_day_ago_queryset = [DeviceCount.objects.get(device_id=x['device_id'], data_time=x['max_time']) + for x in DeviceCount.objects.filter( + data_time__lt=midnight).values('device_id').annotate(max_time=Max('data_time'))] if one_day_ago_queryset: - one_day_ago_count = sum(map(lambda x: int(x['count']), one_day_ago_queryset)) + one_day_ago_count = sum(map(lambda x: int(x.count), one_day_ago_queryset)) daily_count = total_count - one_day_ago_count else: daily_count = 0