dwctl 8.38.2

The Doubleword Control Layer - A self-hostable observability and analytics platform for LLM applications
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Database repository for connections and sync operations.

use crate::db::errors::{DbError, Result};
use crate::db::models::connections::{Connection, SyncEntry, SyncOperation};
use chrono::{DateTime, Utc};
use sqlx::{FromRow, PgConnection};
use tracing::instrument;
use uuid::Uuid;

// ---------------------------------------------------------------------------
// Row structs (map exactly to table columns)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, FromRow)]
struct ConnectionRow {
    pub id: Uuid,
    pub user_id: Uuid,
    pub api_key_id: Option<Uuid>,
    pub kind: String,
    pub provider: String,
    pub name: String,
    pub config_encrypted: Vec<u8>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub deleted_at: Option<DateTime<Utc>>,
}

impl From<ConnectionRow> for Connection {
    fn from(r: ConnectionRow) -> Self {
        Self {
            id: r.id,
            user_id: r.user_id,
            api_key_id: r.api_key_id,
            kind: r.kind,
            provider: r.provider,
            name: r.name,
            config_encrypted: r.config_encrypted,
            created_at: r.created_at,
            updated_at: r.updated_at,
            deleted_at: r.deleted_at,
        }
    }
}

#[derive(Debug, Clone, FromRow)]
struct SyncOperationRow {
    pub id: Uuid,
    pub connection_id: Uuid,
    pub status: String,
    pub strategy: String,
    pub strategy_config: Option<serde_json::Value>,
    pub files_found: i32,
    pub files_skipped: i32,
    pub files_ingested: i32,
    pub files_failed: i32,
    pub batches_created: i32,
    pub error_summary: Option<serde_json::Value>,
    pub triggered_by: Uuid,
    pub sync_config: serde_json::Value,
    pub started_at: Option<DateTime<Utc>>,
    pub completed_at: Option<DateTime<Utc>>,
    pub created_at: DateTime<Utc>,
}

impl From<SyncOperationRow> for SyncOperation {
    fn from(r: SyncOperationRow) -> Self {
        Self {
            id: r.id,
            connection_id: r.connection_id,
            status: r.status,
            strategy: r.strategy,
            strategy_config: r.strategy_config,
            files_found: r.files_found,
            files_skipped: r.files_skipped,
            files_ingested: r.files_ingested,
            files_failed: r.files_failed,
            batches_created: r.batches_created,
            error_summary: r.error_summary,
            triggered_by: r.triggered_by,
            sync_config: r.sync_config,
            started_at: r.started_at,
            completed_at: r.completed_at,
            created_at: r.created_at,
        }
    }
}

/// Maps to `sync_entries` table columns in schema order
/// (090_connections_and_sync.sql + 093_add_sync_entry_validation.sql).
#[derive(Debug, Clone, FromRow)]
struct SyncEntryRow {
    pub id: Uuid,
    pub sync_id: Uuid,
    pub connection_id: Uuid,
    pub external_key: String,
    pub external_last_modified: Option<DateTime<Utc>>,
    pub external_size_bytes: Option<i64>,
    pub status: String,
    pub file_id: Option<Uuid>,
    pub batch_id: Option<Uuid>,
    pub template_count: Option<i32>,
    pub error: Option<String>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    // Added by migration 093 (appended after original columns)
    pub skipped_lines: i32,
    pub validation_errors: Option<serde_json::Value>,
}

impl From<SyncEntryRow> for SyncEntry {
    fn from(r: SyncEntryRow) -> Self {
        Self {
            id: r.id,
            sync_id: r.sync_id,
            connection_id: r.connection_id,
            external_key: r.external_key,
            external_last_modified: r.external_last_modified,
            external_size_bytes: r.external_size_bytes,
            status: r.status,
            file_id: r.file_id,
            batch_id: r.batch_id,
            template_count: r.template_count,
            error: r.error,
            skipped_lines: r.skipped_lines,
            validation_errors: r.validation_errors,
            created_at: r.created_at,
            updated_at: r.updated_at,
        }
    }
}

// ---------------------------------------------------------------------------
// Connections repository
// ---------------------------------------------------------------------------

pub struct Connections<'c> {
    db: &'c mut PgConnection,
}

impl<'c> Connections<'c> {
    pub fn new(db: &'c mut PgConnection) -> Self {
        Self { db }
    }

    #[instrument(skip(self, config_encrypted), fields(name = %name, provider = %provider), err)]
    pub async fn create(
        &mut self,
        user_id: Uuid,
        api_key_id: Option<Uuid>,
        kind: &str,
        provider: &str,
        name: &str,
        config_encrypted: &[u8],
    ) -> Result<Connection> {
        let row = sqlx::query_as!(
            ConnectionRow,
            r#"
            INSERT INTO connections (user_id, api_key_id, kind, provider, name, config_encrypted)
            VALUES ($1, $2, $3, $4, $5, $6)
            RETURNING *
            "#,
            user_id,
            api_key_id,
            kind,
            provider,
            name,
            config_encrypted,
        )
        .fetch_one(&mut *self.db)
        .await?;

        Ok(Connection::from(row))
    }

    #[instrument(skip(self), fields(id = %id), err)]
    pub async fn get_by_id(&mut self, id: Uuid) -> Result<Option<Connection>> {
        let row = sqlx::query_as!(ConnectionRow, "SELECT * FROM connections WHERE id = $1 AND deleted_at IS NULL", id,)
            .fetch_optional(&mut *self.db)
            .await?;

        Ok(row.map(Connection::from))
    }

    /// Bulk fetch connection names and owners by IDs. Returns a map of id → (name, user_id).
    #[instrument(skip(self, ids), fields(count = ids.len()), err)]
    pub async fn get_names_by_ids(&mut self, ids: &[Uuid]) -> Result<std::collections::HashMap<Uuid, (String, Uuid)>> {
        if ids.is_empty() {
            return Ok(std::collections::HashMap::new());
        }
        let rows = sqlx::query!(
            "SELECT id, name, user_id FROM connections WHERE id = ANY($1) AND deleted_at IS NULL",
            ids,
        )
        .fetch_all(&mut *self.db)
        .await?;

        Ok(rows.into_iter().map(|r| (r.id, (r.name, r.user_id))).collect())
    }

    #[instrument(skip(self), fields(user_id = %user_id), err)]
    pub async fn list_by_user(&mut self, user_id: Uuid, kind: Option<&str>) -> Result<Vec<Connection>> {
        let rows = sqlx::query_as!(
            ConnectionRow,
            r#"
            SELECT * FROM connections
            WHERE user_id = $1
              AND deleted_at IS NULL
              AND ($2::text IS NULL OR kind = $2)
            ORDER BY created_at DESC
            "#,
            user_id,
            kind,
        )
        .fetch_all(&mut *self.db)
        .await?;

        Ok(rows.into_iter().map(Connection::from).collect())
    }

    #[instrument(skip(self), fields(id = %id), err)]
    pub async fn soft_delete(&mut self, id: Uuid) -> Result<bool> {
        // Soft-delete sync tracking data — the files and batches in fusillade
        // remain untouched so users can still access them.
        // We mark entries/operations as deleted so dedup won't consider them,
        // but keep the records for audit trail.
        sqlx::query!(
            "UPDATE sync_entries SET status = 'deleted' WHERE connection_id = $1 AND status != 'deleted'",
            id,
        )
        .execute(&mut *self.db)
        .await?;

        sqlx::query!(
            "UPDATE sync_operations SET status = 'deleted' WHERE connection_id = $1 AND status != 'deleted'",
            id,
        )
        .execute(&mut *self.db)
        .await?;

        let result = sqlx::query!("UPDATE connections SET deleted_at = now() WHERE id = $1 AND deleted_at IS NULL", id,)
            .execute(&mut *self.db)
            .await?;

        Ok(result.rows_affected() > 0)
    }

    #[instrument(skip(self, config_encrypted), fields(id = %id), err)]
    pub async fn update(&mut self, id: Uuid, name: Option<&str>, config_encrypted: Option<&[u8]>) -> Result<Connection> {
        let row = sqlx::query_as!(
            ConnectionRow,
            r#"
            UPDATE connections SET
                name = COALESCE($2, name),
                config_encrypted = COALESCE($3, config_encrypted)
            WHERE id = $1 AND deleted_at IS NULL
            RETURNING *
            "#,
            id,
            name,
            config_encrypted,
        )
        .fetch_optional(&mut *self.db)
        .await?
        .ok_or(DbError::NotFound)?;

        Ok(Connection::from(row))
    }
}

// ---------------------------------------------------------------------------
// Sync operations repository
// ---------------------------------------------------------------------------

pub struct SyncOperations<'c> {
    db: &'c mut PgConnection,
}

