# 设备日志统计api初步完成
This commit is contained in:
parent
20961f442d
commit
b1ef4fb97d
|
@ -68,3 +68,40 @@ 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']
|
||||||
|
|
|
@ -1,9 +1,14 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
from .views import DeviceLogListAPIView, DeviceInfoListAPIView
|
from .views import (
|
||||||
|
DeviceLogListAPIView,
|
||||||
|
DeviceInfoListAPIView,
|
||||||
|
DeviceLogStatisticAPIView,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
app_name = 'counter-api'
|
app_name = 'counter-api'
|
||||||
urlpatterns = [
|
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'),
|
||||||
]
|
]
|
|
@ -4,11 +4,17 @@ from rest_framework.generics import (
|
||||||
CreateAPIView
|
CreateAPIView
|
||||||
)
|
)
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from rest_framework.filters import SearchFilter, OrderingFilter
|
from rest_framework.filters import SearchFilter, OrderingFilter
|
||||||
from mosquito.api.pagination import PostLimitOffsetPagination, PostPageNumberPagination
|
from mosquito.api.pagination import PostLimitOffsetPagination, PostPageNumberPagination
|
||||||
from .serializers import DeviceCountSerializer, DeviceInfoSerializer
|
from .serializers import (
|
||||||
|
DeviceCountSerializer,
|
||||||
|
DeviceInfoSerializer,
|
||||||
|
DeviceLogStatisticSerializer,
|
||||||
|
DeviceInfoStatisticSerializer
|
||||||
|
)
|
||||||
from ..models import DeviceCount, DeviceInfo
|
from ..models import DeviceCount, DeviceInfo
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,5 +36,10 @@ class DeviceInfoListAPIView(ListAPIView):
|
||||||
queryset = DeviceInfo.objects.all()
|
queryset = DeviceInfo.objects.all()
|
||||||
|
|
||||||
|
|
||||||
# class DeviceLogStatisticAPIView(APIView):
|
class DeviceLogStatisticAPIView(APIView):
|
||||||
# pass
|
serializer_class = DeviceLogStatisticSerializer
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def get(self, request, *args, **kwargs):
|
||||||
|
serializer = self.serializer_class(DeviceCount)
|
||||||
|
return Response(serializer.data)
|
Loading…
Reference in New Issue