# up total, daily count api
This commit is contained in:
parent
bede31d479
commit
69364bd979
|
@ -1,6 +1,6 @@
|
||||||
import pytz
|
import pytz
|
||||||
from datetime import datetime, time
|
from datetime import datetime, time
|
||||||
from django.db.models import Sum
|
from django.db.models import Sum, Max
|
||||||
from rest_framework.generics import (
|
from rest_framework.generics import (
|
||||||
ListAPIView,
|
ListAPIView,
|
||||||
RetrieveAPIView,
|
RetrieveAPIView,
|
||||||
|
@ -44,9 +44,14 @@ class DeviceLogStatisticAPIView(APIView):
|
||||||
tz = pytz.timezone("UTC")
|
tz = pytz.timezone("UTC")
|
||||||
today = datetime.now(tz).date()
|
today = datetime.now(tz).date()
|
||||||
midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None)
|
midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None)
|
||||||
total_count = DeviceCount.objects.aggregate(total=Sum('count'))['total']
|
# total_count = DeviceCount.objects.aggregate(total=Sum('count'))['total']
|
||||||
daily_count = DeviceCount.objects.filter(
|
total_count_queryset = DeviceCount.objects.values('device_id').annotate(count=Max('count'))
|
||||||
data_time__gt=midnight).aggregate(total=Sum('count'))['total']
|
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}
|
data = {'total_count': total_count, 'daily_count': daily_count}
|
||||||
return Response(data)
|
return Response(data)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue