dwctl 8.40.0

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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//! HTTP handlers for health probe endpoints.

use crate::AppState;
use crate::api::models::probes::{
    CreateProbe, ProbeStatistics, ProbesQuery, ResultsQuery, StatsQuery, TestProbeRequest, UpdateProbeRequest,
};
use crate::auth::permissions::{RequiresPermission, operation, resource};
use crate::db::models::probes::{Probe, ProbeResult};
use crate::errors::Error;
use crate::probes::db::ProbeManager;
use axum::{
    Json,
    extract::{Path, Query, State},
    http::StatusCode,
};
use uuid::Uuid;

#[utoipa::path(
    post,
    path = "/probes",
    tag = "probes",
    summary = "Create a new probe",
    description = "Create a new probe to monitor a deployed model. The probe is automatically activated and starts executing on its configured interval.",
    request_body = CreateProbe,
    responses(
        (status = 201, description = "Probe created successfully", body = Probe),
        (status = 400, description = "Bad request - invalid probe data"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden - admin access required"),
        (status = 404, description = "Deployment not found"),
        (status = 500, description = "Internal server error"),
    ),
    security(
        ("BearerAuth" = []),
        ("CookieAuth" = []),
        ("X-Doubleword-User" = [])
    )
)]
#[tracing::instrument(skip_all)]
pub async fn create_probe(
    State(state): State<AppState>,
    _: RequiresPermission<resource::Probes, operation::CreateAll>,
    Json(probe): Json<CreateProbe>,
) -> Result<(StatusCode, Json<Probe>), Error> {
    let created = ProbeManager::create_probe(&state.db, probe).await?;
    Ok((StatusCode::CREATED, Json(created)))
}

#[utoipa::path(
    get,
    path = "/probes",
    tag = "probes",
    summary = "List all probes",
    description = "List all probes, optionally filtered by status",
    params(
        ProbesQuery
    ),
    responses(
        (status = 200, description = "List of probes", body = Vec<Probe>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden - admin access required"),
        (status = 500, description = "Internal server error"),
    ),
    security(
        ("BearerAuth" = []),
        ("CookieAuth" = []),
        ("X-Doubleword-User" = [])
    )
)]
#[tracing::instrument(skip_all)]
pub async fn list_probes(
    State(state): State<AppState>,
    _: RequiresPermission<resource::Probes, operation::ReadAll>,
    Query(query): Query<ProbesQuery>,
) -> Result<Json<Vec<Probe>>, Error> {
    let probes = match query.status.as_deref() {
        Some("active") => ProbeManager::list_active_probes(&state.db).await?,
        _ => ProbeManager::list_probes(&state.db).await?,
    };
    Ok(Json(probes))
}

#[utoipa::path(
    get,
    path = "/probes/{id}",
    tag = "probes",
    summary = "Get a specific probe",
    description = "Get detailed information about a specific probe by ID",
    params(
        ("id" = uuid::Uuid, Path, description = "Probe ID to retrieve"),
    ),
    responses(
        (status = 200, description = "Probe details", body = Probe),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden - admin access required"),
        (status = 404, description = "Probe not found"),
        (status = 500, description = "Internal server error"),
    ),
    security(
        ("BearerAuth" = []),
        ("CookieAuth" = []),
        ("X-Doubleword-User" = [])
    )
)]
#[tracing::instrument(skip_all)]
pub async fn get_probe(
    State(state): State<AppState>,
    _: RequiresPermission<resource::Probes, operation::ReadAll>,
    Path(id): Path<Uuid>,
) -> Result<Json<Probe>, Error> {
    let probe = ProbeManager::get_probe(&state.db, id).await?;
    Ok(Json(probe))
}

