mockforge-http 0.3.116

HTTP/REST protocol support for MockForge
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
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
//! HTTP handlers for API change forecasting
//!
//! This module provides endpoints for querying and managing forecasts.

use axum::{
    extract::{Path, Query, State},
    http::StatusCode,
    response::Json,
};
use mockforge_core::contract_drift::forecasting::{ChangeForecast, Forecaster};
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[cfg(feature = "database")]
use chrono::{DateTime, Utc};
#[cfg(feature = "database")]
use mockforge_core::contract_drift::forecasting::SeasonalPattern;
#[cfg(feature = "database")]
use uuid::Uuid;

use crate::database::Database;

/// Helper function to map database row to ChangeForecast
#[cfg(feature = "database")]
fn map_row_to_change_forecast(row: &sqlx::postgres::PgRow) -> Result<ChangeForecast, sqlx::Error> {
    use sqlx::Row;

    let service_id: Option<String> = row.try_get("service_id")?;
    let service_name: Option<String> = row.try_get("service_name")?;
    let endpoint: String = row.try_get("endpoint")?;
    let method: String = row.try_get("method")?;
    let forecast_window_days: i32 = row.try_get("forecast_window_days")?;
    let predicted_change_probability: f64 = row.try_get("predicted_change_probability")?;
    let predicted_break_probability: f64 = row.try_get("predicted_break_probability")?;
    let next_expected_change_date: Option<DateTime<Utc>> =
        row.try_get("next_expected_change_date")?;
    let next_expected_break_date: Option<DateTime<Utc>> =
        row.try_get("next_expected_break_date")?;
    let volatility_score: f64 = row.try_get("volatility_score")?;
    let confidence: f64 = row.try_get("confidence")?;
    let seasonal_patterns_json: serde_json::Value =
        row.try_get("seasonal_patterns").unwrap_or_default();
    let predicted_at: DateTime<Utc> = row.try_get("predicted_at")?;
    let expires_at: DateTime<Utc> = row.try_get("expires_at")?;

    // Parse seasonal patterns from JSONB
    let seasonal_patterns: Vec<SeasonalPattern> = if seasonal_patterns_json.is_array() {
        serde_json::from_value(seasonal_patterns_json).unwrap_or_default()
    } else {
        Vec::new()
    };

    Ok(ChangeForecast {
        service_id,
        service_name,
        endpoint,
        method,
        forecast_window_days: forecast_window_days as u32,
        predicted_change_probability,
        predicted_break_probability,
        next_expected_change_date,
        next_expected_break_date,
        volatility_score,
        confidence,
        seasonal_patterns,
        predicted_at,
        expires_at,
    })
}

/// State for forecasting handlers
#[derive(Clone)]
pub struct ForecastingState {
    /// Forecaster engine
    pub forecaster: Arc<Forecaster>,
    /// Database connection (optional)
    pub database: Option<Database>,
}

/// Query parameters for listing forecasts
#[derive(Debug, Deserialize)]
pub struct ListForecastsQuery {
    /// Workspace ID filter
    pub workspace_id: Option<String>,
    /// Service ID filter
    pub service_id: Option<String>,
    /// Endpoint filter
    pub endpoint: Option<String>,
    /// Method filter
    pub method: Option<String>,
    /// Forecast window (30, 90, or 180 days)
    pub window_days: Option<u32>,
}

/// Response for forecast list
#[derive(Debug, Serialize)]
pub struct ForecastListResponse {
    /// Forecasts
    pub forecasts: Vec<ChangeForecast>,
    /// Total count
    pub total: usize,
}

