Skip to main content

systemprompt_analytics/models/
funnel.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use sqlx::FromRow;
4use systemprompt_identifiers::{FunnelId, FunnelProgressId, SessionId};
5
6#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
7pub struct Funnel {
8    pub id: FunnelId,
9    pub name: String,
10    pub description: Option<String>,
11    pub is_active: bool,
12    pub created_at: DateTime<Utc>,
13    pub updated_at: DateTime<Utc>,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
17pub struct FunnelStep {
18    pub funnel_id: FunnelId,
19    pub step_order: i32,
20    pub name: String,
21    pub match_pattern: String,
22    pub match_type: FunnelMatchType,
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, sqlx::Type)]
26#[sqlx(type_name = "text", rename_all = "snake_case")]
27#[serde(rename_all = "snake_case")]
28pub enum FunnelMatchType {
29    UrlExact,
30    UrlPrefix,
31    UrlRegex,
32    EventType,
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
36pub struct FunnelProgress {
37    pub id: FunnelProgressId,
38    pub funnel_id: FunnelId,
39    pub session_id: SessionId,
40    pub current_step: i32,
41    pub completed_at: Option<DateTime<Utc>>,
42    pub dropped_at_step: Option<i32>,
43    pub step_timestamps: serde_json::Value,
44    pub created_at: DateTime<Utc>,
45    pub updated_at: DateTime<Utc>,
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct CreateFunnelInput {
50    pub name: String,
51    pub description: Option<String>,
52    pub steps: Vec<CreateFunnelStepInput>,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct CreateFunnelStepInput {
57    pub name: String,
58    pub match_pattern: String,
59    pub match_type: FunnelMatchType,
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct FunnelWithSteps {
64    pub funnel: Funnel,
65    pub steps: Vec<FunnelStep>,
66}
67
68#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
69pub struct FunnelStepStats {
70    pub step_order: i32,
71    pub entered_count: i64,
72    pub exited_count: i64,
73    pub conversion_rate: f64,
74    pub avg_time_to_next_ms: Option<i64>,
75}
76
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct FunnelStats {
79    pub funnel_id: FunnelId,
80    pub funnel_name: String,
81    pub total_entries: i64,
82    pub total_completions: i64,
83    pub overall_conversion_rate: f64,
84    pub step_stats: Vec<FunnelStepStats>,
85}