Skip to main content

systemprompt_analytics/models/
mod.rs

1//! Public DTO/row types for the analytics crate. Submodules are split by
2//! domain (engagement, events, fingerprint, funnel) plus a `cli` family of
3//! row structs used by `systemprompt-cli` reports.
4
5pub mod cli;
6mod engagement;
7mod events;
8mod fingerprint;
9mod funnel;
10
11pub use cli::*;
12pub use engagement::{CreateEngagementEventInput, EngagementEvent, EngagementOptionalMetrics};
13pub use events::{
14    AnalyticsEventBatchResponse, AnalyticsEventCreated, AnalyticsEventType, ConversionEventData,
15    CreateAnalyticsEventBatchInput, CreateAnalyticsEventInput, EngagementEventData,
16    LinkClickEventData, ScrollEventData,
17};
18pub use fingerprint::{FingerprintAnalysisResult, FingerprintReputation, FlagReason};
19pub use funnel::{
20    CreateFunnelInput, CreateFunnelStepInput, Funnel, FunnelMatchType, FunnelProgress, FunnelStats,
21    FunnelStep, FunnelStepStats, FunnelWithSteps,
22};
23
24use chrono::{DateTime, Utc};
25use serde::{Deserialize, Serialize};
26use sqlx::FromRow;
27use systemprompt_identifiers::{ContextId, SessionId, UserId};
28
29#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
30pub struct UserMetricsWithTrends {
31    #[serde(rename = "users_24h")]
32    pub count_24h: i64,
33    #[serde(rename = "users_7d")]
34    pub count_7d: i64,
35    #[serde(rename = "users_30d")]
36    pub count_30d: i64,
37    #[serde(rename = "users_prev_24h")]
38    pub prev_24h: i64,
39    #[serde(rename = "users_prev_7d")]
40    pub prev_7d: i64,
41    #[serde(rename = "users_prev_30d")]
42    pub prev_30d: i64,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
46pub struct RecentConversation {
47    pub context_id: ContextId,
48    pub agent_name: String,
49    pub user_name: String,
50    pub status: String,
51    pub message_count: i64,
52    pub started_at: DateTime<Utc>,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
56pub struct ContentStat {
57    pub title: String,
58    pub slug: String,
59    pub views_5m: i64,
60    pub views_1h: i64,
61    pub views_1d: i64,
62    pub views_7d: i64,
63    pub views_30d: i64,
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
67pub struct AnalyticsSession {
68    pub session_id: SessionId,
69    pub user_id: Option<UserId>,
70    pub fingerprint_hash: Option<String>,
71    pub ip_address: Option<String>,
72    pub user_agent: Option<String>,
73    pub device_type: Option<String>,
74    pub browser: Option<String>,
75    pub os: Option<String>,
76    pub country: Option<String>,
77    pub city: Option<String>,
78    pub referrer_url: Option<String>,
79    pub utm_source: Option<String>,
80    pub utm_medium: Option<String>,
81    pub utm_campaign: Option<String>,
82    pub utm_content: Option<String>,
83    pub utm_term: Option<String>,
84    pub is_bot: bool,
85    pub is_scanner: Option<bool>,
86    pub is_behavioral_bot: Option<bool>,
87    pub behavioral_bot_reason: Option<String>,
88    pub started_at: Option<DateTime<Utc>>,
89    pub last_activity_at: Option<DateTime<Utc>>,
90    pub ended_at: Option<DateTime<Utc>>,
91    pub request_count: Option<i32>,
92    pub task_count: Option<i32>,
93    pub ai_request_count: Option<i32>,
94    pub message_count: Option<i32>,
95}
96
97#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
98pub struct AnalyticsEvent {
99    pub id: String,
100    pub event_type: String,
101    pub event_category: String,
102    pub severity: String,
103    pub user_id: UserId,
104    pub session_id: Option<SessionId>,
105    pub message: Option<String>,
106    pub metadata: Option<String>,
107    pub timestamp: DateTime<Utc>,
108}
109
110#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
111pub struct ErrorSummary {
112    pub error_type: String,
113    pub count: i64,
114    pub last_occurred: DateTime<Utc>,
115    pub sample_message: Option<String>,
116}
117
118#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
119pub struct PlatformOverview {
120    pub total_users: i64,
121    pub active_users_24h: i64,
122    pub active_users_7d: i64,
123    pub total_sessions: i64,
124    pub active_sessions: i64,
125    pub total_contexts: i64,
126    pub total_tasks: i64,
127    pub total_ai_requests: i64,
128}
129
130#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
131pub struct CostOverview {
132    pub total_cost: f64,
133    pub cost_24h: f64,
134    pub cost_7d: f64,
135    pub cost_30d: f64,
136    pub avg_cost_per_request: f64,
137}
138
139#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
140pub struct ActivityTrend {
141    pub date: DateTime<Utc>,
142    pub sessions: i64,
143    pub contexts: i64,
144    pub tasks: i64,
145    pub ai_requests: i64,
146    pub tool_executions: i64,
147}
148
149#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
150pub struct TopUser {
151    pub user_id: UserId,
152    pub user_name: String,
153    pub session_count: i64,
154    pub task_count: i64,
155    pub ai_request_count: i64,
156    pub total_cost: f64,
157}
158
159#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
160pub struct TopAgent {
161    pub agent_name: String,
162    pub task_count: i64,
163    pub success_rate: f64,
164    pub avg_duration_ms: i64,
165}
166
167#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
168pub struct TopTool {
169    pub tool_name: String,
170    pub execution_count: i64,
171    pub success_rate: f64,
172    pub avg_duration_ms: i64,
173}
174
175#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
176pub struct TrafficSummary {
177    pub total_sessions: i64,
178    pub unique_visitors: i64,
179    pub page_views: i64,
180    pub avg_session_duration_seconds: f64,
181    pub bounce_rate: f64,
182}
183
184#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
185pub struct TrafficSource {
186    pub source: String,
187    pub sessions: i64,
188    pub percentage: f64,
189}
190
191#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
192pub struct DeviceBreakdown {
193    pub device_type: String,
194    pub count: i64,
195    pub percentage: f64,
196}
197
198#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
199pub struct BrowserBreakdown {
200    pub browser: String,
201    pub count: i64,
202    pub percentage: f64,
203}
204
205#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
206pub struct GeographicBreakdown {
207    pub country: String,
208    pub count: i64,
209    pub percentage: f64,
210}
211
212#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, FromRow)]
213pub struct BotTrafficStats {
214    pub total_requests: i64,
215    pub bot_requests: i64,
216    pub human_requests: i64,
217    pub bot_percentage: f64,
218}
219
220#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
221pub struct ConversationSummary {
222    pub total_conversations: i64,
223    pub active_conversations: i64,
224    pub completed_conversations: i64,
225    pub avg_messages_per_conversation: f64,
226    pub avg_duration_minutes: f64,
227}
228
229#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
230pub struct ConversationTrend {
231    pub date: DateTime<Utc>,
232    pub new_conversations: i64,
233    pub completed_conversations: i64,
234    pub total_messages: i64,
235}
236
237#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
238pub struct ConversationByAgent {
239    pub agent_name: String,
240    pub conversation_count: i64,
241    pub avg_messages: f64,
242    pub success_rate: f64,
243}