# bug fixed: statistic using annotate return other filed
This commit is contained in:
parent
03a56d9f10
commit
6ef78351df
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue