From 4a781f45f83c9f63a637ae04b3dcb609f10b99fd Mon Sep 17 00:00:00 2001 From: xianfuxing Date: Wed, 27 May 2020 17:44:10 +0800 Subject: [PATCH] =?UTF-8?q?#=20=E7=BB=9F=E8=AE=A1bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/counter/api/views.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/apps/counter/api/views.py b/apps/counter/api/views.py index ab68e02..300226f 100644 --- a/apps/counter/api/views.py +++ b/apps/counter/api/views.py @@ -72,14 +72,16 @@ class DeviceInfoListAPIView(ListAPIView, RoleMixin): return queryset -class DeviceLogStatisticAPIView(APIView): +class DeviceLogStatisticAPIView(APIView, RoleMixin): permission_classes = [IsAuthenticated] def get(self, request, *args, **kwargs): # calc total count, no cache + devices = self.get_devices() total_count_queryset = [DeviceCount.objects.filter(device_id=x['device_id'], data_time=x['max_time']).order_by('-count')[0] - for x in DeviceCount.objects.values('device_id').annotate(max_time=Max('data_time'))] + for x in DeviceCount.objects.filter( + device_id__in=devices).values('device_id').annotate(max_time=Max('data_time'))] if total_count_queryset: total_count = sum(map(lambda x: int(x.count), total_count_queryset)) else: @@ -94,7 +96,7 @@ class DeviceLogStatisticAPIView(APIView): midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None) one_day_ago_queryset = [DeviceCount.objects.filter(device_id=x['device_id'], data_time=x['max_time']).order_by('-count')[0] for x in DeviceCount.objects.filter( - data_time__lt=midnight).values('device_id').annotate(max_time=Max('data_time'))] + data_time__lt=midnight, device_id__in=devices).values('device_id').annotate(max_time=Max('data_time'))] if one_day_ago_queryset: one_day_ago_count = sum(map(lambda x: int(x.count), one_day_ago_queryset)) daily_count = total_count - one_day_ago_count @@ -104,6 +106,14 @@ class DeviceLogStatisticAPIView(APIView): data = {'total_count': total_count, 'daily_count': daily_count} return Response(data) + def get_devices(self): + user_roles = self.get_role() + queryset = DeviceInfo.objects.get_queryset().order_by('-online') + if 'staff' in user_roles or 'manager' in user_roles: + device_ids = [query.device_id for query in queryset if query.org == self.request.user.org] + queryset = queryset.filter(device_id__in=device_ids) + return queryset + class DeviceInfoStatisticAPIView(APIView, RoleMixin): permission_classes = [IsAuthenticated]