quelch 0.12.0

Ingest data from Jira, Confluence, and more directly into Azure AI Search
Documentation
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
//! Fleet table widget: renders one row per `(CursorKey, Cursor)`.
//!
//! Columns: Owner · Source · Subsource · Last sync · Docs · State

use chrono::Utc;
use ratatui::{
    buffer::Buffer,
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span, Text},
    widgets::{Block, Borders, Cell, Paragraph, Row, Table, Widget},
};

use crate::cosmos::meta::{Cursor, CursorKey};
use crate::tui::app::App;

// ---------------------------------------------------------------------------
// Fleet table
// ---------------------------------------------------------------------------

/// Main fleet table widget — one row per `(CursorKey, Cursor)`.
pub struct FleetTable<'a> {
    pub app: &'a App,
}

impl Widget for FleetTable<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let outer = Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Color::DarkGray))
            .title("Sources");
        let inner = outer.inner(area);
        outer.render(area, buf);

        if self.app.rows.is_empty() {
            Paragraph::new("(no cursors — nothing has synced yet)")
                .style(Style::default().fg(Color::DarkGray))
                .render(inner, buf);
            return;
        }

        let mut rows: Vec<Row> = vec![header_row(), rule_row()];

        for (i, (key, cursor)) in self.app.rows.iter().enumerate() {
            let selected = i == self.app.selected_index;
            rows.push(data_row(key, cursor, selected));
        }

        let widths = [
            Constraint::Length(22), // owner
            Constraint::Length(20), // source
            Constraint::Length(12), // subsource
            Constraint::Length(14), // last sync
            Constraint::Length(8),  // docs
            Constraint::Min(12),    // state
        ];

        Table::new(rows, widths)
            .column_spacing(1)
            .render(inner, buf);
    }
}

fn header_row() -> Row<'static> {
    Row::new(vec![
        Cell::from("Owner"),
        Cell::from("Source"),
        Cell::from("Subsource"),
        Cell::from("Last sync"),
        Cell::from(Text::from("Docs").alignment(Alignment::Right)),
        Cell::from("State"),
    ])
    .style(Style::default().fg(Color::DarkGray))
}

fn rule_row() -> Row<'static> {
    Row::new(vec![
        Cell::from("──────────────────────"),
        Cell::from("────────────────────"),
        Cell::from("────────────"),
        Cell::from("──────────────"),
        Cell::from("────────"),
        Cell::from("────────────"),
    ])
    .style(Style::default().fg(Color::DarkGray))
}

fn data_row(key: &CursorKey, cursor: &Cursor, selected: bool) -> Row<'static> {
    let last_sync = fmt_last_sync(cursor);
    let docs = if cursor.documents_synced_total == 0 {
        "".to_string()
    } else {
        cursor.documents_synced_total.to_string()
    };
    let selector = if selected { "" } else { " " };
    let owner = format!(
        "{selector} {}",
        cursor.owner_instance.as_deref().unwrap_or("")
    );

    let state_cell = state_text(cursor);

    let row = Row::new(vec![
        Cell::from(owner),
        Cell::from(key.source_name.clone()),
        Cell::from(key.subsource.clone()),
        Cell::from(last_sync),
        Cell::from(Text::from(docs).alignment(Alignment::Right)),
        Cell::from(state_cell),
    ]);

    if selected {
        row.style(Style::default().add_modifier(Modifier::REVERSED))
    } else {
        row
    }
}

fn fmt_last_sync(cursor: &Cursor) -> String {
    if cursor.backfill_in_progress {
        return "backfill…".to_string();
    }
    match cursor.last_sync_at {
        None => "".to_string(),
        Some(t) => {
            let secs = Utc::now().signed_duration_since(t).num_seconds();
            if secs < 0 {
                "just now".to_string()
            } else if secs < 120 {
                format!("{secs}s ago")
            } else if secs < 7200 {
                format!("{}m ago", secs / 60)
            } else {
                format!("{}h ago", secs / 3600)
            }
        }
    }
}

