# 优化当月数据统计逻辑

This commit is contained in:
xianfuxing 2020-06-05 14:48:35 +08:00
parent 263c336c04
commit 152da4990c
1 changed files with 19 additions and 31 deletions

View File

@ -97,7 +97,6 @@ class LatestStatisticAPIView(APIView, RoleMixin):
permission_classes = [IsAuthenticated] permission_classes = [IsAuthenticated]
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
# calc total count, no cache
devices = self.get_devices() devices = self.get_devices()
total_count_queryset = [DeviceCount.objects.filter(device_id=x['device_id'], total_count_queryset = [DeviceCount.objects.filter(device_id=x['device_id'],
data_time=x['max_time']).order_by('-count')[0] data_time=x['max_time']).order_by('-count')[0]
@ -111,40 +110,29 @@ class LatestStatisticAPIView(APIView, RoleMixin):
tz = pytz.timezone("UTC") tz = pytz.timezone("UTC")
today = datetime.now(tz).date() today = datetime.now(tz).date()
# calc month_ago_count, put it in redis as cache. cur_month = tz.localize(datetime.combine(datetime(today.year, today.month, 1), time(0, 0)), is_dst=None)
if cache.get('month_ago_count'): one_month_ago_queryset = [
month_ago_count = cache.get('month_ago_count') 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: else:
cur_month = tz.localize(datetime.combine(datetime(today.year, today.month, 1), time(0, 0)), is_dst=None) cur_month_count = total_count
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)
# calc daily count, put it in redis as cache. midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None)
if cache.get('daily_count'): one_day_ago_queryset = [DeviceCount.objects.filter(device_id=x['device_id'], data_time=x['max_time']).order_by('-count')[0]
daily_count = cache.get('daily_count') 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: else:
midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None) daily_count = 0
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)
# print(total_count, month_ago_count) data = {'cur_month_count': cur_month_count, 'daily_count': daily_count}
data = {'month_ago_count': month_ago_count, 'daily_count': daily_count}
return Response(data) return Response(data)
def get_devices(self): def get_devices(self):