impl<'c> SyncOperations<'c> {
    pub fn new(db: &'c mut PgConnection) -> Self {
        Self { db }
    }

    #[instrument(skip(self, sync_config, strategy_config), err)]
    pub async fn create(
        &mut self,
        connection_id: Uuid,
        triggered_by: Uuid,
        strategy: &str,
        strategy_config: Option<&serde_json::Value>,
        sync_config: &serde_json::Value,
    ) -> Result<SyncOperation> {
        let row = sqlx::query_as!(
            SyncOperationRow,
            r#"
            INSERT INTO sync_operations (connection_id, triggered_by, strategy, strategy_config, sync_config)
            VALUES ($1, $2, $3, $4, $5)
            RETURNING *
            "#,
            connection_id,
            triggered_by,
            strategy,
            strategy_config,
            sync_config,
        )
        .fetch_one(&mut *self.db)
        .await?;

        Ok(SyncOperation::from(row))
    }

    #[instrument(skip(self), fields(id = %id), err)]
    pub async fn get_by_id(&mut self, id: Uuid) -> Result<Option<SyncOperation>> {
        let row = sqlx::query_as!(
            SyncOperationRow,
            "SELECT * FROM sync_operations WHERE id = $1 AND status != 'deleted'",
            id,
        )
        .fetch_optional(&mut *self.db)
        .await?;

        Ok(row.map(SyncOperation::from))
    }

    #[instrument(skip(self), fields(connection_id = %connection_id), err)]
    pub async fn list_by_connection(&mut self, connection_id: Uuid) -> Result<Vec<SyncOperation>> {
        let rows = sqlx::query_as!(
            SyncOperationRow,
            "SELECT * FROM sync_operations WHERE connection_id = $1 AND status != 'deleted' ORDER BY created_at DESC",
            connection_id,
        )
        .fetch_all(&mut *self.db)
        .await?;

        Ok(rows.into_iter().map(SyncOperation::from).collect())
    }

    #[instrument(skip(self), fields(id = %id, status = %status), err)]
    pub async fn update_status(&mut self, id: Uuid, status: &str) -> Result<()> {
        let started = if status == "listing" { Some(Utc::now()) } else { None };
        let completed = if matches!(status, "completed" | "failed" | "cancelled") {
            Some(Utc::now())
        } else {
            None
        };

        sqlx::query!(
            r#"
            UPDATE sync_operations SET
                status = $2,
                started_at = COALESCE($3, started_at),
                completed_at = COALESCE($4, completed_at)
            WHERE id = $1 AND status != 'deleted'
            "#,
            id,
            status,
            started,
            completed,
        )
        .execute(&mut *self.db)
        .await?;

        Ok(())
    }

    #[instrument(skip(self), fields(id = %id), err)]
    pub async fn update_counters(
        &mut self,
        id: Uuid,
        files_found: Option<i32>,
        files_skipped: Option<i32>,
        files_ingested: Option<i32>,
        files_failed: Option<i32>,
        batches_created: Option<i32>,
    ) -> Result<()> {
        sqlx::query!(
            r#"
            UPDATE sync_operations SET
                files_found = COALESCE($2, files_found),
                files_skipped = COALESCE($3, files_skipped),
                files_ingested = COALESCE($4, files_ingested),
                files_failed = COALESCE($5, files_failed),
                batches_created = COALESCE($6, batches_created)
            WHERE id = $1
              AND status != 'deleted'
            "#,
            id,
            files_found,
            files_skipped,
            files_ingested,
            files_failed,
            batches_created,
        )
        .execute(&mut *self.db)
        .await?;

        Ok(())
    }

    /// Atomically increment a single counter by 1.
    #[instrument(skip(self), fields(id = %id, field = %field), err)]
    pub async fn increment_counter(&mut self, id: Uuid, field: &str) -> Result<()> {
        // Use dynamic SQL safely — field is validated against an allowlist.
        let query = match field {
            "files_found" => "UPDATE sync_operations SET files_found = files_found + 1 WHERE id = $1 AND status != 'deleted'",
            "files_skipped" => "UPDATE sync_operations SET files_skipped = files_skipped + 1 WHERE id = $1 AND status != 'deleted'",
            "files_ingested" => "UPDATE sync_operations SET files_ingested = files_ingested + 1 WHERE id = $1 AND status != 'deleted'",
            "files_failed" => "UPDATE sync_operations SET files_failed = files_failed + 1 WHERE id = $1 AND status != 'deleted'",
            "batches_created" => "UPDATE sync_operations SET batches_created = batches_created + 1 WHERE id = $1 AND status != 'deleted'",
            _ => return Err(DbError::Other(anyhow::anyhow!("unknown counter field: {}", field))),
        };

        sqlx::query(query).bind(id).execute(&mut *self.db).await?;
        Ok(())
    }

