1
0
Fork 0

Add caching to stats page queries

* Add cache config to docker
environments/stage/deployments/2
Nikos Roussos 2017-10-16 21:06:19 +01:00
parent 473876d60e
commit 9ffef0cfaf
No known key found for this signature in database
GPG Key ID: BADFF1767BA7C8E1
2 changed files with 20 additions and 10 deletions

View File

@ -9,6 +9,7 @@ from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.sites.shortcuts import get_current_site from django.contrib.sites.shortcuts import get_current_site
from django.contrib import messages from django.contrib import messages
from django.core.cache import cache
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.http import HttpResponseNotFound, HttpResponseServerError, HttpResponse, JsonResponse from django.http import HttpResponseNotFound, HttpResponseServerError, HttpResponse, JsonResponse
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
@ -182,16 +183,22 @@ def faq(request):
def stats(request): def stats(request):
"""View to render stats page.""" """View to render stats page."""
satellites = Satellite.objects \ satellites = cache.get('stats_satellites')
.values('name', 'norad_cat_id') \ if not satellites:
.annotate(count=Count('telemetry_data'), satellites = Satellite.objects \
latest_payload=Max('telemetry_data__timestamp')) \ .values('name', 'norad_cat_id') \
.order_by('-count') .annotate(count=Count('telemetry_data'),
observers = DemodData.objects \ latest_payload=Max('telemetry_data__timestamp')) \
.values('observer') \ .order_by('-count')
.annotate(count=Count('observer'), cache.set('stats_satellites', satellites, settings.CACHE_TTL)
latest_payload=Max('timestamp')) \ observers = cache.get('stats_observers')
.order_by('-count') if not observers:
observers = DemodData.objects \
.values('observer') \
.annotate(count=Count('observer'),
latest_payload=Max('timestamp')) \
.order_by('-count')
cache.set('stats_observers', observers, settings.CACHE_TTL)
return render(request, 'base/stats.html', {'satellites': satellites, return render(request, 'base/stats.html', {'satellites': satellites,
'observers': observers}) 'observers': observers})

View File

@ -40,5 +40,8 @@ services:
- ENVIRONMENT=stage - ENVIRONMENT=stage
- DEBUG=True - DEBUG=True
- DATABASE_URL=mysql://satnogsdb:satnogsdb@db/satnogsdb - DATABASE_URL=mysql://satnogsdb:satnogsdb@db/satnogsdb
- CACHE_BACKEND=redis_cache.RedisCache
- CACHE_LOCATION=redis://redis:6379/1
- CACHE_CLIENT_CLASS=django_redis.client.DefaultClient
command: command:
./bin/run-web.sh ./bin/run-web.sh