1
0
Fork 0

Merge pull request #174 from satnogs/cache

Fix cache
merge-requests/175/head
Nikos Roussos 2017-05-10 23:42:22 +03:00 committed by GitHub
commit 7ea0b5a7bc
6 changed files with 78 additions and 46 deletions

View File

@ -1,5 +1,7 @@
from rest_framework.authtoken.models import Token
from django.core.cache import cache
UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWX'
LOWER = 'abcdefghijklmnopqrstuvwx'
@ -38,3 +40,28 @@ def get_apikey(user):
except:
token = Token.objects.create(user=user)
return token
def cache_get_key(*args, **kwargs):
import hashlib
serialise = []
for arg in args:
serialise.append(str(arg))
for key, arg in kwargs.items():
serialise.append(str(key))
serialise.append(str(arg))
key = hashlib.md5("".join(serialise)).hexdigest()
return key
def cache_for(time):
def decorator(fn):
def wrapper(*args, **kwargs):
key = cache_get_key(fn.__name__, *args, **kwargs)
result = cache.get(key)
if not result:
result = fn(*args, **kwargs)
cache.set(key, result, time)
return result
return wrapper
return decorator

View File

@ -14,11 +14,10 @@ from django.http import HttpResponseNotFound, HttpResponseServerError, HttpRespo
from django.shortcuts import render, redirect, get_object_or_404
from django.template.loader import render_to_string
from django.views.decorators.http import require_POST
from django.views.decorators.cache import cache_page
from db.base.models import Mode, Transmitter, Satellite, Suggestion, DemodData
from db.base.forms import SuggestionForm
from db.base.helpers import get_apikey
from db.base.helpers import get_apikey, cache_for
from db.base.tasks import export_frames
@ -194,8 +193,8 @@ def stats(request):
'observers': observers})
@cache_page(settings.CACHE_TTL)
def statistics(request):
@cache_for(settings.CACHE_TTL)
def _calculate_statistics():
"""View to create statistics endpoint."""
satellites = Satellite.objects.all()
transmitters = Transmitter.objects.all()
@ -290,6 +289,11 @@ def statistics(request):
'band_label': band_label_sorted,
'band_data': band_data_sorted
}
return statistics
def statistics(request):
statistics = _calculate_statistics()
return JsonResponse(statistics, safe=False)

View File

@ -1,7 +1,9 @@
from os import path, getenv
import dj_database_url
from os import getenv
from dj_database_url import parse as db_url
from unipath import Path
BASE_DIR = path.dirname(path.dirname(__file__))
ROOT = Path(__file__).parent.parent
ENVIRONMENT = getenv('ENVIRONMENT', 'production')
DEBUG = getenv('DEBUG', False)
@ -90,7 +92,9 @@ USE_TZ = True
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [path.join(BASE_DIR, 'db/templates')],
'DIRS': [
Path('db/templates').resolve(),
],
'OPTIONS': {
'debug': False,
'context_processors': [
@ -115,17 +119,17 @@ TEMPLATES = [
]
# Static & Media
STATIC_ROOT = path.join(path.dirname(BASE_DIR), 'staticfiles')
STATIC_ROOT = Path('staticfiles').resolve()
STATIC_URL = '/static/'
STATICFILES_DIRS = (
path.join(BASE_DIR, 'db/static'),
)
STATICFILES_DIRS = [
Path('db/static').resolve(),
]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
MEDIA_ROOT = path.join(path.dirname(BASE_DIR), 'media')
MEDIA_ROOT = Path('media').resolve()
MEDIA_URL = '/media/'
CRISPY_TEMPLATE_PACK = 'bootstrap3'
SATELLITE_DEFAULT_IMAGE = '/static/img/sat.png'
@ -247,7 +251,7 @@ ALLOWED_HOSTS = [
# Database
DATABASE_URL = getenv('DATABASE_URL', 'sqlite:///db.sqlite3')
DATABASES = {'default': dj_database_url.parse(DATABASE_URL)}
DATABASES = {'default': db_url(DATABASE_URL)}
# NETWORK API
NETWORK_API_ENDPOINT = getenv('NETWORK_API_ENDPOINT', 'https://network.satnogs.org/api/')

View File

@ -111,10 +111,16 @@ body {
width: 80px;
}
#search {
margin: auto;
}
.statistics {
text-align: center;
text-shadow: 1px 1px 2px rgba(150, 150, 150, 0.77);
margin-top: 12px;
display: inline-block;
padding: 0 15px;
}
.statistics > img {

View File

@ -13,38 +13,26 @@
</div>
<div class="col-md-5">
<div class="row hidden-xs hidden-sm">
<div class="{% if suggestions %}col-md-3{% else %}col-md-3{% endif %}">
<p class="statistics">
<img src="{% static 'img/satellites.png' %}">
{{ satellites.count }}
</p>
</div>
<div class="{% if suggestions %}col-md-2{% else %}col-md-3{% endif %}">
<p class="statistics">
<img src="{% static 'img/transmitters.png' %}">
{{ transmitters }}
</p>
</div>
{% if suggestions %}
<div class="col-md-2">
<p class="statistics">
<img src="{% static 'img/suggestions.png' %}">
{{ suggestions }}
</p>
</div>
{% endif %}
<div class="{% if suggestions %}col-md-2{% else %}col-md-3{% endif %}">
<p class="statistics">
<img src="{% static 'img/payloads.png' %}">
{{ payloads }}
</p>
</div>
<div class="{% if suggestions %}col-md-3{% else %}col-md-3{% endif %}">
<p class="statistics">
<img src="{% static 'img/contributors.png' %}">
{{ contributors }}
</p>
</div>
<p class="statistics" title="satellites" data-toggle="tooltip">
<img src="{% static 'img/satellites.png' %}">
{{ satellites.count }}
</p>
<p class="statistics" title="transmitters" data-toggle="tooltip">
<img src="{% static 'img/transmitters.png' %}">
{{ transmitters }}
</p>
<p class="statistics" title="suggestions" data-toggle="tooltip">
<img src="{% static 'img/suggestions.png' %}">
{{ suggestions }}
</p>
<p class="statistics" title="telemetry" data-toggle="tooltip">
<img src="{% static 'img/payloads.png' %}">
{{ payloads }}
</p>
<p class="statistics" title="contributors" data-toggle="tooltip">
<img src="{% static 'img/contributors.png' %}">
{{ contributors }}
</p>
</div>
</div>
</div>

View File

@ -41,6 +41,9 @@ pytz==2017.2 \
--hash=sha256:39504670abb5dae77f56f8eb63823937ce727d7cdd0088e6909e6dcac0f89043 \
--hash=sha256:ddc93b6d41cfb81266a27d23a79e13805d4a5521032b512643af8729041a81b4 \
--hash=sha256:f5c056e8f62d45ba8215e5cb8f50dfccb198b4b9fbea8500674f3443e4689589
Unipath==1.1 \
--hash=sha256:e6257e508d8abbfb6ddd8ec357e33589f1f48b1599127f23b017124d90b0fff7 \
--hash=sha256:09839adcc72e8a24d4f76d63656f30b5a1f721fc40c9bcd79d8c67bdd8b47dae
# Security
django-braces==1.11.0 \