# **GarminSync Application Design (Python Version)** ## **Basic Info** **App Name:** GarminSync **What it does:** A CLI application that downloads `.fit` files for every activity in Garmin Connect. ----- ## **Core Features** ### **CLI Mode (Implemented)** 1. List all activities (`garminsync list --all`) 2. List activities that have not been downloaded (`garminsync list --missing`) 3. List activities that have been downloaded (`garminsync list --downloaded`) 4. Download all missing activities (`garminsync download --missing`) ### **Enhanced Features (Implemented)** 5. **Offline Mode**: List activities without polling Garmin Connect (`garminsync list --missing --offline`) 6. **Daemon Mode**: Run as background service with scheduled downloads (`garminsync daemon --start`) 7. **Web UI**: Browser-based interface for daemon monitoring and configuration (`http://localhost:8080`) ----- ## **Tech Stack 🐍** * **Frontend:** CLI (**Python** with Typer) + Web UI (FastAPI + Jinja2) * **Backend:** **Python** * **Database:** SQLite (`garmin.db`) * **Hosting:** Docker container * **Key Libraries:** * **`python-garminconnect`**: The library for Garmin Connect API communication. * **`typer`**: A modern and easy-to-use CLI framework (built on `click`). * **`python-dotenv`**: For loading credentials from a `.env` file. * **`sqlalchemy`**: A robust ORM for database interaction and schema management. * **`tqdm`**: For creating user-friendly progress bars. * **`fastapi`**: Modern web framework for the daemon web UI. * **`uvicorn`**: ASGI server for running the FastAPI web interface. * **`apscheduler`**: Advanced Python Scheduler for daemon mode scheduling. * **`pydantic`**: Data validation and settings management for configuration. * **`jinja2`**: Template engine for web UI rendering. ----- ## **Data Structure** The application uses SQLAlchemy ORM with expanded models for daemon functionality: **SQLAlchemy Models (`database.py`):** ```python class Activity(Base): __tablename__ = 'activities' activity_id = Column(Integer, primary_key=True) start_time = Column(String, nullable=False) filename = Column(String, unique=True, nullable=True) downloaded = Column(Boolean, default=False, nullable=False) created_at = Column(String, nullable=False) # When record was added last_sync = Column(String, nullable=True) # Last successful sync class DaemonConfig(Base): __tablename__ = 'daemon_config' id = Column(Integer, primary_key=True, default=1) enabled = Column(Boolean, default=True, nullable=False) schedule_cron = Column(String, default="0 */6 * * *", nullable=False) # Every 6 hours last_run = Column(String, nullable=True) next_run = Column(String, nullable=True) status = Column(String, default="stopped", nullable=False) # stopped, running, error class SyncLog(Base): __tablename__ = 'sync_logs' id = Column(Integer, primary_key=True, autoincrement=True) timestamp = Column(String, nullable=False) operation = Column(String, nullable=False) # sync, download, daemon_start, daemon_stop status = Column(String, nullable=False) # success, error, partial message = Column(String, nullable=True) activities_processed = Column(Integer, default=0, nullable=False) activities_downloaded = Column(Integer, default=0, nullable=False) ``` ----- ## **User Flow** ### **CLI Mode (Implemented)** 1. User sets up credentials in `.env` file with `GARMIN_EMAIL` and `GARMIN_PASSWORD` 2. User launches the container: `docker run -it --env-file .env -v $(pwd)/data:/app/data garminsync` 3. User runs commands like `garminsync download --missing` 4. Application syncs with Garmin Connect, shows progress bars, and downloads activities ### **Offline Mode (Implemented)** 1. User runs `garminsync list --missing --offline` to view cached data without API calls 2. Application queries local database only, showing last known state 3. Useful for checking status without network connectivity or API rate limits ### **Daemon Mode (Implemented)** 1. User starts daemon: `garminsync daemon` (runs continuously in foreground) 2. Daemon automatically starts web UI and background scheduler 3. User accesses web UI at `http://localhost:8080` for monitoring and configuration 4. Web UI provides real-time status, logs, and schedule management 5. Daemon can be stopped with `Ctrl+C` or through web UI stop functionality ----- ## **File Structure** ``` /garminsync ├── garminsync/ # Main application package │ ├── __init__.py # Empty package file │ ├── cli.py # Typer CLI commands and main entrypoint │ ├── config.py # Configuration and environment variable loading │ ├── database.py # SQLAlchemy models and database operations │ ├── garmin.py # Garmin Connect client wrapper with robust download logic │ ├── daemon.py # Daemon mode implementation with APScheduler │ ├── utils.py # Shared utilities and helpers │ └── web/ # Web UI components │ ├── __init__.py │ ├── app.py # FastAPI application setup │ ├── routes.py # API endpoints for web UI │ ├── static/ # CSS, JavaScript, images │ │ ├── style.css │ │ └── app.js │ └── templates/ # Jinja2 HTML templates │ ├── base.html │ ├── dashboard.html │ └── config.html ├── data/ # Directory for downloaded .fit files and SQLite DB ├── .env # Stores GARMIN_EMAIL/GARMIN_PASSWORD (gitignored) ├── .gitignore # Excludes .env file and data directory ├── Dockerfile # Production-ready container configuration ├── Design.md # This design document ├── plan.md # Implementation notes and fixes └── requirements.txt # Python dependencies with compatibility fixes ``` ----- ## **Technical Implementation Details** ### **Architecture** - **CLI Framework**: Uses Typer with proper type hints and validation - **Module Separation**: Clear separation between CLI commands, database operations, and Garmin API interactions - **Error Handling**: Comprehensive exception handling with user-friendly error messages - **Session Management**: Proper SQLAlchemy session management with cleanup ### **Authentication & Configuration** - Credentials loaded via `python-dotenv` from environment variables - Configuration validation ensures required credentials are present - Garmin client handles authentication automatically with session persistence ### **Database Operations** - SQLite database with SQLAlchemy ORM for type safety - Database initialization creates tables automatically - Sync functionality reconciles local database with Garmin Connect activities - Proper transaction management with rollback on errors ### **File Management** - Files named with pattern: `activity_{activity_id}_{timestamp}.fit` - Timestamp sanitized for filesystem compatibility (colons and spaces replaced) - Downloads saved to configurable data directory - Database tracks both download status and file paths ### **API Integration** - **Rate Limiting**: 2-second delays between API requests to respect Garmin's servers - **Robust Downloads**: Multiple fallback methods for downloading FIT files: 1. Default download method 2. Explicit 'fit' format parameter 3. Alternative parameter names and formats 4. Graceful fallback with detailed error reporting - **Activity Fetching**: Configurable batch sizes (currently 1000 activities per sync) ### **User Experience** - **Progress Indicators**: tqdm progress bars for all long-running operations - **Informative Output**: Clear status messages and operation summaries - **Input Validation**: Prevents invalid command combinations - **Exit Codes**: Proper exit codes for script integration ----- ## **Development Status ✅** ### **✅ Completed Features** #### **Phase 1: Core Infrastructure** - [x] **Dockerfile**: Production-ready Python 3.10 container with proper layer caching - [x] **Environment Configuration**: `python-dotenv` integration with validation - [x] **CLI Framework**: Complete Typer implementation with type hints and help text - [x] **Garmin Integration**: Robust `python-garminconnect` wrapper with authentication #### **Phase 2: Activity Listing** - [x] **Database Schema**: SQLAlchemy models with proper relationships - [x] **Database Operations**: Session management, initialization, and sync functionality - [x] **List Commands**: All filter options (`--all`, `--missing`, `--downloaded`) implemented - [x] **Progress Display**: tqdm integration for user feedback during operations #### **Phase 3: Download Pipeline** - [x] **FIT File Downloads**: Multi-method download approach with fallback strategies - [x] **Idempotent Operations**: Prevents re-downloading existing files - [x] **Database Updates**: Proper status tracking and file path storage - [x] **File Management**: Safe filename generation and directory creation #### **Phase 4: Enhanced Features** - [x] **Offline Mode**: List activities without API calls using cached data - [x] **Daemon Mode**: Background service with APScheduler for automatic sync - [x] **Web UI**: FastAPI-based dashboard with real-time monitoring - [x] **Schedule Configuration**: Configurable cron-based sync schedules - [x] **Activity Logs**: Comprehensive logging of sync operations #### **Phase 5: Web Interface** - [x] **Dashboard**: Real-time statistics and daemon status monitoring - [x] **API Routes**: RESTful endpoints for configuration and control - [x] **Templates**: Responsive HTML templates with Bootstrap styling - [x] **JavaScript Integration**: Auto-refreshing status and interactive controls - [x] **Configuration Management**: Web-based daemon settings and schedule updates ### **🔧 Recent Fixes and Improvements** #### **Dependency Management** - [x] **Pydantic Compatibility**: Fixed version constraints to avoid conflicts with `garth` - [x] **Requirements Lock**: Updated to `pydantic>=2.0.0,<2.5.0` for stability - [x] **Package Versions**: Verified compatibility across all dependencies #### **Code Quality Fixes** - [x] **Missing Fields**: Added `created_at` field to Activity model and sync operations - [x] **Import Issues**: Resolved circular import problems in daemon module - [x] **Error Handling**: Improved exception handling and logging throughout - [x] **Method Names**: Corrected method calls and parameter names #### **Web UI Enhancements** - [x] **Template Safety**: Added fallback handling for missing template files - [x] **API Error Handling**: Improved error responses and status codes - [x] **JavaScript Functions**: Added missing daemon control functions - [x] **Status Updates**: Real-time status updates with proper data formatting ----- ## **Docker Usage** ### **Build the Container** ```bash docker build -t garminsync . ``` ### **Run with Environment File** ```bash docker run -it --env-file .env -v $(pwd)/data:/app/data garminsync --help ``` ### **Example Commands** ```bash # List all activities docker run -it --env-file .env -v $(pwd)/data:/app/data garminsync list --all # List missing activities offline docker run -it --env-file .env -v $(pwd)/data:/app/data garminsync list --missing --offline # Download missing activities docker run -it --env-file .env -v $(pwd)/data:/app/data garminsync download --missing # Start daemon with web UI docker run -it --env-file .env -v $(pwd)/data:/app/data -p 8080:8080 garminsync daemon ``` ----- ## **Environment Setup** Create a `.env` file in the project root: ``` GARMIN_EMAIL=your_email@example.com GARMIN_PASSWORD=your_password ``` ----- ## **Key Implementation Highlights** ### **Robust Download Logic** The `garmin.py` module implements a sophisticated download strategy that tries multiple methods to handle variations in the Garmin Connect API: ```python methods_to_try = [ lambda: self.client.download_activity(activity_id), lambda: self.client.download_activity(activity_id, fmt='fit'), lambda: self.client.download_activity(activity_id, format='fit'), # ... additional fallback methods ] ``` ### **Database Synchronization** The sync process efficiently updates the local database with new activities from Garmin Connect: ```python def sync_database(garmin_client): """Sync local database with Garmin Connect activities""" activities = garmin_client.get_activities(0, 1000) for activity in activities: # Only add new activities, preserve existing download status existing = session.query(Activity).filter_by(activity_id=activity_id).first() if not existing: new_activity = Activity( activity_id=activity_id, start_time=start_time, downloaded=False, created_at=datetime.now().isoformat(), last_sync=datetime.now().isoformat() ) session.add(new_activity) ``` ### **Daemon Implementation** The daemon uses APScheduler for reliable background task execution: ```python class GarminSyncDaemon: def __init__(self): self.scheduler = BackgroundScheduler() self.running = False self.web_server = None def start(self, web_port=8080): config_data = self.load_config() if config_data['enabled']: self.scheduler.add_job( func=self.sync_and_download, trigger=CronTrigger.from_crontab(config_data['schedule_cron']), id='sync_job', replace_existing=True ) ``` ### **Web API Integration** FastAPI provides RESTful endpoints for daemon control and monitoring: ```python @router.get("/status") async def get_status(): """Get current daemon status with logs""" config = session.query(DaemonConfig).first() logs = session.query(SyncLog).order_by(SyncLog.timestamp.desc()).limit(10).all() return { "daemon": {"running": config.status == "running"}, "recent_logs": [{"timestamp": log.timestamp, "status": log.status} for log in logs] } ``` ----- ## **Known Issues & Limitations** ### **Current Limitations** 1. **Web Interface**: Some components need completion (detailed below) 2. **Error Recovery**: Limited automatic retry logic for failed downloads 3. **Batch Processing**: No support for selective activity date range downloads 4. **Authentication**: No support for two-factor authentication (2FA) ### **Dependency Issues Resolved** - ✅ **Pydantic Conflicts**: Fixed version constraints to avoid `garth` compatibility issues - ✅ **Missing Fields**: Added all required database fields - ✅ **Import Errors**: Resolved circular import problems ----- ## **Performance Considerations** - **Rate Limiting**: 2-second delays between API requests prevent server overload - **Batch Processing**: Fetches up to 1000 activities per sync operation - **Efficient Queries**: Database queries optimized for filtering operations - **Memory Management**: Proper session cleanup and resource management - **Docker Optimization**: Layer caching and minimal base image for faster builds - **Background Processing**: Daemon mode prevents blocking CLI operations ----- ## **Security Considerations** - **Credential Storage**: Environment variables prevent hardcoded credentials - **File Permissions**: Docker container runs with appropriate user permissions - **API Rate Limiting**: Respects Garmin Connect rate limits to prevent account restrictions - **Error Logging**: Sensitive information excluded from logs and error messages ----- ## **Documentation 📚** Here are links to the official documentation for the key libraries used: * **Garmin API:** [python-garminconnect](https://github.com/cyberjunky/python-garminconnect) * **CLI Framework:** [Typer](https://typer.tiangolo.com/) * **Environment Variables:** [python-dotenv](https://github.com/theskumar/python-dotenv) * **Database ORM:** [SQLAlchemy](https://docs.sqlalchemy.org/en/20/) * **Progress Bars:** [tqdm](https://github.com/tqdm/tqdm) * **Web Framework:** [FastAPI](https://fastapi.tiangolo.com/) * **Task Scheduler:** [APScheduler](https://apscheduler.readthedocs.io/) ----- ## **Web Interface Implementation Steps** ### **🎯 Missing Components to Complete** #### **1. Enhanced Dashboard Components** **A. Real-time Activity Counter** - **File:** `garminsync/web/templates/dashboard.html` - **Implementation:** ```html
Current Operation
| Timestamp | Operation | Status | Message | Activities |
|---|