frankensearch-ops 0.2.0

Operations control-plane TUI for frankensearch fleet monitoring
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Operator view presets and density modes for progressive disclosure.
//!
//! View presets configure which information is visible and at what density,
//! letting operators switch between high-level triage and deep-dive modes
//! without reconfiguring individual panels.

use serde::{Deserialize, Serialize};

// ─── Density ────────────────────────────────────────────────────────────────

/// Display density mode controlling the amount of detail shown.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Density {
    /// Compact: minimal chrome, one-line rows, maximized data density.
    Compact,
    /// Normal: balanced layout with reasonable spacing.
    #[default]
    Normal,
    /// Expanded: extra detail panes, wider spacing, inline metrics.
    Expanded,
}

impl Density {
    /// All densities in order.
    pub const ALL: &'static [Self] = &[Self::Compact, Self::Normal, Self::Expanded];

    /// Human-readable label.
    #[must_use]
    pub const fn label(self) -> &'static str {
        match self {
            Self::Compact => "Compact",
            Self::Normal => "Normal",
            Self::Expanded => "Expanded",
        }
    }

    /// Cycle to the next density mode.
    #[must_use]
    pub const fn cycle_next(self) -> Self {
        match self {
            Self::Compact => Self::Normal,
            Self::Normal => Self::Expanded,
            Self::Expanded => Self::Compact,
        }
    }

    /// Cycle to the previous density mode.
    #[must_use]
    pub const fn cycle_prev(self) -> Self {
        match self {
            Self::Compact => Self::Expanded,
            Self::Normal => Self::Compact,
            Self::Expanded => Self::Normal,
        }
    }

    /// Whether detail panes should be shown.
    #[must_use]
    pub const fn show_details(self) -> bool {
        matches!(self, Self::Expanded)
    }

    /// Whether inline metrics should be shown (expanded + normal).
    #[must_use]
    pub const fn show_inline_metrics(self) -> bool {
        !matches!(self, Self::Compact)
    }

    /// Suggested table row height.
    #[must_use]
    pub const fn row_height(self) -> u16 {
        match self {
            Self::Compact | Self::Normal => 1,
            Self::Expanded => 2,
        }
    }
}

impl std::fmt::Display for Density {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.label())
    }
}

// ─── View Preset ────────────────────────────────────────────────────────────

/// Predefined view configurations for common operational workflows.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ViewPreset {
    /// Fleet triage: compact overview of all instances, health-first sorting.
    #[default]
    FleetTriage,
    /// Project deep-dive: expanded view of a single project with full metrics.
    ProjectDeepDive,
    /// Incident mode: high-contrast, alerts prominent, unhealthy instances first.
    IncidentMode,
    /// Low-noise mode: hide healthy instances, only show actionable items.
    LowNoise,
}

impl ViewPreset {
    /// All presets.
    pub const ALL: &'static [Self] = &[
        Self::FleetTriage,
        Self::ProjectDeepDive,
        Self::IncidentMode,
        Self::LowNoise,
    ];

    /// Human-readable label.
    #[must_use]
    pub const fn label(self) -> &'static str {
        match self {
            Self::FleetTriage => "Fleet Triage",
            Self::ProjectDeepDive => "Project Deep Dive",
            Self::IncidentMode => "Incident Mode",
            Self::LowNoise => "Low Noise",
        }
    }

    /// Short description for the command palette.
    #[must_use]
    pub const fn description(self) -> &'static str {
        match self {
            Self::FleetTriage => "Compact fleet overview, health-first sorting",
            Self::ProjectDeepDive => "Expanded single-project view with full metrics",
            Self::IncidentMode => "High-contrast, alerts prominent, unhealthy first",
            Self::LowNoise => "Hide healthy instances, show only actionable items",
        }
    }

    /// Recommended density for this preset.
    #[must_use]
    pub const fn default_density(self) -> Density {
        match self {
            Self::ProjectDeepDive => Density::Expanded,
            Self::IncidentMode => Density::Normal,
            Self::FleetTriage | Self::LowNoise => Density::Compact,
        }
    }

    /// Whether to use high-contrast theme by default.
    #[must_use]
    pub const fn prefer_high_contrast(self) -> bool {
        matches!(self, Self::IncidentMode)
    }

    /// Whether to hide healthy instances by default.
    #[must_use]
    pub const fn hide_healthy(self) -> bool {
        matches!(self, Self::LowNoise)
    }

    /// Whether to sort unhealthy instances first.
    #[must_use]
    pub const fn unhealthy_first(self) -> bool {
        matches!(self, Self::IncidentMode | Self::LowNoise)
    }
}

