# tsffarias/docstream architecture

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

- Interactive diagram: https://gitdiagram.com/tsffarias/docstream
- Repository: https://github.com/tsffarias/docstream

## Overview

##### Project Type and Purpose
- DocStream is a full-stack data-engineering pipeline for automating PDF data extraction, transformation, storage, and visualization.
- Purpose: Ingest PDFs into AWS S3, trigger processing via SQS, extract tables/text with Python, land raw data in PostgreSQL, transform via DBT (bronze→silver→gold), and present KPIs through a Streamlit dashboard.

##### File Structure
- Top-level:
- docker/: Dockerfile, docker-compose for containerizing services
- src/: core Python code, configs, dbt project, file-stores, frontend
- src/configs/: extraction/queue/database rules and AWS tool wrappers (boto3)
- src/dbt_dw/: DBT project with models (bronze/silver/gold layers), macros, tests
- src/files/: sample CSV/PDF data organized by “jornada” and “redrex”
- src/frontend/: Streamlit app (app.py)
- extractor scripts at project root: pdf_viz.py, table_pdf_extractor.py, text_pdf_extractor.py
- Patterns:
- Event-driven ETL: S3 → SQS → worker
- Layered data transformations (DBT bronze/silver/gold)
- Separation of concerns: extraction logic, config, storage, analytics.

##### Extracted Insights from README
- Describes AWS services: S3 (storage), SQS (messaging), EC2 (compute)
- Postgres as OLTP/analytical store
- DBT for SQL-based transformations and data modeling
- Streamlit for interactive frontend

##### Main Components & Their Interactions
a. Components
1. Client/User: accesses Streamlit UI to upload PDFs or view dashboards
2. AWS S3 Bucket: stores incoming PDF files
3. AWS SQS Queue: receives S3-object-created events
4. EC2 Worker (or container):
- Polls SQS every 2 minutes via Python script (src/__init__.py)
- Downloads PDF from S3
- Invokes extraction modules:
- table_pdf_extractor.py (Camelot/Tabula)
- text_pdf_extractor.py (PyPDF2/Regex)
- Writes raw records to PostgreSQL
5. PostgreSQL Database: initial landing (raw tables) and final store
6. DBT Transformation Engine:
- Bronze layer (raw staging models)
- Silver layer (cleaned, joined models)
- Gold layer (analytics-ready tables)
7. Streamlit Frontend:
- src/frontend/app.py reads from Postgres via SQLAlchemy
- Displays KPIs, allows file upload (direct to S3 or local tests)
b. Data Flow
- User → Streamlit UI → (direct S3 upload or local file drop)
- S3 upload → S3 emits event → SQS queue
- Worker polls SQS → fetch message → download PDF → extract → write to Postgres
- DBT processes raw tables → transforms → loads gold tables
- Streamlit queries Postgres gold tables → renders dashboards

