# 优化当月数据统计逻辑
This commit is contained in:
parent
263c336c04
commit
152da4990c
|
@ -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,10 +110,6 @@ 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.
|
|
||||||
if cache.get('month_ago_count'):
|
|
||||||
month_ago_count = cache.get('month_ago_count')
|
|
||||||
else:
|
|
||||||
cur_month = tz.localize(datetime.combine(datetime(today.year, today.month, 1), time(0, 0)), is_dst=None)
|
cur_month = tz.localize(datetime.combine(datetime(today.year, today.month, 1), time(0, 0)), is_dst=None)
|
||||||
one_month_ago_queryset = [
|
one_month_ago_queryset = [
|
||||||
DeviceCount.objects.filter(device_id=x['device_id'], data_time=x['max_time']).order_by('-count')[0]
|
DeviceCount.objects.filter(device_id=x['device_id'], data_time=x['max_time']).order_by('-count')[0]
|
||||||
|
@ -123,15 +118,10 @@ class LatestStatisticAPIView(APIView, RoleMixin):
|
||||||
max_time=Max('data_time'))]
|
max_time=Max('data_time'))]
|
||||||
if one_month_ago_queryset:
|
if one_month_ago_queryset:
|
||||||
one_month_ago_count = sum(map(lambda x: int(x.count), 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
|
cur_month_count = total_count - one_month_ago_count
|
||||||
else:
|
else:
|
||||||
month_ago_count = 0
|
cur_month_count = total_count
|
||||||
cache.set('month_ago_count', month_ago_count, 60 * 5)
|
|
||||||
|
|
||||||
# calc daily count, put it in redis as cache.
|
|
||||||
if cache.get('daily_count'):
|
|
||||||
daily_count = cache.get('daily_count')
|
|
||||||
else:
|
|
||||||
midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None)
|
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]
|
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(
|
for x in DeviceCount.objects.filter(
|
||||||
|
@ -141,10 +131,8 @@ class LatestStatisticAPIView(APIView, RoleMixin):
|
||||||
daily_count = total_count - one_day_ago_count
|
daily_count = total_count - one_day_ago_count
|
||||||
else:
|
else:
|
||||||
daily_count = 0
|
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):
|
||||||
|
|
Loading…
Reference in New Issue