impl std::fmt::Display for ViewPreset {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.label())
    }
}

// ─── View State ─────────────────────────────────────────────────────────────

/// Current view configuration combining preset and overrides.
///
/// The `ViewState` holds the active preset, density, and filtering options.
/// These can be changed at runtime via keyboard shortcuts or the command palette.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ViewState {
    /// Active view preset.
    pub preset: ViewPreset,
    /// Current density mode (may differ from preset default if user overrode it).
    pub density: Density,
    /// Whether to hide healthy instances.
    pub hide_healthy: bool,
    /// Whether to sort unhealthy instances first.
    pub unhealthy_first: bool,
    /// Optional project filter (only show instances matching this project).
    pub project_filter: Option<String>,
}

impl Default for ViewState {
    fn default() -> Self {
        Self::from_preset(ViewPreset::default())
    }
}

impl ViewState {
    /// Create a view state from a preset with its default settings.
    #[must_use]
    pub const fn from_preset(preset: ViewPreset) -> Self {
        Self {
            preset,
            density: preset.default_density(),
            hide_healthy: preset.hide_healthy(),
            unhealthy_first: preset.unhealthy_first(),
            project_filter: None,
        }
    }

    /// Apply a new preset, resetting overrides to preset defaults.
    pub fn apply_preset(&mut self, preset: ViewPreset) {
        *self = Self::from_preset(preset);
    }

    /// Cycle density mode forward.
    pub const fn cycle_density(&mut self) {
        self.density = self.density.cycle_next();
    }

    /// Toggle the hide-healthy filter.
    pub const fn toggle_hide_healthy(&mut self) {
        self.hide_healthy = !self.hide_healthy;
    }

    /// Toggle unhealthy-first sorting.
    pub const fn toggle_unhealthy_first(&mut self) {
        self.unhealthy_first = !self.unhealthy_first;
    }

    /// Set a project filter.
    pub fn set_project_filter(&mut self, project: impl Into<String>) {
        self.project_filter = Some(project.into());
    }