#[utoipa::path(
    delete,
    path = "/probes/{id}",
    tag = "probes",
    summary = "Delete a probe",
    description = "Delete a probe and stop its scheduler",
    params(
        ("id" = uuid::Uuid, Path, description = "Probe ID to delete"),
    ),
    responses(
        (status = 204, description = "Probe deleted successfully"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden - admin access required"),
        (status = 404, description = "Probe not found"),
        (status = 500, description = "Internal server error"),
    ),
    security(
        ("BearerAuth" = []),
        ("CookieAuth" = []),
        ("X-Doubleword-User" = [])
    )
)]
#[tracing::instrument(skip_all)]
pub async fn delete_probe(
    State(state): State<AppState>,
    _: RequiresPermission<resource::Probes, operation::DeleteAll>,
    Path(id): Path<Uuid>,
) -> Result<StatusCode, Error> {
    ProbeManager::delete_probe(&state.db, id).await?;
    Ok(StatusCode::NO_CONTENT)
}

#[utoipa::path(
    patch,
    path = "/probes/{id}/activate",
    tag = "probes",
    summary = "Activate a probe",
    description = "Activate a probe and start its scheduler to begin executing at its configured interval",
    params(
        ("id" = uuid::Uuid, Path, description = "Probe ID to activate"),
    ),
    responses(
        (status = 200, description = "Probe activated successfully", body = Probe),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden - admin access required"),
        (status = 404, description = "Probe not found"),
        (status = 500, description = "Internal server error"),
    ),
    security(
        ("BearerAuth" = []),
        ("CookieAuth" = []),
        ("X-Doubleword-User" = [])
    )
)]
#[tracing::instrument(skip_all)]
pub async fn activate_probe(
    State(state): State<AppState>,
    _: RequiresPermission<resource::Probes, operation::UpdateAll>,
    Path(id): Path<Uuid>,
) -> Result<Json<Probe>, Error> {
    let probe = ProbeManager::activate_probe(&state.db, id).await?;
    Ok(Json(probe))
}

#[utoipa::path(
    patch,
    path = "/probes/{id}/deactivate",
    tag = "probes",
    summary = "Deactivate a probe",
    description = "Deactivate a probe and stop its scheduler to stop executing",
    params(
        ("id" = uuid::Uuid, Path, description = "Probe ID to deactivate"),
    ),
    responses(
        (status = 200, description = "Probe deactivated successfully", body = Probe),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden - admin access required"),
        (status = 404, description = "Probe not found"),
        (status = 500, description = "Internal server error"),
    ),
    security(
        ("BearerAuth" = []),
        ("CookieAuth" = []),
        ("X-Doubleword-User" = [])
    )
)]
#[tracing::instrument(skip_all)]
pub async fn deactivate_probe(
    State(state): State<AppState>,
    _: RequiresPermission<resource::Probes, operation::UpdateAll>,
    Path(id): Path<Uuid>,
) -> Result<Json<Probe>, Error> {
    let probe = ProbeManager::deactivate_probe(&state.db, id).await?;
    Ok(Json(probe))
}

#[utoipa::path(
    patch,
    path = "/probes/{id}",
    tag = "probes",
    summary = "Update a probe",
    description = "Update probe configuration such as execution interval",
    params(
        ("id" = uuid::Uuid, Path, description = "Probe ID to update"),
    ),
    request_body = UpdateProbeRequest,
    responses(
        (status = 200, description = "Probe updated successfully", body = Probe),
        (status = 400, description = "Bad request - invalid update data"),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden - admin access required"),
        (status = 404, description = "Probe not found"),
        (status = 500, description = "Internal server error"),
    ),
    security(
        ("BearerAuth" = []),
        ("CookieAuth" = []),
        ("X-Doubleword-User" = [])
    )
)]
#[tracing::instrument(skip_all)]
pub async fn update_probe(
    State(state): State<AppState>,
    _: RequiresPermission<resource::Probes, operation::UpdateAll>,
    Path(id): Path<Uuid>,
    Json(update): Json<UpdateProbeRequest>,
) -> Result<Json<Probe>, Error> {
    let probe = ProbeManager::update_probe(&state.db, id, update).await?;
    Ok(Json(probe))
}

