From 7d370fe4b2ef963d666405361cdb4a71a098a6b3 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 26 Apr 2020 04:06:15 +0000 Subject: [PATCH] Add function to calculate member signup counts --- .../api/management/commands/run_hourly.py | 14 ++++++++------ apiserver/apiserver/api/utils_stats.py | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/apiserver/apiserver/api/management/commands/run_hourly.py b/apiserver/apiserver/api/management/commands/run_hourly.py index 165caf8..0a7713a 100644 --- a/apiserver/apiserver/api/management/commands/run_hourly.py +++ b/apiserver/apiserver/api/management/commands/run_hourly.py @@ -1,26 +1,28 @@ from django.core.management.base import BaseCommand, CommandError -from django.utils.timezone import now, pytz +from django.utils.timezone import now 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() member_count, green_count = utils_stats.calc_member_counts() + signup_count = utils_stats.calc_signup_counts() # do this hourly in case an admin causes a change models.StatsMemberCount.objects.update_or_create( - date=today_alberta_tz(), + date=utils.today_alberta_tz(), defaults=dict(member_count=member_count, green_count=green_count), ) + models.StatsSignupCount.objects.update_or_create( + month=utils.today_alberta_tz().replace(day=1), + defaults=dict(signup_count=signup_count), + ) + def handle(self, *args, **options): self.stdout.write('{} - Beginning hourly tasks'.format(str(now()))) diff --git a/apiserver/apiserver/api/utils_stats.py b/apiserver/apiserver/api/utils_stats.py index b85bcc7..da53fc6 100644 --- a/apiserver/apiserver/api/utils_stats.py +++ b/apiserver/apiserver/api/utils_stats.py @@ -2,13 +2,16 @@ import logging logger = logging.getLogger(__name__) import time -import datetime +from datetime import date, datetime import requests from django.core.cache import cache -from django.utils.timezone import now +from django.utils.timezone import now, pytz from apiserver.api import models from apiserver import secrets +def today_alberta_tz(): + return datetime.now(pytz.timezone('America/Edmonton')).date() + DEFAULTS = { 'last_card_change': time.time(), 'next_meeting': None, @@ -63,6 +66,15 @@ def calc_member_counts(): return member_count, green_count +def calc_signup_counts(): + month_beginning = today_alberta_tz().replace(day=1) + + members = models.Member.objects + new_members = members.filter(application_date__gte=month_beginning) + num_new_members = new_members.count() + + return num_new_members + def check_minecraft_server(): if secrets.MINECRAFT: url = 'https://api.minetools.eu/ping/' + secrets.MINECRAFT