    /// Clear the project filter.
    pub fn clear_project_filter(&mut self) {
        self.project_filter = None;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn density_all_values() {
        assert_eq!(Density::ALL.len(), 3);
    }

    #[test]
    fn density_cycle_next() {
        assert_eq!(Density::Compact.cycle_next(), Density::Normal);
        assert_eq!(Density::Normal.cycle_next(), Density::Expanded);
        assert_eq!(Density::Expanded.cycle_next(), Density::Compact);
    }

    #[test]
    fn density_cycle_prev() {
        assert_eq!(Density::Compact.cycle_prev(), Density::Expanded);
        assert_eq!(Density::Normal.cycle_prev(), Density::Compact);
        assert_eq!(Density::Expanded.cycle_prev(), Density::Normal);
    }

    #[test]
    fn density_display() {
        assert_eq!(Density::Compact.to_string(), "Compact");
        assert_eq!(Density::Normal.to_string(), "Normal");
        assert_eq!(Density::Expanded.to_string(), "Expanded");
    }

    #[test]
    fn density_show_details() {
        assert!(!Density::Compact.show_details());
        assert!(!Density::Normal.show_details());
        assert!(Density::Expanded.show_details());
    }

    #[test]
    fn density_show_inline_metrics() {
        assert!(!Density::Compact.show_inline_metrics());
        assert!(Density::Normal.show_inline_metrics());
        assert!(Density::Expanded.show_inline_metrics());
    }

    #[test]
    fn density_row_height() {
        assert_eq!(Density::Compact.row_height(), 1);
        assert_eq!(Density::Normal.row_height(), 1);
        assert_eq!(Density::Expanded.row_height(), 2);
    }

    #[test]
    fn density_serde_roundtrip() {
        for density in Density::ALL {
            let json = serde_json::to_string(density).unwrap();
            let decoded: Density = serde_json::from_str(&json).unwrap();
            assert_eq!(decoded, *density);
        }
    }

    #[test]
    fn view_preset_all_values() {
        assert_eq!(ViewPreset::ALL.len(), 4);
    }

    #[test]
    fn view_preset_labels() {
        assert_eq!(ViewPreset::FleetTriage.label(), "Fleet Triage");
        assert_eq!(ViewPreset::IncidentMode.label(), "Incident Mode");
    }

    #[test]
    fn view_preset_descriptions() {
        for preset in ViewPreset::ALL {
            assert!(!preset.description().is_empty());
        }
    }

    #[test]
    fn view_preset_default_densities() {
        assert_eq!(ViewPreset::FleetTriage.default_density(), Density::Compact);
        assert_eq!(
            ViewPreset::ProjectDeepDive.default_density(),
            Density::Expanded
        );
        assert_eq!(ViewPreset::IncidentMode.default_density(), Density::Normal);
        assert_eq!(ViewPreset::LowNoise.default_density(), Density::Compact);
    }

    #[test]
    fn view_preset_contrast_preferences() {
        assert!(ViewPreset::IncidentMode.prefer_high_contrast());
        assert!(!ViewPreset::FleetTriage.prefer_high_contrast());
    }

    #[test]
    fn view_preset_filtering() {
        assert!(ViewPreset::LowNoise.hide_healthy());
        assert!(!ViewPreset::FleetTriage.hide_healthy());
        assert!(ViewPreset::IncidentMode.unhealthy_first());
        assert!(ViewPreset::LowNoise.unhealthy_first());
        assert!(!ViewPreset::FleetTriage.unhealthy_first());
    }

    #[test]
    fn view_preset_serde_roundtrip() {
        for preset in ViewPreset::ALL {
            let json = serde_json::to_string(preset).unwrap();
            let decoded: ViewPreset = serde_json::from_str(&json).unwrap();
            assert_eq!(decoded, *preset);
        }
    }

    #[test]
    fn view_preset_display() {
        assert_eq!(ViewPreset::FleetTriage.to_string(), "Fleet Triage");
        assert_eq!(ViewPreset::LowNoise.to_string(), "Low Noise");
    }

    #[test]
    fn view_state_default() {
        let state = ViewState::default();
        assert_eq!(state.preset, ViewPreset::FleetTriage);
        assert_eq!(state.density, Density::Compact);
        assert!(!state.hide_healthy);
        assert!(!state.unhealthy_first);
        assert!(state.project_filter.is_none());
    }

    #[test]
    fn view_state_from_preset() {
        let state = ViewState::from_preset(ViewPreset::IncidentMode);
        assert_eq!(state.preset, ViewPreset::IncidentMode);
        assert_eq!(state.density, Density::Normal);
        assert!(!state.hide_healthy);
        assert!(state.unhealthy_first);
    }

    #[test]
    fn view_state_apply_preset() {
        let mut state = ViewState {
            density: Density::Expanded,
            ..ViewState::default()
        };
        state.apply_preset(ViewPreset::LowNoise);
        assert_eq!(state.preset, ViewPreset::LowNoise);
        assert_eq!(state.density, Density::Compact); // Reset to preset default.
        assert!(state.hide_healthy);
    }

    #[test]
    fn view_state_cycle_density() {
        let mut state = ViewState::default();
        assert_eq!(state.density, Density::Compact);
        state.cycle_density();
        assert_eq!(state.density, Density::Normal);
        state.cycle_density();
        assert_eq!(state.density, Density::Expanded);
        state.cycle_density();
        assert_eq!(state.density, Density::Compact);
    }

    #[test]
    fn view_state_toggle_filters() {
        let mut state = ViewState::default();
        assert!(!state.hide_healthy);
        state.toggle_hide_healthy();
        assert!(state.hide_healthy);
        state.toggle_hide_healthy();
        assert!(!state.hide_healthy);
    }

    #[test]
    fn view_state_project_filter() {
        let mut state = ViewState::default();
        state.set_project_filter("cass");
        assert_eq!(state.project_filter.as_deref(), Some("cass"));
        state.clear_project_filter();
        assert!(state.project_filter.is_none());
    }

    #[test]
    fn view_state_serde_roundtrip() {
        let mut state = ViewState::from_preset(ViewPreset::ProjectDeepDive);
        state.set_project_filter("xf");
        let json = serde_json::to_string(&state).unwrap();
        let decoded: ViewState = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded, state);
    }
}