# rafaelsancheznavarro/tfg_app architecture

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

- Interactive diagram: https://gitdiagram.com/rafaelsancheznavarro/tfg_app
- Repository: https://github.com/rafaelsancheznavarro/tfg_app

## Overview

##### Proyecto y propósito
Este es una aplicación web en Python/Streamlit para analizar modelos de Machine Learning (clasificación, clustering, detección de anomalías) sobre tráfico de red, con simulación de datos en tiempo real usando Kafka.

##### Componentes principales
- Frontend: Streamlit (app.py + app/views/*).
- Lógica de negocio: módulos en app/logic (carga de datos, streaming Kafka Producer/Consumer, evaluación).
- Almacenamiento local: CSVs y modelos .pkl en app/data.
- Streaming: Kafka + Zookeeper (docker-compose).
- Utilidades: app/utils (configuración, helpers, detección de anomalías).

##### Relaciones e interacciones
- Usuario interactúa con Streamlit → vistas llaman funciones de app/logic.
- Producer lee CSVs → envía paquetes a Kafka.
- Consumer suscrito a Kafka → recibe paquetes → alimenta pipelines de evaluación/clustering/anomalías.
- Lógica de evaluación usa modelos .pkl y datos entrantes → devuelve métricas.
- Streamlit actualiza gráficos y tablas con resultados.

##### Patrones y tecnologías clave
- Patrón MVC ligero: views (UI) ↔ logic (control) ↔ data (modelos y CSV).
- Microservicio de streaming interno: Producer/Consumer con threads y Kafka.
- Docker Compose para orquestar Kafka/Zookeeper.
- Frameworks: Python 3.10+, Streamlit, confluent-kafka (o kafka-python), scikit-learn, t-SNE.

##### Cómo dibujar el diagrama
a. Capas/Componentes:
- UI (Streamlit).
- Controladores (views → logic).
- Servicio de streaming (Kafka Producer/Consumer + Docker).
- Almacenamiento (CSV local, modelos .pkl).
- Lógica de ML (clasificación, DBSCAN, Isolation Forest).

b. Flujo de datos (flechas):
1. UI → load_data.py → datos CSV.
2. Producer → Kafka topic.
3. Consumer → logic → modelos → métricas.
4. Lógica → UI (resultados).

c. Destacar:
- Docker Compose orquesta Kafka/ZK (resaltar en un rectángulo separado).
- Threading interno en Producer (nota).

##### Recomendaciones de formato
- Etiquetas claras para cada rectángulo.
- Flechas direccionales con nombres (p.ej. “send()”, “consume()”).
- Colores/formas:
- Azul: componentes Python/Streamlit.
- Verde: pipelines ML.
- Naranja: Kafka/Zookeeper.
- Gris: almacenamiento local.

##### Toques finales
- Incluye leyenda de colores.
- Agrupa en capas horizontales: UI arriba, lógica en medio, datos/servicios abajo.
- Añade notas breves (p.ej. “Threaded Producer”).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "UI Layer"
        direction TB
        A_app["app.py"]:::frontend
        A_inicio["inicio.py"]:::frontend
        A_arch2["show_architecture_2.py"]:::frontend
        A_dbscan["show_dbscan.py"]:::frontend
        A_iso["show_isolation.py"]:::frontend
    end

    %% Business Logic Layer
    subgraph "Business Logic"
        direction TB
        L_load["load_data.py"]:::backend
        L_process["data_processing.py"]:::backend
        L_model["model_loading.py"]:::backend
        L_eval["evaluate_architecture_2.py"]:::ml
    end

    %% Streaming Service
    subgraph "Streaming Service"
        direction TB
        S_docker["Docker Compose\n(Kafka & Zookeeper)"]:::streaming
        S_prod["producer.py\n(Threaded Producer)"]:::streaming
        S_cons["consumer.py"]:::streaming
    end

    %% Utilities
    subgraph "Utilities"
        direction TB
        U_config["config_loader.py"]:::util
        U_helpers["helpers.py"]:::util
        U_anom["anomalies.py"]:::util
    end

    %% Storage Layer
    subgraph "Storage"
        direction TB
        D_data["app/data"]:::storage
        C_models["Models & CSVs"]:::storage
    end

    %% Configuration
    Cfg["config.yaml"]:::config

    %% Data Flows
    A_app -->|"load_csv()"| L_load
    A_app -->|"invoke views"| A_inicio
    A_app -->|"invoke views"| A_arch2
    A_app -->|"invoke views"| A_dbscan
    A_app -->|"invoke views"| A_iso

    L_load -->|"reads CSV"| D_data
    L_load -->|"passes data"| S_prod
    S_prod -->|"send() to topic"| S_docker
    S_cons -->|"consume() from topic"| S_docker
    S_cons -->|"data stream"| L_process
    L_process -->|"preprocess"| L_model
    L_model -->|"load .pkl"| C_models
    L_eval -->|"evaluate"| L_model
    L_process -->|"features"| L_eval
    L_eval -->|"metrics"| A_app

    U_config -->|loads| Cfg
    U_helpers -->|assist| L_load
    U_anom -->|detect| L_process

    %% Click Events
    click A_app "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app.py"
    click A_inicio "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/views/inicio.py"
    click A_arch2 "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/views/show_architecture_2.py"
    click A_dbscan "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/views/show_dbscan.py"
    click A_iso "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/views/show_isolation.py"
    click L_load "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/logic/load_data.py"
    click L_process "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/logic/data_processing.py"
    click L_model "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/logic/model_loading.py"
    click L_eval "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/logic/evaluate_architecture_2.py"
    click S_prod "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/logic/producer.py"
    click S_cons "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/logic/consumer.py"
    click D_data "https://github.com/rafaelsancheznavarro/tfg_app/tree/main/app/data/"
    click U_config "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/utils/config_loader.py"
    click U_helpers "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/utils/helpers.py"
    click U_anom "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/app/utils/anomalies.py"
    click Cfg "https://github.com/rafaelsancheznavarro/tfg_app/blob/main/config.yaml"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef backend fill:#87CEEB,stroke:#333,stroke-width:1px
    classDef ml fill:#98FB98,stroke:#333,stroke-width:1px
    classDef streaming fill:#FFA500,stroke:#333,stroke-width:1px
    classDef storage fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef util fill:#FFE4B5,stroke:#333,stroke-width:1px
    classDef config fill:#E0E0E0,stroke:#333,stroke-width:1px
```
