This document describes the expected database schema for the MetricWatch anomaly detection tool. The tool has been designed to work with ClickHouse databases but can be adapted for other SQL databases.
Contains core application data including user information and subscriptions.
app.users
id(UInt64) - Unique user identifierdate_registered(DateTime) - User registration timestampdate_joined(DateTime) - User first visit timestampcountry_id(UInt64) - Reference to country dictionarysub_created_at_least(DateTime) - Earliest subscription creation date
app.subscriptions
user_id(UInt64) - Reference to users.idcreated_at(DateTime) - Subscription creation timestamptype(String) - Subscription type ('personal', 'premium', etc.)
app.users_dict (ClickHouse Dictionary)
id(UInt64) - User IDcountry_id(UInt64) - Country identifier
app.events
user_id(UInt64) - Reference to app.users.iddt(DateTime) - Activity timestampdt_registered(DateTime) - User registration timestampdt_joined(DateTime) - User join timestampdate(Date) - Activity dateaction(String) - Activity type ('view', 'completed_submission', 'failed_submission', 'rejected_submission')registration_date(Date) - User registration date
app.sales
user_id(UInt64) - Reference to app.users.idamount_usd_cents(UInt64) - Sale amount in USD centsproduct_type(String) - Product type ('personal', 'premium', etc.)
app.countries_dict (ClickHouse Dictionary)
id(UInt64) - Country IDname(String) - Country name
app.platforms_dict (ClickHouse Dictionary)
id(UInt64) - User IDplatform(String) - Platform name ('iOS', 'Android', 'web')
Used by the tool to store generated views and intermediate results.
The schema supports tracking user journeys through these key events:
- Registration: When users sign up (
date_registered) - First Visit: When users first interact (
date_joined) - Activity: User interactions with content (
user_activity) - Subscriptions: When users subscribe to services
- Sales: Revenue tracking
All tables should include appropriate timestamp fields to support:
- Hourly granularity analysis
- Daily, weekly, and monthly aggregations
- Lag-based metrics (10-day, 14-day lags)
- Retention analysis (2nd week retention)
The schema supports slicing metrics by:
- Country: Geographic segmentation
- Platform: Device/platform segmentation (iOS, Android, web)
- Product Type: Subscription tier analysis
- Create the required databases:
app,metric_watch - Create tables with the schema described above
- Set up ClickHouse dictionaries for efficient lookups
- Ensure proper indexing for time-based queries
- Configure environment variables for database connections
CLICKHOUSE_HOST_URL=http://your-clickhouse-host:8123
CH_USER=your_username
CH_PASSWORD=your_password- This schema is designed for ClickHouse but can be adapted for other SQL databases
- Dictionary tables are ClickHouse-specific and may need alternative implementations in other databases
- Ensure proper data retention policies for large analytics tables
- Consider partitioning strategies for time-series data