#[utoipa::path(
    post,
    path = "/probes/{id}/execute",
    tag = "probes",
    summary = "Execute a probe immediately",
    description = "Manually trigger a probe execution without waiting for the scheduled interval",
    params(
        ("id" = uuid::Uuid, Path, description = "Probe ID to execute"),
    ),
    responses(
        (status = 201, description = "Probe executed successfully", body = ProbeResult),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden - admin access required"),
        (status = 404, description = "Probe not found"),
        (status = 500, description = "Internal server error"),
    ),
    security(
        ("BearerAuth" = []),
        ("CookieAuth" = []),
        ("X-Doubleword-User" = [])
    )
)]
#[tracing::instrument(skip_all)]
pub async fn execute_probe(
    State(state): State<AppState>,
    _: RequiresPermission<resource::Probes, operation::UpdateAll>,
    Path(id): Path<Uuid>,
) -> Result<(StatusCode, Json<ProbeResult>), Error> {
    let config = state.current_config();
    let result = ProbeManager::execute_probe(&state.db, id, &config).await?;
    Ok((StatusCode::CREATED, Json(result)))
}

#[utoipa::path(
    post,
    path = "/probes/test/{deployment_id}",
    tag = "probes",
    summary = "Test a probe configuration",
    description = "Test a probe configuration for a deployment without creating an actual probe",
    params(
        ("deployment_id" = uuid::Uuid, Path, description = "Deployment ID to test probe against"),
    ),
    responses(
        (status = 200, description = "Probe test executed successfully", body = ProbeResult),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden - admin access required"),
        (status = 404, description = "Deployment not found"),
        (status = 500, description = "Internal server error"),
    ),
    security(
        ("BearerAuth" = []),
        ("CookieAuth" = []),
        ("X-Doubleword-User" = [])
    )
)]
#[tracing::instrument(skip_all)]
pub async fn test_probe(
    State(state): State<AppState>,
    _: RequiresPermission<resource::Probes, operation::ReadAll>,
    Path(deployment_id): Path<Uuid>,
    Json(request): Json<Option<TestProbeRequest>>,
) -> Result<(StatusCode, Json<ProbeResult>), Error> {
    let config = state.current_config();
    let (http_method, request_path, request_body) = if let Some(req) = request {
        (req.http_method, req.request_path, req.request_body)
    } else {
        (None, None, None)
    };

    let result = ProbeManager::test_probe(&state.db, deployment_id, &config, http_method, request_path, request_body).await?;
    Ok((StatusCode::OK, Json(result)))
}

#[utoipa::path(
    get,
    path = "/probes/{id}/results",
    tag = "probes",
    summary = "Get probe execution results",
    description = "Retrieve historical execution results for a probe, optionally filtered by time range and limited",
    params(
        ("id" = uuid::Uuid, Path, description = "Probe ID to get results for"),
        ResultsQuery
    ),
    responses(
        (status = 200, description = "List of probe execution results", body = Vec<ProbeResult>),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden - admin access required"),
        (status = 404, description = "Probe not found"),
        (status = 500, description = "Internal server error"),
    ),
    security(
        ("BearerAuth" = []),
        ("CookieAuth" = []),
        ("X-Doubleword-User" = [])
    )
)]
#[tracing::instrument(skip_all)]
pub async fn get_probe_results(
    State(state): State<AppState>,
    _: RequiresPermission<resource::Probes, operation::ReadAll>,
    Path(id): Path<Uuid>,
    Query(query): Query<ResultsQuery>,
) -> Result<Json<Vec<ProbeResult>>, Error> {
    let results = ProbeManager::get_probe_results(&state.db, id, query.start_time, query.end_time, query.limit).await?;
    Ok(Json(results))
}

