diff --git a/apps/counter/api/views.py b/apps/counter/api/views.py index ce5242f..029a246 100644 --- a/apps/counter/api/views.py +++ b/apps/counter/api/views.py @@ -1,6 +1,7 @@ import pytz from datetime import datetime, time from django.db.models import Sum, Max +from django.core.cache import cache from rest_framework.generics import ( ListAPIView, RetrieveAPIView, @@ -41,11 +42,7 @@ class DeviceLogStatisticAPIView(APIView): permission_classes = [IsAuthenticated] def get(self, request, *args, **kwargs): - tz = pytz.timezone("UTC") - today = datetime.now(tz).date() - midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None) - - # calc total count + # calc total count, no cache 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'))] if total_count_queryset: @@ -53,15 +50,23 @@ class DeviceLogStatisticAPIView(APIView): else: total_count = 0 - # calc daily count - 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 + # calc daily count, put it in redis as cache. + if cache.has_key('daily_count'): + daily_count = cache.get('daily_count') + pass 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} return Response(data) diff --git a/mosqkiller/settings.py b/mosqkiller/settings.py index 1f4485f..a35a5c4 100644 --- a/mosqkiller/settings.py +++ b/mosqkiller/settings.py @@ -185,4 +185,15 @@ JWT_AUTH = { # Custom auth backend # AUTHENTICATION_BACKENDS = ( # 'accounts.api.views.CustomBackend', -# ) \ No newline at end of file +# ) + +# CACHES +CACHES = { + "default": { + "BACKEND": "django_redis.cache.RedisCache", + "LOCATION": "redis://:IySpkVL39NeOgyC@47.106.73.20:6389/1", + "OPTIONS": { + "CLIENT_CLASS": "django_redis.client.DefaultClient", + } + } +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 1862ad4..5ec1ab9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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-cors-headers==2.4.0 +django-cors-headers==2.3.0 +django-crispy-forms==1.7.2 django-filter==1.1.0 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-jwt==1.11.0 -gunicorn==19.9.0 -mysqlclient==1.3.13 +enum34==1.1.6 +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 -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 -pytz==2018.5 +python-dateutil==2.7.3 +pytz==2018.4 +redis==2.10.6 +requests==2.19.1 six==1.11.0 +texttable==1.4.0 +urllib3==1.23 +xlrd==1.1.0 +xlwt==1.3.0