MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / read_hook_activity

Function read_hook_activity

atomic-cli/src/commands/diagnostics/collect.rs:805–849  ·  view source on GitHub ↗

Read immutable observations without starting the service or opening the repository for writes. Any unreadable source makes the latest time unknown.

(
    root: &Path,
)

Source from the content-addressed store, hash-verified

803/// Read immutable observations without starting the service or opening the
804/// repository for writes. Any unreadable source makes the latest time unknown.
805fn read_hook_activity(
806 root: &Path,
807) -> Result<BTreeMap<String, chrono::DateTime<chrono::Utc>>, &'static str> {
808 use atomic_core::pristine::tables::SESSION_LOG;
809 use redb::{ReadableDatabase, ReadableTable};
810
811 let dot = atomic_repository::Repository::canonical_dot_dir(root)
812 .map_err(|_| "hook history location cannot be resolved")?;
813 let database = redb::ReadOnlyDatabase::open(dot.join("atomic.redb"))
814 .map_err(|_| "hook history cannot be read or is in use")?;
815 let txn = database
816 .begin_read()
817 .map_err(|_| "hook history cannot be read")?;
818 let mut times = BTreeMap::new();
819 let mut observe = |event: atomic_agent::hook_log::SessionLogEvent| -> Result<(), &'static str> {
820 let at = chrono::DateTime::parse_from_rfc3339(&event.at)
821 .map_err(|_| "hook history contains an invalid timestamp")?
822 .with_timezone(&chrono::Utc);
823 times
824 .entry(event.agent)
825 .and_modify(|last: &mut chrono::DateTime<chrono::Utc>| {
826 *last = (*last).max(at);
827 })
828 .or_insert(at);
829 Ok(())
830 };
831 match txn.open_table(SESSION_LOG) {
832 Ok(table) => {
833 for entry in table.iter().map_err(|_| "hook history cannot be read")? {
834 let (_, value) = entry.map_err(|_| "hook history cannot be read")?;
835 let event = serde_json::from_slice(value.value())
836 .map_err(|_| "hook history contains an invalid event")?;
837 observe(event)?;
838 }
839 }
840 Err(redb::TableError::TableDoesNotExist(_)) => {}
841 Err(_) => return Err("hook history cannot be read"),
842 }
843 for (_, event) in
844 atomic_agent::hook_log::pending(root).map_err(|_| "pending hook history cannot be read")?
845 {
846 observe(event)?;
847 }
848 Ok(times)
849}
850
851// Receipts and files
852

Callers 1

collect_fromFunction · 0.85

Calls 6

openFunction · 0.85
observeFunction · 0.85
pendingFunction · 0.85
begin_readMethod · 0.80
valueMethod · 0.80
iterMethod · 0.45

Tested by

no test coverage detected