##### Architectural Patterns & Principles
- Event-Driven Architecture: decouples ingestion (S3) from processing (worker via SQS)
- Layered ETL/ELT: raw staging → curated → analytics
- Modular Design: separate configs, extraction logic, transformation models, UI
- Infrastructure as Code: Docker + Makefile + docker-compose for local dev parity
- Scalability: AWS managed services (S3/SQS), containerizable worker and DBT tasks

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend/UI
    User["User (Streamlit UI)"]:::ui
    Streamlit["Streamlit Frontend"]:::ui
    click Streamlit "https://github.com/tsffarias/docstream/blob/main/src/frontend/app.py"

    %% AWS Services
    subgraph "AWS Services"
        S3["AWS S3 Bucket"]:::aws
        SQS["AWS SQS Queue"]:::aws
    end

    %% Compute Worker
    subgraph "Compute Worker"
        EC2["EC2 Worker"]:::compute
        subgraph "Worker Modules"
            A1["S3 Wrapper"]:::config
            A2["SQS Wrapper"]:::config
            A3["Queue Abstraction"]:::config
            A4["Postgres Util"]:::config
            A5["Extraction Rules"]:::config
            A6["Table Extractor"]:::compute
            A7["Text Extractor"]:::compute
            A8["PDF Viz Utility"]:::compute
            A9["Worker Orchestrator"]:::compute
        end
    end
    click A1 "https://github.com/tsffarias/docstream/blob/main/src/configs/tools/aws/s3.py"
    click A2 "https://github.com/tsffarias/docstream/blob/main/src/configs/tools/aws/sqs.py"
    click A3 "https://github.com/tsffarias/docstream/blob/main/src/configs/tools/queue.py"
    click A4 "https://github.com/tsffarias/docstream/blob/main/src/configs/tools/postgre.py"
    click A5 "https://github.com/tsffarias/docstream/blob/main/src/configs/rules/notas.py"
    click A6 "https://github.com/tsffarias/docstream/blob/main/src/table_pdf_extractor.py"
    click A7 "https://github.com/tsffarias/docstream/blob/main/src/text_pdf_extractor.py"
    click A8 "https://github.com/tsffarias/docstream/blob/main/src/pdf_viz.py"
    click A9 "https://github.com/tsffarias/docstream/blob/main/src/__init__.py"

    %% Data Store
    subgraph "Data Store"
        Postgres["PostgreSQL Database"]:::db
    end

    %% DBT Transformation
    subgraph "DBT Transformation"
        DBT["DBT Engine"]:::transform
        subgraph "Models"
            Bronze["Bronze Models"]:::transform
            Silver["Silver Models"]:::transform
            Gold["Gold Models"]:::transform
        end
    end
    click DBT "https://github.com/tsffarias/docstream/blob/main/src/dbt_dw/dbt_project.yml"
    click Bronze "https://github.com/tsffarias/docstream/tree/main/src/dbt_dw/models/bronze"
    click Silver "https://github.com/tsffarias/docstream/tree/main/src/dbt_dw/models/silver"
    click Gold "https://github.com/tsffarias/docstream/tree/main/src/dbt_dw/models/gold"

    %% Containerization Infrastructure
    subgraph "Containerization"
        Dockerfile["Dockerfile"]:::config
        Compose["docker-compose.yml"]:::config
        Makefile["Makefile"]:::config
    end
    click Dockerfile "https://github.com/tsffarias/docstream/tree/main/docker/Dockerfile"
    click Compose "https://github.com/tsffarias/docstream/blob/main/docker/docker-compose.yml"
    click Makefile "https://github.com/tsffarias/docstream/tree/main/Makefile"

    %% Sample File Store
    Sample["Sample File Store"]:::db
    click Sample "https://github.com/tsffarias/docstream/tree/main/src/files"

    %% Connections
    User -->|access/upload| Streamlit
    Streamlit -->|upload PDF| S3
    S3 -->|event notification| SQS
    SQS -->|poll every 2min| EC2
    EC2 -->|download PDF| S3
    EC2 -->|invoke table extraction| A6
    EC2 -->|invoke text extraction| A7
    EC2 -->|invoke PDF viz| A8
    A6 -->|write raw records| Postgres
    A7 -->|write raw records| Postgres
    Postgres -->|raw tables| DBT
    DBT -->|load gold tables| Postgres
    Streamlit -->|query gold tables| Postgres

    %% Styles
    classDef aws fill:#F58536,stroke:#333,stroke-width:1px;
    classDef compute fill:#5DA5DA,stroke:#333,stroke-width:1px;
    classDef db fill:#60BD68,stroke:#333,stroke-width:1px;
    classDef transform fill:#B276B2,stroke:#333,stroke-width:1px;
    classDef ui fill:#4EBADC,stroke:#333,stroke-width:1px;
    classDef config fill:#CCCCCC,stroke:#333,stroke-width:1px;
```
