Skip to main content

stackless_integrations/providers/mixpanel/
analytics.rs

1//! `mixpanel/analytics` integration.
2
3use std::collections::BTreeMap;
4
5use serde::Serialize;
6use stackless_stripe_projects::catalog::verify::CatalogService;
7use stackless_stripe_projects::provision::ProvisionContext;
8
9use super::FamilyResource;
10use crate::error::IntegrationError;
11use crate::hostable::{ConfigScope, Hostable, IntegrationHosting};
12
13pub const RESOURCE_KIND: &str = "integration-mixpanel";
14
15#[derive(Debug, Serialize)]
16pub struct MixpanelAnalyticsConfig {}
17
18impl CatalogService for MixpanelAnalyticsConfig {
19    const REFERENCE: &'static str = "mixpanel/analytics";
20}
21
22#[derive(Debug)]
23pub struct MixpanelAnalytics;
24
25impl Hostable for MixpanelAnalytics {
26    const PROVIDER: &'static str = "mixpanel";
27    const HOSTING: IntegrationHosting = IntegrationHosting::Managed;
28    const CONFIG_SCOPE: ConfigScope = ConfigScope::GlobalOnly;
29    const RESOURCE_KIND: &'static str = RESOURCE_KIND;
30    const OUTPUTS: &'static [&'static str] = &[
31        "api_url",
32        "ingestion_url",
33        "project_id",
34        "project_token",
35        "service_account_secret",
36        "service_account_username",
37    ];
38}
39
40impl FamilyResource for MixpanelAnalytics {
41    type Config = MixpanelAnalyticsConfig;
42    const PROVIDER_PREFIX: &'static str = "MIXPANEL";
43    const OUTPUT_FIELDS: &'static [(&'static str, &'static str, bool)] = &[
44        ("API_URL", "api_url", true),
45        ("INGESTION_URL", "ingestion_url", true),
46        ("PROJECT_ID", "project_id", true),
47        ("PROJECT_TOKEN", "project_token", true),
48        ("SERVICE_ACCOUNT_SECRET", "service_account_secret", true),
49        ("SERVICE_ACCOUNT_USERNAME", "service_account_username", true),
50    ];
51
52    fn build_config(
53        ctx: &ProvisionContext<'_>,
54    ) -> Result<MixpanelAnalyticsConfig, IntegrationError> {
55        let _ = super::integration_config(ctx)?;
56        Ok(MixpanelAnalyticsConfig {})
57    }
58}
59
60pub fn validate_config(
61    name: &str,
62    config: &BTreeMap<String, toml::Value>,
63) -> Result<(), IntegrationError> {
64    let _ = (name, config);
65    Ok(())
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71    use crate::ProviderOps;
72    use crate::resource::ResourcePayload;
73    use stackless_core::def::StackDef;
74    use stackless_stripe_projects::stripe::StripeProjects;
75    use stackless_stripe_projects::test_support;
76
77    #[test]
78    fn config_matches_catalog() {
79        const FIXTURE: &str = include_str!(concat!(
80            env!("CARGO_MANIFEST_DIR"),
81            "/../stackless-stripe-projects/tests/fixtures/catalog.json"
82        ));
83        let catalog = stackless_stripe_projects::Catalog::from_json_envelope(FIXTURE).unwrap();
84        let failures =
85            stackless_stripe_projects::verify_service(&catalog, &MixpanelAnalyticsConfig {});
86        assert!(
87            failures.is_empty(),
88            "mixpanel/analytics catalog gaps:\n{}",
89            failures.join("\n")
90        );
91    }
92
93    const CATALOG_ENVELOPE: &str = r##"{"ok":true,"command":"projects catalog","data":{"last_updated":"2026-07-11T00:00:00Z","services":[{"id":"prvsvc_analytics","object":"v2.provisioning.provider_service_detail","provider_id":"prvdr_mixpanel","provider_name":"Mixpanel","service_id":"analytics","categories":["database"],"kind":"deployable","scope":"project","availability":"available","development":false,"livemode":true,"pricing":{"type":"component"},"configuration_schema":{"type":"object","required":[],"additionalProperties":false,"properties":{}}}]}}"##;
94
95    fn test_def() -> StackDef {
96        StackDef::parse(
97            r#"
98[stack]
99name = "atto"
100[stack.projects.stripe]
101project = "project_1"
102[integrations.res]
103provider = "mixpanel"
104[services.api]
105source = { repo = "r", ref = "main" }
106env = { OUT = "${integrations.res.project_token}" }
107health = { path = "/health" }
108[services.api.local]
109run = "true"
110"#,
111        )
112        .unwrap()
113    }
114
115    #[tokio::test]
116    async fn provision_records_outputs() {
117        let runner = test_support::provision_script(
118            CATALOG_ENVELOPE,
119            serde_json::json!({"MIXPANEL_API_URL": "val_api_url", "MIXPANEL_INGESTION_URL": "val_ingestion_url", "MIXPANEL_PROJECT_ID": "val_project_id", "MIXPANEL_PROJECT_TOKEN": "val_project_token", "MIXPANEL_SERVICE_ACCOUNT_SECRET": "val_service_account_secret", "MIXPANEL_SERVICE_ACCOUNT_USERNAME": "val_service_account_username"}),
120            0,
121        );
122        let dir = tempfile::tempdir().unwrap();
123        std::fs::write(
124            dir.path().join("stackless.toml"),
125            "[stack]\nname=\"atto\"\n",
126        )
127        .unwrap();
128        let stripe = StripeProjects::new(&runner, dir.path());
129
130        let resource = MixpanelAnalytics
131            .provision(
132                &stripe.as_dyn(),
133                &test_def(),
134                dir.path(),
135                "demo",
136                "res",
137                "local",
138                false,
139            )
140            .await
141            .unwrap();
142        assert_eq!(resource.resource_kind, "integration-mixpanel");
143        let payload: ResourcePayload = serde_json::from_str(&resource.payload).unwrap();
144        assert_eq!(payload.outputs["project_token"], "val_project_token");
145    }
146}