second-brain-api 0.13.3

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
use anyhow::Result;
use chrono::Utc;
use second_brain_core::sqlite_store::SqliteStore;
use second_brain_sync::import::{ImportStats, import_values};

pub struct Applied {
    pub stats: ImportStats,
    /// The highest seq of the source's log applied here without a gap.
    pub max_seq: u64,
    pub reached: u64,
}

/// Imports records from `source`'s log. Records kept from earlier imports
/// are retried first; a record that still fails is kept, retried on later
/// imports and holds the gapless seq below it until it applies or settles.
/// A stream that names no source keeps the per-call watermark.
pub fn import_from(
    sqlite: &SqliteStore,
    source: Option<&str>,
    records: &[serde_json::Value],
) -> Result<Applied> {
    let now = Utc::now().timestamp_micros();
    if let Some(source) = source {
        let retried = sqlite.retry_failed_records(source, now)?;
        if retried.applied + retried.settled > 0 {
            tracing::info!(
                source,
                applied = retried.applied,
                settled = retried.settled,
                "retried kept sync records"
            );
        }
    }
    let (mut stats, watermark) = import_values(sqlite, records)?;
    let reached = stats.reached.max(watermark);
    let Some(source) = source else {
        return Ok(Applied {
            stats,
            max_seq: watermark,
            reached,
        });
    };
    for (record, error) in std::mem::take(&mut stats.failed) {
        sqlite.note_failed_record(source, &record, &error, now)?;
    }
    let max_seq = sqlite.applied_through(source, reached)?;
    Ok(Applied {
        stats,
        max_seq,
        reached,
    })
}