# fabiolabowlesdiaz/dym-wsr-generator architecture

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

- Interactive diagram: https://gitdiagram.com/fabiolabowlesdiaz/dym-wsr-generator
- Repository: https://github.com/fabiolabowlesdiaz/dym-wsr-generator

## Overview

1. **Repository purpose**
   This repo is a Python-based batch system that pulls sales data from a PostgreSQL DWH, computes WSR metrics, optionally enriches analysis with LLM-generated commentary, and emits a final HTML Weekly Sales Report for DYM.

2. **Main execution paths**
   The primary entry point is `wsr_generator_main.py`, with `generate_report.py` as an alternate runner. These scripts orchestrate the full pipeline: connect to the database, fetch source data, process metrics, generate charts/tables, and write the report to `./output/`.

3. **Data access boundary**
   `core/database.py` centralizes database connectivity and query execution against the DWH. The README indicates credentials and runtime configuration come from `.env`, and the system is intended to use read-only access to PostgreSQL.

4. **Core report processing**
   `core/data_processor.py` is the main transformation layer for WSR-specific business logic: filtering, exclusions, aggregation by brand/city/channel, and calculation of executive-summary metrics. This is where raw database rows become report-ready datasets.

5. **Report rendering**
   `core/html_generator.py` and `utils/html_tables.py` build the final HTML document and its tables. `core/trend_chart_generator.py` generates trend visualizations, so the final report combines structured tables with embedded charts in a single HTML artifact.

6. **AI-assisted narrative layer**
   `utils/llm_processor.py` is the shared LLM integration used to analyze manager comments and produce narrative text. The README shows OpenRouter/Claude configuration, so external model calls are part of the report-generation path when commentary analysis is enabled.

7. **Secondary projection subsystem**
   The `proyeccion_objetiva/` package is a distinct analytics domain for objective projections/nowcasting. It has its own config, data fetcher, statistical engine, driver-based operational engine, narrative generation, and visualization modules, suggesting a parallel pipeline for projection analysis rather than the core WSR report itself.

8. **Projection internal structure**
   Inside `proyeccion_objetiva/`, `pilar2_estadistica/` handles statistical/event-calendar logic, while `pilar3_operativa/` handles operational driver analysis and driver narratives. `nowcast_engine.py` and `projection_processor.py` appear to coordinate these pieces into projection outputs.

9. **Testing and validation**
   There are both package tests under `proyeccion_objetiva/tests/` and repository-level validation scripts such as `test_*.py`, `verify_calculation.py`, and `scripts/validate_drivers.py`. This suggests the codebase mixes unit tests with data/query validation checks against real business logic.

10. **Operational runtime and outputs**
   The system is designed for scheduled batch execution via cron or Windows Task Scheduler, not a long-running service. Its observable outputs are HTML reports in `./output/` and execution logs in `wsr_generator.log`, which form the main operational artifacts.

11. **Documentation and governance**
   The `.planning/` tree contains architecture, roadmap, milestone, and phase documents, while `manuales/` holds usage, installation, and maintenance guides. These do not affect runtime flow, but they indicate the repository is managed as a documented operational system with phased delivery and maintenance procedures.

12. **Key architectural boundary to draw**
   The clearest diagram boundary is: **source data and external services in**, **core processing and optional AI narrative in the middle**, **HTML report and charts out**. The projection package should be shown as a related but separable analytics subsystem that can be reused or extended independently of the main WSR generation flow.

