use anomalyzer_ts::{AnomalyzerConf, PersistentAnomalyzer};
use std::path::Path;
const DATA_DIR: &str = "/tmp/anomalyzer-demo";
fn conf() -> AnomalyzerConf {
AnomalyzerConf {
active_size: 1,
n_seasons: 4,
methods: vec!["magnitude".to_string(), "highrank".to_string()],
..Default::default()
}
}
fn run_one() {
println!("── Run 1 (first process) ──────────────────────────");
let mut detector = PersistentAnomalyzer::open(DATA_DIR, conf())
.expect("failed to open persistence dir");
let baseline = [10.0f64, 10.1, 10.2, 10.0, 10.15];
for v in baseline {
let prob = detector.push(v).unwrap();
println!(" baseline {v:>6.2} → prob {prob:.3}");
}
let prob = detector.push(18.5).unwrap();
println!(" spike {:>6.2} → prob {prob:.3} {}", 18.5, if prob > 0.7 { "⚠️ ANOMALY" } else { "" });
detector.flush().unwrap();
println!(" WAL entries after flush: {}", detector.pending_wal_entries());
println!(" history saved to {DATA_DIR}\n");
}
fn run_two() {
println!("── Run 2 (process restart) ────────────────────────");
let mut detector = PersistentAnomalyzer::open(DATA_DIR, conf())
.expect("failed to open persistence dir");
let prob = detector.eval();
println!(" restored state → prob {prob:.3} (spike still in window)");
let new_values = [10.2f64, 10.0, 10.3];
println!(" pushing new values after restart:");
for v in new_values {
let prob = detector.push(v).unwrap();
println!(" {v:>6.2} → prob {prob:.3}");
}
println!("\n WAL size: {} bytes", detector.wal_size_bytes().unwrap());
}
fn main() {
if Path::new(DATA_DIR).exists() {
std::fs::remove_dir_all(DATA_DIR).unwrap();
}
run_one();
run_two();
}