diff --git a/apps/counter/api/views.py b/apps/counter/api/views.py index 9840ee4..1f47d1f 100644 --- a/apps/counter/api/views.py +++ b/apps/counter/api/views.py @@ -97,7 +97,6 @@ class LatestStatisticAPIView(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] @@ -111,40 +110,29 @@ class LatestStatisticAPIView(APIView, RoleMixin): tz = pytz.timezone("UTC") today = datetime.now(tz).date() - # calc month_ago_count, put it in redis as cache. - if cache.get('month_ago_count'): - month_ago_count = cache.get('month_ago_count') + cur_month = tz.localize(datetime.combine(datetime(today.year, today.month, 1), time(0, 0)), is_dst=None) + one_month_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=cur_month, device_id__in=devices).values('device_id').annotate( + max_time=Max('data_time'))] + if one_month_ago_queryset: + one_month_ago_count = sum(map(lambda x: int(x.count), one_month_ago_queryset)) + cur_month_count = total_count - one_month_ago_count else: - cur_month = tz.localize(datetime.combine(datetime(today.year, today.month, 1), time(0, 0)), is_dst=None) - one_month_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=cur_month, device_id__in=devices).values('device_id').annotate( - max_time=Max('data_time'))] - if one_month_ago_queryset: - one_month_ago_count = sum(map(lambda x: int(x.count), one_month_ago_queryset)) - month_ago_count = total_count - one_month_ago_count - else: - month_ago_count = 0 - cache.set('month_ago_count', month_ago_count, 60 * 5) + cur_month_count = total_count - # calc daily count, put it in redis as cache. - if cache.get('daily_count'): - daily_count = cache.get('daily_count') + 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, 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 else: - 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, 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 - else: - daily_count = 0 - cache.set('daily_count', daily_count, 60 * 5) + daily_count = 0 - # print(total_count, month_ago_count) - data = {'month_ago_count': month_ago_count, 'daily_count': daily_count} + data = {'cur_month_count': cur_month_count, 'daily_count': daily_count} return Response(data) def get_devices(self):