# hash2004/nettrafficmonitor_cpp_js_flask architecture

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

- Interactive diagram: https://gitdiagram.com/hash2004/nettrafficmonitor_cpp_js_flask
- Repository: https://github.com/hash2004/nettrafficmonitor_cpp_js_flask

## Overview

##### Main Components
- C++ Network Monitor:
- File: src/network_monitor.cpp
- Function: Uses libpcap (and pthread) to capture network traffic.
- Deployment: Compiled using g++ and run with root privileges.
- Python FastAPI Backend:
- Files: main.py, requirements.txt.
- Function: Exposes API endpoints (hosted via uvicorn) to serve network data, likely reading from generated data assets.
- Frontend:
- Files: frontend/app.js, frontend/index.html, frontend/styles.css.
- Data Storage:
- Directory: data (contains CSV files such as connections.csv, protocol_counts.csv, total_packets.csv).
- Function: Stores the output (or processed result) of the network monitoring operations.

##### Relationships and Data Flow
- The C++ component is responsible for capturing network traffic and saving the results as CSV files in the “data” folder.
- The Python FastAPI backend (launched with uvicorn as per README instructions) acts as an intermediary: reading the CSV files and providing an API for the frontend.
- The frontend then consumes this API to display network monitoring statistics to the user.
- “Network Monitor (C++)” → “Data Storage” (CSV files updated with traffic data).
- “Data Storage” → “FastAPI Backend” (reads/serves data).
- “FastAPI Backend” → “Frontend” (serves API responses).

##### Architectural Patterns and Design Principles
- Separation of Concerns: Each component is built using a language suited for its purpose (fast low-level monitoring in C++, efficient API/backend in Python, responsive UI in JavaScript/HTML/CSS).
- Loose Coupling: The components interact via well-defined data storage (CSV files) and API endpoints.
- Modularity: Components are separated into different directories (src for C++ code, frontend for UI, data for persistence), making it easy to modify one part of the system with minimal impact on others.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main Components
    A["C++ Network Monitor"]:::cpp
    B["Data Storage (CSV Files)"]:::data

    %% FastAPI Backend Subgraph
    subgraph "FastAPI Backend"
        direction TB
        B1["main.py"]:::python
        B2["requirements.txt"]:::python
    end

    %% Frontend UI Subgraph
    subgraph "Frontend UI"
        direction TB
        F1["FrontendApp"]:::frontend
        F2["app.js"]:::frontend
        F3["index.html"]:::frontend
        F4["styles.css"]:::frontend
    end

    %% Auxiliary Notes
    G["g++ Build (libpcap, pthread)"]:::note
    U["uvicorn Serve (FastAPI)"]:::note

    %% Relationships and Data Flow
    A -->|"writesCSV"| B
    B -->|"readsCSV"| B1
    B1 -->|"providesAPI"| F1
    F1 -->|"APIrequests"| B1

    %% Annotations for Build/Serve Processes
    A ---|"build"| G
    B1 ---|"serve"| U

    %% Click Events
    click A "https://github.com/hash2004/nettrafficmonitor_cpp_js_flask/blob/main/src/network_monitor.cpp"
    click B "https://github.com/hash2004/nettrafficmonitor_cpp_js_flask/tree/main/data"
    click B1 "https://github.com/hash2004/nettrafficmonitor_cpp_js_flask/blob/main/main.py"
    click B2 "https://github.com/hash2004/nettrafficmonitor_cpp_js_flask/blob/main/requirements.txt"
    click F2 "https://github.com/hash2004/nettrafficmonitor_cpp_js_flask/blob/main/frontend/app.js"
    click F3 "https://github.com/hash2004/nettrafficmonitor_cpp_js_flask/blob/main/frontend/index.html"
    click F4 "https://github.com/hash2004/nettrafficmonitor_cpp_js_flask/blob/main/frontend/styles.css"

    %% Styles
    classDef cpp fill:#AED6F1,stroke:#2471A3,stroke-width:2px;
    classDef data fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef python fill:#A9DFBF,stroke:#1E8449,stroke-width:2px;
    classDef frontend fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
    classDef note fill:#D7BDE2,stroke:#8E44AD,stroke-dasharray: 5 5,stroke-width:2px;
```
