Architecture¶
mixpanel_data follows a layered architecture with clear separation of concerns.
Explore on DeepWiki
๐ค Architecture Deep Dive โ
Ask questions about the architecture, trace data flows, or explore component relationships interactively.
Layer Diagram¶
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CLI Layer (Typer) โ
โ Argument parsing, output formatting, progress โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Public API Layer โ
โ Workspace ยท Account/Session ยท accounts/session/targets โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Service Layer โ
โ DiscoveryService, LiveQueryService โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Infrastructure Layer โ
โ ConfigManager, MixpanelAPIClient โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Components¶
Workspace (Facade)¶
The Workspace class is the unified entry point that coordinates all services:
- Session resolution โ Three independent axes resolved via
env > param > target > bridge > [active] > default_project. Single resolver in_internal/auth/resolver.py; no silent cross-axis fallback. - In-session switching โ
Workspace.use(account=, project=, workspace=, target=)returnsselffor chaining and preserves the underlyinghttpx.Clientand per-account/mecache (O(1) per swap). - Service Orchestration โ Creates and manages service instances
- Entity CRUD โ Direct App API access for dashboards, reports, cohorts (workspace-scoped) and feature flags, experiments (project-scoped)
- Data Governance โ Schema registry, enforcement, auditing, volume anomalies, event deletion requests, Lexicon definitions, drop filters, custom properties, custom events, and lookup tables
- Resource Management โ Context manager support for cleanup
Services¶
DiscoveryService¶
Schema introspection with session-scoped caching:
list_events()โ All event names (cached)list_properties(event)โ Properties for an event (cached per event)list_property_values(property, event)โ Sample values (cached)list_funnels()โ Saved funnels (cached)list_cohorts()โ Saved cohorts (cached)list_top_events()โ Today's top events (NOT cached, real-time)
LiveQueryService¶
Executes live analytics queries against Mixpanel Query API:
- Segmentation, funnels, retention, JQL
- Event counts, property counts
- Activity feed, saved reports, flows, frequency
- Numeric aggregations (bucket, sum, average)
Infrastructure¶
ConfigManager¶
TOML-based account management at ~/.mp/config.toml (single schema; legacy v1/v2 do not load):
- Account CRUD over
[accounts.NAME]blocks - Target CRUD over
[targets.NAME]blocks - Active-session read/write over the
[active]block (account + optional workspace) - Atomic writes via temp-file + rename
MixpanelAPIClient¶
HTTP client with Mixpanel-specific features:
- Service account authentication
- Regional endpoint routing (US, EU, India)
- Automatic rate limit handling with exponential backoff
- Streaming JSONL parsing for large exports
Three-Axis Hierarchy¶
The 0.4.0 redesign organizes auth around three independent axes:
- Account โ who is authenticating. Three first-class types managed through one surface:
service_account(Basic Auth),oauth_browser(PKCE flow, tokens auto-refreshed),oauth_token(static bearer for CI/agents). - Project โ which Mixpanel project the calls run against. Lives on the active account as
default_project; can be overridden per call. - Workspace โ which workspace inside the project. Optional; lazy-resolves to the project's default workspace on first workspace-scoped call.
Persisted (account, project, optional workspace) bundles are called targets and act as named cursor positions: mp target add ecom --account team --project 3018488 then mp target use ecom.
Data Paths¶
Live Query Path¶
User Request โ Workspace โ LiveQueryService โ MixpanelAPIClient โ Mixpanel API
โ
Typed Result (e.g., SegmentationResult)
Best for:
- Real-time data needs
- One-off analysis
- Pre-computed Mixpanel reports
Streaming Path¶
User Request โ Workspace โ MixpanelAPIClient โ Mixpanel Export API
โ
Iterator[dict] (no storage)
โ
Process each record inline
Best for:
- ETL pipelines to external systems
- One-time processing without storage
- Memory-constrained environments
- Unix pipeline integration (CLI
--stdout)
Key Design Decisions¶
Streaming Data Access¶
The API client returns iterators for memory-efficient processing of large datasets without loading everything into memory.
Immutable Session¶
A Session (account + project + optional workspace) is resolved once at Workspace construction; Workspace.use() swaps in a new Session atomically. The httpx.Client and per-account /me cache are preserved across swaps, so cross-project iteration is O(1) per turn.
Dependency Injection¶
All services accept their dependencies as constructor arguments. This enables:
- Easy testing with mocks
- Flexible composition
- Clear dependency relationships
Technology Stack¶
| Component | Technology | Purpose |
|---|---|---|
| Language | Python 3.10+ | Type hints, modern syntax |
| CLI Framework | Typer | Declarative CLI building |
| Output Formatting | Rich | Tables, progress bars, colors |
| Validation | Pydantic | Data validation, settings |
| HTTP Client | httpx | Async-capable HTTP |
Package Structure¶
src/mixpanel_data/
โโโ __init__.py # Public exports (Workspace, Account, Session, namespaces, exceptions, types)
โโโ workspace.py # Workspace facade with Workspace.use()
โโโ auth_types.py # Public auth surface (Account union, Session, Region, OAuthTokens, BridgeFile, ...)
โโโ accounts.py # mp.accounts namespace (add/list/use/login/test/...)
โโโ session.py # mp.session namespace (show/use)
โโโ targets.py # mp.targets namespace (saved cursors)
โโโ exceptions.py # Exception hierarchy (incl. AccountInUseError, WorkspaceScopeError)
โโโ types.py # Result dataclasses (SegmentationResult, AccountSummary, Target, ...)
โโโ py.typed # PEP 561 marker
โโโ _internal/
โ โโโ config.py # ConfigManager (single TOML schema)
โ โโโ api_client.py # MixpanelAPIClient
โ โโโ me.py # MeService + per-account MeCache
โ โโโ pagination.py # Cursor-based App API pagination
โ โโโ auth/
โ โ โโโ account.py # Account variants (ServiceAccount/OAuthBrowserAccount/OAuthTokenAccount)
โ โ โโโ session.py # Session, Project, WorkspaceRef, ActiveSession
โ โ โโโ resolver.py # env > param > target > bridge > [active] resolver
โ โ โโโ token_resolver.py# OnDiskTokenResolver
โ โ โโโ token.py # OAuthTokens, OAuthClientInfo
โ โ โโโ flow.py # OAuth PKCE browser flow
โ โ โโโ bridge.py # Cowork bridge file v2
โ โ โโโ storage.py # account_dir, ensure_account_dir (atomic 0o600 writes)
โ โ โโโ pkce.py # PKCE challenge generation (RFC 7636)
โ โ โโโ callback_server.py # Local HTTP callback server
โ โ โโโ client_registration.py # Dynamic Client Registration (RFC 7591)
โ โโโ services/
โ โโโ discovery.py # DiscoveryService
โ โโโ live_query.py # LiveQueryService
โโโ cli/
โโโ main.py # Typer app + global flags (-a / -p / -w / -t)
โโโ commands/ # account / project / workspace / target / session + query / inspect / ...
โโโ formatters.py # Output formatters
โโโ utils.py # CLI utilities