util: add jsonl_load/jsonl_append helpers, convert graph.rs
Shared JSONL read/write in util.rs replaces hand-rolled patterns. graph.rs metrics load/save converted as first consumer. Co-Authored-By: ProofOfConcept <poc@bcachefs.org>
This commit is contained in:
parent
0e1e5a1981
commit
c6bb7c3910
2 changed files with 31 additions and 18 deletions
|
|
@ -524,27 +524,12 @@ fn metrics_log_path() -> std::path::PathBuf {
|
|||
|
||||
/// Load previous metrics snapshots
|
||||
pub fn load_metrics_history() -> Vec<MetricsSnapshot> {
|
||||
let path = metrics_log_path();
|
||||
let content = match std::fs::read_to_string(&path) {
|
||||
Ok(c) => c,
|
||||
Err(_) => return Vec::new(),
|
||||
};
|
||||
content.lines()
|
||||
.filter_map(|line| serde_json::from_str(line).ok())
|
||||
.collect()
|
||||
crate::util::jsonl_load(&metrics_log_path())
|
||||
}
|
||||
|
||||
/// Append a metrics snapshot to the log
|
||||
pub fn save_metrics_snapshot(snap: &MetricsSnapshot) {
|
||||
let path = metrics_log_path();
|
||||
if let Ok(json) = serde_json::to_string(snap) {
|
||||
use std::io::Write;
|
||||
if let Ok(mut f) = std::fs::OpenOptions::new()
|
||||
.create(true).append(true).open(&path)
|
||||
{
|
||||
let _ = writeln!(f, "{}", json);
|
||||
}
|
||||
}
|
||||
let _ = crate::util::jsonl_append(&metrics_log_path(), snap);
|
||||
}
|
||||
|
||||
/// Compute current graph metrics as a snapshot (no side effects).
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue