compute_stats.py 1.38 KB
Newer Older
1
from datetime import timedelta
2
3
from collections import Counter
from backend_app.models.university import University
4

5
6
7
8
9
10
11

from stats_app.models import DailyConnections, DailyExchangeContributionsInfo
from stats_app.utils import (
    get_daily_connections,
    get_today_as_datetime,
    get_contributions_profiles,
)
12
13
14
15
16
17
18
19
20


def update_daily_connections():
    yesterday = get_today_as_datetime() - timedelta(days=1)
    DailyConnections.objects.update_or_create(
        date=yesterday, defaults=dict(nb_connections=get_daily_connections())
    )


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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),
        )


39
40
def update_all_stats():
    update_daily_connections()
41
    update_daily_exchange_contributions_info()