headless_lms_models/library/
page_visit_stats.rs

1//! Functions for precomputing anonymous aggregate statistics about page visits.
2
3use crate::prelude::*;
4
5/// Calculates the latest statistics for dates that are not yet calculated yet.
6pub async fn calculate_latest(conn: &mut PgConnection) -> ModelResult<()> {
7    let latest_date =
8        crate::page_visit_datum_summary_by_courses::get_latest_date(&mut *conn).await?;
9    let date_today = Utc::now().naive_utc().date();
10    let yesterday = date_today - chrono::Duration::days(1);
11    let cutoff_date = {
12        let cutoff_date = latest_date.map(|d| d - chrono::Duration::days(1));
13        if let Some(cutoff_date) = cutoff_date {
14            cutoff_date
15        } else {
16            let oldest_date = crate::page_visit_datum::get_oldest_date(&mut *conn).await?;
17            oldest_date.unwrap_or(date_today) - chrono::Duration::days(1)
18        }
19    };
20
21    info!(
22        "Calculating page view daily stats from {} to {}",
23        cutoff_date, yesterday
24    );
25    let mut current_date = cutoff_date;
26    while current_date <= yesterday {
27        let mut tx = conn.begin().await?;
28        info!("Calculating page view daily stats for {}", current_date);
29        info!("Calculating page view daily stats by courses");
30        let res_by_courses =
31            crate::page_visit_datum_summary_by_courses::calculate_and_update_for_date(
32                &mut tx,
33                current_date,
34            )
35            .await?;
36        info!(
37            "Calculated {} page view daily stats by courses",
38            res_by_courses.len()
39        );
40        info!("Calculating page view daily stats by pages");
41        let res_by_pages = crate::page_visit_datum_summary_by_pages::calculate_and_update_for_date(
42            &mut tx,
43            current_date,
44        )
45        .await?;
46        info!(
47            "Calculated {} page view daily stats by pages",
48            res_by_pages.len()
49        );
50        info!("Calculating page view daily stats by courses device types");
51        let res_by_courses_device_types =
52            crate::page_visit_datum_summary_by_courses_device_types::calculate_and_update_for_date(
53                &mut tx,
54                current_date,
55            )
56            .await?;
57        info!(
58            "Calculated {} page view daily stats by courses device types",
59            res_by_courses_device_types.len()
60        );
61        info!("Calculating page view daily stats by courses countries");
62        let res_by_courses_countries =
63            crate::page_visit_datum_summary_by_courses_countries::calculate_and_update_for_date(
64                &mut tx,
65                current_date,
66            )
67            .await?;
68        info!(
69            "Calculated {} page view daily stats by courses countries",
70            res_by_courses_countries.len()
71        );
72        tx.commit().await?;
73        current_date += chrono::Duration::days(1);
74    }
75
76    Ok(())
77}