# up total, daily count api

This commit is contained in:
xianfuxing 2018-08-10 23:59:53 +08:00
parent bede31d479
commit 69364bd979
1 changed files with 9 additions and 4 deletions

View File

@ -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)