# add redis cache in statistic
This commit is contained in:
parent
6ef78351df
commit
3d7be52923
|
@ -1,6 +1,7 @@
|
||||||
import pytz
|
import pytz
|
||||||
from datetime import datetime, time
|
from datetime import datetime, time
|
||||||
from django.db.models import Sum, Max
|
from django.db.models import Sum, Max
|
||||||
|
from django.core.cache import cache
|
||||||
from rest_framework.generics import (
|
from rest_framework.generics import (
|
||||||
ListAPIView,
|
ListAPIView,
|
||||||
RetrieveAPIView,
|
RetrieveAPIView,
|
||||||
|
@ -41,11 +42,7 @@ class DeviceLogStatisticAPIView(APIView):
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
tz = pytz.timezone("UTC")
|
# calc total count, no cache
|
||||||
today = datetime.now(tz).date()
|
|
||||||
midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None)
|
|
||||||
|
|
||||||
# calc total count
|
|
||||||
total_count_queryset = [DeviceCount.objects.get(device_id=x['device_id'], data_time=x['max_time'])
|
total_count_queryset = [DeviceCount.objects.get(device_id=x['device_id'], data_time=x['max_time'])
|
||||||
for x in DeviceCount.objects.values('device_id').annotate(max_time=Max('data_time'))]
|
for x in DeviceCount.objects.values('device_id').annotate(max_time=Max('data_time'))]
|
||||||
if total_count_queryset:
|
if total_count_queryset:
|
||||||
|
@ -53,15 +50,23 @@ class DeviceLogStatisticAPIView(APIView):
|
||||||
else:
|
else:
|
||||||
total_count = 0
|
total_count = 0
|
||||||
|
|
||||||
# calc daily count
|
# calc daily count, put it in redis as cache.
|
||||||
one_day_ago_queryset = [DeviceCount.objects.get(device_id=x['device_id'], data_time=x['max_time'])
|
if cache.has_key('daily_count'):
|
||||||
for x in DeviceCount.objects.filter(
|
daily_count = cache.get('daily_count')
|
||||||
data_time__lt=midnight).values('device_id').annotate(max_time=Max('data_time'))]
|
pass
|
||||||
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:
|
||||||
daily_count = 0
|
tz = pytz.timezone("UTC")
|
||||||
|
today = datetime.now(tz).date()
|
||||||
|
midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None)
|
||||||
|
one_day_ago_queryset = [DeviceCount.objects.get(device_id=x['device_id'], data_time=x['max_time'])
|
||||||
|
for x in DeviceCount.objects.filter(
|
||||||
|
data_time__lt=midnight).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 * 60)
|
||||||
data = {'total_count': total_count, 'daily_count': daily_count}
|
data = {'total_count': total_count, 'daily_count': daily_count}
|
||||||
return Response(data)
|
return Response(data)
|
||||||
|
|
||||||
|
|
|
@ -186,3 +186,14 @@ JWT_AUTH = {
|
||||||
# AUTHENTICATION_BACKENDS = (
|
# AUTHENTICATION_BACKENDS = (
|
||||||
# 'accounts.api.views.CustomBackend',
|
# 'accounts.api.views.CustomBackend',
|
||||||
# )
|
# )
|
||||||
|
|
||||||
|
# CACHES
|
||||||
|
CACHES = {
|
||||||
|
"default": {
|
||||||
|
"BACKEND": "django_redis.cache.RedisCache",
|
||||||
|
"LOCATION": "redis://:IySpkVL39NeOgyC@47.106.73.20:6389/1",
|
||||||
|
"OPTIONS": {
|
||||||
|
"CLIENT_CLASS": "django_redis.client.DefaultClient",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,43 @@
|
||||||
Django==2.0.7
|
aliyun-log-python-sdk==0.6.30.4
|
||||||
|
aliyun-python-sdk-core-v3==2.8.9
|
||||||
|
aliyun-python-sdk-rds==2.1.3
|
||||||
|
certifi==2018.4.16
|
||||||
|
chardet==3.0.4
|
||||||
|
Django==2.0.5
|
||||||
django-appconf==1.0.2
|
django-appconf==1.0.2
|
||||||
django-cors-headers==2.4.0
|
django-cors-headers==2.3.0
|
||||||
|
django-crispy-forms==1.7.2
|
||||||
django-filter==1.1.0
|
django-filter==1.1.0
|
||||||
django-imagekit==4.0.2
|
django-imagekit==4.0.2
|
||||||
|
django-markdown-deux==1.0.5
|
||||||
|
django-pagedown==1.0.4
|
||||||
|
django-redis==4.9.0
|
||||||
djangorestframework==3.8.2
|
djangorestframework==3.8.2
|
||||||
djangorestframework-jwt==1.11.0
|
djangorestframework-jwt==1.11.0
|
||||||
gunicorn==19.9.0
|
enum34==1.1.6
|
||||||
mysqlclient==1.3.13
|
et-xmlfile==1.0.1
|
||||||
|
idna==2.7
|
||||||
|
jdcal==1.4
|
||||||
|
lml==0.0.1
|
||||||
|
Markdown==2.6.11
|
||||||
|
markdown2==2.3.5
|
||||||
|
mysqlclient==1.3.12
|
||||||
|
openpyxl==2.5.4
|
||||||
pilkit==2.0
|
pilkit==2.0
|
||||||
Pillow==5.2.0
|
Pillow==5.1.0
|
||||||
|
protobuf==3.6.0
|
||||||
|
pyexcel==0.5.8
|
||||||
|
pyexcel-io==0.5.7
|
||||||
|
pyexcel-xls==0.5.7
|
||||||
|
pyexcel-xlsx==0.5.6
|
||||||
|
Pygments==2.2.0
|
||||||
PyJWT==1.6.4
|
PyJWT==1.6.4
|
||||||
pytz==2018.5
|
python-dateutil==2.7.3
|
||||||
|
pytz==2018.4
|
||||||
|
redis==2.10.6
|
||||||
|
requests==2.19.1
|
||||||
six==1.11.0
|
six==1.11.0
|
||||||
|
texttable==1.4.0
|
||||||
|
urllib3==1.23
|
||||||
|
xlrd==1.1.0
|
||||||
|
xlwt==1.3.0
|
||||||
|
|
Loading…
Reference in New Issue