Skip to main content

headless_lms_credit_registration/use_cases/
ledger_snapshot.rs

1//! The `ledger-snapshot` phase: the day's queue depth for every ledger state.
2
3use chrono::{Duration, NaiveTime, Utc};
4use headless_lms_models::credit_registration_daily_snapshots::{
5    count_states_for_day, write_snapshot_for_date,
6};
7use sqlx::PgPool;
8
9use crate::error::CreditRegistrationResult;
10use crate::workflow::Counts;
11
12/// Its own phase rather than folded into `config-validation`'s per-module check: a scoped run must
13/// never write a snapshot that claims to cover every course, and `ScopeSupport::NONE` only enforces
14/// that if the write has no other job sharing its dispatch.
15pub(crate) async fn run(pool: &PgPool) -> CreditRegistrationResult<Counts> {
16    let mut conn = pool.acquire().await?;
17    let today = Utc::now().date_naive();
18    let day_start = today.and_time(NaiveTime::MIN).and_utc();
19    let counts = count_states_for_day(&mut conn, day_start, day_start + Duration::days(1)).await?;
20    write_snapshot_for_date(&mut conn, today, &counts).await?;
21    Ok(Counts::processed(counts.len() as i64))
22}