# joaoclaudio82/apimovias architecture

> Architecture diagram of the GitHub repository joaoclaudio82/apimovias, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/joaoclaudio82/apimovias
- Repository: https://github.com/joaoclaudio82/apimovias

## Overview

1. **The repository is a three-service stack, not a single app.**  
   - `extractor`: FastAPI service that reads from an external database and produces/updates a shared CSV.  
   - `ai`: FastAPI service that manages vehicle profiles, training, ingestion, and predictions.  
   - `gateway`: FastAPI entrypoint that exposes one public URL and prefixes the other two services behind it.

2. **`extractor` is the ingestion/ETL boundary.**  
   Its internal shape is a classic API-service-repository layout: `routes` → `services` → `repositories`, with schemas for request validation and small utils for CSV/path handling. The repository talks to an already-existing external database configured by `DATABASE_*` environment variables, and the service materializes the result into `extractor/data/movias.csv`.

3. **`ai` is the ML serving and lifecycle boundary.**  
   It is split into API routers, service layer, schemas, and configuration modules for data ingestion, prediction, training, and vehicle profiles. This service is responsible for syncing profile data, running predictions, and likely orchestrating model training or re-training workflows.

4. **`ai` also has its own persistence layer.**  
   The presence of `database.py`, `models.py`, and Alembic migrations indicates the AI service stores structured application state in a database separate from the extractor’s source DB. That DB likely holds profile metadata, ingestion state, training metadata, or similar operational records.

5. **The `ai/movias` package is the machine-learning core.**  
   This is where the actual domain logic lives: dataset loading, feature extraction, normalization, clustering, classification, prediction, training, metrics, and evaluation. The API layer appears to call into this package rather than implement ML logic inline.

6. **Model artifacts are first-class repository assets.**  
   Under `ai/models`, the repo stores versioned, pre-trained artifacts for multiple model families:
   - classification models in `stage1` and `stage2`
   - time-series models under `ts`, with families such as `DLinearModel`, `LightGBMModel`, `LinearRegressionModel`, `NLinearModel`, `TSMixerModel`, and `XGBModel`  
   The artifact folders also include metadata, scalers, checkpoints, and both `.pkl` and `.onnx` forms for some models, suggesting the runtime can load different formats depending on deployment/use case.

7. **The main runtime data flow is file-based and shared across containers.**  
   The extractor writes `movias.csv`, and the AI service consumes that file from a shared volume. The README says the AI monitors the CSV automatically and syncs vehicle profiles when it changes. This is the key coupling between the two services.

8. **Vehicle profiles are a central domain concept in the AI service.**  
   There are dedicated config, schema, router, and service modules for vehicle profiles. The README also names two synchronized targets, `km_dia_clean` and `h_dia_clean`, which implies the AI service derives or maintains multiple profile sets from the shared input data.

9. **`gateway` is a thin aggregation layer, not a business-logic service.**  
   It is the only service exposed on the host network. Requests are routed under `/ai/*` and `/extractor/*`, and the gateway also provides the unified Swagger surface. Architecturally, it acts as the single ingress and API composition point.

10. **Configuration is split by responsibility and deployment target.**  
   - Root `.env` contains external infrastructure settings, especially the extractor DB connection.  
   - `ai/config/*.yaml` and `extractor/api/settings.py` hold service-specific defaults.  
   - Docker Compose wires the services together and mounts the shared CSV directory.  
   This means the stack is designed for containerized deployment with clear service isolation.

11. **The repo supports both offline ML work and online API serving.**  
   The `ai/movias` package plus the stored artifacts suggest a training/evaluation pipeline that can be run independently of the API, while the FastAPI service exposes the operational side: ingestion, profile synchronization, predictions, and training triggers.

12. **For an architecture diagram, the main boundaries are:**  
   external source DB → `extractor` → shared CSV volume → `ai` ML/profile pipeline → prediction APIs, all fronted by `gateway`. The AI service’s own DB is a separate internal persistence boundary, while the model artifact tree is the offline/serving asset boundary.

