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 is_bot: bool,
80    pub is_scanner: Option<bool>,
81    pub is_behavioral_bot: Option<bool>,
82    pub behavioral_bot_reason: Option<String>,
83    pub started_at: Option<DateTime<Utc>>,
84    pub last_activity_at: Option<DateTime<Utc>>,
85    pub ended_at: Option<DateTime<Utc>>,
86    pub request_count: Option<i32>,
87    pub task_count: Option<i32>,
88    pub ai_request_count: Option<i32>,
89    pub message_count: Option<i32>,
90}
91
92#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
93pub struct AnalyticsEvent {
94    pub id: String,
95    pub event_type: String,
96    pub event_category: String,
97    pub severity: String,
98    pub user_id: UserId,
99    pub session_id: Option<SessionId>,
100    pub message: Option<String>,
101    pub metadata: Option<String>,
102    pub timestamp: DateTime<Utc>,
103}
104
105#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
106pub struct ErrorSummary {
107    pub error_type: String,
108    pub count: i64,
109    pub last_occurred: DateTime<Utc>,
110    pub sample_message: Option<String>,
111}
112
113#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
114pub struct PlatformOverview {
115    pub total_users: i64,
116    pub active_users_24h: i64,
117    pub active_users_7d: i64,
118    pub total_sessions: i64,
119    pub active_sessions: i64,
120    pub total_contexts: i64,
121    pub total_tasks: i64,
122    pub total_ai_requests: i64,
123}
124
125#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
126pub struct CostOverview {
127    pub total_cost: f64,
128    pub cost_24h: f64,
129    pub cost_7d: f64,
130    pub cost_30d: f64,
131    pub avg_cost_per_request: f64,
132}
133
134#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
135pub struct ActivityTrend {
136    pub date: DateTime<Utc>,
137    pub sessions: i64,
138    pub contexts: i64,
139    pub tasks: i64,
140    pub ai_requests: i64,
141    pub tool_executions: i64,
142}
143
144#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
145pub struct TopUser {
146    pub user_id: UserId,
147    pub user_name: String,
148    pub session_count: i64,
149    pub task_count: i64,
150    pub ai_request_count: i64,
151    pub total_cost: f64,
152}
153
154#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
155pub struct TopAgent {
156    pub agent_name: String,
157    pub task_count: i64,
158    pub success_rate: f64,
159    pub avg_duration_ms: i64,
160}
161
162#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
163pub struct TopTool {
164    pub tool_name: String,
165    pub execution_count: i64,
166    pub success_rate: f64,
167    pub avg_duration_ms: i64,
168}
169
170#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
171pub struct TrafficSummary {
172    pub total_sessions: i64,
173    pub unique_visitors: i64,
174    pub page_views: i64,
175    pub avg_session_duration_seconds: f64,
176    pub bounce_rate: f64,
177}
178
179#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
180pub struct TrafficSource {
181    pub source: String,
182    pub sessions: i64,
183    pub percentage: f64,
184}
185
186#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
187pub struct DeviceBreakdown {
188    pub device_type: String,
189    pub count: i64,
190    pub percentage: f64,
191}
192
193#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
194pub struct BrowserBreakdown {
195    pub browser: String,
196    pub count: i64,
197    pub percentage: f64,
198}
199
200#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
201pub struct GeographicBreakdown {
202    pub country: String,
203    pub count: i64,
204    pub percentage: f64,
205}
206
207#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, FromRow)]
208pub struct BotTrafficStats {
209    pub total_requests: i64,
210    pub bot_requests: i64,
211    pub human_requests: i64,
212    pub bot_percentage: f64,
213}
214
215#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
216pub struct ConversationSummary {
217    pub total_conversations: i64,
218    pub active_conversations: i64,
219    pub completed_conversations: i64,
220    pub avg_messages_per_conversation: f64,
221    pub avg_duration_minutes: f64,
222}
223
224#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
225pub struct ConversationTrend {
226    pub date: DateTime<Utc>,
227    pub new_conversations: i64,
228    pub completed_conversations: i64,
229    pub total_messages: i64,
230}
231
232#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
233pub struct ConversationByAgent {
234    pub agent_name: String,
235    pub conversation_count: i64,
236    pub avg_messages: f64,
237    pub success_rate: f64,
238}