    /// Check if all entries for a sync are in a terminal state, and if so mark
    /// the sync_operation as completed (or failed if all entries failed).
    ///
    /// Returns true if the sync was marked terminal.
    #[instrument(skip(self), fields(id = %sync_id), err)]
    pub async fn try_complete(&mut self, sync_id: Uuid) -> Result<bool> {
        // Count entries by terminal vs non-terminal status in a single query
        let row = sqlx::query!(
            r#"
            SELECT
                COUNT(*) AS "total!",
                COUNT(*) FILTER (WHERE status IN ('activated', 'failed', 'skipped')) AS "terminal!",
                COUNT(*) FILTER (WHERE status = 'failed') AS "failed!"
            FROM sync_entries
            WHERE sync_id = $1 AND status != 'deleted'
            "#,
            sync_id,
        )
        .fetch_one(&mut *self.db)
        .await?;

        if row.total == 0 || row.terminal < row.total {
            return Ok(false);
        }

        // All entries are terminal — determine final sync status
        let final_status = if row.failed == row.total { "failed" } else { "completed" };

        sqlx::query!(
            "UPDATE sync_operations SET status = $2, completed_at = now() WHERE id = $1 AND completed_at IS NULL AND status != 'deleted'",
            sync_id,
            final_status,
        )
        .execute(&mut *self.db)
        .await?;

        Ok(true)
    }
}

// ---------------------------------------------------------------------------
// Sync entries repository
// ---------------------------------------------------------------------------

pub struct SyncEntries<'c> {
    db: &'c mut PgConnection,
}

impl<'c> SyncEntries<'c> {
    pub fn new(db: &'c mut PgConnection) -> Self {
        Self { db }
    }

    /// Bulk-insert sync entries for discovered files.
    #[allow(clippy::type_complexity)]
    #[instrument(skip(self, entries), fields(sync_id = %sync_id, count = entries.len()), err)]
    pub async fn bulk_create(
        &mut self,
        sync_id: Uuid,
        connection_id: Uuid,
        entries: &[(String, Option<DateTime<Utc>>, Option<i64>)], // (key, last_modified, size)
    ) -> Result<Vec<SyncEntry>> {
        if entries.is_empty() {
            return Ok(vec![]);
        }

        let keys: Vec<&str> = entries.iter().map(|(k, _, _)| k.as_str()).collect();
        let last_mods: Vec<Option<DateTime<Utc>>> = entries.iter().map(|(_, lm, _)| *lm).collect();
        let sizes: Vec<Option<i64>> = entries.iter().map(|(_, _, s)| *s).collect();

        let rows = sqlx::query_as!(
            SyncEntryRow,
            r#"
            INSERT INTO sync_entries (sync_id, connection_id, external_key, external_last_modified, external_size_bytes)
            SELECT $1, $2, t.key, t.last_modified, t.size_bytes
            FROM unnest($3::text[], $4::timestamptz[], $5::bigint[]) AS t(key, last_modified, size_bytes)
            RETURNING *
            "#,
            sync_id,
            connection_id,
            &keys as &[&str],
            &last_mods as &[Option<DateTime<Utc>>],
            &sizes as &[Option<i64>],
        )
        .fetch_all(&mut *self.db)
        .await?;

        Ok(rows.into_iter().map(SyncEntry::from).collect())
    }

    /// Find previously-synced entries for dedup.
    #[instrument(skip(self), fields(connection_id = %connection_id), err)]
    pub async fn find_existing(
        &mut self,
        connection_id: Uuid,
        keys_and_dates: &[(String, Option<DateTime<Utc>>)],
    ) -> Result<Vec<(String, Option<DateTime<Utc>>)>> {
        if keys_and_dates.is_empty() {
            return Ok(vec![]);
        }

        let keys: Vec<&str> = keys_and_dates.iter().map(|(k, _)| k.as_str()).collect();
        let dates: Vec<Option<DateTime<Utc>>> = keys_and_dates.iter().map(|(_, d)| *d).collect();

        let rows = sqlx::query!(
            r#"
            SELECT se.external_key, se.external_last_modified
            FROM sync_entries se
            INNER JOIN unnest($2::text[], $3::timestamptz[]) AS input(key, last_modified)
              ON se.external_key = input.key
              AND se.external_last_modified IS NOT DISTINCT FROM input.last_modified
            WHERE se.connection_id = $1
              AND se.status IN ('activated', 'failed')
            "#,
            connection_id,
            &keys as &[&str],
            &dates as &[Option<DateTime<Utc>>],
        )
        .fetch_all(&mut *self.db)
        .await?;

        Ok(rows.into_iter().map(|r| (r.external_key, r.external_last_modified)).collect())
    }

