use std::path::Path;
use balise_core::journal::{DIR, FILE, PREVIOUS};
use balise_core::json::{self, Value};
use crate::value::{get, number, text};
pub struct Record {
pub value: Value,
}
impl Record {
pub fn kind(&self) -> &str {
text(&self.value, "kind").unwrap_or_default()
}
pub fn ms(&self) -> u64 {
number(&self.value, "ms") as u64
}
pub fn event(&self) -> Option<&Value> {
get(&self.value, "event")
}
pub fn text(&self) -> &str {
text(&self.value, "text").unwrap_or_default()
}
pub fn request_id(&self) -> Option<&str> {
match self.kind() {
"route" => text(&self.value, "request_id"),
_ => self.event().and_then(|event| text(event, "request_id")),
}
}
}
pub fn read(root: &Path) -> Option<Vec<Record>> {
let dir = root.join(DIR);
let mut found = false;
let mut records = Vec::new();
for name in [PREVIOUS, FILE] {
let Ok(content) = std::fs::read_to_string(dir.join(name)) else {
continue;
};
found = true;
records.extend(
content
.lines()
.filter_map(|line| json::parse(line.as_bytes()).ok())
.map(|value| Record { value }),
);
}
found.then_some(records)
}