quelch 0.10.1

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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! Per-cycle ingest algorithm.
//!
//! `cycle::run` is the main entrypoint: it reads the cursor, delegates to
//! backfill if needed, or advances the steady-state incremental window.

use chrono::Utc;
use serde_json::Value;

use crate::{
    cosmos::meta::CursorKey,
    cosmos::{CosmosBackend, meta},
    ingest::{backfill, config::CycleConfig, window},
    sources::{Companions, SourceConnector, SourceDocument},
};

// ---------------------------------------------------------------------------
// Outcome
// ---------------------------------------------------------------------------

/// The result of one `cycle::run` call.
#[derive(Debug)]
pub enum CycleOutcome {
    /// Cursor advanced; window completed; N docs written.
    Advanced {
        documents_written: usize,
        window_end: chrono::DateTime<Utc>,
    },
    /// Backfill in progress; this cycle made progress but isn't done yet.
    BackfillInProgress { documents_written: usize },
    /// Backfill completed.
    BackfillCompleted {
        documents_written: usize,
        target: chrono::DateTime<Utc>,
    },
    /// Cursor already past `target_now - safety_lag`; nothing to do.
    NothingToDo,
    /// Cycle failed mid-flight; cursor not advanced.
    Failed { error: String },
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Run one ingest cycle for `(key.source_name, key.subsource)`.
///
/// Reads the cursor from Cosmos, then:
///
/// - If `backfill_in_progress` → delegates to [`backfill::resume`].
/// - If `last_complete_minute` is `None` → starts a fresh backfill.
/// - Otherwise → advances the steady-state incremental window.
pub async fn run<C>(
    connector: &C,
    cosmos: &dyn CosmosBackend,
    key: &CursorKey,
    cfg: &CycleConfig,
) -> CycleOutcome
where
    C: SourceConnector,
{
    let cursor = match meta::load(cosmos, &cfg.meta_container, key).await {
        Ok(c) => c,
        Err(e) => {
            return CycleOutcome::Failed {
                error: format!("load cursor: {e}"),
            };
        }
    };

    // Delegate to backfill path if needed.
    if cursor.backfill_in_progress {
        return backfill::resume(connector, cosmos, key, cursor, cfg).await;
    }

    if cursor.last_complete_minute.is_none() {
        // First-ever cycle for this (source, subsource): start backfill.
        return backfill::start(connector, cosmos, key, cfg).await;
    }

    // -----------------------------------------------------------------------
    // Steady-state incremental window.
    // -----------------------------------------------------------------------
    let now = Utc::now();
    let Some(w) =
        window::plan_next_window(cursor.last_complete_minute, now, cfg.safety_lag_minutes)
    else {
        return CycleOutcome::NothingToDo;
    };

    let mut total_written = 0usize;
    let mut page_token: Option<String> = None;

    loop {
        let page = match connector
            .fetch_window(
                &key.subsource,
                w.start,
                w.end,
                cfg.batch_size,
                page_token.as_deref(),
            )
            .await
        {
            Ok(p) => p,
            Err(e) => {
                return CycleOutcome::Failed {
                    error: format!("fetch_window: {e}"),
                };
            }
        };

        if !page.documents.is_empty() {
            let docs: Vec<Value> = page
                .documents
                .iter()
                .map(|d| document_envelope(d, connector.source_name()))
                .collect();
            if let Err(e) = cosmos
                .bulk_upsert(connector.primary_container(), docs)
                .await
            {
                return CycleOutcome::Failed {
                    error: format!("upsert: {e}"),
                };
            }
        }
        total_written += page.documents.len();

        match page.next_page_token {
            Some(t) => page_token = Some(t),
            None => break,
        }
    }

    // Companions (sprints, fix_versions, projects, spaces). Fetch once per cycle.
    // Failures here are non-fatal — primary docs already landed and the cycle
    // shouldn't be marked failed because a sprint list 500'd. Log so an operator
    // notices recurring drift.
    match connector.fetch_companions(&key.subsource).await {
        Ok(companions) => {
            if let Err(e) = upsert_companions(cosmos, &companions, cfg).await {
                tracing::warn!(
                    error = %e,
                    source = connector.source_name(),
                    subsource = %key.subsource,
                    "companion upsert failed; primary docs unaffected"
                );
            }
        }
        Err(e) => {
            tracing::warn!(
                error = %e,
                source = connector.source_name(),
                subsource = %key.subsource,
                "companion fetch failed; primary docs unaffected"
            );
        }
    }

    // Advance cursor — only on full success.
    let mut new_cursor = cursor;
    new_cursor.last_complete_minute = Some(w.end);
    new_cursor.documents_synced_total += total_written as u64;
    new_cursor.last_sync_at = Some(Utc::now());
    new_cursor.last_error = None;

    if let Err(e) = meta::save(cosmos, &cfg.meta_container, key, &new_cursor).await {
        return CycleOutcome::Failed {
            error: format!("save cursor: {e}"),
        };
    }

    CycleOutcome::Advanced {
        documents_written: total_written,
        window_end: w.end,
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Build the Cosmos document envelope for a [`SourceDocument`].
pub(crate) fn document_envelope(doc: &SourceDocument, source_name: &str) -> Value {
    let mut map = serde_json::Map::new();
    map.insert("id".into(), doc.id.clone().into());
    map.insert("_partition_key".into(), doc.partition_key.clone().into());
    map.insert("source_name".into(), source_name.to_string().into());
    map.insert("source_link".into(), doc.source_link.clone().into());
    map.insert("updated".into(), doc.updated_at.to_rfc3339().into());
    for (k, v) in &doc.fields {
        map.insert(k.clone(), v.clone());
    }
    Value::Object(map)
}

/// Upsert companion documents to their dedicated containers.
async fn upsert_companions(
    cosmos: &dyn CosmosBackend,
    companions: &Companions,
    cfg: &CycleConfig,
) -> Result<(), String> {
    let pairs: &[(&str, &[SourceDocument])] = &[
        ("sprints", &companions.sprints),
        ("fix_versions", &companions.fix_versions),
        ("projects", &companions.projects),
        ("spaces", &companions.spaces),
    ];

    for (category, docs) in pairs {
        if docs.is_empty() {
            continue;
        }
        let Some(container) = cfg.companion_containers.get(*category) else {
            continue;
        };
        let values: Vec<Value> = docs
            .iter()
            .map(|d| document_envelope(d, category))
            .collect();
        cosmos
            .bulk_upsert(container, values)
            .await
            .map_err(|e| format!("companion upsert ({category}): {e}"))?;
    }
    Ok(())
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{Duration, TimeZone};

    use crate::{
        cosmos::{InMemoryCosmos, meta::CursorKey},
        ingest::{
            config::CycleConfig,
            test_helpers::{MockConnector, make_source_doc},
        },
        sources::Companions,
    };

    fn make_key() -> CursorKey {
        CursorKey {
            deployment_name: "test".into(),
            source_name: "mock".into(),
            subsource: "DO".into(),
        }
    }

    fn ts(h: u32, m: u32) -> chrono::DateTime<Utc> {
        Utc.with_ymd_and_hms(2024, 6, 1, h, m, 0).single().unwrap()
    }

    /// Seed the cosmos meta container with a cursor pointing at `last_minute`.
    async fn seed_cursor(
        cosmos: &InMemoryCosmos,
        key: &CursorKey,
        last_minute: chrono::DateTime<Utc>,
    ) {
        let cursor = crate::cosmos::meta::Cursor {
            last_complete_minute: Some(last_minute),
            ..Default::default()
        };
        meta::save(cosmos, "quelch-meta", key, &cursor)
            .await
            .unwrap();
    }

    // -------------------------------------------------------------------------
    // cycle_advances_cursor_after_full_window
    // -------------------------------------------------------------------------
    #[tokio::test]
    async fn cycle_advances_cursor_after_full_window() {
        let cosmos = InMemoryCosmos::new();
        let key = make_key();

        // Seed cursor at 10:00; now will be ~10:10, lag=2 → window [10:00, 10:08)
        seed_cursor(&cosmos, &key, ts(10, 0)).await;

        let connector = MockConnector::new("mock", "jira-issues");
        // Two docs, single page, no next_page_token.
        connector.push_window_page(
            vec![
                make_source_doc("doc-A", "DO"),
                make_source_doc("doc-B", "DO"),
            ],
            None,
        );

        // Use a fixed "now" by manipulating the safety_lag to guarantee a window.
        // With last=10:00, now=10:10, lag=2 → target=10:08.
        // We override safety_lag to 2 so the window is non-empty.
        let cfg = CycleConfig {
            safety_lag_minutes: 2,
            ..CycleConfig::default()
        };

        // We can't directly control "now" in cycle::run — we need
        // last_complete_minute to be sufficiently behind real time.
        // Seed the cursor at a point far enough in the past that
        // the safety-lagged target is > last_complete_minute.
        // Re-seed with a timestamp 10 minutes in the past.
        let past = Utc::now() - Duration::minutes(15);
        let past = crate::ingest::window::floor_to_minute(past);
        {
            let cursor = crate::cosmos::meta::Cursor {
                last_complete_minute: Some(past),
                ..Default::default()
            };
            meta::save(&cosmos, "quelch-meta", &key, &cursor)
                .await
                .unwrap();
        }

        let outcome = run(&connector, &cosmos, &key, &cfg).await;
        assert!(
            matches!(
                outcome,
                CycleOutcome::Advanced {
                    documents_written: 2,
                    ..
                }
            ),
            "expected Advanced(2), got {outcome:?}"
        );

        // Cursor must have advanced.
        let loaded = meta::load(&cosmos, "quelch-meta", &key).await.unwrap();
        assert!(loaded.last_complete_minute.unwrap() > past);
        assert_eq!(loaded.documents_synced_total, 2);
        assert!(loaded.last_error.is_none());
    }

    // -------------------------------------------------------------------------
    // cycle_writes_docs_to_primary_container
    // -------------------------------------------------------------------------
    #[tokio::test]
    async fn cycle_writes_docs_to_primary_container() {
        let cosmos = InMemoryCosmos::new();
        let key = make_key();

        let past = crate::ingest::window::floor_to_minute(Utc::now() - Duration::minutes(15));
        {
            let cursor = crate::cosmos::meta::Cursor {
                last_complete_minute: Some(past),
                ..Default::default()
            };
            meta::save(&cosmos, "quelch-meta", &key, &cursor)
                .await
                .unwrap();
        }

        let connector = MockConnector::new("mock", "jira-issues");
        connector.push_window_page(vec![make_source_doc("issue-1", "DO")], None);

        let cfg = CycleConfig::default();
        run(&connector, &cosmos, &key, &cfg).await;

        // The doc must be in the primary container.
        let doc = cosmos.get("jira-issues", "issue-1", "DO").await.unwrap();
        assert!(doc.is_some(), "expected doc in jira-issues container");
        let doc = doc.unwrap();
        assert_eq!(doc["id"], "issue-1");
        assert_eq!(doc["source_name"], "mock");
    }

    // -------------------------------------------------------------------------
    // cycle_does_not_advance_cursor_on_failure
    // -------------------------------------------------------------------------
    #[tokio::test]
    async fn cycle_does_not_advance_cursor_on_failure() {
        let cosmos = InMemoryCosmos::new();
        let key = make_key();

        let past = crate::ingest::window::floor_to_minute(Utc::now() - Duration::minutes(15));
        {
            let cursor = crate::cosmos::meta::Cursor {
                last_complete_minute: Some(past),
                ..Default::default()
            };
            meta::save(&cosmos, "quelch-meta", &key, &cursor)
                .await
                .unwrap();
        }

        let connector = MockConnector::new("mock", "jira-issues");
        // First page succeeds, second page errors.
        connector.push_window_page(vec![make_source_doc("doc-1", "DO")], Some("page2".into()));
        connector.push_window_error("network failure");

        let cfg = CycleConfig::default();
        let outcome = run(&connector, &cosmos, &key, &cfg).await;

        assert!(
            matches!(outcome, CycleOutcome::Failed { .. }),
            "expected Failed, got {outcome:?}"
        );

        // Cursor must NOT have advanced.
        let loaded = meta::load(&cosmos, "quelch-meta", &key).await.unwrap();
        assert_eq!(
            loaded.last_complete_minute.unwrap(),
            past,
            "cursor should not have advanced after failure"
        );
    }

    // -------------------------------------------------------------------------
    // cycle_skips_when_no_progress_possible
    // -------------------------------------------------------------------------
    #[tokio::test]
    async fn cycle_skips_when_no_progress_possible() {
        let cosmos = InMemoryCosmos::new();
        let key = make_key();

        // Seed cursor with a timestamp only 1 minute in the past, lag=2 → no window.
        let recent = crate::ingest::window::floor_to_minute(Utc::now() - Duration::minutes(1));
        {
            let cursor = crate::cosmos::meta::Cursor {
                last_complete_minute: Some(recent),
                ..Default::default()
            };
            meta::save(&cosmos, "quelch-meta", &key, &cursor)
                .await
                .unwrap();
        }

        let connector = MockConnector::new("mock", "jira-issues");
        let cfg = CycleConfig {
            safety_lag_minutes: 2,
            ..CycleConfig::default()
        };
        let outcome = run(&connector, &cosmos, &key, &cfg).await;

        assert!(
            matches!(outcome, CycleOutcome::NothingToDo),
            "expected NothingToDo, got {outcome:?}"
        );
    }

    // -------------------------------------------------------------------------
    // cycle_writes_companions
    // -------------------------------------------------------------------------
    #[tokio::test]
    async fn cycle_writes_companions() {
        let cosmos = InMemoryCosmos::new();
        let key = make_key();

        let past = crate::ingest::window::floor_to_minute(Utc::now() - Duration::minutes(15));
        {
            let cursor = crate::cosmos::meta::Cursor {
                last_complete_minute: Some(past),
                ..Default::default()
            };
            meta::save(&cosmos, "quelch-meta", &key, &cursor)
                .await
                .unwrap();
        }

        let connector = MockConnector::new("mock", "jira-issues");
        connector.push_window_page(vec![], None);

        // Set up companions — one sprint.
        let mut sprint = make_source_doc("sprint-1", "DO");
        sprint.id = "sprint-1".into();
        let companions = Companions {
            sprints: vec![sprint],
            ..Default::default()
        };
        connector.set_companions(companions);

        let cfg = CycleConfig::default();
        run(&connector, &cosmos, &key, &cfg).await;

        // Sprint must be in the sprints container.
        let sprint_doc = cosmos.get("jira-sprints", "sprint-1", "DO").await.unwrap();
        assert!(
            sprint_doc.is_some(),
            "expected sprint in jira-sprints container"
        );
    }

    // -------------------------------------------------------------------------
    // cycle_starts_backfill_when_cursor_unset
    // -------------------------------------------------------------------------
    #[tokio::test]
    async fn cycle_starts_backfill_when_cursor_unset() {
        let cosmos = InMemoryCosmos::new();
        let key = make_key();
        // No cursor saved → last_complete_minute is None.

        let connector = MockConnector::new("mock", "jira-issues");
        // No backfill pages → immediate end of backfill.

        let cfg = CycleConfig::default();
        let outcome = run(&connector, &cosmos, &key, &cfg).await;

        // With no backfill pages, backfill::resume immediately completes.
        assert!(
            matches!(
                outcome,
                CycleOutcome::BackfillCompleted { .. } | CycleOutcome::BackfillInProgress { .. }
            ),
            "expected BackfillCompleted or BackfillInProgress, got {outcome:?}"
        );
    }

    // -------------------------------------------------------------------------
    // document_envelope helper
    // -------------------------------------------------------------------------
    #[test]
    fn document_envelope_includes_required_fields() {
        let doc = make_source_doc("my-id", "my-pk");
        let env = document_envelope(&doc, "test-source");
        assert_eq!(env["id"], "my-id");
        assert_eq!(env["_partition_key"], "my-pk");
        assert_eq!(env["source_name"], "test-source");
        assert!(env.get("source_link").is_some());
        assert!(env.get("updated").is_some());
        assert_eq!(env["title"], "Doc my-id");
    }
}