# mewset/lagerhantering architecture

> Architecture diagram of the GitHub repository mewset/lagerhantering, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mewset/lagerhantering
- Repository: https://github.com/mewset/lagerhantering

## Overview

##### Project Type and Purpose
- Full-stack Flask web application for managing spare-parts inventory.
- Modular, layered architecture with REST API, UI templates, JSON storage, scheduled backup and updater services.

##### Key Components (to include
A. Presentation Layer (Client)
- Browser / HTTP client
- Static assets: static/css/style.css, static/js/*.js
- HTML templates: templates/index.html, admin.html, dashboard.html, logs.html, changelog.html, footer.html

B. API Layer (Flask App)
- app.py (Flask application factory, dependency injection)
- config.py (Singleton configuration provider)
- routes/ Blueprints:
- inventory.py
- settings.py
- logs.py

C. Service Layer (Business Logic)
- services/inventory_service.py
- services/settings_service.py
- services/backup_service.py (scheduled backups)
- services/updater_service.py (in-process updater)

D. Data Access Layer
- models/inventory.py (InventoryModel, InventoryItem)
- models/__init__.py

E. Shared Utilities
- utils/logger.py (structured logging)
- utils/file_handler.py (thread-safe file I/O)
- utils/validation.py (input schemas)
- utils/exceptions.py (custom exception hierarchy)
- utils/decorators.py (error handling, validation wrappers)

F. Data Stores
- data/inventory.json
- data/dashboard_settings.json
- db_backup/ (rotating file backups)
- version_backup/ (pre-update code snapshots)

G. Background Daemons
- updater.py (standalone updater with `--daemon` flag, uses schedule library)
- start_updater_daemon.bat / test_updater.bat
- backup_service triggers from within the Flask process or can be drawn as a scheduled task

H. External Dependencies
- Git (for updater)
- schedule (Python package for cron-like scheduling)
- Flask (web framework)

##### Relationships and Data Flow
1. User → Browser → HTTP(S) → Flask (app.py)
- Requests hit the registered Blueprints in routes/.
2. Routes → Service Layer
- routes/*.py validate input (utils/validation), call services/*.py.
3. Services → Models + Utils
- business logic in inventory_service.py calls InventoryModel for CRUD.
- backup_service and updater_service orchestrate file_handler, logger, Git operations.
4. Models → JSON Data
- InventoryModel reads/writes inventory.json via file_handler, maintains in-memory cache.
5. Background Daemons
- updater.py process invokes updater_service logic; schedules Git pull → version_backup → graceful restart of app.
- backup_service schedules file‐level backups to db_backup/ with retention policy.
6. Logging
- All components use utils/logger.py to append to app.log or updater.log.

##### Architectural Patterns & Principles
- Layered Architecture: Presentation → API → Services → Data Access → Storage.
- Blueprint Pattern for route modularity (routes/).
- Service Layer for business rules.
- Singleton Pattern for global config (config.py).
- Dependency Injection in app.py for swapping mocks in tests.
- Thread-safe file I/O and caching in InventoryModel.
- Scheduler library for recurring tasks (backup, updater).

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Frontend (Client)"
        Browser["Browser / HTTP Client"]:::frontend
        subgraph "Static Assets"
            CSS["static/css/style.css"]:::frontend
            AdminJS["static/js/admin.js"]:::frontend
            DashboardJS["static/js/dashboard.js"]:::frontend
            IndexJS["static/js/index.js"]:::frontend
        end
        subgraph "HTML Templates"
            TIndex["templates/index.html"]:::frontend
            TAdmin["templates/admin.html"]:::frontend
            TDashboard["templates/dashboard.html"]:::frontend
            TLogs["templates/logs.html"]:::frontend
            TChangelog["templates/changelog.html"]:::frontend
            TFooter["templates/footer.html"]:::frontend
        end
    end

    Browser -->|HTTP Request| AppFactory

    subgraph "Backend (Flask App)"
        AppFactory["app.py\nFlask App Factory"]:::backend
        Config["config.py\nSingleton Configuration"]:::util
        AppFactory --> Config

        subgraph "API Layer - Routes"
            InventoryRoute["routes/inventory.py"]:::backend
            SettingsRoute["routes/settings.py"]:::backend
            LogsRoute["routes/logs.py"]:::backend
        end

        AppFactory --> InventoryRoute
        AppFactory --> SettingsRoute
        AppFactory --> LogsRoute

        Config --> InventoryRoute
        Config --> SettingsRoute
        Config --> LogsRoute

        subgraph "Service Layer"
            InvService["services/inventory_service.py"]:::backend
            SetService["services/settings_service.py"]:::backend
            BackupService["services/backup_service.py"]:::backend
            UpdaterService["services/updater_service.py"]:::backend
        end

        InventoryRoute --> InvService
        SettingsRoute --> SetService
        LogsRoute --> InvService

        subgraph "Data Access Layer"
            InventoryModel["models/inventory.py\nInventoryModel"]:::backend
            InitModel["models/__init__.py"]:::util
        end

        InvService --> InventoryModel

        subgraph "Shared Utilities"
            Logger["utils/logger.py"]:::util
            FileHandler["utils/file_handler.py"]:::util
            Validation["utils/validation.py"]:::util
            Exceptions["utils/exceptions.py"]:::util
            Decorators["utils/decorators.py"]:::util
        end

        InvService --> FileHandler
        BackupService --> FileHandler
        UpdaterService --> FileHandler
        InvService --> Logger
        BackupService --> Logger
        UpdaterService --> Logger
        InventoryRoute --> Validation

        subgraph "Data Stores"
            InventoryJSON[(inventory.json)]:::datastore
            DBBackup[(db_backup/)]:::datastore
            VersionBackup[(version_backup/)]:::datastore
        end

        InventoryModel --> InventoryJSON
        BackupService --> DBBackup
        UpdaterService --> VersionBackup
    end

    subgraph "Background Daemons"
        UpdaterDaemon["updater.py\n--daemon"]:::daemon
        StartDaemon["start_updater_daemon.bat"]:::daemon
        TestDaemon["test_updater.bat"]:::daemon
        UpdaterLog["updater.log"]:::daemon

        StartDaemon --> UpdaterDaemon
        TestDaemon --> UpdaterDaemon
        UpdaterDaemon --> UpdaterService
        UpdaterDaemon --> UpdaterLog
    end

    click CSS "https://github.com/mewset/lagerhantering/blob/main/static/css/style.css"
    click AdminJS "https://github.com/mewset/lagerhantering/blob/main/static/js/admin.js"
    click DashboardJS "https://github.com/mewset/lagerhantering/blob/main/static/js/dashboard.js"
    click IndexJS "https://github.com/mewset/lagerhantering/blob/main/static/js/index.js"
    click TIndex "https://github.com/mewset/lagerhantering/blob/main/templates/index.html"
    click TAdmin "https://github.com/mewset/lagerhantering/blob/main/templates/admin.html"
    click TDashboard "https://github.com/mewset/lagerhantering/blob/main/templates/dashboard.html"
    click TLogs "https://github.com/mewset/lagerhantering/blob/main/templates/logs.html"
    click TChangelog "https://github.com/mewset/lagerhantering/blob/main/templates/changelog.html"
    click TFooter "https://github.com/mewset/lagerhantering/blob/main/templates/footer.html"
    click AppFactory "https://github.com/mewset/lagerhantering/blob/main/app.py"
    click Config "https://github.com/mewset/lagerhantering/blob/main/config.py"
    click InventoryRoute "https://github.com/mewset/lagerhantering/blob/main/routes/inventory.py"
    click SettingsRoute "https://github.com/mewset/lagerhantering/blob/main/routes/settings.py"
    click LogsRoute "https://github.com/mewset/lagerhantering/blob/main/routes/logs.py"
    click InvService "https://github.com/mewset/lagerhantering/blob/main/services/inventory_service.py"
    click SetService "https://github.com/mewset/lagerhantering/blob/main/services/settings_service.py"
    click BackupService "https://github.com/mewset/lagerhantering/blob/main/services/backup_service.py"
    click UpdaterService "https://github.com/mewset/lagerhantering/blob/main/services/updater_service.py"
    click InventoryModel "https://github.com/mewset/lagerhantering/blob/main/models/inventory.py"
    click InitModel "https://github.com/mewset/lagerhantering/blob/main/models/__init__.py"
    click Logger "https://github.com/mewset/lagerhantering/blob/main/utils/logger.py"
    click FileHandler "https://github.com/mewset/lagerhantering/blob/main/utils/file_handler.py"
    click Validation "https://github.com/mewset/lagerhantering/blob/main/utils/validation.py"
    click Exceptions "https://github.com/mewset/lagerhantering/blob/main/utils/exceptions.py"
    click Decorators "https://github.com/mewset/lagerhantering/blob/main/utils/decorators.py"
    click InventoryJSON "https://github.com/mewset/lagerhantering/blob/main/inventory.json"
    click UpdaterDaemon "https://github.com/mewset/lagerhantering/blob/main/updater.py"
    click StartDaemon "https://github.com/mewset/lagerhantering/blob/main/start_updater_daemon.bat"
    click TestDaemon "https://github.com/mewset/lagerhantering/blob/main/test_updater.bat"
    click UpdaterLog "https://github.com/mewset/lagerhantering/blob/main/updater.log"

    classDef frontend fill:#D6E9FE,stroke:#0366d6,stroke-width:2px
    classDef backend fill:#D0F0C0,stroke:#228B22,stroke-width:2px
    classDef util fill:#F5F5F5,stroke:#666,stroke-width:1px
    classDef daemon fill:#FFE0B2,stroke:#FF8C00,stroke-width:2px
    classDef datastore fill:#FFF4E6,stroke:#A0522D,stroke-width:2px
```
