Add hourly task to calculate next events and member counts

This commit is contained in:
Tanner Collin 2020-02-10 08:01:43 +00:00
parent 47cf9532e7
commit 49f6da8ae5
2 changed files with 47 additions and 3 deletions

View 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)
))

View File

@ -1,13 +1,14 @@
from django.core.cache import cache
import time
from django.core.cache import cache
from apiserver.api import models
DEFAULTS = {
'last_card_change': time.time(),
'next_meeting': None,
'next_clean': None,
'member_count': None,
'due_members': None,
'old_members': None,
'paused_count': None,
'green_count': None,
'bay_108_temp': None,
'bay_110_temp': None,
}
@ -18,3 +19,24 @@ def changed_card():
user status becoming overdue by 3 months
'''
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)