# reywaltz/telemetr architecture

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

- Interactive diagram: https://gitdiagram.com/reywaltz/telemetr
- Repository: https://github.com/reywaltz/telemetr

## Overview

##### Project type and purpose
- Telemetr is a Python-based monitoring tool for collecting statistics about Telegram channels.
- It bundles three “mini-apps” (an auth bot, a fetcher, and a REST API) into a cohesive service that reads/writes data to a Postgres database and logs events.

##### File structure
- Top-level orchestrator: app.py.
- Apps folder:
- apps/auth_bot – a Telegram bot that handles user authentication and commands.
- apps/fetcher – a worker that periodically scrapes channel stats.
- apps/telemetr/api – a Flask/FastAPI-style REST API exposing stats.
- internal: domain logic modules (admin, categories, channels, users), plus infrastructure adapters:
- internal/telegram – Telegram client wrapper.
- internal/postgres – ORM/DAO layer.
- pkg/log: centralized logging (file logger + interface).
- schema.sql + requirements.txt.

##### Key insights from README
- Single-purpose: monitor Telegram channel statistics.
- No frontend beyond the Telegram bot and the JSON API.

##### Main components
a. External services
- Telegram Bot API (for auth_bot).
- Telemetr web source (for fetcher scrapes).
b. Micro-apps (deployed together or as separate processes)
- Auth Bot
- Fetcher Worker
- Telemetr REST API
c. Database
- Postgres (tables: users, categories, channels, admin, stats).
d. Shared libraries/internals
- Domain logic (internal/*).
- Infrastructure adapters (internal/postgres, internal/telegram).
- Logging (pkg/log).

##### Relationships and data flows
- Auth Bot ↔ Telegram API: receives commands → uses internal modules → reads/writes Postgres → replies to user.
- Fetcher → Telemetr website: scrapes stats → transforms via internal/channel & internal/categories → writes to Postgres.
- REST API ← clients: HTTP requests → handlers in apps/telemetr/api → query Postgres via internal/postgres → respond.
- All components import pkg/log to record events.

##### Architectural patterns and principles
- Layered architecture:
- Presentation/adapters: bots + API.
- Domain: internal/* modules encapsulating business logic.
- Infrastructure: DB adapters, HTTP/Telegram clients, logging.
- Separation of concerns: each app has a single responsibility.
- Dependency inversion: domain modules don’t depend on external libraries directly; they use small adapters.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Services
    subgraph "External Services"
        direction TB
        TelAPI["Telegram Bot API"]:::external
        WebSource["Telemetr Web Source"]:::external
        Clients["API Clients"]:::external
    end

    %% Orchestrator
    Orchestrator["Orchestrator (app.py)"]:::app

    %% Application Processes
    subgraph "Application Processes"
        direction TB
        AuthBot["Auth Bot"]:::app
        Fetcher["Fetcher Worker"]:::app
        subgraph "REST API"
            direction TB
            APIHandlers["API Handlers"]:::app
            APIAdditions["API Additions"]:::app
        end
    end

    %% Database
    Postgres["Postgres DB"]:::db

    %% Shared Libraries
    subgraph "Shared Libraries"
        direction TB
        subgraph "Domain Logic Modules"
            direction TB
            AdminLogic["Admin Logic"]:::lib
            CatLogic["Category Logic"]:::lib
            ChanLogic["Channel Logic"]:::lib
            UserLogic["User Logic"]:::lib
        end
        subgraph "Infra Adapters"
            direction TB
            TelAdapter["Telegram Client Adapter"]:::lib
            PGConnector["Postgres Connector"]:::lib
            AdminDAO["Admin DAO"]:::lib
            CatDAO["Category DAO"]:::lib
            ChanDAO["Channel DAO"]:::lib
            UserDAO["User DAO"]:::lib
        end
        subgraph "Logging Library"
            direction TB
            LoggerIF["Logger Interface"]:::lib
            FileLogger["File Logger"]:::lib
        end
    end

    %% Connections
    Orchestrator -->|starts| AuthBot
    Orchestrator -->|starts| Fetcher
    Orchestrator -->|starts| APIHandlers

    AuthBot <-->|"bot message"| TelAPI
    AuthBot -->|"uses adapter"| TelAdapter
    AuthBot -->|"uses logic"| AdminLogic
    AuthBot -->|"uses logic"| CatLogic
    AuthBot -->|"uses logic"| ChanLogic
    AuthBot -->|"uses logic"| UserLogic
    AuthBot -->|"SQL query"| PGConnector
    AuthBot -->|"logs"| LoggerIF

    Fetcher -->|"scrape"| WebSource
    Fetcher -->|"uses logic"| ChanLogic
    Fetcher -->|"uses logic"| CatLogic
    Fetcher -->|"SQL insert"| PGConnector
    Fetcher -->|"logs"| LoggerIF

    Clients -->|"HTTP request"| APIHandlers
    APIHandlers -->|"calls additions"| APIAdditions
    APIHandlers -->|"uses logic"| AdminLogic
    APIHandlers -->|"uses logic"| CatLogic
    APIHandlers -->|"uses logic"| ChanLogic
    APIHandlers -->|"uses logic"| UserLogic
    APIHandlers -->|"SQL query"| PGConnector
    APIHandlers -->|"logs"| LoggerIF

    %% Domain to DAO
    AdminLogic -->|"uses DAO"| AdminDAO
    CatLogic -->|"uses DAO"| CatDAO
    ChanLogic -->|"uses DAO"| ChanDAO
    UserLogic -->|"uses DAO"| UserDAO

    %% DAOs to Connector
    AdminDAO -->|"connect"| PGConnector
    CatDAO -->|"connect"| PGConnector
    ChanDAO -->|"connect"| PGConnector
    UserDAO -->|"connect"| PGConnector

    %% Logging
    LoggerIF -->|"writes"| FileLogger

    %% Click Events
    click AuthBot "https://github.com/reywaltz/telemetr/blob/master/apps/auth_bot/bot.py"
    click Fetcher "https://github.com/reywaltz/telemetr/blob/master/apps/fetcher/fetcher.py"
    click APIHandlers "https://github.com/reywaltz/telemetr/blob/master/apps/telemetr/api/handlers.py"
    click APIAdditions "https://github.com/reywaltz/telemetr/blob/master/apps/telemetr/api/additions.py"
    click Orchestrator "https://github.com/reywaltz/telemetr/blob/master/app.py"
    click TelAdapter "https://github.com/reywaltz/telemetr/blob/master/internal/telegram/client.py"
    click PGConnector "https://github.com/reywaltz/telemetr/blob/master/internal/postgres/postgres.py"
    click AdminDAO "https://github.com/reywaltz/telemetr/blob/master/internal/postgres/admin.py"
    click CatDAO "https://github.com/reywaltz/telemetr/blob/master/internal/postgres/category.py"
    click ChanDAO "https://github.com/reywaltz/telemetr/blob/master/internal/postgres/channel.py"
    click UserDAO "https://github.com/reywaltz/telemetr/blob/master/internal/postgres/user.py"
    click AdminLogic "https://github.com/reywaltz/telemetr/blob/master/internal/admin/admin.py"
    click CatLogic "https://github.com/reywaltz/telemetr/blob/master/internal/categories/category.py"
    click ChanLogic "https://github.com/reywaltz/telemetr/blob/master/internal/channels/channel.py"
    click UserLogic "https://github.com/reywaltz/telemetr/blob/master/internal/users/user.py"
    click LoggerIF "https://github.com/reywaltz/telemetr/blob/master/pkg/log/logger.py"
    click FileLogger "https://github.com/reywaltz/telemetr/blob/master/pkg/log/filelogger.py"
    click Postgres "https://github.com/reywaltz/telemetr/blob/master/schema.sql"

    %% Styles
    classDef external fill:#D0E6F6,stroke:#333,stroke-width:1px;
    classDef app fill:#CFF7C4,stroke:#333,stroke-width:1px;
    classDef db fill:#F7D8B5,stroke:#333,stroke-width:1px;
    classDef lib fill:#E0E0E0,stroke:#333,stroke-width:1px;
```
