diff --git a/komorebi-recorder/src/analysis/schemas.rs b/komorebi-recorder/src/analysis/schemas.rs index 75c00ea6..b119d484 100644 --- a/komorebi-recorder/src/analysis/schemas.rs +++ b/komorebi-recorder/src/analysis/schemas.rs @@ -23,6 +23,7 @@ use super::compare::CompareData; use super::envelope::Envelope; use super::framediff::FrameDiffData; use super::history::HistoryData; +use super::lag::LagData; use super::logq::LogqData; use super::measure::MeasureData; use super::motion::MotionData; @@ -180,6 +181,11 @@ pub const ALL: &[Entry] = &[ about: "`findings`: detector findings (with stable ids) and an agent's findings, together", schema: of::, }, + Entry { + name: "lag/1", + about: "`lag`: episodes of a window off its layout rect (or a border off its window), with duration and offsets", + schema: of::, + }, Entry { name: "assert/1", about: "`assert`: pass, fail or error for each scripted check, with what was observed", diff --git a/komorebi-recorder/src/cli/lag.rs b/komorebi-recorder/src/cli/lag.rs new file mode 100644 index 00000000..499c707a --- /dev/null +++ b/komorebi-recorder/src/cli/lag.rs @@ -0,0 +1,91 @@ +//! `lag`: how long, and by how much, windows were off the rect komorebi's layout gave them. +//! +//! The answer is [`Analysis::geometry_lag`] (`lag/1` with `--json`); see [`crate::analysis::lag`] +//! for what is measured, what is not flagged and why KDE has no answer. `--tolerance` and +//! `--grace-ms` change what counts as off, and `--window` narrows it to one window. +//! +//! Covered by the tests of the analysis and `tests/lag.rs`, which runs the command through the +//! real binary. + +use super::print_json; +use crate::analysis::Analysis; +use crate::analysis::envelope::Envelope; +use crate::analysis::lag::DEFAULT_GRACE_MS; +use crate::analysis::lag::DEFAULT_TOLERANCE; +use crate::analysis::lag::LagData; +use crate::analysis::lag::LagOptions; +use crate::analysis::lag::SCHEMA_LAG; +use crate::timecode; +use clap::Args; +use color_eyre::eyre; +use std::fmt::Write; +use std::path::PathBuf; + +/// The arguments of `lag`. +#[derive(Args, Debug)] +pub struct LagArgs { + /// Session directory + pub session: PathBuf, + /// Only this window (its id) + #[arg(long, value_name = "ID")] + pub window: Option, + /// The most an edge may be off, in the track's units (pixels, or points on a Mac), before the + /// window counts as off + #[arg(long, value_name = "N", default_value_t = DEFAULT_TOLERANCE)] + pub tolerance: i64, + /// How long after a layout change nothing is flagged, in milliseconds: komorebi's animations + /// move a window to its new rect over a fraction of a second + #[arg(long, value_name = "MS", default_value_t = DEFAULT_GRACE_MS)] + pub grace_ms: u64, + /// Machine-readable JSON output + #[arg(long)] + pub json: bool, +} + +/// Renders the answer as text, one episode to a line. +#[must_use] +pub fn lag_text(data: &LagData) -> String { + let mut out = String::new(); + let _ = writeln!( + out, + "{} episode(s) of geometry lag (tolerance {} {}, grace {} ms)", + data.episodes.len(), + data.tolerance, + data.unit, + data.grace_ms + ); + if let Some(note) = &data.note { + let _ = writeln!(out, " note: {note}"); + } + if data.track_available && data.independent && !data.borders_seen { + let _ = writeln!( + out, + " the track has no border windows, so which of border and window lagged is unknown" + ); + } + for episode in &data.episodes { + let _ = writeln!( + out, + " {} to {}: {}", + timecode::format(episode.start_video_s), + timecode::format(episode.end_video_s), + episode.describe(&data.unit) + ); + } + out +} + +pub(super) fn lag(args: &LagArgs) -> eyre::Result<()> { + let analysis = Analysis::open(&args.session)?; + let data = analysis.geometry_lag(&LagOptions { + tolerance: args.tolerance, + grace_ms: args.grace_ms, + window: args.window, + })?; + + if args.json { + return print_json(&Envelope::ok(SCHEMA_LAG, data).warnings(analysis.warnings())); + } + print!("{}", lag_text(&data)); + Ok(()) +} diff --git a/komorebi-recorder/src/cli/mod.rs b/komorebi-recorder/src/cli/mod.rs index 2030cf8b..9d8a0e8c 100644 --- a/komorebi-recorder/src/cli/mod.rs +++ b/komorebi-recorder/src/cli/mod.rs @@ -11,6 +11,7 @@ mod assert; mod doctor; mod findings; mod investigate; +mod lag; mod logq; pub mod mark; pub mod outputs; @@ -48,6 +49,7 @@ pub use investigate::EventsArgs; pub use investigate::StateArgs; pub use investigate::TraceArgs; pub use investigate::WindowArgs; +pub use lag::LagArgs; pub use logq::LogqArgs; pub use query::AtArgs; pub use query::FilterArgs; @@ -154,6 +156,8 @@ pub enum Command { Code(CodeArgs), /// What the detectors found and what an agent concluded, in one list, with stable ids Findings(FindingsArgs), + /// How long, and by how much, windows were off the rect komorebi's layout gave them (needs a window track) + Lag(LagArgs), /// Add, edit or delete a session's bug regions, notes and an agent's findings Annotate(AnnotateArgs), /// List a session's bug regions, notes and findings @@ -246,6 +250,7 @@ impl Command { Self::Reindex(args) => args.json, Self::Selftest(args) => args.json, Self::Findings(args) => args.json, + Self::Lag(args) => args.json, _ => false, } } @@ -321,6 +326,7 @@ pub fn run(command: Command) -> eyre::Result<()> { Command::Measure(args) => video::measure(&args), Command::Compare(args) => video::compare(&args), Command::Findings(args) => findings::findings(&args), + Command::Lag(args) => lag::lag(&args), Command::Annotate(args) => annotate::annotate(&args), Command::Annotations(args) => annotate::list(&args), Command::Mark(args) => mark::mark(&args), diff --git a/komorebi-recorder/tests/lag.rs b/komorebi-recorder/tests/lag.rs new file mode 100644 index 00000000..0ab7a6f4 --- /dev/null +++ b/komorebi-recorder/tests/lag.rs @@ -0,0 +1,104 @@ +//! `lag` and the `geometry-lag` finding, through the real binary. + +use komorebi_recorder::analysis::flavor::Flavor; +use komorebi_recorder::testkit::SessionBuilder; +use komorebi_recorder::testkit::container; +use komorebi_recorder::testkit::monitor; +use komorebi_recorder::testkit::state; +use komorebi_recorder::testkit::window; +use komorebi_recorder::testkit::workspace; +use serde_json::Value; +use serde_json::json; +use std::process::Command; +use std::process::Output; + +fn recorder(args: &[&str]) -> Output { + Command::new(env!("CARGO_BIN_EXE_komorebi-recorder")) + .args(args) + .output() + .expect("run komorebi-recorder") +} + +fn json_of(output: &Output) -> Value { + serde_json::from_slice(&output.stdout).expect("JSON") +} + +fn session(name: &str, flavor: Flavor) -> String { + let layout = [[0, 0, 960, 1040]]; + let one = state( + vec![monitor( + 1, + [0, 0, 1920, 1080], + vec![workspace( + "main", + vec![container("c", vec![window(7, "term", Some(layout[0]))])], + 0, + &layout, + )], + 0, + )], + 0, + ); + let (session, _) = SessionBuilder::new(name) + .flavor(flavor) + .length(10.0) + .event(1.0, json!({"type": "Retile"}), Some(one)) + .window_track(&[ + (1.0, &[(7, "term", [100, 0, 1060, 1040])]), + (4.0, &[(7, "term", [0, 0, 960, 1040])]), + ]) + .build(); + session.dir.display().to_string() +} + +#[test] +fn lag_lists_the_episode_as_json_and_as_text() { + let dir = session("lag-cli", Flavor::Macos); + let answer = json_of(&recorder(&["lag", &dir, "--json"])); + assert_eq!(answer["schema"], "lag/1"); + let episode = &answer["data"]["episodes"][0]; + assert_eq!(episode["window_id"], 7); + assert_eq!(episode["duration_ms"], 2500); + assert_eq!(episode["max_px"], 100); + assert_eq!(answer["data"]["unit"], "points"); + + let text = String::from_utf8_lossy(&recorder(&["lag", &dir]).stdout).into_owned(); + assert!(text.starts_with("1 episode(s) of geometry lag"), "{text}"); + assert!(text.contains("term (window 7) was off its layout rect for 2.5 s")); + + // a wider grace than the episode leaves nothing + let none = json_of(&recorder(&["lag", &dir, "--grace-ms", "5000", "--json"])); + assert!(none["data"]["episodes"].as_array().unwrap().is_empty()); +} + +#[test] +fn the_finding_is_listed_and_kde_has_none() { + let dir = session("lag-findings", Flavor::Macos); + let found = json_of(&recorder(&[ + "findings", + &dir, + "--kind", + "geometry-lag", + "--json", + ])); + assert_eq!(found["data"]["detector"], 1); + assert_eq!(found["data"]["findings"][0]["kind"], "geometry-lag:window"); + + let kde = session("lag-kde-cli", Flavor::Kde); + let none = json_of(&recorder(&[ + "findings", + &kde, + "--kind", + "geometry-lag", + "--json", + ])); + assert_eq!(none["data"]["total"], 0); + let answer = json_of(&recorder(&["lag", &kde, "--json"])); + assert_eq!(answer["data"]["independent"], false); + assert!( + answer["data"]["note"] + .as_str() + .unwrap() + .contains("not independent") + ); +}