Skip to main content

lean_ctx/core/
proxy_introspect.rs

1// SPDX-License-Identifier: Apache-2.0
2//! The proxy's persisted request introspection: the proxy process writes
3//! `proxy-introspect.json` (`proxy::introspect`), `gain` and the dashboard read it.
4
5/// Load persisted proxy introspection data from disk.
6/// Returns `None` if the file doesn't exist or is stale (> `max_age_secs`).
7pub fn load_persisted(max_age_secs: u64) -> Option<serde_json::Value> {
8    let data_dir = crate::core::data_dir::lean_ctx_data_dir().ok()?;
9    let path = data_dir.join("proxy-introspect.json");
10    let content = std::fs::read_to_string(&path).ok()?;
11    let val: serde_json::Value = serde_json::from_str(&content).ok()?;
12
13    let ts = val
14        .get("ts")
15        .and_then(serde_json::Value::as_u64)
16        .unwrap_or(0);
17    let now = std::time::SystemTime::now()
18        .duration_since(std::time::UNIX_EPOCH)
19        .unwrap_or_default()
20        .as_secs();
21    if now.saturating_sub(ts) > max_age_secs {
22        return None;
23    }
24    Some(val)
25}