Skip to main content

mockforge_intelligence/failure_analysis/
context_collector.rs

1//! Failure context collector
2//!
3//! Collects comprehensive context about a request failure, including
4//! active configurations, rules, and execution details.
5
6use chrono::Utc;
7use mockforge_foundation::Result;
8use serde_json::Value;
9use std::collections::HashMap;
10
11use super::types::*;
12
13/// Collector for failure context
14#[derive(Debug)]
15pub struct FailureContextCollector;
16
17impl FailureContextCollector {
18    /// Create a new failure context collector
19    pub fn new() -> Self {
20        Self
21    }
22
23    /// Collect failure context from request execution details.
24    ///
25    /// This method builds a normalized context object from the provided
26    /// request/response inputs with safe defaults for optional integrations.
27    pub fn collect_context(
28        &self,
29        method: &str,
30        path: &str,
31        status_code: Option<u16>,
32        error_message: Option<String>,
33    ) -> Result<FailureContext> {
34        // Build request details
35        let request = RequestDetails {
36            method: method.to_string(),
37            path: path.to_string(),
38            headers: HashMap::new(),
39            query_params: HashMap::new(),
40            body: None,
41        };
42
43        // Build response details if status code is available
44        let response = status_code.map(|code| ResponseDetails {
45            status_code: code,
46            headers: HashMap::new(),
47            body: None,
48            duration_ms: None,
49        });
50
51        // For now, return a basic context structure
52        // In a full implementation, this would collect from:
53        // - Chaos config registry
54        // - Consistency rule engine
55        // - Contract validation system
56        // - Behavioral rule system
57        // - Hook execution tracker
58        Ok(FailureContext {
59            request,
60            response,
61            chaos_configs: Vec::new(),
62            consistency_rules: Vec::new(),
63            contract_validation: None,
64            behavioral_rules: Vec::new(),
65            hook_results: Vec::new(),
66            error_message,
67            timestamp: Utc::now(),
68        })
69    }
70
71    /// Collect context with additional details
72    #[allow(clippy::too_many_arguments)]
73    pub fn collect_context_with_details(
74        &self,
75        method: &str,
76        path: &str,
77        headers: HashMap<String, String>,
78        query_params: HashMap<String, String>,
79        body: Option<Value>,
80        status_code: Option<u16>,
81        response_headers: HashMap<String, String>,
82        response_body: Option<Value>,
83        duration_ms: Option<u64>,
84        error_message: Option<String>,
85        chaos_configs: Vec<ChaosConfigInfo>,
86        consistency_rules: Vec<ConsistencyRuleInfo>,
87        contract_validation: Option<ContractValidationInfo>,
88        behavioral_rules: Vec<BehavioralRuleInfo>,
89        hook_results: Vec<HookExecutionInfo>,
90    ) -> Result<FailureContext> {
91        let request = RequestDetails {
92            method: method.to_string(),
93            path: path.to_string(),
94            headers,
95            query_params,
96            body,
97        };
98
99        let response = status_code.map(|code| ResponseDetails {
100            status_code: code,
101            headers: response_headers,
102            body: response_body,
103            duration_ms,
104        });
105
106        Ok(FailureContext {
107            request,
108            response,
109            chaos_configs,
110            consistency_rules,
111            contract_validation,
112            behavioral_rules,
113            hook_results,
114            error_message,
115            timestamp: Utc::now(),
116        })
117    }
118}
119
120impl Default for FailureContextCollector {
121    fn default() -> Self {
122        Self::new()
123    }
124}