1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Functions for precomputing anonymous aggregate statistics about page visits.

use crate::prelude::*;

/// Calculates the latest statistics for dates that are not yet calculated yet.
pub async fn calculate_latest(conn: &mut PgConnection) -> ModelResult<()> {
    let latest_date =
        crate::page_visit_datum_summary_by_courses::get_latest_date(&mut *conn).await?;
    let date_today = Utc::now().naive_utc().date();
    let yesterday = date_today - chrono::Duration::days(1);
    let cutoff_date = {
        let cutoff_date = latest_date.map(|d| d - chrono::Duration::days(1));
        if let Some(cutoff_date) = cutoff_date {
            cutoff_date
        } else {
            let oldest_date = crate::page_visit_datum::get_oldest_date(&mut *conn).await?;
            oldest_date.unwrap_or(date_today) - chrono::Duration::days(1)
        }
    };

    info!(
        "Calculating page view daily stats from {} to {}",
        cutoff_date, yesterday
    );
    let mut current_date = cutoff_date;
    while current_date <= yesterday {
        let mut tx = conn.begin().await?;
        info!("Calculating page view daily stats for {}", current_date);
        info!("Calculating page view daily stats by courses");
        let res_by_courses =
            crate::page_visit_datum_summary_by_courses::calculate_and_update_for_date(
                &mut tx,
                current_date,
            )
            .await?;
        info!(
            "Calculated {} page view daily stats by courses",
            res_by_courses.len()
        );
        info!("Calculating page view daily stats by pages");
        let res_by_pages = crate::page_visit_datum_summary_by_pages::calculate_and_update_for_date(
            &mut tx,
            current_date,
        )
        .await?;
        info!(
            "Calculated {} page view daily stats by pages",
            res_by_pages.len()
        );
        info!("Calculating page view daily stats by courses device types");
        let res_by_courses_device_types =
            crate::page_visit_datum_summary_by_courses_device_types::calculate_and_update_for_date(
                &mut tx,
                current_date,
            )
            .await?;
        info!(
            "Calculated {} page view daily stats by courses device types",
            res_by_courses_device_types.len()
        );
        info!("Calculating page view daily stats by courses countries");
        let res_by_courses_countries =
            crate::page_visit_datum_summary_by_courses_countries::calculate_and_update_for_date(
                &mut tx,
                current_date,
            )
            .await?;
        info!(
            "Calculated {} page view daily stats by courses countries",
            res_by_courses_countries.len()
        );
        tx.commit().await?;
        current_date += chrono::Duration::days(1);
    }

    Ok(())
}