# 完善首页统计api
This commit is contained in:
parent
b1ef4fb97d
commit
5caa3442e9
|
@ -1,4 +1,5 @@
|
||||||
from django.utils import timezone
|
import pytz
|
||||||
|
from datetime import datetime, time
|
||||||
from django.db.models import Sum
|
from django.db.models import Sum
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from counter.models import DeviceCount, DeviceInfo
|
from counter.models import DeviceCount, DeviceInfo
|
||||||
|
@ -68,40 +69,3 @@ class DeviceInfoSerializer(serializers.ModelSerializer):
|
||||||
if obj.last_offline_time:
|
if obj.last_offline_time:
|
||||||
return obj.last_offline_time.strftime('%Y-%m-%d %H:%M:%S')
|
return obj.last_offline_time.strftime('%Y-%m-%d %H:%M:%S')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
class DeviceLogStatisticSerializer(serializers.ModelSerializer):
|
|
||||||
total_count = serializers.SerializerMethodField()
|
|
||||||
daily_count = serializers.SerializerMethodField()
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = DeviceCount
|
|
||||||
fields = [
|
|
||||||
'total_count',
|
|
||||||
'daily_count',
|
|
||||||
]
|
|
||||||
|
|
||||||
def get_total_count(self, obj):
|
|
||||||
return obj.objects.aggregate(total=Sum('count'))['total']
|
|
||||||
|
|
||||||
def get_daily_count(self, obj):
|
|
||||||
today = timezone.datetime.now().date()
|
|
||||||
return obj.objects.filter(data_time__gt=today).aggregate(total=Sum('count'))['total']
|
|
||||||
|
|
||||||
|
|
||||||
class DeviceInfoStatisticSerializer(serializers.ModelSerializer):
|
|
||||||
online_count = serializers.SerializerMethodField()
|
|
||||||
offline_count = serializers.SerializerMethodField()
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = DeviceInfo
|
|
||||||
fields = [
|
|
||||||
'online_count',
|
|
||||||
'offline_count'
|
|
||||||
]
|
|
||||||
|
|
||||||
def get_online_count(self, obj):
|
|
||||||
return obj.objects.filter(online=1).count()['total']
|
|
||||||
|
|
||||||
def get_offline_count(self, obj):
|
|
||||||
return obj.objects.filter(online=0).count()['total']
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ from .views import (
|
||||||
DeviceLogListAPIView,
|
DeviceLogListAPIView,
|
||||||
DeviceInfoListAPIView,
|
DeviceInfoListAPIView,
|
||||||
DeviceLogStatisticAPIView,
|
DeviceLogStatisticAPIView,
|
||||||
|
DeviceInfoStatisticAPIView
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,4 +12,5 @@ urlpatterns = [
|
||||||
path('logs/', DeviceLogListAPIView.as_view(), name='logs'),
|
path('logs/', DeviceLogListAPIView.as_view(), name='logs'),
|
||||||
path('device/', DeviceInfoListAPIView.as_view(), name='device'),
|
path('device/', DeviceInfoListAPIView.as_view(), name='device'),
|
||||||
path('logs/statistic', DeviceLogStatisticAPIView.as_view(), name='logs-statistic'),
|
path('logs/statistic', DeviceLogStatisticAPIView.as_view(), name='logs-statistic'),
|
||||||
|
path('device/statistic', DeviceInfoStatisticAPIView.as_view(), name='device-statistic'),
|
||||||
]
|
]
|
|
@ -1,3 +1,6 @@
|
||||||
|
import pytz
|
||||||
|
from datetime import datetime, time
|
||||||
|
from django.db.models import Sum
|
||||||
from rest_framework.generics import (
|
from rest_framework.generics import (
|
||||||
ListAPIView,
|
ListAPIView,
|
||||||
RetrieveAPIView,
|
RetrieveAPIView,
|
||||||
|
@ -12,8 +15,6 @@ from mosquito.api.pagination import PostLimitOffsetPagination, PostPageNumberPag
|
||||||
from .serializers import (
|
from .serializers import (
|
||||||
DeviceCountSerializer,
|
DeviceCountSerializer,
|
||||||
DeviceInfoSerializer,
|
DeviceInfoSerializer,
|
||||||
DeviceLogStatisticSerializer,
|
|
||||||
DeviceInfoStatisticSerializer
|
|
||||||
)
|
)
|
||||||
from ..models import DeviceCount, DeviceInfo
|
from ..models import DeviceCount, DeviceInfo
|
||||||
|
|
||||||
|
@ -37,9 +38,24 @@ class DeviceInfoListAPIView(ListAPIView):
|
||||||
|
|
||||||
|
|
||||||
class DeviceLogStatisticAPIView(APIView):
|
class DeviceLogStatisticAPIView(APIView):
|
||||||
serializer_class = DeviceLogStatisticSerializer
|
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
serializer = self.serializer_class(DeviceCount)
|
tz = pytz.timezone("UTC")
|
||||||
return Response(serializer.data)
|
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']
|
||||||
|
data = {'total_count': total_count, 'daily_count': daily_count}
|
||||||
|
return Response(data)
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceInfoStatisticAPIView(APIView):
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
online_count = DeviceInfo.objects.filter(online=1).count()
|
||||||
|
offline_count = DeviceInfo.objects.filter(online=0).count()
|
||||||
|
data = {'online_count': online_count, 'offline_count': offline_count}
|
||||||
|
return Response(data)
|
||||||
|
|
Loading…
Reference in New Issue