How botirbakhtiyarov/get_stat works
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