#[utoipa::path(
    get,
    path = "/probes/{id}/statistics",
    tag = "probes",
    summary = "Get probe statistics",
    description = "Get aggregated statistics for a probe including success rates, response times, and percentiles",
    params(
        ("id" = uuid::Uuid, Path, description = "Probe ID to get statistics for"),
        StatsQuery
    ),
    responses(
        (status = 200, description = "Probe statistics", body = ProbeStatistics),
        (status = 401, description = "Unauthorized"),
        (status = 403, description = "Forbidden - admin access required"),
        (status = 404, description = "Probe not found"),
        (status = 500, description = "Internal server error"),
    ),
    security(
        ("BearerAuth" = []),
        ("CookieAuth" = []),
        ("X-Doubleword-User" = [])
    )
)]
#[tracing::instrument(skip_all)]
pub async fn get_statistics(
    State(state): State<AppState>,
    _: RequiresPermission<resource::Probes, operation::ReadAll>,
    Path(id): Path<Uuid>,
    Query(query): Query<StatsQuery>,
) -> Result<Json<ProbeStatistics>, Error> {
    let stats = ProbeManager::get_statistics(&state.db, id, query.start_time, query.end_time).await?;
    Ok(Json(stats))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        api::models::users::Role,
        db::models::probes::Probe,
        test::utils::{add_auth_headers, create_test_admin_user, create_test_app, create_test_user},
    };
    use sqlx::PgPool;

    async fn setup_test_deployment(pool: &PgPool, user_id: Uuid) -> Uuid {
        let unique_id = Uuid::new_v4();
        let endpoint_name = format!("test-endpoint-{}", unique_id);
        let model_name = format!("test-model-{}", unique_id);

        let endpoint_id = sqlx::query_scalar!(
            "INSERT INTO inference_endpoints (name, url, created_by) VALUES ($1, $2, $3) RETURNING id",
            endpoint_name,
            "http://localhost:8080",
            user_id
        )
        .fetch_one(pool)
        .await
        .unwrap();

        sqlx::query_scalar!(
            "INSERT INTO deployed_models (model_name, alias, type, hosted_on, created_by) VALUES ($1, $2, $3, $4, $5) RETURNING id",
            model_name.clone(),
            model_name,
            "chat" as _,
            endpoint_id,
            user_id
        )
        .fetch_one(pool)
        .await
        .unwrap()
    }

    #[sqlx::test]
    #[test_log::test]
    async fn test_create_probe(pool: PgPool) {
        let (app, _bg_services) = create_test_app(pool.clone(), false).await;
        let user = create_test_admin_user(&pool, Role::PlatformManager).await;
        let deployment_id = setup_test_deployment(&pool, user.id).await;

        let payload = serde_json::json!({
            "name": "Test Probe",
            "deployment_id": deployment_id,
            "interval_seconds": 60
        });

        let response = app
            .post("/admin/api/v1/probes")
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .json(&payload)
            .await;

        response.assert_status(axum::http::StatusCode::CREATED);
        let probe: Probe = response.json();
        assert_eq!(probe.name, "Test Probe");
        assert_eq!(probe.deployment_id, deployment_id);
        assert_eq!(probe.interval_seconds, 60);
        assert!(probe.active);
    }

    #[sqlx::test]
    #[test_log::test]
    async fn test_create_probe_unauthorized(pool: PgPool) {
        let (app, _bg_services) = create_test_app(pool.clone(), false).await;
        let user = create_test_user(&pool, Role::StandardUser).await;
        let deployment_id = setup_test_deployment(&pool, user.id).await;

        let payload = serde_json::json!({
            "name": "Test Probe",
            "deployment_id": deployment_id,
            "interval_seconds": 60
        });

        let response = app
            .post("/admin/api/v1/probes")
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .json(&payload)
            .await;

        response.assert_status(axum::http::StatusCode::FORBIDDEN);
    }

    #[sqlx::test]
    #[test_log::test]
    async fn test_list_probes(pool: PgPool) {
        let (app, _bg_services) = create_test_app(pool.clone(), false).await;
        let user = create_test_admin_user(&pool, Role::PlatformManager).await;

        // Create test probes
        let deployment_id1 = setup_test_deployment(&pool, user.id).await;
        let deployment_id2 = setup_test_deployment(&pool, user.id).await;

        ProbeManager::create_probe(
            &pool,
            CreateProbe {
                name: "Probe 1".to_string(),
                deployment_id: deployment_id1,
                interval_seconds: 60,
                http_method: "POST".to_string(),
                request_path: None,
                request_body: None,
            },
        )
        .await
        .unwrap();

        ProbeManager::create_probe(
            &pool,
            CreateProbe {
                name: "Probe 2".to_string(),
                deployment_id: deployment_id2,
                interval_seconds: 120,
                http_method: "POST".to_string(),
                request_path: None,
                request_body: None,
            },
        )
        .await
        .unwrap();

        let response = app
            .get("/admin/api/v1/probes")
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .await;

        response.assert_status_ok();
        let probes: Vec<Probe> = response.json();
        assert_eq!(probes.len(), 2);
    }

    #[sqlx::test]
    #[test_log::test]
    async fn test_get_probe(pool: PgPool) {
        let (app, _bg_services) = create_test_app(pool.clone(), false).await;
        let user = create_test_admin_user(&pool, Role::PlatformManager).await;
        let deployment_id = setup_test_deployment(&pool, user.id).await;

        let created = ProbeManager::create_probe(
            &pool,
            CreateProbe {
                name: "Test Probe".to_string(),
                deployment_id,
                interval_seconds: 60,
                http_method: "POST".to_string(),
                request_path: None,
                request_body: None,
            },
        )
        .await
        .unwrap();

        let response = app
            .get(&format!("/admin/api/v1/probes/{}", created.id))
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .await;

        response.assert_status_ok();
        let probe: Probe = response.json();
        assert_eq!(probe.id, created.id);
        assert_eq!(probe.name, "Test Probe");
    }

    #[sqlx::test]
    #[test_log::test]
    async fn test_get_probe_not_found(pool: PgPool) {
        let (app, _bg_services) = create_test_app(pool.clone(), false).await;
        let user = create_test_admin_user(&pool, Role::PlatformManager).await;
        let non_existent_id = Uuid::new_v4();

        let response = app
            .get(&format!("/admin/api/v1/probes/{}", non_existent_id))
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .await;

        response.assert_status_not_found();
    }

    #[sqlx::test]
    #[test_log::test]
    async fn test_update_probe(pool: PgPool) {
        let (app, _bg_services) = create_test_app(pool.clone(), false).await;
        let user = create_test_admin_user(&pool, Role::PlatformManager).await;
        let deployment_id = setup_test_deployment(&pool, user.id).await;

        let created = ProbeManager::create_probe(
            &pool,
            CreateProbe {
                name: "Original Name".to_string(),
                deployment_id,
                interval_seconds: 60,
                http_method: "POST".to_string(),
                request_path: None,
                request_body: None,
            },
        )
        .await
        .unwrap();

        let payload = serde_json::json!({
            "interval_seconds": 120
        });

        let response = app
            .patch(&format!("/admin/api/v1/probes/{}", created.id))
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .json(&payload)
            .await;

        response.assert_status_ok();
        let probe: Probe = response.json();
        assert_eq!(probe.name, "Original Name"); // Name should not change
        assert_eq!(probe.interval_seconds, 120);
    }

    #[sqlx::test]
    #[test_log::test]
    async fn test_activate_probe(pool: PgPool) {
        let (app, _bg_services) = create_test_app(pool.clone(), false).await;
        let user = create_test_admin_user(&pool, Role::PlatformManager).await;
        let deployment_id = setup_test_deployment(&pool, user.id).await;

        let created = ProbeManager::create_probe(
            &pool,
            CreateProbe {
                name: "Test Probe".to_string(),
                deployment_id,
                interval_seconds: 60,
                http_method: "POST".to_string(),
                request_path: None,
                request_body: None,
            },
        )
        .await
        .unwrap();

        // First deactivate it
        ProbeManager::deactivate_probe(&pool, created.id).await.unwrap();

        let response = app
            .patch(&format!("/admin/api/v1/probes/{}/activate", created.id))
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .await;

        response.assert_status_ok();
        let probe: Probe = response.json();
        assert!(probe.active);
    }

    #[sqlx::test]
    #[test_log::test]
    async fn test_deactivate_probe(pool: PgPool) {
        let (app, _bg_services) = create_test_app(pool.clone(), false).await;
        let user = create_test_admin_user(&pool, Role::PlatformManager).await;
        let deployment_id = setup_test_deployment(&pool, user.id).await;

        let created = ProbeManager::create_probe(
            &pool,
            CreateProbe {
                name: "Test Probe".to_string(),
                deployment_id,
                interval_seconds: 60,
                http_method: "POST".to_string(),
                request_path: None,
                request_body: None,
            },
        )
        .await
        .unwrap();

        let response = app
            .patch(&format!("/admin/api/v1/probes/{}/deactivate", created.id))
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .await;

        response.assert_status_ok();
        let probe: Probe = response.json();
        assert!(!probe.active);
    }

    #[sqlx::test]
    #[test_log::test]
    async fn test_delete_probe(pool: PgPool) {
        let (app, _bg_services) = create_test_app(pool.clone(), false).await;
        let user = create_test_admin_user(&pool, Role::PlatformManager).await;
        let deployment_id = setup_test_deployment(&pool, user.id).await;

        let created = ProbeManager::create_probe(
            &pool,
            CreateProbe {
                name: "Test Probe".to_string(),
                deployment_id,
                interval_seconds: 60,
                http_method: "POST".to_string(),
                request_path: None,
                request_body: None,
            },
        )
        .await
        .unwrap();

        let response = app
            .delete(&format!("/admin/api/v1/probes/{}", created.id))
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .await;

        response.assert_status(axum::http::StatusCode::NO_CONTENT);

        // Verify probe is deleted
        let get_response = app
            .get(&format!("/admin/api/v1/probes/{}", created.id))
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .await;

        get_response.assert_status_not_found();
    }

    #[sqlx::test]
    #[test_log::test]
    async fn test_get_probe_results(pool: PgPool) {
        let (app, _bg_services) = create_test_app(pool.clone(), false).await;
        let user = create_test_admin_user(&pool, Role::PlatformManager).await;
        let deployment_id = setup_test_deployment(&pool, user.id).await;

        let created = ProbeManager::create_probe(
            &pool,
            CreateProbe {
                name: "Test Probe".to_string(),
                deployment_id,
                interval_seconds: 60,
                http_method: "POST".to_string(),
                request_path: None,
                request_body: None,
            },
        )
        .await
        .unwrap();

        let response = app
            .get(&format!("/admin/api/v1/probes/{}/results", created.id))
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .await;

        response.assert_status_ok();
        let results: Vec<ProbeResult> = response.json();
        assert!(results.is_empty()); // No results initially
    }

    #[sqlx::test]
    #[test_log::test]
    async fn test_get_statistics(pool: PgPool) {
        let (app, _bg_services) = create_test_app(pool.clone(), false).await;
        let user = create_test_admin_user(&pool, Role::PlatformManager).await;
        let deployment_id = setup_test_deployment(&pool, user.id).await;

        let created = ProbeManager::create_probe(
            &pool,
            CreateProbe {
                name: "Test Probe".to_string(),
                deployment_id,
                interval_seconds: 60,
                http_method: "POST".to_string(),
                request_path: None,
                request_body: None,
            },
        )
        .await
        .unwrap();

        let response = app
            .get(&format!("/admin/api/v1/probes/{}/statistics", created.id))
            .add_header(&add_auth_headers(&user)[0].0, &add_auth_headers(&user)[0].1)
            .add_header(&add_auth_headers(&user)[1].0, &add_auth_headers(&user)[1].1)
            .await;

        response.assert_status_ok();
        let stats: ProbeStatistics = response.json();
        assert_eq!(stats.total_executions, 0);
    }
}