Skip to content

Commit

Permalink
user logic & timezone fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rachllee committed Apr 5, 2024
1 parent 2ee7af0 commit 4b6fe90
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
1 change: 1 addition & 0 deletions backend/ohq/management/commands/course_stat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.core.management.base import BaseCommand
from django.utils import timezone
import datetime

from ohq.models import Course, Question
from ohq.statistics import (
Expand Down
38 changes: 15 additions & 23 deletions backend/ohq/management/commands/user_stat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.core.management.base import BaseCommand
from django.utils import timezone
from datetime import timedelta
from django.conf import settings
import datetime

from ohq.models import Profile, Course, Question
from ohq.statistics import (
Expand All @@ -12,54 +14,44 @@
)
from django.db.models import Q

from django.contrib.auth import get_user_model

User = get_user_model()




class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("--hist", action="store_true", help="Calculate all historic statistics")

def calculate_statistics(self, profiles, courses, earliest_date):
yesterday = timezone.datetime.today().date() - timezone.timedelta(days=1)
active_users_today = set()

def calculate_statistics(self, courses, earliest_date):
for course in courses:
if not earliest_date:
course_questions = Question.objects.filter(queue__course=course)
earliest_date = (
timezone.template_localtime(
course_questions.earliest("time_asked").time_asked
).date()
if course_questions
else yesterday
)

questions_queryset = Question.objects.filter(queue__course=course, time_asked__gte=earliest_date)
users_union = (
Profile.objects.filter(
User.objects.filter(
Q(id__in=questions_queryset.values_list("asked_by", flat=True)) |
Q(id__in=questions_queryset.values_list("responded_to_by", flat=True))
)
)

for user in users_union:
user_calculate_questions_asked(user.user)
user_calculate_questions_answered(user.user)
user_calculate_time_helped(user.user)
user_calculate_time_helping(user.user)
user_calculate_students_helped(user.user)
user_calculate_questions_asked(user)
user_calculate_questions_answered(user)
user_calculate_time_helped(user)
user_calculate_time_helping(user)
user_calculate_students_helped(user)


def handle(self, *args, **kwargs):
if kwargs["hist"]:
courses = Course.objects.all()
profiles = Profile.objects.all()
earliest_date = None
earliest_date = timezone.make_aware(datetime.datetime.utcfromtimestamp(0))
else:
courses = Course.objects.filter(archived=False)
profiles = Profile.objects.all()
earliest_date = timezone.now().date() - timedelta(days=1)

self.calculate_statistics(profiles, courses, earliest_date)
self.calculate_statistics(courses, earliest_date)


7 changes: 7 additions & 0 deletions k8s/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ export class MyChart extends PennLabsChart {
secret: secret,
cmd: ['python', 'manage.py', 'course_stat'],
});

new CronJob(this, 'calculate-user-stat', {
schedule: cronTime.everyDayAt(8),
image: backendImage,
secret: secret,
cmd: ['python', 'manage.py', 'user_stat'],
});
}
}

Expand Down

0 comments on commit 4b6fe90

Please sign in to comment.