Main entry points: [`core/data_processor.py`](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/core/data_processor.py) (Data processor), [`proyeccion_objetiva/projection_processor.py`](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/projection_processor.py) (Projection flow), [`core/html_generator.py`](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/core/html_generator.py) (HTML report), [`proyeccion_objetiva/pilar2_estadistica/statistical_engine.py`](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/pilar2_estadistica/statistical_engine.py) (Statistical engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_main_wsr["WSR Generator"]
  node_wsr_generator_main{{"Main runner<br/>entrypoint"}}
  node_generate_report{{"Alt runner<br/>entrypoint<br/>[generate_report.py]"}}
  node_database[("DB access<br/>postgres adapter<br/>[database.py]")]
  node_data_processor["Data processor<br/>wsr transforms<br/>[data_processor.py]"]
  node_trend_chart_generator["Trend charts<br/>charting"]
  node_html_generator["HTML report<br/>renderer<br/>[html_generator.py]"]
  node_html_tables["HTML tables<br/>table builder<br/>[html_tables.py]"]
  node_llm_processor["LLM narrative<br/>ai integration<br/>[llm_processor.py]"]
  node_output["Output report<br/>artifact"]
end

subgraph group_projection["Projection Analytics"]
  node_proyeccion_config["Projection config<br/>[config.py]"]
  node_data_fetcher[("Projection data<br/>data access<br/>[data_fetcher.py]")]
  node_statistical_engine["Statistical engine<br/>analytics"]
  node_event_calendar["Event calendar<br/>domain logic<br/>[event_calendar.py]"]
  node_drivers_engine["Drivers engine<br/>operational analytics<br/>[drivers_engine.py]"]
  node_drivers_narrative["Drivers narrative"]
  node_projection_processor{{"Projection flow<br/>orchestrator"}}
  node_nowcast_engine{{"Nowcast engine<br/>orchestrator<br/>[nowcast_engine.py]"}}
  node_projection_chart_generator["Projection charts<br/>charting"]
  node_projection_html_generator["Projection HTML<br/>renderer"]
end

subgraph group_support["Support & Docs"]
  node_tests["Validation<br/>tests and checks"]
  node_docs["Docs & planning<br/>documentation"]
end

node_wsr_generator_main -->|"read data"| node_database
node_generate_report -->|"read data"| node_database
node_database -->|"raw rows"| node_data_processor
node_data_processor -->|"metrics"| node_trend_chart_generator
node_data_processor -->|"table data"| node_html_tables
node_data_processor -->|"comments"| node_llm_processor
node_trend_chart_generator -->|"charts"| node_html_generator
node_html_tables -->|"tables"| node_html_generator
node_llm_processor -->|"narrative"| node_html_generator
node_html_generator -->|"write html"| node_output
node_proyeccion_config -->|"settings"| node_data_fetcher
node_data_fetcher -->|"projection inputs"| node_statistical_engine
node_event_calendar -->|"calendar logic"| node_statistical_engine
node_statistical_engine -->|"stats output"| node_projection_processor
node_drivers_engine -->|"driver output"| node_projection_processor
node_projection_processor -->|"analysis"| node_drivers_narrative
node_projection_processor -->|"series"| node_projection_chart_generator
node_projection_chart_generator -->|"charts"| node_projection_html_generator
node_drivers_narrative -->|"narrative"| node_projection_html_generator
node_projection_html_generator -->|"write html"| node_output
node_tests -.->|"validate"| node_data_processor
node_tests -.->|"validate"| node_statistical_engine
node_docs -.->|"guide"| node_wsr_generator_main
node_docs -.->|"guide"| node_projection_processor

click node_wsr_generator_main "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/wsr_generator_main.py"
click node_generate_report "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/generate_report.py"
click node_database "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/core/database.py"
click node_data_processor "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/core/data_processor.py"
click node_trend_chart_generator "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/core/trend_chart_generator.py"
click node_html_generator "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/core/html_generator.py"
click node_html_tables "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/utils/html_tables.py"
click node_llm_processor "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/utils/llm_processor.py"
click node_proyeccion_config "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/config.py"
click node_data_fetcher "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/data_fetcher.py"
click node_statistical_engine "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/pilar2_estadistica/statistical_engine.py"
click node_event_calendar "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/pilar2_estadistica/event_calendar.py"
click node_drivers_engine "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/pilar3_operativa/drivers_engine.py"
click node_drivers_narrative "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/pilar3_operativa/drivers_narrative.py"
click node_projection_processor "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/projection_processor.py"
click node_nowcast_engine "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/nowcast_engine.py"
click node_projection_chart_generator "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/visualizacion/projection_chart_generator.py"
click node_projection_html_generator "https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/visualizacion/projection_html_generator.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_wsr_generator_main,node_generate_report,node_database,node_data_processor,node_trend_chart_generator,node_html_generator,node_html_tables,node_llm_processor,node_output toneBlue
class node_proyeccion_config,node_data_fetcher,node_statistical_engine,node_event_calendar,node_drivers_engine,node_drivers_narrative,node_projection_processor,node_nowcast_engine,node_projection_chart_generator,node_projection_html_generator toneAmber
class node_tests,node_docs toneMint
```

## Components

### WSR Generator

Primary weekly sales report pipeline and outputs.

- [Main runner](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/wsr_generator_main.py): entrypoint · Primary batch entry point for WSR generation. · `wsr_generator_main.py`
- [Alt runner](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/generate_report.py): entrypoint · Alternative report execution script. · `generate_report.py`
- [DB access](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/core/database.py): postgres adapter · Centralized database connectivity and query execution. · `core/database.py`
- [Data processor](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/core/data_processor.py): wsr transforms · Filters, aggregates, and computes report metrics. · `core/data_processor.py`
- [Trend charts](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/core/trend_chart_generator.py): charting · Generates trend visualizations for the report. · `core/trend_chart_generator.py`
- [HTML report](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/core/html_generator.py): renderer · Assembles the final HTML report document. · `core/html_generator.py`
- [HTML tables](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/utils/html_tables.py): table builder · Builds reusable HTML table fragments. · `utils/html_tables.py`
- [LLM narrative](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/utils/llm_processor.py): ai integration · Generates narrative from manager comments and analysis. · `utils/llm_processor.py`
- **Output report**: artifact · Generated HTML reports written to disk.

### Projection Analytics

Secondary nowcasting and projection subsystem.

- [Projection config](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/config.py): config · Configuration for the projection subsystem. · `proyeccion_objetiva/config.py`
- [Projection data](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/data_fetcher.py): data access · Fetches source data for projection analysis. · `proyeccion_objetiva/data_fetcher.py`
- [Statistical engine](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/pilar2_estadistica/statistical_engine.py): analytics · Handles statistical and event-calendar projection logic. · `proyeccion_objetiva/pilar2_estadistica/statistical_engine.py`
- [Event calendar](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/pilar2_estadistica/event_calendar.py): domain logic · Calendar-aware statistical support for projections. · `proyeccion_objetiva/pilar2_estadistica/event_calendar.py`
- [Drivers engine](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/pilar3_operativa/drivers_engine.py): operational analytics · Analyzes operational drivers for projection outputs. · `proyeccion_objetiva/pilar3_operativa/drivers_engine.py`
- [Drivers narrative](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/pilar3_operativa/drivers_narrative.py): narrative · Produces explanatory text for driver-based analysis. · `proyeccion_objetiva/pilar3_operativa/drivers_narrative.py`
- [Projection flow](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/projection_processor.py): orchestrator · Coordinates projection processing across statistical and operational pillars. · `proyeccion_objetiva/projection_processor.py`
- [Nowcast engine](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/nowcast_engine.py): orchestrator · Coordinates nowcast-style projection execution. · `proyeccion_objetiva/nowcast_engine.py`
- [Projection charts](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/visualizacion/projection_chart_generator.py): charting · Generates projection visualizations. · `proyeccion_objetiva/visualizacion/projection_chart_generator.py`
- [Projection HTML](https://github.com/fabiolabowlesdiaz/dym-wsr-generator/blob/main/proyeccion_objetiva/visualizacion/projection_html_generator.py): renderer · Renders projection reports as HTML. · `proyeccion_objetiva/visualizacion/projection_html_generator.py`

### Support & Docs

Validation, scripts, manuals, and planning artifacts.

- **Validation**: tests and checks · Repository-level and package-level validation scripts and tests.
- **Docs & planning**: documentation · Operational manuals and planning artifacts.

## Connections

- Main runner → DB access: read data · Loads source sales data from PostgreSQL.
- Alt runner → DB access: read data · Alternative runner also pulls from the DWH.
- DB access → Data processor: raw rows · Provides queried data for WSR business logic.
- Data processor → Trend charts: metrics · Feeds processed trends into chart generation.
- Data processor → HTML tables: table data · Supplies structured datasets for HTML tables.
- Data processor → LLM narrative: comments · Optional narrative enrichment based on manager comments.
- Trend charts → HTML report: charts · Embeds charts into the final report.
- HTML tables → HTML report: tables · Renders tabular sections into the report.
- LLM narrative → HTML report: narrative · Adds AI-generated commentary to the HTML output.
- HTML report → Output report: write html · Writes the final report artifact to disk.
- Projection config → Projection data: settings · Provides runtime configuration for projection data access.
- Projection data → Statistical engine: projection inputs · Feeds fetched data into statistical analysis.
- Event calendar → Statistical engine: calendar logic · Supplies event-aware statistical context.
- Statistical engine → Projection flow: stats output · Contributes statistical projection signals.
- Drivers engine → Projection flow: driver output · Provides operational driver analysis.
- Projection flow → Drivers narrative: analysis · Generates explanatory narrative from projection results.
- Projection flow → Projection charts: series · Feeds data into projection visualization.
- Projection charts → Projection HTML: charts · Embeds projection charts into HTML.
- Drivers narrative → Projection HTML: narrative · Adds driver-based narrative to the projection report.
- Projection HTML → Output report: write html · Projection subsystem also emits HTML artifacts.
- Validation → Data processor: validate · Checks core business logic and calculations.
- Validation → Statistical engine: validate · Exercises projection statistics behavior.
- Docs & planning → Main runner: guide · Documents runtime usage and maintenance.
- Docs & planning → Projection flow: guide · Documents the projection subsystem design and flow.
