Add periodic task to cache Stations success rates
parent
684d2ec51a
commit
8ad90cb19d
|
@ -9,6 +9,7 @@ from orbit import satellite
|
|||
|
||||
from django.conf import settings
|
||||
from django.contrib.sites.models import Site
|
||||
from django.core.cache import cache
|
||||
from django.utils.timezone import now
|
||||
|
||||
from network.base.models import Satellite, Tle, Mode, Transmitter, Observation, Station
|
||||
|
@ -134,7 +135,7 @@ def archive_audio(obs_id):
|
|||
obs.payload.delete()
|
||||
|
||||
|
||||
@app.task
|
||||
@app.task(ignore_result=True)
|
||||
def clean_observations():
|
||||
"""Task to clean up old observations that lack actual data."""
|
||||
threshold = now() - timedelta(days=int(settings.OBSERVATION_OLD_RANGE))
|
||||
|
@ -160,3 +161,15 @@ def station_status_update():
|
|||
else:
|
||||
station.status = 2
|
||||
station.save()
|
||||
|
||||
|
||||
@app.task(ignore_result=True)
|
||||
def stations_cache_rates():
|
||||
stations = Station.objects.all()
|
||||
for station in stations:
|
||||
observations = station.observations.exclude(testing=True)
|
||||
has_audio = observations.filter(id__in=(o.id for o in observations if o.has_audio))
|
||||
success = has_audio.count()
|
||||
if observations:
|
||||
rate = int(100 * (float(success) / float(observations.count())))
|
||||
cache.set('sat-{0}-rate'.format(station.id), rate, 60 * 60 * 2)
|
||||
|
|
|
@ -21,7 +21,7 @@ app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
|
|||
@app.on_after_finalize.connect
|
||||
def setup_periodic_tasks(sender, **kwargs):
|
||||
from network.base.tasks import (update_all_tle, fetch_data, clean_observations,
|
||||
station_status_update)
|
||||
station_status_update, stations_cache_rates)
|
||||
|
||||
sender.add_periodic_task(RUN_HOURLY, update_all_tle.s(),
|
||||
name='update-all-tle')
|
||||
|
@ -35,6 +35,9 @@ def setup_periodic_tasks(sender, **kwargs):
|
|||
sender.add_periodic_task(RUN_DAILY, clean_observations.s(),
|
||||
name='clean-observations')
|
||||
|
||||
sender.add_periodic_task(RUN_HOURLY, stations_cache_rates.s(),
|
||||
name='stations-cache-rates')
|
||||
|
||||
|
||||
if settings.ENVIRONMENT == 'production' or settings.ENVIRONMENT == 'stage':
|
||||
from opbeat.contrib.django.models import client, logger, register_handlers
|
||||
|
|
Loading…
Reference in New Issue