Commit 63164bd7 authored by Maxime Emschwiller's avatar Maxime Emschwiller
Browse files

refacto(backend): refacto utils and computing functions to use them in backfill

parent 49edc878
from datetime import timedelta from datetime import datetime, timedelta
from collections import Counter from collections import Counter
from typing import Optional
from backend_app.models.university import University from backend_app.models.university import University
...@@ -11,23 +13,26 @@ from stats_app.utils import ( ...@@ -11,23 +13,26 @@ from stats_app.utils import (
) )
def update_daily_connections(): def update_daily_connections(date: Optional[datetime] = None):
yesterday = get_today_as_datetime() - timedelta(days=1) if date is None:
date = get_today_as_datetime() - timedelta(days=1)
DailyConnections.objects.update_or_create( DailyConnections.objects.update_or_create(
date=yesterday, defaults=dict(nb_connections=get_daily_connections()) date=date, defaults=dict(nb_connections=get_daily_connections(date))
) )
def update_daily_exchange_contributions_info(): def update_daily_exchange_contributions_info(date: Optional[datetime] = None):
yesterday = get_today_as_datetime() - timedelta(days=1) if date is None:
date = get_today_as_datetime() - timedelta(days=1)
contributions_profiles = get_contributions_profiles() contributions_profiles = get_contributions_profiles(date)
nb_contributions_by_profile = Counter(contributions_profiles) nb_contributions_by_profile = Counter(contributions_profiles)
for contribution_profile, nb_contributions in nb_contributions_by_profile.items(): for contribution_profile, nb_contributions in nb_contributions_by_profile.items():
university = University.objects.get(pk=contribution_profile.university_pk) university = University.objects.get(pk=contribution_profile.university_pk)
DailyExchangeContributionsInfo.objects.update_or_create( DailyExchangeContributionsInfo.objects.update_or_create(
date=yesterday, date=date,
major=contribution_profile.major, major=contribution_profile.major,
minor=contribution_profile.minor, minor=contribution_profile.minor,
exchange_semester=contribution_profile.exchange_semester, exchange_semester=contribution_profile.exchange_semester,
...@@ -36,6 +41,9 @@ def update_daily_exchange_contributions_info(): ...@@ -36,6 +41,9 @@ def update_daily_exchange_contributions_info():
) )
def update_all_stats(): def update_all_stats(date: Optional[datetime] = None):
update_daily_connections() if date is None:
update_daily_exchange_contributions_info() date = get_today_as_datetime() - timedelta(days=1)
update_daily_connections(date)
update_daily_exchange_contributions_info(date)
...@@ -10,17 +10,16 @@ from backend_app.models.courseFeedback import CourseFeedback ...@@ -10,17 +10,16 @@ from backend_app.models.courseFeedback import CourseFeedback
from backend_app.models.exchange import Exchange from backend_app.models.exchange import Exchange
def get_today_as_datetime(): def get_today_as_datetime() -> datetime:
now = datetime.now() now = datetime.now()
today = now.replace(hour=0, minute=0, second=0, microsecond=1) today = now.replace(hour=0, minute=0, second=0, microsecond=1)
return make_aware(today) return make_aware(today)
def get_daily_connections() -> int: def get_daily_connections(date: datetime) -> int:
today = get_today_as_datetime() date_to = date + timedelta(days=1)
yesterday = today - timedelta(days=1)
nb_connections = User.objects.filter( nb_connections = User.objects.filter(
last_login__gte=yesterday, last_login__lt=today last_login__gte=date, last_login__lt=date_to
).count() ).count()
return nb_connections return nb_connections
...@@ -46,19 +45,19 @@ def _get_profile_from_exchange(exchange: Exchange) -> ContributionProfile: ...@@ -46,19 +45,19 @@ def _get_profile_from_exchange(exchange: Exchange) -> ContributionProfile:
) )
def get_contributions_profiles() -> List[ContributionProfile]: def get_contributions_profiles(date: datetime) -> List[ContributionProfile]:
""" """
return the yesterday contributions profiles Return the contributions profiles from the 24 hours following the date arg.
If no university if associated with a contribution we don't return it If no university if associated with a contribution we don't return it.
""" """
today = get_today_as_datetime() date_to = date + timedelta(days=1)
yesterday = today - timedelta(days=1)
contributions_profiles = [] contributions_profiles = []
exchange_feedbacks = ExchangeFeedback.objects.filter( exchange_feedbacks = ExchangeFeedback.objects.filter(
updated_on__gte=yesterday, updated_on__gte=date,
updated_on__lt=today, updated_on__lt=date_to,
exchange__university__isnull=False, exchange__university__isnull=False,
untouched=False, untouched=False,
).prefetch_related("exchange") ).prefetch_related("exchange")
...@@ -69,8 +68,8 @@ def get_contributions_profiles() -> List[ContributionProfile]: ...@@ -69,8 +68,8 @@ def get_contributions_profiles() -> List[ContributionProfile]:
contributions_profiles.append(contribution_profile) contributions_profiles.append(contribution_profile)
course_feedbacks = CourseFeedback.objects.filter( course_feedbacks = CourseFeedback.objects.filter(
updated_on__gte=yesterday, updated_on__gte=date,
updated_on__lt=today, updated_on__lt=date_to,
course__exchange__university__isnull=False, course__exchange__university__isnull=False,
untouched=False, untouched=False,
).prefetch_related("course__exchange") ).prefetch_related("course__exchange")
......
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