# 统计bug修复

This commit is contained in:
xianfuxing 2020-05-27 17:44:10 +08:00
parent eb0b4f665a
commit 4a781f45f8
1 changed files with 13 additions and 3 deletions

View File

@ -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]