# 修改api名称、latest daily 统计逻辑完善
This commit is contained in:
parent
ce32e2cd00
commit
0c48a3d47c
|
@ -121,7 +121,7 @@ class DeviceInfoSerializer(serializers.ModelSerializer):
|
|||
return None
|
||||
|
||||
|
||||
class LogHistorySerializer(serializers.ModelSerializer):
|
||||
class LatestDailySerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = MosqPostStatistic
|
||||
|
|
|
@ -2,10 +2,10 @@ from django.urls import path
|
|||
from .views import (
|
||||
DeviceLogListAPIView,
|
||||
DeviceInfoListAPIView,
|
||||
DeviceLogStatisticAPIView,
|
||||
DeviceInfoStatisticAPIView,
|
||||
LogHistoryListAPIView,
|
||||
DeviceLogHistoryListAPIView
|
||||
LatestStatisticAPIView,
|
||||
DeviceStatusAPIView,
|
||||
LatestDailyListAPIView,
|
||||
DeviceDailyListAPIView
|
||||
)
|
||||
|
||||
|
||||
|
@ -13,8 +13,8 @@ app_name = 'counter-api'
|
|||
urlpatterns = [
|
||||
path('logs/', DeviceLogListAPIView.as_view(), name='logs'),
|
||||
path('device/', DeviceInfoListAPIView.as_view(), name='device'),
|
||||
path('logs/statistic/', DeviceLogStatisticAPIView.as_view(), name='logs-statistic'),
|
||||
path('logs/statistic/history/', LogHistoryListAPIView.as_view(), name='logs-statistic-history'),
|
||||
path('device/logs/history/', DeviceLogHistoryListAPIView.as_view(), name='device-logs-history'),
|
||||
path('device/statistic/', DeviceInfoStatisticAPIView.as_view(), name='device-statistic'),
|
||||
path('latest/statistic/', LatestStatisticAPIView.as_view(), name='latest-statistic'),
|
||||
path('latest/daily/', LatestDailyListAPIView.as_view(), name='latest-logs'),
|
||||
path('device/daily/', DeviceDailyListAPIView.as_view(), name='device-daily'),
|
||||
path('device/status/', DeviceStatusAPIView.as_view(), name='device-status'),
|
||||
]
|
|
@ -14,7 +14,7 @@ from rest_framework.response import Response
|
|||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.filters import SearchFilter, OrderingFilter
|
||||
from mosquito.api.pagination import (
|
||||
PostLimitOffsetPagination,
|
||||
LatestDailyPagination,
|
||||
PostPageNumberPagination,
|
||||
DeviceLogListPagination,
|
||||
DeviceLogHistoryPagination
|
||||
|
@ -22,7 +22,7 @@ from mosquito.api.pagination import (
|
|||
from .serializers import (
|
||||
DeviceCountSerializer,
|
||||
DeviceInfoSerializer,
|
||||
LogHistorySerializer,
|
||||
LatestDailySerializer,
|
||||
DeviceLogHistorySerializer,
|
||||
)
|
||||
from ..mixins.role import RoleMixin, DeviceListMixin
|
||||
|
@ -93,7 +93,7 @@ class DeviceInfoListAPIView(ListAPIView, RoleMixin):
|
|||
return queryset
|
||||
|
||||
|
||||
class DeviceLogStatisticAPIView(APIView, RoleMixin):
|
||||
class LatestStatisticAPIView(APIView, RoleMixin):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
|
@ -126,7 +126,7 @@ class DeviceLogStatisticAPIView(APIView, RoleMixin):
|
|||
month_ago_count = total_count - one_month_ago_count
|
||||
else:
|
||||
month_ago_count = 0
|
||||
cache.set('month_ago_count', month_ago_count, 60 * 60)
|
||||
cache.set('month_ago_count', month_ago_count, 60 * 5)
|
||||
|
||||
# calc daily count, put it in redis as cache.
|
||||
if cache.get('daily_count'):
|
||||
|
@ -141,7 +141,7 @@ class DeviceLogStatisticAPIView(APIView, RoleMixin):
|
|||
daily_count = total_count - one_day_ago_count
|
||||
else:
|
||||
daily_count = 0
|
||||
cache.set('daily_count', daily_count, 60 * 60)
|
||||
cache.set('daily_count', daily_count, 60 * 5)
|
||||
|
||||
# print(total_count, month_ago_count)
|
||||
data = {'month_ago_count': month_ago_count, 'daily_count': daily_count}
|
||||
|
@ -156,7 +156,7 @@ class DeviceLogStatisticAPIView(APIView, RoleMixin):
|
|||
return queryset
|
||||
|
||||
|
||||
class DeviceInfoStatisticAPIView(APIView, RoleMixin):
|
||||
class DeviceStatusAPIView(APIView, RoleMixin):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
|
@ -175,16 +175,40 @@ class DeviceInfoStatisticAPIView(APIView, RoleMixin):
|
|||
return Response(data)
|
||||
|
||||
|
||||
class LogHistoryListAPIView(ListAPIView):
|
||||
serializer_class = LogHistorySerializer
|
||||
class LatestDailyListAPIView(ListAPIView, RoleMixin, DeviceListMixin):
|
||||
"""
|
||||
用户下所有设备总数统计
|
||||
"""
|
||||
serializer_class = LatestDailySerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
filter_backends = [SearchFilter, OrderingFilter]
|
||||
pagination_class = PostLimitOffsetPagination
|
||||
pagination_class = LatestDailyPagination
|
||||
search_fields = ['date']
|
||||
queryset = MosqPostStatistic.objects.all().order_by('-date')
|
||||
|
||||
def list(self, request, *args, **kwargs):
|
||||
user_roles = self.get_role()
|
||||
resp = super().list(request, *args, **kwargs)
|
||||
results = resp.data['results']
|
||||
if 'staff' in user_roles or 'manager' in user_roles:
|
||||
devices = self.get_device_list()
|
||||
device_ids = [device.device_id for device in devices if device.org == self.request.user.org]
|
||||
device_daily_qs = DevicePostStatistic.objects.filter(device_id__in=device_ids)
|
||||
for item in results:
|
||||
date = item['date']
|
||||
data = device_daily_qs.filter(date=date)
|
||||
total = sum(map(lambda x: x.total, data))
|
||||
increment = sum(map(lambda x: x.increment, data))
|
||||
item['total'] = total
|
||||
item['increment'] = increment
|
||||
|
||||
class DeviceLogHistoryListAPIView(ListAPIView, RoleMixin, DeviceListMixin):
|
||||
return resp
|
||||
|
||||
|
||||
class DeviceDailyListAPIView(ListAPIView, RoleMixin, DeviceListMixin):
|
||||
"""
|
||||
设备每天日志
|
||||
"""
|
||||
serializer_class = DeviceLogHistorySerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
filter_backends = [SearchFilter, OrderingFilter]
|
||||
|
|
|
@ -10,6 +10,13 @@ class PostPageNumberPagination(PageNumberPagination):
|
|||
page_size = 10
|
||||
|
||||
|
||||
class LatestDailyPagination(PageNumberPagination):
|
||||
page_size = 7
|
||||
page_size_query_param = 'limit'
|
||||
page_query_param = 'page'
|
||||
max_page_size = 1000
|
||||
|
||||
|
||||
class DeviceLogListPagination(PageNumberPagination):
|
||||
page_size = 10
|
||||
page_size_query_param = 'limit'
|
||||
|
|
Loading…
Reference in New Issue