Add hourly task to calculate next events and member counts
This commit is contained in:
parent
47cf9532e7
commit
49f6da8ae5
22
apiserver/apiserver/api/management/commands/run_hourly.py
Normal file
22
apiserver/apiserver/api/management/commands/run_hourly.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
|
from apiserver.api import models, utils, utils_stats
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = 'Tasks to run on the portal hourly.'
|
||||||
|
|
||||||
|
def generate_stats(self):
|
||||||
|
utils_stats.calc_next_events()
|
||||||
|
utils_stats.calc_member_counts()
|
||||||
|
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
start = time.time()
|
||||||
|
|
||||||
|
count = self.generate_stats()
|
||||||
|
self.stdout.write('Generated stats')
|
||||||
|
|
||||||
|
self.stdout.write('Completed tasks in {} s'.format(
|
||||||
|
str(time.time() - start)
|
||||||
|
))
|
|
@ -1,13 +1,14 @@
|
||||||
from django.core.cache import cache
|
|
||||||
import time
|
import time
|
||||||
|
from django.core.cache import cache
|
||||||
|
from apiserver.api import models
|
||||||
|
|
||||||
DEFAULTS = {
|
DEFAULTS = {
|
||||||
'last_card_change': time.time(),
|
'last_card_change': time.time(),
|
||||||
'next_meeting': None,
|
'next_meeting': None,
|
||||||
'next_clean': None,
|
'next_clean': None,
|
||||||
'member_count': None,
|
'member_count': None,
|
||||||
'due_members': None,
|
'paused_count': None,
|
||||||
'old_members': None,
|
'green_count': None,
|
||||||
'bay_108_temp': None,
|
'bay_108_temp': None,
|
||||||
'bay_110_temp': None,
|
'bay_110_temp': None,
|
||||||
}
|
}
|
||||||
|
@ -18,3 +19,24 @@ def changed_card():
|
||||||
user status becoming overdue by 3 months
|
user status becoming overdue by 3 months
|
||||||
'''
|
'''
|
||||||
cache.set('last_card_change', time.time())
|
cache.set('last_card_change', time.time())
|
||||||
|
|
||||||
|
def calc_next_events():
|
||||||
|
sessions = models.Session.objects
|
||||||
|
|
||||||
|
member_meeting = sessions.filter(course=317).last()
|
||||||
|
monthly_clean = sessions.filter(course=273).last()
|
||||||
|
|
||||||
|
cache.set('next_meeting', member_meeting.datetime)
|
||||||
|
cache.set('next_clean', monthly_clean.datetime)
|
||||||
|
|
||||||
|
def calc_member_counts():
|
||||||
|
members = models.Member.objects
|
||||||
|
|
||||||
|
num_not_paused = members.filter(paused_date__isnull=True).count()
|
||||||
|
num_paused = members.filter(paused_date__isnull=False).count()
|
||||||
|
num_current = members.filter(status='Current').count()
|
||||||
|
num_prepaid = members.filter(status='Prepaid').count()
|
||||||
|
|
||||||
|
cache.set('member_count', num_not_paused)
|
||||||
|
cache.set('paused_count', num_paused)
|
||||||
|
cache.set('green_count', num_current + num_prepaid)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user