Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

1.8 KB
Raw Blame
//! Recording what a planning run has found in Notion so far, so the user
//! sees the scan grow while the inventory walks the workspace.

use macro_user_id::user_id::MacroUserIdStr;
use tokio::sync::watch;
use tokio::time::Instant;

use super::super::notion_workspace::NOTIFY_EVERY;
use crate::domain::models::{ImportSource, ScanCounts};
use crate::domain::ports::NotionPlanRepo;

#[cfg(test)]
mod test;

/// Write the newest counts from `counts` to the run, at most every
/// [`NOTIFY_EVERY`], and the last ones as soon as the sender is dropped.
/// Returns once the sender is gone and everything sent is written.
pub(in crate::domain::service) async fn record_scan<R: NotionPlanRepo>(
    repo: &R,
    user: &MacroUserIdStr<'static>,
    mut counts: watch::Receiver<ScanCounts>,
    source: ImportSource,
) {
    let mut unwritten = false;
    let mut next_write = Instant::now();
    loop {
        tokio::select! {
            changed = counts.changed() => match changed {
                Ok(()) => unwritten = true,
                Err(_) => break,
            },
            () = tokio::time::sleep_until(next_write), if unwritten => {
                let latest = *counts.borrow();
                write(repo, user, latest, source).await;
                unwritten = false;
                next_write = Instant::now() + NOTIFY_EVERY;
            }
        }
    }
    if unwritten {
        let latest = *counts.borrow();
        write(repo, user, latest, source).await;
    }
}

async fn write<R: NotionPlanRepo>(
    repo: &R,
    user: &MacroUserIdStr<'static>,
    scanned: ScanCounts,
    source: ImportSource,
) {
    if let Err(error) = repo.record_scan(user, source, scanned).await {
        tracing::warn!(error = ?error, "could not record the workspace scan progress");
    }
}