    /// Get all terminal sync entries for a connection (for UI status display).
    /// Returns (key, last_modified, status) where status is 'activated' or 'failed'.
    /// The frontend uses this to show Synced/Failed/Modified states in the file browser.
    #[instrument(skip(self), fields(connection_id = %connection_id), err)]
    #[allow(clippy::type_complexity)]
    pub async fn list_synced_keys(&mut self, connection_id: Uuid) -> Result<Vec<(String, Option<DateTime<Utc>>, String)>> {
        let rows = sqlx::query!(
            r#"
            SELECT DISTINCT ON (external_key)
                   external_key,
                   external_last_modified AS "last_modified",
                   status AS "status!"
            FROM sync_entries
            WHERE connection_id = $1
              AND status IN ('activated', 'failed')
            ORDER BY external_key, external_last_modified DESC NULLS LAST, updated_at DESC
            "#,
            connection_id,
        )
        .fetch_all(&mut *self.db)
        .await?;

        Ok(rows.into_iter().map(|r| (r.external_key, r.last_modified, r.status)).collect())
    }

    /// Returns true if the row was updated, false if it was already deleted.
    #[instrument(skip(self), fields(id = %id, status = %status), err)]
    pub async fn update_status(&mut self, id: Uuid, status: &str, error: Option<&str>) -> Result<bool> {
        let result = sqlx::query!(
            "UPDATE sync_entries SET status = $2, error = $3 WHERE id = $1 AND status != 'deleted'",
            id,
            status,
            error,
        )
        .execute(&mut *self.db)
        .await?;

        Ok(result.rows_affected() > 0)
    }

    /// Returns true if the row was updated, false if it was already deleted.
    #[instrument(skip(self, validation_errors), fields(id = %id), err)]
    pub async fn set_ingested(
        &mut self,
        id: Uuid,
        file_id: Uuid,
        template_count: i32,
        skipped_lines: i32,
        validation_errors: Option<&serde_json::Value>,
    ) -> Result<bool> {
        let result = sqlx::query!(
            r#"
            UPDATE sync_entries
            SET status = 'ingested', file_id = $2, template_count = $3,
                skipped_lines = $4, validation_errors = $5
            WHERE id = $1 AND status != 'deleted'
            "#,
            id,
            file_id,
            template_count,
            skipped_lines,
            validation_errors,
        )
        .execute(&mut *self.db)
        .await?;

        Ok(result.rows_affected() > 0)
    }

    #[instrument(skip(self), fields(id = %id), err)]
    pub async fn set_activated(&mut self, id: Uuid, batch_id: Uuid) -> Result<()> {
        sqlx::query!(
            "UPDATE sync_entries SET status = 'activated', batch_id = $2 WHERE id = $1 AND status != 'deleted'",
            id,
            batch_id,
        )
        .execute(&mut *self.db)
        .await?;

        Ok(())
    }

    #[instrument(skip(self), fields(sync_id = %sync_id), err)]
    pub async fn list_by_sync(&mut self, sync_id: Uuid) -> Result<Vec<SyncEntry>> {
        let rows = sqlx::query_as!(
            SyncEntryRow,
            "SELECT * FROM sync_entries WHERE sync_id = $1 ORDER BY external_key",
            sync_id,
        )
        .fetch_all(&mut *self.db)
        .await?;

        Ok(rows.into_iter().map(SyncEntry::from).collect())
    }

    /// Get entry by ID.
    #[instrument(skip(self), fields(id = %id), err)]
    pub async fn get_by_id(&mut self, id: Uuid) -> Result<Option<SyncEntry>> {
        let row = sqlx::query_as!(SyncEntryRow, "SELECT * FROM sync_entries WHERE id = $1", id,)
            .fetch_optional(&mut *self.db)
            .await?;

        Ok(row.map(SyncEntry::from))
    }
}