# botirbakhtiyarov/get_stat architecture

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

- Interactive diagram: https://gitdiagram.com/botirbakhtiyarov/get_stat
- Repository: https://github.com/botirbakhtiyarov/get_stat

## Overview

##### Main Components

- Backend Server (Django):
- Represented by the “StatsProject” directory.
- Contains standard Django elements such as settings, URLs, and WSGI/ASGI files.
- Includes the “stats” application, which covers models (like Computer, AppUsage, and SystemMetrics), views, migrations, and custom management commands (e.g., createapikey.py).
- Exposes two main API endpoints: POST /api/data (for metric submission from the agent) and GET /api/stats (for retrieving aggregated stats).

- Database:
- In this case, a SQLite database (db.sqlite3) is used by Django to store metrics and configuration data.

- Agent:
- A standalone Python script (agent.py) acting as the client to collect system metrics (CPU, RAM, application usage) and send them periodically (e.g., every 5 minutes) to the backend via the /api/data endpoint.
- Necessity to configure SERVER_URL (the backend API’s URL) in the agent script.

- Frontend Dashboard:
- A simple HTML page (data.html) that uses JavaScript (likely leveraging Chart.js and Axios for data visualization and API calls) to display the collected data.
- It interacts with the backend's GET /api/stats endpoint and requires an API key for authentication.

##### Component Interactions

- Data Flow:
- The Agent collects metrics from the host machine and sends a POST request to the backend’s /api/data endpoint.
- The Django backend processes and stores these metrics in the SQLite database.
- The Frontend dashboard (data.html) sends GET requests (with required API key in the header) to fetch aggregated data from /api/stats for visualization.

- API Key Verification:
- When the frontend queries for data, it includes an Authorization header (“Api-Key YOUR_API_KEY”) ensuring that only authorized requests are served by the server.

##### Architectural Patterns and Technologies

- MVC Framework:
- Django’s built-in pattern where models, views, and controllers (through URL routing) structure the application.

- RESTful API Design:
- The project defines clear endpoints that separate data collection (/api/data) from data retrieval (/api/stats).

- Extensibility:
- The modularity of the stats app and the separate agent script imply that the monitoring metrics and functionalities can be easily extended or modified.

- Technology Stack:
- Python (>=3.8)
- Django 4.2
- SQLite for the database (in this project instance)
- Additional libraries for the agent (psutil, requests, pywin32 for Windows)
- Chart.js & Axios (implied in data.html) for frontend visualization

## Diagram (Mermaid)

```mermaid
flowchart TD
    A["Agent (agent.py)"]:::client
    F["Frontend Dashboard (data.html)"]:::client

    subgraph "Backend Server"
        BS["Main Server (Django) [API:/api/data, /api/stats]"]:::backend
        SA["Django App (stats):models,views,commands"]:::backend
        BS -->|"internal"| SA
    end

    DB["SQLite Database (db.sqlite3)"]:::database

    A -->|"POST:/api/data"| BS
    F -->|"GET:/api/stats(Api-Key)"| BS
    BS -->|"stores/retrieves"| DB

    click BS "https://github.com/botirbakhtiyarov/get_stat/tree/master/StatsProject/StatsProject"
    click SA "https://github.com/botirbakhtiyarov/get_stat/tree/master/StatsProject/stats"
    click DB "https://github.com/botirbakhtiyarov/get_stat/blob/master/StatsProject/db.sqlite3"
    click A "https://github.com/botirbakhtiyarov/get_stat/blob/master/agent.py"
    click F "https://github.com/botirbakhtiyarov/get_stat/blob/master/data.html"

    classDef backend fill:#add8e6,stroke:#0000ff,stroke-width:2px;
    classDef database fill:#90ee90,stroke:#006400,stroke-width:2px;
    classDef client fill:#ffa07a,stroke:#ff4500,stroke-width:2px;
```
