# Templates / Heartrate Documentation – TypeScript ## Included Files 1. templates/heartrate/heartrate.mdx ## Live Heart Rate Monitoring Template Source: templates/heartrate/heartrate.mdx Build a real-time health analytics dashboard with Moose, Streamlit, and Python # Live Heart Rate Leaderboard Template ## Overview The Live Heart Rate Leaderboard template demonstrates how to build a real-time health monitoring application using Moose. It features a Streamlit-based dashboard that displays live heart rate data, calculates performance metrics, and maintains a competitive leaderboard. ## Features - Real-time heart rate monitoring dashboard with interactive graphs - Live leaderboard tracking multiple users - Heart rate zone visualization - Performance metrics calculation (power output and calories burned) - User-specific data tracking and visualization ## Architecture ### Moose Data Pipeline Backend The template implements a three-stage data processing pipeline: 1. **Raw Data Ingestion** (`RawAntHRPacket`) - Captures raw heart rate data from ANT+ devices - Includes basic device and timestamp information 2. **Data Processing** (`ProcessedAntHRPacket`) - Transforms raw data into processed format - Adds calculated metrics and validation 3. **Unified Format** (`UnifiedHRPacket`) - Standardizes heart rate data for analytics - Includes user information and derived metrics ### Streamlit Frontend Dashboard The Streamlit dashboard (`streamlit_app.py`) provides: - Real-time heart rate visualization - Performance metrics display - Interactive user selection - Live-updating leaderboard - Heart rate zone indicators ## Getting Started Python 3.12+ Moose CLI ```bash bash -i <(curl -fsSL https://fiveonefour.com/install.sh) moose ``` 1. Create a new Moose project from the template: ```bash moose init moose-heartrate live-heartrate-leaderboard cd moose-heartrate ``` 2. Create a new virtual environment and install the dependencies: ```bash python -m venv venv source venv/bin/activate pip install -r requirements.txt ``` 3. Configure your environment variables in `.env` 4. Start the Moose pipeline: ```bash moose dev ``` 5. Launch the Streamlit dashboard: ```bash streamlit run streamlit_app.py ``` ## API Endpoints The template exposes two main API endpoints: - `/api/getUserLiveHeartRateStats` - Parameters: user_name, window_seconds - Returns: Recent heart rate statistics for a specific user - `/api/getLeaderboard` - Parameters: time_window_seconds, limit - Returns: Ranked list of users based on performance metrics ## Data Models ### UnifiedHRPacket ```python from moose_lib import Key from pydantic import BaseModel from datetime import datetime class UnifiedHRPacket(BaseModel): user_id: Key[int] user_name: str device_id: int hr_timestamp_seconds: float hr_value: float rr_interval_ms: float processed_timestamp: datetime ``` ## Performance Calculations The template includes calculations for: - Heart rate zones (1-5) - Estimated power output - Cumulative calories burned - Average performance metrics ## Customization You can customize the template by: - Modifying heart rate zone thresholds - Adjusting performance calculation formulas - Extending the data pipeline with additional metrics - Customizing the dashboard layout and visualizations