Main entry points: [`ai/api/services`](https://github.com/joaoclaudio82/apimovias/tree/main/ai/api/services) (Services), [`extractor/api/services/relatorio_service.py`](https://github.com/joaoclaudio82/apimovias/blob/main/extractor/api/services/relatorio_service.py) (Service), [`ai/moviasai`](https://github.com/joaoclaudio82/apimovias/tree/main/ai/moviasai) (MOVIAS core), [`ai/api/routers`](https://github.com/joaoclaudio82/apimovias/tree/main/ai/api/routers) (Routers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_external["External"]
  node_source_db[("Source DB<br/>external database")]
  node_extractor_csv["Shared CSV<br/>handoff file"]
end

subgraph group_extractor["Extractor"]
  node_extractor_routes["Routes<br/>[relatorio_route.py]"]
  node_extractor_schemas["Schemas"]
  node_extractor_service["Service"]
  node_extractor_repo["Repository"]
  node_extractor_utils["CSV utils<br/>file helpers<br/>[csv_utils.py]"]
end

subgraph group_ai["AI Service"]
  node_ai_app["API app<br/>[app.py]"]
  node_ai_routers["Routers"]
  node_ai_schemas["Schemas"]
  node_ai_services["Services<br/>service layer"]
  node_ai_vehicle_profiles["Profiles<br/>profile sync"]
  node_ai_configs["Configs<br/>runtime config"]
  node_ai_db[("AI DB<br/>application database<br/>[database.py]")]
  node_ai_models["ORM models<br/>db models<br/>[models.py]"]
  node_ai_migrations["Migrations"]
  node_ai_core["MOVIAS core<br/>ML pipeline"]
  node_ai_classification_models["Classifiers<br/>model artifacts"]
  node_ai_ts_models["TS models<br/>model artifacts"]
end

subgraph group_gateway["Gateway"]
  node_gateway_app["Gateway<br/>API ingress<br/>[app.py]"]
end

node_extractor_routes -->|"validates"| node_extractor_schemas
node_extractor_routes -->|"calls"| node_extractor_service
node_extractor_service -->|"queries"| node_extractor_repo
node_extractor_repo -->|"reads"| node_source_db
node_extractor_service -->|"writes CSV"| node_extractor_utils
node_extractor_service -->|"produces"| node_extractor_csv
node_ai_app -->|"mounts"| node_ai_routers
node_ai_routers -->|"validates"| node_ai_schemas
node_ai_routers -->|"calls"| node_ai_services
node_ai_services -->|"reads config"| node_ai_configs
node_ai_services -->|"persists"| node_ai_db
node_ai_db -->|"maps"| node_ai_models
node_ai_migrations -->|"migrates"| node_ai_db
node_ai_services -->|"syncs"| node_ai_vehicle_profiles
node_ai_vehicle_profiles -->|"consumes"| node_extractor_csv
node_ai_services -->|"uses"| node_ai_core
node_ai_core -->|"loads"| node_ai_classification_models
node_ai_core -->|"loads"| node_ai_ts_models
node_ai_core -->|"computes"| node_ai_vehicle_profiles
node_gateway_app -->|"prefix /extractor"| node_extractor_routes
node_gateway_app -->|"prefix /ai"| node_ai_routers

click node_extractor_routes "https://github.com/joaoclaudio82/apimovias/blob/main/extractor/api/routes/relatorio_route.py"
click node_extractor_schemas "https://github.com/joaoclaudio82/apimovias/blob/main/extractor/api/schemas/relatorio_request.py"
click node_extractor_service "https://github.com/joaoclaudio82/apimovias/blob/main/extractor/api/services/relatorio_service.py"
click node_extractor_repo "https://github.com/joaoclaudio82/apimovias/blob/main/extractor/api/repositories/relatorio_repository.py"
click node_extractor_utils "https://github.com/joaoclaudio82/apimovias/blob/main/extractor/api/utils/csv_utils.py"
click node_ai_app "https://github.com/joaoclaudio82/apimovias/blob/main/ai/api/app.py"
click node_ai_routers "https://github.com/joaoclaudio82/apimovias/tree/main/ai/api/routers"
click node_ai_schemas "https://github.com/joaoclaudio82/apimovias/tree/main/ai/api/schemas"
click node_ai_services "https://github.com/joaoclaudio82/apimovias/tree/main/ai/api/services"
click node_ai_vehicle_profiles "https://github.com/joaoclaudio82/apimovias/blob/main/ai/api/services/vehicle_profile_service.py"
click node_ai_configs "https://github.com/joaoclaudio82/apimovias/tree/main/ai/api/config"
click node_ai_db "https://github.com/joaoclaudio82/apimovias/blob/main/ai/api/database.py"
click node_ai_models "https://github.com/joaoclaudio82/apimovias/blob/main/ai/api/models.py"
click node_ai_migrations "https://github.com/joaoclaudio82/apimovias/blob/main/ai/api/migrations/versions/20260226_1839_3458e3990574_initial_schema.py"
click node_ai_core "https://github.com/joaoclaudio82/apimovias/tree/main/ai/moviasai"
click node_ai_classification_models "https://github.com/joaoclaudio82/apimovias/tree/main/ai/models/classification"
click node_ai_ts_models "https://github.com/joaoclaudio82/apimovias/tree/main/ai/models/ts"
click node_gateway_app "https://github.com/joaoclaudio82/apimovias/blob/main/gateway/app.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_source_db,node_extractor_csv toneBlue
class node_extractor_routes,node_extractor_schemas,node_extractor_service,node_extractor_repo,node_extractor_utils toneAmber
class node_ai_app,node_ai_routers,node_ai_schemas,node_ai_services,node_ai_vehicle_profiles,node_ai_configs,node_ai_db,node_ai_models,node_ai_migrations,node_ai_core,node_ai_classification_models,node_ai_ts_models toneMint
class node_gateway_app toneRose
```

## Components

### External

Outside systems and shared runtime inputs

- **Source DB**: external database · Upstream database read by the extractor
- **Shared CSV**: handoff file · File-based output consumed by the AI service

### Extractor

ETL service that reads source data and materializes the shared CSV

- [Routes](https://github.com/joaoclaudio82/apimovias/blob/main/extractor/api/routes/relatorio_route.py): FastAPI routes · HTTP endpoints for report extraction · `extractor/api/routes/relatorio_route.py`
- [Schemas](https://github.com/joaoclaudio82/apimovias/blob/main/extractor/api/schemas/relatorio_request.py): request schemas · Validation models for extractor requests · `extractor/api/schemas/relatorio_request.py`
- [Service](https://github.com/joaoclaudio82/apimovias/blob/main/extractor/api/services/relatorio_service.py): ETL service · Coordinates extraction and CSV generation · `extractor/api/services/relatorio_service.py`
- [Repository](https://github.com/joaoclaudio82/apimovias/blob/main/extractor/api/repositories/relatorio_repository.py): DB repository · Queries the external source database · `extractor/api/repositories/relatorio_repository.py`
- [CSV utils](https://github.com/joaoclaudio82/apimovias/blob/main/extractor/api/utils/csv_utils.py): file helpers · Shared path and CSV handling utilities · `extractor/api/utils/csv_utils.py`

### AI Service

ML serving, profile sync, persistence, and model assets

- [API app](https://github.com/joaoclaudio82/apimovias/blob/main/ai/api/app.py): FastAPI app · AI service entrypoint and router assembly · `ai/api/app.py`
- [Routers](https://github.com/joaoclaudio82/apimovias/tree/main/ai/api/routers): API routers · Endpoints for ingestion, profiles, prediction, and training · `ai/api/routers`
- [Schemas](https://github.com/joaoclaudio82/apimovias/tree/main/ai/api/schemas): domain schemas · Request and response validation models · `ai/api/schemas`
- [Services](https://github.com/joaoclaudio82/apimovias/tree/main/ai/api/services): service layer · Coordinates ingestion, prediction, training, and profile workflows · `ai/api/services`
- [Profiles](https://github.com/joaoclaudio82/apimovias/blob/main/ai/api/services/vehicle_profile_service.py): profile sync · Vehicle profile synchronization and derived profile sets · `ai/api/services/vehicle_profile_service.py`
- [Configs](https://github.com/joaoclaudio82/apimovias/tree/main/ai/api/config): runtime config · Service-specific configuration for ingestion, prediction, training, and profiles · `ai/api/config`
- [AI DB](https://github.com/joaoclaudio82/apimovias/blob/main/ai/api/database.py): application database · Separate persistence boundary for AI operational state · `ai/api/database.py`
- [ORM models](https://github.com/joaoclaudio82/apimovias/blob/main/ai/api/models.py): db models · Structured application state mapped for persistence · `ai/api/models.py`
- [Migrations](https://github.com/joaoclaudio82/apimovias/blob/main/ai/api/migrations/versions/20260226_1839_3458e3990574_initial_schema.py): schema migrations · Alembic migrations for the AI database · `ai/api/migrations/versions/20260226_1839_3458e3990574_initial_schema.py`
- [MOVIAS core](https://github.com/joaoclaudio82/apimovias/tree/main/ai/moviasai): ML pipeline · Dataset loading, feature engineering, clustering, classification, training, and prediction logic · `ai/moviasai`
- [Classifiers](https://github.com/joaoclaudio82/apimovias/tree/main/ai/models/classification): model artifacts · Versioned classification artifacts for stage 1 and stage 2 · `ai/models/classification`
- [TS models](https://github.com/joaoclaudio82/apimovias/tree/main/ai/models/ts): model artifacts · Versioned time-series model families and checkpoints · `ai/models/ts`

### Gateway

Thin public ingress and API composition layer

- [Gateway](https://github.com/joaoclaudio82/apimovias/blob/main/gateway/app.py): API ingress · Public FastAPI entrypoint that composes the extractor and AI services · `gateway/app.py`

## Connections

- Routes → Schemas: validates · Request payloads are validated with schema models
- Routes → Service: calls · Routes delegate orchestration to the service layer
- Service → Repository: queries · Service reads source data through the repository
- Repository → Source DB: reads · Repository connects to the external database
- Service → CSV utils: writes CSV · Service uses file helpers to materialize the shared CSV
- Service → Shared CSV: produces · Extractor outputs the shared handoff file
- API app → Routers: mounts · FastAPI app assembles the API routers
- Routers → Schemas: validates · Routers rely on typed schemas
- Routers → Services: calls · HTTP handlers delegate business logic
- Services → Configs: reads config · Services use configuration modules and YAML-backed settings
- Services → AI DB: persists · Operational state is stored in the AI database
- AI DB → ORM models: maps · ORM models define database structures
- Migrations → AI DB: migrates · Alembic evolves the AI database schema
- Services → Profiles: syncs · Profile service keeps derived vehicle profiles up to date
- Profiles → Shared CSV: consumes · Profile sync is driven by the shared extractor output
- Services → MOVIAS core: uses · Service layer delegates ML work to the MOVIAS core
- MOVIAS core → Classifiers: loads · Core loads classification artifacts for inference and training
- MOVIAS core → TS models: loads · Core loads time-series artifacts and checkpoints
- MOVIAS core → Profiles: computes · Core derives and updates profile-related outputs
- Gateway → Routes: prefix /extractor · Gateway exposes the extractor surface behind a common ingress
- Gateway → Routers: prefix /ai · Gateway exposes the AI surface behind a common ingress
