Skip to main content

systemprompt_analytics/models/
mod.rs

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