# surendhiraraj/project_final architecture

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

- Interactive diagram: https://gitdiagram.com/surendhiraraj/project_final
- Repository: https://github.com/surendhiraraj/project_final

## Overview

##### Project Type and Purpose
- This project is an IoT data monitoring system that collects metrics from IoT devices via MQTT and displays this data in a Web UI.
- The README indicates a full-stack design with a FastAPI backend server and a React-based frontend. Additionally, there are specialized components for pub/sub (subscriber.py and publisher.py) and a background service for CSV generation (csv_generator_bw.py).

##### File Structure
- Top-Level Files:
- app.py: The FastAPI application serving the API endpoints.
- initlizer.py: Likely responsible for initial project/environment setup.
- subscriber.py and publisher.py: Dedicated modules for handling MQTT subscription and publication logic.
- csv_generator_bw.py: A background process that possibly generates CSV reports on a regular basis.
- util.py and __init__.py: Utility functions and package initialization.
- templates/: Contains the index.html template, which may be used by the FastAPI server or as part of a lightweight web UI.
- Configuration:
- requirements.txt identifies Python dependencies.
- .gitignore and README.md give context on code management and project setup.

##### README for Additional Insights
- The README describes the tech-stack and provides commands to run each component:
- FastAPI server (app.py) serves data through API endpoints.
- The pub/sub mechanism is started via subscriber.py and publisher.py.
- CSV generator is a separate process (csv_generator_bw.py) processing and outputting CSV files.
- The client technology (React, Redux, TailwindCSS) implies there is a separate front-end codebase (not visible in this file tree) that interacts with the backend.

a. Main Components to Represent:
- IoT Devices: External devices that publish metrics via MQTT.
- MQTT Broker: An external or third-party broker that routes messages between IoT devices and the system (this may be an external service not hosted directly in the project).
- Publisher/Subscriber Services: These two components (represented by publisher.py and subscriber.py) handle MQTT messages. They might either be distinct processes or logically grouped together.
- FastAPI Backend Server (app.py): Exposes REST API endpoints that interact with data coming from the MQTT services and possibly serve templates.
- CSV Generator (csv_generator_bw.py): A background process that generates CSV reports from the collected data.
- Frontend Client: The React/Redux based Web UI that consumes API data from the FastAPI server.

b. Relationships and Interactions:
- IoT Devices publish metric data to the MQTT Broker.
- The Publisher module sends data into the broker (or forwards contextual data downstream).
- The Subscriber module listens to MQTT topics, receives the metrics, and then communicates with the FastAPI server (or directly writes to a datastore if one exists, though the structure hints at in-memory or lightweight architectural patterns).
- The FastAPI server acts as a central point to serve the Web UI (and may also render templates from /templates).
- The CSV Generator runs concurrently to process historical or real-time data and produce downloadable CSV reports.
- The Frontend client interacts with the FastAPI server via HTTP requests (RESTful API calls) to display live metrics and reports.

c. Architectural Patterns and Design Principles:
- Separation of Concerns: Different processes are responsible for handling backend web requests (FastAPI), MQTT messaging (subscriber and publisher), and background data processing (CSV generator).
- Event-Driven Architecture: Utilizes MQTT pub/sub messaging for decoupling data ingestion from data consumption.
- Microservices-like separation (even if within the same repository): Although the components run as separate processes, they communicate via defined protocols (MQTT and HTTP APIs).

d. Technologies, Frameworks, and Libraries:
- FastAPI is central to the backend.
- MQTT (though not detailed in the file tree) is an implied dependency.
- Python utilities are supported by the various Python scripts (app.py, util.py, etc.)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    IOT["IoT Devices"]:::external
    MQTTBroker["MQTT Broker"]:::external
    React["React/Redux Frontend"]:::frontend

    %% MQTT Services
    subgraph "MQTT Services"
        Publisher["Publisher Service"]:::service
        Subscriber["Subscriber Service"]:::service
    end

    %% Backend Services
    subgraph "Backend Services"
        FastAPI["FastAPI Server"]:::backend
        CSVGen["CSV Generator"]:::backend
        Init["Initialization Utility"]:::utility
        Util["Utility Functions"]:::utility
        Template["Frontend Template"]:::template
    end

    %% Relationships
    IOT -->|"publishesMetrics"| MQTTBroker
    Publisher -->|"publishesData"| MQTTBroker
    MQTTBroker -->|"distributesData"| Subscriber
    Subscriber -->|"forwardsData"| FastAPI
    FastAPI -->|"servesAPI"| React
    FastAPI -->|"rendersTemplate"| Template
    FastAPI -->|"providesData"| CSVGen
    Init -->|"initializes"| FastAPI
    Util -->|"supports"| FastAPI

    %% Click Events
    click FastAPI "https://github.com/surendhiraraj/project_final/blob/main/app.py"
    click Publisher "https://github.com/surendhiraraj/project_final/blob/main/publisher.py"
    click Subscriber "https://github.com/surendhiraraj/project_final/blob/main/subscriber.py"
    click CSVGen "https://github.com/surendhiraraj/project_final/blob/main/csv_generator_bw.py"
    click Template "https://github.com/surendhiraraj/project_final/blob/main/templates/index.html"
    click Init "https://github.com/surendhiraraj/project_final/blob/main/initlizer.py"
    click Util "https://github.com/surendhiraraj/project_final/blob/main/util.py"

    %% Styles
    classDef external fill:#FFCC00,stroke:#333,stroke-width:2px;
    classDef frontend fill:#FF9999,stroke:#333,stroke-width:2px;
    classDef service fill:#99CCFF,stroke:#333,stroke-width:2px;
    classDef backend fill:#CCFFCC,stroke:#333,stroke-width:2px;
    classDef utility fill:#FFCCFF,stroke:#333,stroke-width:2px;
    classDef template fill:#CCFFFF,stroke:#333,stroke-width:2px;
```