fn state_text(cursor: &Cursor) -> Text<'static> {
    if cursor.last_error.is_some() {
        return Text::from("error").style(Style::default().fg(Color::Red));
    }
    if cursor.backfill_in_progress {
        return Text::from("backfilling").style(Style::default().fg(Color::Yellow));
    }
    Text::from("ok").style(Style::default().fg(Color::Green))
}

// ---------------------------------------------------------------------------
// Detail pane
// ---------------------------------------------------------------------------

/// Detail pane widget — shown below the table for the selected row.
pub struct DetailPane<'a> {
    pub app: &'a App,
}

impl Widget for DetailPane<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let outer = Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Color::DarkGray))
            .title("Details");
        let inner = outer.inner(area);
        outer.render(area, buf);

        let Some((key, cursor)) = self.app.selected_row() else {
            Paragraph::new("(no selection)")
                .style(Style::default().fg(Color::DarkGray))
                .render(inner, buf);
            return;
        };

        let cols = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
            .split(inner);

        let left = detail_left(key, cursor);
        let right = detail_right(cursor);

        Paragraph::new(left).render(cols[0], buf);
        Paragraph::new(right).render(cols[1], buf);
    }
}

fn fmt_opt_dt(dt: &Option<chrono::DateTime<Utc>>) -> String {
    match dt {
        None => "".to_string(),
        Some(t) => t.format("%Y-%m-%d %H:%M:%S UTC").to_string(),
    }
}

fn detail_left(key: &CursorKey, cursor: &Cursor) -> Vec<Line<'static>> {
    let owner = cursor.owner_instance.as_deref().unwrap_or("").to_string();
    vec![
        kv("Owner", &owner),
        kv("Source", &key.source_name),
        kv("Subsource", &key.subsource),
    ]
    .into_iter()
    .chain(std::iter::once(kv(
        "Docs synced",
        &cursor.documents_synced_total.to_string(),
    )))
    .chain(std::iter::once(kv(
        "Last sync",
        &fmt_opt_dt(&cursor.last_sync_at),
    )))
    .chain(std::iter::once(kv(
        "Last complete min",
        &fmt_opt_dt(&cursor.last_complete_minute),
    )))
    .collect()
}

fn detail_right(cursor: &Cursor) -> Vec<Line<'static>> {
    let backfill = if cursor.backfill_in_progress {
        "yes".to_string()
    } else {
        "no".to_string()
    };
    let error = cursor
        .last_error
        .as_deref()
        .unwrap_or("")
        .chars()
        .take(60)
        .collect::<String>();
    let reconciliation = fmt_opt_dt(&cursor.last_reconciliation_at);

    vec![
        kv("Backfill", &backfill),
        kv("Last reconcile", &reconciliation),
        kv("Error", &error),
    ]
}

