Skip to main content

systemprompt_analytics/models/
mod.rs

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