Skip to main content

kontext_dev_sdk/management/
traces.rs

1//! Traces and events resources.
2
3use std::sync::Arc;
4
5use kontext_dev_sdk_core::types::{
6    ListEventsParams, ListEventsResponse, ListTracesParams, ListTracesResponse, TraceStatsParams,
7    TraceStatsResponse,
8};
9
10use crate::http_client::{HttpClient, HttpError, RequestOptions, RequestParams};
11
12#[derive(Clone)]
13pub struct TracesResource {
14    http: Arc<HttpClient>,
15}
16
17#[derive(Clone)]
18pub struct EventsResource {
19    http: Arc<HttpClient>,
20}
21
22impl TracesResource {
23    pub fn new(http: Arc<HttpClient>) -> Self {
24        Self { http }
25    }
26
27    pub async fn list(
28        &self,
29        params: Option<ListTracesParams>,
30    ) -> Result<ListTracesResponse, HttpError> {
31        let options = params.map(|p| {
32            let mut query = RequestParams::new();
33            if let Some(limit) = p.limit {
34                query.insert("limit".to_string(), limit.to_string());
35            }
36            if let Some(cursor) = p.cursor {
37                query.insert("cursor".to_string(), cursor);
38            }
39            if let Some(session_id) = p.session_id {
40                query.insert("sessionId".to_string(), session_id);
41            }
42            if let Some(agent_id) = p.agent_id {
43                query.insert("agentId".to_string(), agent_id);
44            }
45            if let Some(owner_user_id) = p.owner_user_id {
46                query.insert("ownerUserId".to_string(), owner_user_id);
47            }
48            if let Some(started_after) = p.started_after {
49                query.insert("startedAfter".to_string(), started_after);
50            }
51            if let Some(started_before) = p.started_before {
52                query.insert("startedBefore".to_string(), started_before);
53            }
54            RequestOptions {
55                params: Some(query),
56                headers: None,
57            }
58        });
59
60        self.http.get("/traces", options).await
61    }
62
63    pub async fn stats(
64        &self,
65        params: Option<TraceStatsParams>,
66    ) -> Result<TraceStatsResponse, HttpError> {
67        let options = params.map(|p| {
68            let mut query = RequestParams::new();
69            if let Some(session_id) = p.session_id {
70                query.insert("sessionId".to_string(), session_id);
71            }
72            if let Some(agent_id) = p.agent_id {
73                query.insert("agentId".to_string(), agent_id);
74            }
75            if let Some(started_after) = p.started_after {
76                query.insert("startedAfter".to_string(), started_after);
77            }
78            if let Some(started_before) = p.started_before {
79                query.insert("startedBefore".to_string(), started_before);
80            }
81            RequestOptions {
82                params: Some(query),
83                headers: None,
84            }
85        });
86
87        self.http.get("/traces/stats", options).await
88    }
89}
90
91impl EventsResource {
92    pub fn new(http: Arc<HttpClient>) -> Self {
93        Self { http }
94    }
95
96    pub async fn list(
97        &self,
98        params: Option<ListEventsParams>,
99    ) -> Result<ListEventsResponse, HttpError> {
100        let options = params.map(|p| {
101            let mut query = RequestParams::new();
102            if let Some(limit) = p.limit {
103                query.insert("limit".to_string(), limit.to_string());
104            }
105            if let Some(cursor) = p.cursor {
106                query.insert("cursor".to_string(), cursor);
107            }
108            if let Some(session_id) = p.session_id {
109                query.insert("sessionId".to_string(), session_id);
110            }
111            if let Some(trace_id) = p.trace_id {
112                query.insert("traceId".to_string(), trace_id);
113            }
114            if let Some(agent_id) = p.agent_id {
115                query.insert("agentId".to_string(), agent_id);
116            }
117            if let Some(event_type) = p.event_type {
118                query.insert("eventType".to_string(), event_type);
119            }
120            if let Some(status) = p.status {
121                query.insert("status".to_string(), status);
122            }
123            if let Some(started_after) = p.started_after {
124                query.insert("startedAfter".to_string(), started_after);
125            }
126            if let Some(started_before) = p.started_before {
127                query.insert("startedBefore".to_string(), started_before);
128            }
129            RequestOptions {
130                params: Some(query),
131                headers: None,
132            }
133        });
134
135        self.http.get("/events", options).await
136    }
137}