/// Get forecasts
///
/// GET /api/v1/forecasts
#[cfg(feature = "database")]
pub async fn list_forecasts(
    State(state): State<ForecastingState>,
    Query(params): Query<ListForecastsQuery>,
) -> Result<Json<ForecastListResponse>, StatusCode> {
    let pool = match state.database.as_ref().and_then(|db| db.pool()) {
        Some(pool) => pool,
        None => {
            return Ok(Json(ForecastListResponse {
                forecasts: Vec::new(),
                total: 0,
            }));
        }
    };

    // Build query with filters
    let mut query = String::from(
        "SELECT id, workspace_id, service_id, service_name, endpoint, method,
         forecast_window_days, predicted_change_probability, predicted_break_probability,
         next_expected_change_date, next_expected_break_date, volatility_score, confidence,
         seasonal_patterns, predicted_at, expires_at
         FROM api_change_forecasts WHERE expires_at > NOW()",
    );

    let mut bind_index = 1;

    if params.workspace_id.is_some() {
        query.push_str(&format!(" AND workspace_id = ${}", bind_index));
        bind_index += 1;
    }

    if params.service_id.is_some() {
        query.push_str(&format!(" AND service_id = ${}", bind_index));
        bind_index += 1;
    }

    if params.endpoint.is_some() {
        query.push_str(&format!(" AND endpoint = ${}", bind_index));
        bind_index += 1;
    }

    if params.method.is_some() {
        query.push_str(&format!(" AND method = ${}", bind_index));
        bind_index += 1;
    }

    if let Some(_window) = params.window_days {
        query.push_str(&format!(" AND forecast_window_days = ${}", bind_index));
    }

    query.push_str(" ORDER BY predicted_at DESC LIMIT 100");

    // Build query with proper bindings using sqlx
    let mut query_builder = sqlx::query(&query);

    if let Some(ws_id) = &params.workspace_id {
        let uuid = Uuid::parse_str(ws_id).ok();
        query_builder = query_builder.bind(uuid);
    }

    if let Some(svc_id) = &params.service_id {
        query_builder = query_builder.bind(svc_id);
    }

    if let Some(ep) = &params.endpoint {
        query_builder = query_builder.bind(ep);
    }

    if let Some(m) = &params.method {
        query_builder = query_builder.bind(m);
    }

    if let Some(window) = params.window_days {
        query_builder = query_builder.bind(window as i32);
    }

    // Execute query
    let rows = query_builder.fetch_all(pool).await.map_err(|e| {
        tracing::error!("Failed to query forecasts: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })?;

    // Map rows to ChangeForecast
    let mut forecasts = Vec::new();
    for row in rows {
        match map_row_to_change_forecast(&row) {
            Ok(forecast) => forecasts.push(forecast),
            Err(e) => {
                tracing::warn!("Failed to map forecast row: {}", e);
                continue;
            }
        }
    }

    let total = forecasts.len();
    Ok(Json(ForecastListResponse { forecasts, total }))
}

/// List forecasts (no database)
///
/// GET /api/v1/forecasts
#[cfg(not(feature = "database"))]
pub async fn list_forecasts(
    State(_state): State<ForecastingState>,
    Query(_params): Query<ListForecastsQuery>,
) -> Result<Json<ForecastListResponse>, StatusCode> {
    Ok(Json(ForecastListResponse {
        forecasts: Vec::new(),
        total: 0,
    }))
}

/// Get service-level forecasts
///
/// GET /api/v1/forecasts/service/{service_id}
#[cfg(feature = "database")]
pub async fn get_service_forecasts(
    State(state): State<ForecastingState>,
    Path(service_id): Path<String>,
    Query(_params): Query<ListForecastsQuery>,
) -> Result<Json<ForecastListResponse>, StatusCode> {
    let pool = match state.database.as_ref().and_then(|db| db.pool()) {
        Some(pool) => pool,
        None => {
            return Ok(Json(ForecastListResponse {
                forecasts: Vec::new(),
                total: 0,
            }));
        }
    };

    // Query forecasts for this service
    let rows = sqlx::query(
        "SELECT * FROM api_change_forecasts
         WHERE service_id = $1 AND expires_at > NOW()
         ORDER BY predicted_at DESC LIMIT 50",
    )
    .bind(&service_id)
    .fetch_all(pool)
    .await
    .map_err(|e| {
        tracing::error!("Failed to query service forecasts: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })?;

    // Map rows to forecasts
    let mut forecasts = Vec::new();
    for row in rows {
        match map_row_to_change_forecast(&row) {
            Ok(forecast) => forecasts.push(forecast),
            Err(e) => {
                tracing::warn!("Failed to map service forecast row: {}", e);
                continue;
            }
        }
    }

    let total = forecasts.len();
    Ok(Json(ForecastListResponse { forecasts, total }))
}

/// Get service-level forecasts (no database)
///
/// GET /api/v1/forecasts/service/{service_id}
#[cfg(not(feature = "database"))]
pub async fn get_service_forecasts(
    State(_state): State<ForecastingState>,
    Path(_service_id): Path<String>,
    Query(_params): Query<ListForecastsQuery>,
) -> Result<Json<ForecastListResponse>, StatusCode> {
    Ok(Json(ForecastListResponse {
        forecasts: Vec::new(),
        total: 0,
    }))
}

/// Get endpoint-level forecasts
///
/// GET /api/v1/forecasts/endpoint/{endpoint}
#[cfg(feature = "database")]
pub async fn get_endpoint_forecasts(
    State(state): State<ForecastingState>,
    Path(endpoint): Path<String>,
    Query(params): Query<ListForecastsQuery>,
) -> Result<Json<ForecastListResponse>, StatusCode> {
    let pool = match state.database.as_ref().and_then(|db| db.pool()) {
        Some(pool) => pool,
        None => {
            return Ok(Json(ForecastListResponse {
                forecasts: Vec::new(),
                total: 0,
            }));
        }
    };

    let method = params.method.as_deref().unwrap_or("%");

    let rows = sqlx::query(
        "SELECT * FROM api_change_forecasts
         WHERE endpoint = $1 AND method LIKE $2 AND expires_at > NOW()
         ORDER BY predicted_at DESC LIMIT 50",
    )
    .bind(&endpoint)
    .bind(method)
    .fetch_all(pool)
    .await
    .map_err(|e| {
        tracing::error!("Failed to query endpoint forecasts: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })?;

    // Map rows to forecasts
    let mut forecasts = Vec::new();
    for row in rows {
        match map_row_to_change_forecast(&row) {
            Ok(forecast) => forecasts.push(forecast),
            Err(e) => {
                tracing::warn!("Failed to map endpoint forecast row: {}", e);
                continue;
            }
        }
    }

    let total = forecasts.len();
    Ok(Json(ForecastListResponse { forecasts, total }))
}

/// Get endpoint-level forecasts (no database)
///
/// GET /api/v1/forecasts/endpoint/{endpoint}
#[cfg(not(feature = "database"))]
pub async fn get_endpoint_forecasts(
    State(_state): State<ForecastingState>,
    Path(_endpoint): Path<String>,
    Query(_params): Query<ListForecastsQuery>,
) -> Result<Json<ForecastListResponse>, StatusCode> {
    Ok(Json(ForecastListResponse {
        forecasts: Vec::new(),
        total: 0,
    }))
}

/// Request to refresh forecasts
#[derive(Debug, Deserialize)]
pub struct RefreshForecastsRequest {
    /// Workspace ID
    pub workspace_id: Option<String>,
    /// Service ID
    pub service_id: Option<String>,
    /// Endpoint (optional)
    pub endpoint: Option<String>,
    /// Method (optional)
    pub method: Option<String>,
}

/// Refresh forecasts
///
/// POST /api/v1/forecasts/refresh
#[cfg(feature = "database")]
pub async fn refresh_forecasts(
    State(state): State<ForecastingState>,
    Json(request): Json<RefreshForecastsRequest>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let pool = match state.database.as_ref().and_then(|db| db.pool()) {
        Some(pool) => pool,
        None => {
            return Ok(Json(serde_json::json!({
                "success": false,
                "error": "Database not available"
            })));
        }
    };

    // Query historical incidents for forecasting
    let mut incident_query = String::from(
        "SELECT id, workspace_id, endpoint, method, incident_type, severity, status,
         detected_at, details, created_at, updated_at
         FROM drift_incidents WHERE 1=1",
    );

    if let Some(_ws_id) = &request.workspace_id {
        incident_query.push_str(" AND workspace_id = $1");
    }

    // Execute query to get incidents
    let rows = sqlx::query(&incident_query).fetch_all(pool).await.map_err(|e| {
        tracing::error!("Failed to query drift incidents: {}", e);
        StatusCode::INTERNAL_SERVER_ERROR
    })?;

    // Map rows to DriftIncident and generate forecasts
    use mockforge_core::incidents::types::{IncidentSeverity, IncidentStatus, IncidentType};
    use sqlx::Row;
    let mut incidents = Vec::new();
    for row in rows {
        let id: uuid::Uuid = row.try_get("id").map_err(|e| {
            tracing::error!("Failed to get id from row: {}", e);
            StatusCode::INTERNAL_SERVER_ERROR
        })?;
        let workspace_id: Option<uuid::Uuid> = row.try_get("workspace_id").ok();
        let endpoint: String = match row.try_get("endpoint") {
            Ok(e) => e,
            Err(_) => continue,
        };
        let method: String = match row.try_get("method") {
            Ok(m) => m,
            Err(_) => continue,
        };
        let incident_type_str: String = match row.try_get("incident_type") {
            Ok(s) => s,
            Err(_) => continue,
        };
        let severity_str: String = match row.try_get("severity") {
            Ok(s) => s,
            Err(_) => continue,
        };
        let status_str: String = match row.try_get("status") {
            Ok(s) => s,
            Err(_) => continue,
        };
        let detected_at: DateTime<Utc> = match row.try_get("detected_at") {
            Ok(dt) => dt,
            Err(_) => continue,
        };
        let details_json: serde_json::Value = row.try_get("details").unwrap_or_default();
        let created_at: DateTime<Utc> = match row.try_get("created_at") {
            Ok(dt) => dt,
            Err(_) => continue,
        };
        let updated_at: DateTime<Utc> = match row.try_get("updated_at") {
            Ok(dt) => dt,
            Err(_) => continue,
        };

        let incident_type = match incident_type_str.as_str() {
            "breaking_change" => IncidentType::BreakingChange,
            "threshold_exceeded" => IncidentType::ThresholdExceeded,
            _ => continue, // Skip invalid types
        };

        let severity = match severity_str.as_str() {
            "low" => IncidentSeverity::Low,
            "medium" => IncidentSeverity::Medium,
            "high" => IncidentSeverity::High,
            "critical" => IncidentSeverity::Critical,
            _ => continue, // Skip invalid severity
        };

        let status = match status_str.as_str() {
            "open" => IncidentStatus::Open,
            "acknowledged" => IncidentStatus::Acknowledged,
            "resolved" => IncidentStatus::Resolved,
            "closed" => IncidentStatus::Closed,
            _ => continue, // Skip invalid status
        };

        incidents.push(DriftIncident {
            id: id.to_string(),
            budget_id: None,
            workspace_id: workspace_id.map(|u| u.to_string()),
            endpoint,
            method,
            incident_type,
            severity,
            status,
            detected_at: detected_at.timestamp(),
            resolved_at: None,
            details: details_json,
            external_ticket_id: None,
            external_ticket_url: None,
            created_at: created_at.timestamp(),
            updated_at: updated_at.timestamp(),
            sync_cycle_id: None,
            contract_diff_id: None,
            before_sample: None,
            after_sample: None,
            fitness_test_results: Vec::new(),
            affected_consumers: None,
            protocol: None,
        });
    }

    // Generate forecasts from incidents by grouping by endpoint/method
    use mockforge_core::incidents::types::DriftIncident;
    use std::collections::HashMap;
    let mut forecasts_generated = 0;
    let mut endpoint_groups: HashMap<(String, String), Vec<DriftIncident>> = HashMap::new();

    for incident in incidents {
        endpoint_groups
            .entry((incident.endpoint.clone(), incident.method.clone()))
            .or_default()
            .push(incident);
    }

    for ((endpoint, method), group_incidents) in endpoint_groups {
        if let Some(forecast) = state.forecaster.generate_forecast(
            &group_incidents,
            request.workspace_id.clone(),
            None, // service_id
            None, // service_name
            endpoint,
            method,
            30, // forecast_window_days
        ) {
            // Persist forecast to database
            if let Err(e) = store_forecast(pool, &forecast, request.workspace_id.as_deref()).await {
                tracing::warn!("Failed to store forecast: {}", e);
            }
            forecasts_generated += 1;
        }
    }

    Ok(Json(serde_json::json!({
        "success": true,
        "message": "Forecasts refreshed",
        "forecasts_generated": forecasts_generated
    })))
}

/// Refresh forecasts (no database)
///
/// POST /api/v1/forecasts/refresh
#[cfg(not(feature = "database"))]
pub async fn refresh_forecasts(
    State(_state): State<ForecastingState>,
    Json(_request): Json<RefreshForecastsRequest>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    Ok(Json(serde_json::json!({
        "success": false,
        "error": "Database not available"
    })))
}

/// Store a forecast in the database
#[cfg(feature = "database")]
pub async fn store_forecast(
    pool: &sqlx::PgPool,
    forecast: &ChangeForecast,
    workspace_id: Option<&str>,
) -> Result<(), sqlx::Error> {
    let id = Uuid::new_v4();
    let workspace_uuid = workspace_id.and_then(|id| Uuid::parse_str(id).ok());

    sqlx::query(
        r#"
        INSERT INTO api_change_forecasts (
            id, workspace_id, service_id, service_name, endpoint, method,
            forecast_window_days, predicted_change_probability, predicted_break_probability,
            next_expected_change_date, next_expected_break_date, volatility_score, confidence,
            seasonal_patterns, predicted_at, expires_at
        ) VALUES (
            $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16
        )
        ON CONFLICT (workspace_id, service_id, endpoint, method, forecast_window_days)
        DO UPDATE SET
            predicted_change_probability = EXCLUDED.predicted_change_probability,
            predicted_break_probability = EXCLUDED.predicted_break_probability,
            next_expected_change_date = EXCLUDED.next_expected_change_date,
            next_expected_break_date = EXCLUDED.next_expected_break_date,
            volatility_score = EXCLUDED.volatility_score,
            confidence = EXCLUDED.confidence,
            seasonal_patterns = EXCLUDED.seasonal_patterns,
            predicted_at = EXCLUDED.predicted_at,
            expires_at = EXCLUDED.expires_at,
            updated_at = NOW()
        "#,
    )
    .bind(id)
    .bind(workspace_uuid)
    .bind(forecast.service_id.as_deref())
    .bind(forecast.service_name.as_deref())
    .bind(&forecast.endpoint)
    .bind(&forecast.method)
    .bind(forecast.forecast_window_days as i32)
    .bind(forecast.predicted_change_probability)
    .bind(forecast.predicted_break_probability)
    .bind(forecast.next_expected_change_date)
    .bind(forecast.next_expected_break_date)
    .bind(forecast.volatility_score)
    .bind(forecast.confidence)
    .bind(serde_json::to_value(&forecast.seasonal_patterns).unwrap_or_default())
    .bind(forecast.predicted_at)
    .bind(forecast.expires_at)
    .execute(pool)
    .await?;

    Ok(())
}

/// Create router for forecasting endpoints
pub fn forecasting_router(state: ForecastingState) -> axum::Router {
    use axum::routing::{get, post};
    use axum::Router;

    Router::new()
        .route("/api/v1/forecasts", get(list_forecasts))
        .route("/api/v1/forecasts/service/{service_id}", get(get_service_forecasts))
        .route("/api/v1/forecasts/endpoint/{endpoint}", get(get_endpoint_forecasts))
        .route("/api/v1/forecasts/refresh", post(refresh_forecasts))
        .with_state(state)
}