-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #309 from pennlabs/user-analytics
Add User Lifetime Statistics
- Loading branch information
Showing
8 changed files
with
474 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import datetime | ||
from datetime import timedelta | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
from django.db.models import Q | ||
from django.utils import timezone | ||
|
||
from ohq.models import Course, Question | ||
from ohq.statistics import ( | ||
user_calculate_questions_answered, | ||
user_calculate_questions_asked, | ||
user_calculate_students_helped, | ||
user_calculate_time_helped, | ||
user_calculate_time_helping, | ||
) | ||
|
||
|
||
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, courses, earliest_date): | ||
for course in courses: | ||
|
||
questions_queryset = Question.objects.filter( | ||
queue__course=course, time_asked__gte=earliest_date | ||
) | ||
users_union = 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_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() | ||
earliest_date = timezone.make_aware(datetime.datetime.utcfromtimestamp(0)) | ||
else: | ||
courses = Course.objects.filter(archived=False) | ||
earliest_date = timezone.now().date() - timedelta(days=1) | ||
|
||
self.calculate_statistics(courses, earliest_date) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Generated by Django 3.1.7 on 2024-03-26 02:26 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
("ohq", "0019_auto_20211114_1800"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="UserStatistic", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
), | ||
), | ||
( | ||
"metric", | ||
models.CharField( | ||
choices=[ | ||
("TOTAL_QUESTIONS_ASKED", "Total questions asked"), | ||
("TOTAL_QUESTIONS_ANSWERED", "Total questions answered"), | ||
("TOTAL_TIME_BEING_HELPED", "Total time being helped"), | ||
("TOTAL_TIME_HELPING", "Total time helping"), | ||
("TOTAL_STUDENTS_HELPED", "Total students helped"), | ||
], | ||
max_length=256, | ||
), | ||
), | ||
("value", models.DecimalField(decimal_places=8, max_digits=16)), | ||
( | ||
"user", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL | ||
), | ||
), | ||
], | ||
), | ||
migrations.AddConstraint( | ||
model_name="userstatistic", | ||
constraint=models.UniqueConstraint( | ||
fields=("user", "metric"), name="unique_user_statistic" | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.