1use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "lowercase")]
10pub enum Granularity {
11 Minute,
12 Hour,
13 Day,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
18pub struct MetricsAggregate {
19 pub id: Option<i64>,
20 pub timestamp: i64,
21 pub protocol: String,
22 pub method: Option<String>,
23 pub endpoint: Option<String>,
24 pub status_code: Option<i32>,
25 pub workspace_id: Option<String>,
26 pub environment: Option<String>,
27 pub request_count: i64,
28 pub error_count: i64,
29 pub latency_sum: f64,
30 pub latency_min: Option<f64>,
31 pub latency_max: Option<f64>,
32 pub latency_p50: Option<f64>,
33 pub latency_p95: Option<f64>,
34 pub latency_p99: Option<f64>,
35 pub bytes_sent: i64,
36 pub bytes_received: i64,
37 pub active_connections: Option<i64>,
38 pub created_at: Option<i64>,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
43pub struct HourMetricsAggregate {
44 pub id: Option<i64>,
45 pub timestamp: i64,
46 pub protocol: String,
47 pub method: Option<String>,
48 pub endpoint: Option<String>,
49 pub status_code: Option<i32>,
50 pub workspace_id: Option<String>,
51 pub environment: Option<String>,
52 pub request_count: i64,
53 pub error_count: i64,
54 pub latency_sum: f64,
55 pub latency_min: Option<f64>,
56 pub latency_max: Option<f64>,
57 pub latency_p50: Option<f64>,
58 pub latency_p95: Option<f64>,
59 pub latency_p99: Option<f64>,
60 pub bytes_sent: i64,
61 pub bytes_received: i64,
62 pub active_connections_avg: Option<f64>,
63 pub active_connections_max: Option<i64>,
64 pub created_at: Option<i64>,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
69pub struct DayMetricsAggregate {
70 pub id: Option<i64>,
71 pub date: String,
72 pub timestamp: i64,
73 pub protocol: String,
74 pub method: Option<String>,
75 pub endpoint: Option<String>,
76 pub status_code: Option<i32>,
77 pub workspace_id: Option<String>,
78 pub environment: Option<String>,
79 pub request_count: i64,
80 pub error_count: i64,
81 pub latency_sum: f64,
82 pub latency_min: Option<f64>,
83 pub latency_max: Option<f64>,
84 pub latency_p50: Option<f64>,
85 pub latency_p95: Option<f64>,
86 pub latency_p99: Option<f64>,
87 pub bytes_sent: i64,
88 pub bytes_received: i64,
89 pub active_connections_avg: Option<f64>,
90 pub active_connections_max: Option<i64>,
91 pub unique_clients: Option<i64>,
92 pub peak_hour: Option<i32>,
93 pub created_at: Option<i64>,
94}
95
96#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
98pub struct EndpointStats {
99 pub id: Option<i64>,
100 pub endpoint: String,
101 pub protocol: String,
102 pub method: Option<String>,
103 pub workspace_id: Option<String>,
104 pub environment: Option<String>,
105 pub total_requests: i64,
106 pub total_errors: i64,
107 pub avg_latency_ms: Option<f64>,
108 pub min_latency_ms: Option<f64>,
109 pub max_latency_ms: Option<f64>,
110 pub p95_latency_ms: Option<f64>,
111 pub status_codes: Option<String>, pub total_bytes_sent: i64,
113 pub total_bytes_received: i64,
114 pub first_seen: i64,
115 pub last_seen: i64,
116 pub updated_at: Option<i64>,
117}
118
119#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct StatusCodeBreakdown {
122 pub status_codes: HashMap<u16, i64>,
123}
124
125impl EndpointStats {
126 pub fn get_status_code_breakdown(&self) -> Result<StatusCodeBreakdown, serde_json::Error> {
128 if let Some(ref json) = self.status_codes {
129 let map: HashMap<String, i64> = serde_json::from_str(json)?;
130 let status_codes = map
131 .into_iter()
132 .filter_map(|(k, v)| k.parse::<u16>().ok().map(|code| (code, v)))
133 .collect();
134 Ok(StatusCodeBreakdown { status_codes })
135 } else {
136 Ok(StatusCodeBreakdown {
137 status_codes: HashMap::new(),
138 })
139 }
140 }
141
142 pub fn set_status_code_breakdown(
144 &mut self,
145 breakdown: &StatusCodeBreakdown,
146 ) -> Result<(), serde_json::Error> {
147 let map: HashMap<String, i64> =
148 breakdown.status_codes.iter().map(|(k, v)| (k.to_string(), *v)).collect();
149 self.status_codes = Some(serde_json::to_string(&map)?);
150 Ok(())
151 }
152}
153
154#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
156pub struct ErrorEvent {
157 pub id: Option<i64>,
158 pub timestamp: i64,
159 pub protocol: String,
160 pub method: Option<String>,
161 pub endpoint: Option<String>,
162 pub status_code: Option<i32>,
163 pub error_type: Option<String>,
164 pub error_message: Option<String>,
165 pub error_category: Option<String>,
166 pub request_id: Option<String>,
167 pub trace_id: Option<String>,
168 pub span_id: Option<String>,
169 pub client_ip: Option<String>,
170 pub user_agent: Option<String>,
171 pub workspace_id: Option<String>,
172 pub environment: Option<String>,
173 pub metadata: Option<String>, pub created_at: Option<i64>,
175}
176
177#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
179#[serde(rename_all = "snake_case")]
180pub enum ErrorCategory {
181 ClientError, ServerError, NetworkError,
184 TimeoutError,
185 Other,
186}
187
188impl ErrorCategory {
189 #[must_use]
191 pub const fn from_status_code(status_code: u16) -> Self {
192 match status_code {
193 400..=499 => Self::ClientError,
194 500..=599 => Self::ServerError,
195 _ => Self::Other,
196 }
197 }
198
199 #[must_use]
201 pub const fn as_str(&self) -> &'static str {
202 match self {
203 Self::ClientError => "client_error",
204 Self::ServerError => "server_error",
205 Self::NetworkError => "network_error",
206 Self::TimeoutError => "timeout_error",
207 Self::Other => "other",
208 }
209 }
210}
211
212#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
214pub struct ClientAnalytics {
215 pub id: Option<i64>,
216 pub timestamp: i64,
217 pub client_ip: String,
218 pub user_agent: Option<String>,
219 pub user_agent_family: Option<String>,
220 pub user_agent_version: Option<String>,
221 pub protocol: String,
222 pub workspace_id: Option<String>,
223 pub environment: Option<String>,
224 pub request_count: i64,
225 pub error_count: i64,
226 pub avg_latency_ms: Option<f64>,
227 pub bytes_sent: i64,
228 pub bytes_received: i64,
229 pub top_endpoints: Option<String>, pub created_at: Option<i64>,
231}
232
233#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
235pub struct TrafficPattern {
236 pub id: Option<i64>,
237 pub date: String,
238 pub hour: i32,
239 pub day_of_week: i32,
240 pub protocol: String,
241 pub workspace_id: Option<String>,
242 pub environment: Option<String>,
243 pub request_count: i64,
244 pub error_count: i64,
245 pub avg_latency_ms: Option<f64>,
246 pub unique_clients: Option<i64>,
247 pub created_at: Option<i64>,
248}
249
250#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
252pub struct AnalyticsSnapshot {
253 pub id: Option<i64>,
254 pub timestamp: i64,
255 pub snapshot_type: String,
256 pub total_requests: i64,
257 pub total_errors: i64,
258 pub avg_latency_ms: Option<f64>,
259 pub active_connections: Option<i64>,
260 pub protocol_stats: Option<String>, pub top_endpoints: Option<String>, pub memory_usage_bytes: Option<i64>,
263 pub cpu_usage_percent: Option<f64>,
264 pub thread_count: Option<i32>,
265 pub uptime_seconds: Option<i64>,
266 pub workspace_id: Option<String>,
267 pub environment: Option<String>,
268 pub created_at: Option<i64>,
269}
270
271#[derive(Debug, Clone, Default, Serialize, Deserialize)]
273pub struct AnalyticsFilter {
274 pub start_time: Option<i64>,
275 pub end_time: Option<i64>,
276 pub protocol: Option<String>,
277 pub endpoint: Option<String>,
278 pub method: Option<String>,
279 pub status_code: Option<i32>,
280 pub workspace_id: Option<String>,
281 pub environment: Option<String>,
282 pub limit: Option<i64>,
283}
284
285#[derive(Debug, Clone, Serialize, Deserialize)]
287pub struct OverviewMetrics {
288 pub total_requests: i64,
289 pub total_errors: i64,
290 pub error_rate: f64,
291 pub avg_latency_ms: f64,
292 pub p95_latency_ms: f64,
293 pub p99_latency_ms: f64,
294 pub active_connections: i64,
295 pub total_bytes_sent: i64,
296 pub total_bytes_received: i64,
297 pub requests_per_second: f64,
298 pub top_protocols: Vec<ProtocolStat>,
299 pub top_endpoints: Vec<EndpointStat>,
300}
301
302#[derive(Debug, Clone, Serialize, Deserialize)]
304pub struct ProtocolStat {
305 pub protocol: String,
306 pub request_count: i64,
307 pub error_count: i64,
308 pub avg_latency_ms: f64,
309}
310
311#[derive(Debug, Clone, Serialize, Deserialize)]
313pub struct EndpointStat {
314 pub endpoint: String,
315 pub protocol: String,
316 pub method: Option<String>,
317 pub request_count: i64,
318 pub error_count: i64,
319 pub error_rate: f64,
320 pub avg_latency_ms: f64,
321 pub p95_latency_ms: f64,
322}
323
324#[derive(Debug, Clone, Serialize, Deserialize)]
326pub struct TimeSeriesPoint {
327 pub timestamp: i64,
328 pub value: f64,
329}
330
331#[derive(Debug, Clone, Serialize, Deserialize)]
333pub struct TimeSeries {
334 pub label: String,
335 pub data: Vec<TimeSeriesPoint>,
336}
337
338#[derive(Debug, Clone, Serialize, Deserialize)]
340pub struct LatencyTrend {
341 pub timestamp: i64,
342 pub p50: f64,
343 pub p95: f64,
344 pub p99: f64,
345 pub avg: f64,
346 pub min: f64,
347 pub max: f64,
348}
349
350#[derive(Debug, Clone, Serialize, Deserialize)]
352pub struct ErrorSummary {
353 pub error_type: String,
354 pub error_category: String,
355 pub count: i64,
356 pub endpoints: Vec<String>,
357 pub last_occurrence: DateTime<Utc>,
358}
359
360#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
362#[serde(rename_all = "lowercase")]
363pub enum ExportFormat {
364 Csv,
365 Json,
366}
367
368#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
374pub struct ScenarioUsageMetrics {
375 pub id: Option<i64>,
376 pub scenario_id: String,
377 pub workspace_id: Option<String>,
378 pub org_id: Option<String>,
379 pub usage_count: i64,
380 pub last_used_at: Option<i64>,
381 pub usage_pattern: Option<String>, pub created_at: Option<i64>,
383 pub updated_at: Option<i64>,
384}
385
386#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
388pub struct PersonaCIHit {
389 pub id: Option<i64>,
390 pub persona_id: String,
391 pub workspace_id: Option<String>,
392 pub org_id: Option<String>,
393 pub ci_run_id: Option<String>,
394 pub hit_count: i64,
395 pub hit_at: i64,
396 pub created_at: Option<i64>,
397}
398
399#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
401pub struct EndpointCoverage {
402 pub id: Option<i64>,
403 pub endpoint: String,
404 pub method: Option<String>,
405 pub protocol: String,
406 pub workspace_id: Option<String>,
407 pub org_id: Option<String>,
408 pub test_count: i64,
409 pub last_tested_at: Option<i64>,
410 pub coverage_percentage: Option<f64>,
411 pub created_at: Option<i64>,
412 pub updated_at: Option<i64>,
413}
414
415#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
417pub struct RealityLevelStaleness {
418 pub id: Option<i64>,
419 pub workspace_id: String,
420 pub org_id: Option<String>,
421 pub endpoint: Option<String>,
422 pub method: Option<String>,
423 pub protocol: Option<String>,
424 pub current_reality_level: Option<String>,
425 pub last_updated_at: Option<i64>,
426 pub staleness_days: Option<i32>,
427 pub created_at: Option<i64>,
428 pub updated_at: Option<i64>,
429}
430
431#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
433pub struct DriftPercentageMetrics {
434 pub id: Option<i64>,
435 pub workspace_id: String,
436 pub org_id: Option<String>,
437 pub total_mocks: i64,
438 pub drifting_mocks: i64,
439 pub drift_percentage: f64,
440 pub measured_at: i64,
441 pub created_at: Option<i64>,
442}