Add member count stats table and update it hourly

This commit is contained in:
Tanner Collin 2020-04-21 05:45:20 +00:00
parent 0c3805d86d
commit 8d4a63fbfc
3 changed files with 25 additions and 7 deletions

View File

@ -1,15 +1,25 @@
from django.core.management.base import BaseCommand, CommandError
from django.utils.timezone import now
from django.utils.timezone import now, pytz
from apiserver.api import models, utils, utils_stats
from datetime import datetime
import time
def today_alberta_tz():
return datetime.now(pytz.timezone('America/Edmonton')).date()
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()
member_count, green_count = utils_stats.calc_member_counts()
# do this hourly in case an admin causes a change
models.StatsMemberCount.objects.update_or_create(
date=today_alberta_tz(),
defaults=dict(member_count=member_count, green_count=green_count),
)
def handle(self, *args, **options):

View File

@ -128,6 +128,11 @@ class Training(models.Model):
class MetaInfo(models.Model):
backup_id = models.TextField()
class StatsMemberCount(models.Model):
date = models.DateField(default=today_alberta_tz)
member_count = models.IntegerField()
green_count = models.IntegerField()
class HistoryIndex(models.Model):
content_type = models.ForeignKey(ContentType, null=True, on_delete=models.SET_NULL)
object_id = models.PositiveIntegerField()

View File

@ -53,12 +53,15 @@ def calc_member_counts():
num_due = not_paused.filter(status='Due').count()
num_overdue = not_paused.filter(status='Overdue').count()
num_active = num_current + num_prepaid + num_due + num_overdue
num_former = members.count() - num_active
member_count = num_current + num_prepaid + num_due + num_overdue
paused_count = members.count() - member_count
green_count = num_current + num_prepaid
cache.set('member_count', num_active)
cache.set('paused_count', num_former)
cache.set('green_count', num_current + num_prepaid)
cache.set('member_count', member_count)
cache.set('paused_count', paused_count)
cache.set('green_count', green_count)
return member_count, green_count
def check_minecraft_server():
if secrets.MINECRAFT: