# saifhuseni/trafficiq architecture

> Architecture diagram of the GitHub repository saifhuseni/trafficiq, 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/saifhuseni/trafficiq
- Repository: https://github.com/saifhuseni/trafficiq

## Overview

##### Main Components
- Frontend: Represent the React.js UI along with relevant subcomponents such as pages (Home, Login, Dashboard, TrafficCongestion, UploadPage), reusable components (Navbar, Footer, forms) and context/services for API calls.
- Backend (Node.js/Express): Highlight key modules such as:
– Server setup (server.js)
– Controllers (authController, userController)
– Routes (authRoutes, userRoutes)
– Middleware (authMiddleware, errorMiddleware)
– Utility functions (logger) and configuration (db.js)
– Data models (User.js)
- AI & Computer Vision Service (Python/FastAPI): Stress its role as a dedicated service for processing video:
– Main FastAPI application (main.py)
– AI models and scripts (tracker.py, model.pkl, yolov8s.pt)
– Temporary directories for video uploads and processed results
- Database: Even though it’s not detailed very deeply, indicate that MongoDB (mentioned in the README) is used by the backend to store user data and possibly tracking data.
- External Data Sources: Optionally include APIs or external services that feed data into the congestion prediction module.

##### Relationships and Interactions
- Frontend sends API requests (video uploads, authentication data, traffic queries) to the Node.js backend.
- The Node.js backend receives these requests, performs initial processing (e.g., authentication, data validation), and then, for video processing, forwards the relevant media to the FastAPI service.
- The FastAPI (Python) service runs the AI algorithms (using YOLO and OpenCV) to analyze traffic videos, detect vehicle speeds, count vehicles, and predict congestion. It may return processed videos (output.avi) or prediction data.
- The Node.js backend then returns the final response to the frontend for rendering results, which may be displayed in dashboards or used to control the user interface.
- The database (MongoDB) connects with the Node.js backend for user account management and persistence of traffic data.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Clearly separate presentation (React frontend), business logic (Node.js backend), and specialized computational tasks (Python FastAPI).
- Microservice-like Handling: Although contained within one repository, the split between Express and FastAPI services demonstrates a modular design where each service handles a specific domain.
- Clear division of responsibilities: UI rendering, REST API handling, and machine learning-driven video processing are strictly delineated.

##### Relevant Technologies to Highlight
- Frontend: React.js, Tailwind CSS.
- Backend: Node.js, Express.js, possibly MongoDB.
- AI & Computer Vision: OpenCV, YOLO (yolov8s.pt), Python-based FastAPI.
- Additional Tools: Python notebooks (.ipynb; speed_estimate.ipynb, TrafficIQ.ipynb) used for prototyping or visualizing the AI tasks.

##### Details
- Every component from the file tree that plays a role in the system is accounted for.
- Detail how the machine learning module (with its model files, tracker.py, and video uploads) works in tandem with the web server.
- Be explicit about the roles played by various directories such as backend/config, backend/controllers, frontend/src, and backend/fastapi.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Subgraph
    subgraph "React Frontend"
        direction TB
        %% Pages Subgraph
        subgraph "Pages"
            HomePage["HomePage"]
            LoginPage["LoginPage"]
            Dashboard["Dashboard"]
            TrafficCongestion["TrafficCongestion"]
            UploadPage["UploadPage"]
        end
        %% Reusable Components Subgraph
        subgraph "Reusable Components"
            Navbar["Navbar"]
            Footer["Footer"]
            Error["Error"]
            Loading["Loading"]
            PredictionForm["PredictionForm"]
            PrivateRoute["PrivateRoute"]
            Upload["Upload"]
        end
        %% Context & Services Subgraph
        subgraph "Context & Services"
            AuthContext["AuthContext"]
            APIService["API Service"]
            AuthService["Auth Service"]
        end
    end

    %% Backend Subgraph
    subgraph "Express Backend"
        direction TB
        ExpressServer["Express Server"]
        subgraph "Controllers"
            AuthController["AuthController"]
            UserController["UserController"]
        end
        subgraph "Routes"
            AuthRoutes["AuthRoutes"]
            UserRoutes["UserRoutes"]
        end
        subgraph "Middleware"
            AuthMiddleware["AuthMiddleware"]
            ErrorMiddleware["ErrorMiddleware"]
        end
        DBConfig["DB Config"]
        UserModel["User Model"]
        Logger["Logger"]
    end

    %% AI & Computer Vision Service Subgraph
    subgraph "FastAPI AI Service"
        direction TB
        FastAPIMain["FastAPI Main"]
        Tracker["Tracker"]
        ModelFile["Model File"]
        YOLOModel["YOLO Model"]
        TempVideo["Temp Video Storage"]
        UploadsDir["Uploads Directory"]
    end

    %% Database Node
    MongoDB["MongoDB Database"]

    %% Interactions
    HomePage & LoginPage & Dashboard & TrafficCongestion & UploadPage -->|"APIRequest"| ExpressServer
    Navbar & Footer & Error & Loading & PredictionForm & PrivateRoute & Upload -->|"UI Actions"| ExpressServer
    ExpressServer -->|"VideoProcessing"| FastAPIMain
    FastAPIMain -->|"ProcessedResults"| ExpressServer
    ExpressServer -->|"DataPersistence"| MongoDB

    %% Additional internal backend interactions
    ExpressServer --> AuthController
    ExpressServer --> UserController
    ExpressServer --> AuthRoutes
    ExpressServer --> UserRoutes
    ExpressServer --> AuthMiddleware
    ExpressServer --> ErrorMiddleware
    ExpressServer --> DBConfig
    ExpressServer --> UserModel
    ExpressServer --> Logger

    %% Additional FastAPI interactions
    FastAPIMain --> Tracker
    FastAPIMain --> ModelFile
    FastAPIMain --> YOLOModel
    FastAPIMain --> TempVideo
    FastAPIMain --> UploadsDir

    %% Click Events for Frontend Pages
    click HomePage "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/pages/HomePage.js"
    click LoginPage "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/pages/LoginPage.js"
    click Dashboard "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/pages/Dashboard.js"
    click TrafficCongestion "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/pages/TrafficCongestion.js"
    click UploadPage "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/pages/UploadPage.js"

    %% Click Events for Reusable Components
    click Navbar "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/components/Layout/Navbar.js"
    click Footer "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/components/Layout/Footer.js"
    click Error "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/components/Error.js"
    click Loading "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/components/Loading.js"
    click PredictionForm "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/components/PredictionForm.js"
    click PrivateRoute "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/components/PrivateRoute.js"
    click Upload "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/components/Upload.js"

    %% Click Events for Context & Services
    click AuthContext "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/context/AuthContext.js"
    click APIService "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/services/api.js"
    click AuthService "https://github.com/saifhuseni/trafficiq/blob/main/frontend/src/services/authService.js"

    %% Click Events for Express Backend
    click ExpressServer "https://github.com/saifhuseni/trafficiq/blob/main/backend/server.js"
    click AuthController "https://github.com/saifhuseni/trafficiq/blob/main/backend/controllers/authController.js"
    click UserController "https://github.com/saifhuseni/trafficiq/blob/main/backend/controllers/userController.js"
    click AuthRoutes "https://github.com/saifhuseni/trafficiq/blob/main/backend/routes/authRoutes.js"
    click UserRoutes "https://github.com/saifhuseni/trafficiq/blob/main/backend/routes/userRoutes.js"
    click AuthMiddleware "https://github.com/saifhuseni/trafficiq/blob/main/backend/middleware/authMiddleware.js"
    click ErrorMiddleware "https://github.com/saifhuseni/trafficiq/blob/main/backend/middleware/errorMiddleware.js"
    click DBConfig "https://github.com/saifhuseni/trafficiq/blob/main/backend/config/db.js"
    click UserModel "https://github.com/saifhuseni/trafficiq/blob/main/backend/models/User.js"
    click Logger "https://github.com/saifhuseni/trafficiq/blob/main/backend/utils/logger.js"

    %% Click Events for FastAPI AI Service
    click FastAPIMain "https://github.com/saifhuseni/trafficiq/blob/main/backend/fastapi/main.py"
    click Tracker "https://github.com/saifhuseni/trafficiq/blob/main/backend/fastapi/tracker.py"
    click ModelFile "https://github.com/saifhuseni/trafficiq/blob/main/backend/fastapi/model.pkl"
    click YOLOModel "https://github.com/saifhuseni/trafficiq/blob/main/backend/fastapi/yolov8s.pt"
    click TempVideo "https://github.com/saifhuseni/trafficiq/tree/main/backend/fastapi/temp_video"
    click UploadsDir "https://github.com/saifhuseni/trafficiq/tree/main/backend/fastapi/uploads"

    %% Styling
    classDef frontend fill:#FFEBEE,stroke:#D32F2F,stroke-width:2px;
    classDef backend fill:#E8F5E9,stroke:#388E3C,stroke-width:2px;
    classDef ai fill:#E3F2FD,stroke:#1976D2,stroke-width:2px;
    classDef database fill:#FFF3E0,stroke:#F57C00,stroke-width:2px;

    class HomePage,LoginPage,Dashboard,TrafficCongestion,UploadPage,Navbar,Footer,Error,Loading,PredictionForm,PrivateRoute,Upload,AuthContext,APIService,AuthService frontend;
    class ExpressServer,AuthController,UserController,AuthRoutes,UserRoutes,AuthMiddleware,ErrorMiddleware,DBConfig,UserModel,Logger backend;
    class FastAPIMain,Tracker,ModelFile,YOLOModel,TempVideo,UploadsDir ai;
    class MongoDB database;
```
