Commit 1fc1f920 authored by Maxime Emschwiller's avatar Maxime Emschwiller
Browse files

feature(backend): Add compute functions and utils to get stats about contributions

parent 23db719f
from datetime import timedelta
from collections import Counter
from backend_app.models.university import University
from stats_app.models import DailyConnections
from stats_app.utils import get_daily_connections, get_today_as_datetime
from stats_app.models import DailyConnections, DailyExchangeContributionsInfo
from stats_app.utils import (
get_daily_connections,
get_today_as_datetime,
get_contributions_profiles,
)
def update_daily_connections():
......@@ -11,5 +18,24 @@ def update_daily_connections():
)
def update_daily_exchange_contributions_info():
yesterday = get_today_as_datetime() - timedelta(days=1)
contributions_profiles = get_contributions_profiles()
nb_contributions_by_profile = Counter(contributions_profiles)
for contribution_profile, nb_contributions in nb_contributions_by_profile.items():
university = University.objects.get(pk=contribution_profile.university_pk)
DailyExchangeContributionsInfo.objects.update_or_create(
date=yesterday,
major=contribution_profile.major,
minor=contribution_profile.minor,
exchange_semester=contribution_profile.exchange_semester,
university=university,
defaults=dict(nb_contributions=nb_contributions),
)
def update_all_stats():
update_daily_connections()
update_daily_exchange_contributions_info()
from datetime import datetime, timedelta
from django.utils.timezone import make_aware
from dataclasses import dataclass
from typing import List
from base_app.models import User
from backend_app.models.exchangeFeedback import ExchangeFeedback
from backend_app.models.courseFeedback import CourseFeedback
from backend_app.models.exchange import Exchange
def get_today_as_datetime():
now = datetime.now()
......@@ -17,3 +23,60 @@ def get_daily_connections() -> int:
last_login__gte=yesterday, last_login__lt=today
).count()
return nb_connections
@dataclass(eq=True, frozen=True)
class ContributionProfile:
"""
Class for keeping track a profile.
"""
major: str
minor: str
exchange_semester: str
university_pk: int
def _get_profile_from_exchange(exchange: Exchange) -> ContributionProfile:
return ContributionProfile(
major=exchange.student_major if exchange.student_major is not None else "",
minor=exchange.student_minor if exchange.student_minor is not None else "",
exchange_semester=f"{exchange.semester}{exchange.year}",
university_pk=exchange.university.pk,
)
def get_contributions_profiles() -> List[ContributionProfile]:
"""
return the yesterday contributions profiles
If no university if associated with a contribution we don't return it
"""
today = get_today_as_datetime()
yesterday = today - timedelta(days=1)
contributions_profiles = []
exchange_feedbacks = ExchangeFeedback.objects.filter(
updated_on__gte=yesterday,
updated_on__lt=today,
exchange__university__isnull=False,
untouched=False,
).prefetch_related("exchange")
for exchange_feedback in exchange_feedbacks:
exchange = exchange_feedback.exchange
contribution_profile = _get_profile_from_exchange(exchange)
contributions_profiles.append(contribution_profile)
course_feedbacks = CourseFeedback.objects.filter(
updated_on__gte=yesterday,
updated_on__lt=today,
course__exchange__university__isnull=False,
untouched=False,
).prefetch_related("course__exchange")
for course_feedback in course_feedbacks:
exchange = course_feedback.course.exchange
contribution_profile = _get_profile_from_exchange(exchange)
contributions_profiles.append(contribution_profile)
return contributions_profiles
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment