# bug fixed: statistic using annotate return other filed

This commit is contained in:
xianfuxing 2018-08-11 14:37:13 +08:00
parent 03a56d9f10
commit 6ef78351df
1 changed files with 7 additions and 5 deletions

View File

@ -46,17 +46,19 @@ class DeviceLogStatisticAPIView(APIView):
midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None) midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None)
# calc total count # 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: 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: else:
total_count = 0 total_count = 0
# calc daily count # calc daily count
one_day_ago_queryset = DeviceCount.objects.filter( one_day_ago_queryset = [DeviceCount.objects.get(device_id=x['device_id'], data_time=x['max_time'])
data_time__gt=midnight).values('device_id').annotate(count=Max('count')) for x in DeviceCount.objects.filter(
data_time__lt=midnight).values('device_id').annotate(max_time=Max('data_time'))]
if one_day_ago_queryset: 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 daily_count = total_count - one_day_ago_count
else: else:
daily_count = 0 daily_count = 0