Skip to main content

systemprompt_analytics/
extension.rs

1//! Registers analytics-owned schemas, rollups, report views and migrations.
2//!
3//! That covers the engagement and reputation schemas, the hourly tool and
4//! agent-task rollups, and the report views over source tables. It depends on
5//! `agent` (and through it `ai` and `mcp`) because the rollup triggers sit on
6//! their tables.
7//!
8//! Copyright (c) systemprompt.io — Business Source License 1.1.
9//! See <https://systemprompt.io> for licensing details.
10
11use systemprompt_extension::prelude::*;
12
13#[derive(Debug, Clone, Copy, Default)]
14pub struct AnalyticsExtension;
15
16impl Extension for AnalyticsExtension {
17    fn metadata(&self) -> ExtensionMetadata {
18        ExtensionMetadata {
19            id: "analytics",
20            name: "Analytics",
21            version: env!("CARGO_PKG_VERSION"),
22        }
23    }
24
25    fn schemas(&self) -> Vec<SchemaDefinition> {
26        vec![
27            SchemaDefinition::new(
28                "engagement_events",
29                include_str!("../schema/engagement_events.sql"),
30            )
31            .with_required_columns(vec![
32                "id".into(),
33                "session_id".into(),
34                "created_at".into(),
35            ]),
36            SchemaDefinition::new(
37                "fingerprint_reputation",
38                include_str!("../schema/fingerprint_reputation.sql"),
39            )
40            .with_required_columns(vec!["fingerprint_hash".into()]),
41            SchemaDefinition::new(
42                "tool_usage_rollup_hourly",
43                include_str!("../schema/usage_rollups.sql"),
44            )
45            .with_required_columns(vec!["bucket".into(), "execution_count".into()]),
46            SchemaDefinition::new(
47                "agent_task_rollup_hourly",
48                include_str!("../schema/agent_task_rollup_hourly.sql"),
49            )
50            .with_required_columns(vec!["bucket".into(), "task_count".into()]),
51            SchemaDefinition::sql_only(include_str!("../schema/report_views.sql")),
52        ]
53    }
54
55    fn dependencies(&self) -> Vec<&'static str> {
56        vec!["users", "agent"]
57    }
58
59    fn migrations(&self) -> Vec<Migration> {
60        extension_migrations!()
61    }
62}
63
64register_extension!(AnalyticsExtension);