fn kv(label: &str, value: &str) -> Line<'static> {
    Line::from(vec![
        Span::styled(
            format!("{label:<18} "),
            Style::default().fg(Color::DarkGray),
        ),
        Span::raw(value.to_string()),
    ])
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cosmos::meta::{Cursor, CursorKey};
    use crate::tui::app::App;
    use chrono::Utc;
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    fn key(source: &str, subsource: &str) -> CursorKey {
        CursorKey {
            source_name: source.to_string(),
            subsource: subsource.to_string(),
        }
    }

    fn cursor_with_owner(owner: &str) -> Cursor {
        Cursor {
            owner_instance: Some(owner.to_string()),
            ..Default::default()
        }
    }

    fn rendered_text(app: &App, w: u16, h: u16) -> String {
        let backend = TestBackend::new(w, h);
        let mut term = Terminal::new(backend).unwrap();
        term.draw(|f| {
            f.render_widget(FleetTable { app }, f.area());
        })
        .unwrap();
        let buf = term.backend().buffer();
        (0..buf.area.height)
            .map(|y| {
                (0..buf.area.width)
                    .map(|x| buf[(x, y)].symbol())
                    .collect::<String>()
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    fn detail_text(app: &App, w: u16, h: u16) -> String {
        let backend = TestBackend::new(w, h);
        let mut term = Terminal::new(backend).unwrap();
        term.draw(|f| {
            f.render_widget(DetailPane { app }, f.area());
        })
        .unwrap();
        let buf = term.backend().buffer();
        (0..buf.area.height)
            .map(|y| {
                (0..buf.area.width)
                    .map(|x| buf[(x, y)].symbol())
                    .collect::<String>()
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    #[test]
    fn renders_column_headings() {
        let mut app = App::new();
        app.handle_poll_result(Ok(vec![(key("jira", "DO"), Cursor::default())]));
        let text = rendered_text(&app, 120, 10);
        assert!(text.contains("Owner"), "missing Owner: {text}");
        assert!(text.contains("Source"), "missing Source: {text}");
        assert!(text.contains("Subsource"), "missing Subsource: {text}");
        assert!(text.contains("Last sync"), "missing Last sync: {text}");
        assert!(text.contains("Docs"), "missing Docs: {text}");
        assert!(text.contains("State"), "missing State: {text}");
    }

    #[test]
    fn renders_two_rows() {
        let mut app = App::new();
        app.handle_poll_result(Ok(vec![
            (key("jira-cloud", "DO"), cursor_with_owner("ingest-prod")),
            (key("jira-cloud", "INT"), cursor_with_owner("ingest-prod")),
        ]));
        let text = rendered_text(&app, 120, 12);
        assert!(text.contains("ingest-prod"), "missing owner: {text}");
        assert!(text.contains("DO"), "missing DO: {text}");
        assert!(text.contains("INT"), "missing INT: {text}");
    }

    #[test]
    fn backfill_row_shows_distinctly() {
        let mut app = App::new();
        let c = Cursor {
            backfill_in_progress: true,
            ..Default::default()
        };
        app.handle_poll_result(Ok(vec![(key("jira", "DO"), c)]));
        let text = rendered_text(&app, 120, 10);
        assert!(
            text.contains("backfill"),
            "backfill not shown distinctly: {text}"
        );
    }

    #[test]
    fn error_row_shows_distinctly() {
        let mut app = App::new();
        let c = Cursor {
            last_error: Some("429 rate limit".into()),
            ..Default::default()
        };
        app.handle_poll_result(Ok(vec![(key("jira", "DO"), c)]));
        let text = rendered_text(&app, 120, 10);
        assert!(text.contains("error"), "error not shown distinctly: {text}");
    }

    #[test]
    fn selected_row_has_selection_indicator() {
        let mut app = App::new();
        app.handle_poll_result(Ok(vec![
            (key("jira", "DO"), Cursor::default()),
            (key("jira", "INT"), Cursor::default()),
        ]));
        app.selected_index = 1;
        let text = rendered_text(&app, 120, 12);
        // The second row should have the ▶ marker.
        assert!(text.contains(''), "selection marker missing: {text}");
    }

    #[test]
    fn empty_table_shows_placeholder() {
        let app = App::new();
        let text = rendered_text(&app, 120, 10);
        assert!(text.contains("no cursors"), "placeholder missing: {text}");
    }

    #[test]
    fn detail_pane_shows_selected_row() {
        let mut app = App::new();
        let c = Cursor {
            documents_synced_total: 9999,
            last_sync_at: Some(Utc::now()),
            owner_instance: Some("ingest-prod".into()),
            ..Default::default()
        };
        app.handle_poll_result(Ok(vec![(key("my-jira", "DO"), c)]));
        let text = detail_text(&app, 120, 8);
        assert!(text.contains("ingest-prod"), "owner missing: {text}");
        assert!(text.contains("my-jira"), "source missing: {text}");
        assert!(text.contains("9999"), "doc count missing: {text}");
    }

    #[test]
    fn detail_pane_shows_error_field() {
        let mut app = App::new();
        let c = Cursor {
            last_error: Some("rate limited".into()),
            ..Default::default()
        };
        app.handle_poll_result(Ok(vec![(key("jira", "DO"), c)]));
        let text = detail_text(&app, 120, 8);
        assert!(
            text.contains("rate limited"),
            "error message missing: {text}"
        );
    }

    #[test]
    fn detail_pane_empty_when_no_rows() {
        let app = App::new();
        let text = detail_text(&app, 120, 8);
        assert!(text.contains("no selection"), "placeholder missing: {text}");
    }
}