How joaoclaudio82/apimovias works
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: services Services · relatorio_service.py Service · moviasai MOVIAS core · routers Routers
Components 20
External
Outside systems and shared runtime inputs
- Source DB · Upstream database read by the extractor
- Shared CSV · File-based output consumed by the AI service
Extractor
ETL service that reads source data and materializes the shared CSV
- Routes · HTTP endpoints for report extraction
extractor/api/routes/relatorio_route.py - Schemas · Validation models for extractor requests
extractor/api/schemas/relatorio_request.py - Service · Coordinates extraction and CSV generation
extractor/api/services/relatorio_service.py - Repository · Queries the external source database
extractor/api/repositories/relatorio_repository.py - CSV utils · Shared path and CSV handling utilities
extractor/api/utils/csv_utils.py
AI Service
ML serving, profile sync, persistence, and model assets
- API app · AI service entrypoint and router assembly
ai/api/app.py - Routers · Endpoints for ingestion, profiles, prediction, and training
ai/api/routers - Schemas · Request and response validation models
ai/api/schemas - Services · Coordinates ingestion, prediction, training, and profile workflows
ai/api/services - Profiles · Vehicle profile synchronization and derived profile sets
ai/api/services/vehicle_profile_service.py - Configs · Service-specific configuration for ingestion, prediction, training, and profiles
ai/api/config - AI DB · Separate persistence boundary for AI operational state
ai/api/database.py - ORM models · Structured application state mapped for persistence
ai/api/models.py - Migrations · Alembic migrations for the AI database
ai/api/migrations/versions/20260226_1839_3458e3990574_initial_schema.py - MOVIAS core · Dataset loading, feature engineering, clustering, classification, training, and prediction logic
ai/moviasai - Classifiers · Versioned classification artifacts for stage 1 and stage 2
ai/models/classification - TS models · Versioned time-series model families and checkpoints
ai/models/ts
Gateway
Thin public ingress and API composition layer
- Gateway · Public FastAPI entrypoint that composes the extractor and AI services
gateway/app.py
Connections 21
- 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