# ai-portfolio-study-org/grafana_monitor architecture

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

- Interactive diagram: https://gitdiagram.com/ai-portfolio-study-org/grafana_monitor
- Repository: https://github.com/ai-portfolio-study-org/grafana_monitor

## Overview

##### Project type and purpose
- This is a real-time anomaly-detection and monitoring pipeline built as a Docker-Compose–orchestrated microservices system. It uses Kafka for event ingestion, Spark for streaming analytics, Airflow for orchestration, FastAPI as a metrics/API service, and Prometheus + Grafana for observability.

##### File structure
- Top-level services (via docker-compose.yml):
- backend/ – FastAPI service (server.py + metric endpoints)
- dags/ – Airflow DAG definitions (model_training.py)
- kafka/ – Kafka broker config & JMX exporter
- spark/ – Spark jobs (anomaly_detection.py, kafka_test.py) + JMX exporter
- prometheus/ – Prometheus scrape configuration
- grafana/ – Grafana dashboards + datasource provisioning
- graph/ – Graph-neural-network model code (dataset.py, gnn_model.py)
- send_transactions.py – synthetic transaction producer
- Key patterns: stream-processing (Spark), pub/sub (Kafka), orchestration (Airflow), service/API layer (FastAPI), monitoring (Prometheus/Grafana).

##### README for additional insights
- Confirms full pipeline:
- Real-time transaction ingestion → Spark streaming anomaly detection → metric emission → API serving & monitoring
- Periodic offline evaluation / model retraining via Airflow → metrics POSTed to FastAPI → Prometheus updates → Grafana dashboards

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Ingestion Layer
    subgraph "Ingestion Layer"
        Producer["Data Producer\n(send_transactions.py)"]:::compute
        KafkaBroker["Kafka Broker\n(9092)"]:::data
    end

    %% Processing Layer
    subgraph "Processing Layer"
        SparkEngine["Spark Structured Streaming\n(anomaly_detection.py)"]:::compute
    end

    %% Orchestration Layer
    subgraph "Orchestration Layer"
        Airflow["Airflow DAG\n(model_training.py)"]:::compute
    end

    %% Serving Layer
    subgraph "Serving Layer"
        FastAPIService["FastAPI Metrics Server\n(server.py)"]:::api
    end

    %% Observability Layer
    subgraph "Observability Layer"
        Prometheus["Prometheus\n(prometheus.yml)"]:::monitor
        Grafana["Grafana Dashboards\n(fastapi_metrics_dashboard.json)"]:::monitor
    end

    %% Container Orchestration
    DockerCompose["Docker-Compose"]:::compute

    %% Connections
    Producer -->|"publishes events"| KafkaBroker
    KafkaBroker -->|"subscribes"| SparkEngine
    SparkEngine -->|dashed metrics POST| FastAPIService
    Airflow -.->|triggers retraining| SparkEngine
    Airflow -.->|posts offline metrics| FastAPIService
    FastAPIService -->|"exposes metrics"| Prometheus
    KafkaBroker -->|"JMX metrics"| Prometheus
    SparkEngine -->|"JMX metrics"| Prometheus
    Prometheus -->|"feeds metrics"| Grafana
    DockerCompose -->|orchestrates| Producer
    DockerCompose -->|orchestrates| KafkaBroker
    DockerCompose -->|orchestrates| SparkEngine
    DockerCompose -->|orchestrates| Airflow
    DockerCompose -->|orchestrates| FastAPIService
    DockerCompose -->|orchestrates| Prometheus
    DockerCompose -->|orchestrates| Grafana

    %% Click Events
    click Producer "https://github.com/ai-portfolio-study-org/grafana_monitor/blob/main/send_transactions.py"
    click KafkaBroker "https://github.com/ai-portfolio-study-org/grafana_monitor/blob/main/kafka/kafka-2_0_0.yml"
    click SparkEngine "https://github.com/ai-portfolio-study-org/grafana_monitor/blob/main/spark/anomaly_detection.py"
    click Airflow "https://github.com/ai-portfolio-study-org/grafana_monitor/blob/main/dags/model_training.py"
    click FastAPIService "https://github.com/ai-portfolio-study-org/grafana_monitor/blob/main/backend/app/server.py"
    click Prometheus "https://github.com/ai-portfolio-study-org/grafana_monitor/blob/main/prometheus/prometheus.yml"
    click Grafana "https://github.com/ai-portfolio-study-org/grafana_monitor/blob/main/grafana/dashboards/fastapi_metrics_dashboard.json"
    click DockerCompose "https://github.com/ai-portfolio-study-org/grafana_monitor/blob/main/docker-compose.yml"

    %% Styles
    classDef compute fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef data fill:#90EE90,stroke:#333,stroke-width:1px
    classDef api fill:#FFA500,stroke:#333,stroke-width:1px
    classDef monitor fill:#FFB6C1,stroke:#333,stroke-width:1px
```
