[
  {
    "path": ".gitignore",
    "content": ".venv\n*.egg-info/\n*.parquet\n*.db\ntmp*\n__pycache__\ntarget/\ndbt_packages/\nlogs/\n*.duckdb\n*.env\n**/.logs_queue\n**/.nux\n**/.telemetry\n**/history\n**/schedules\n**/storage\ndbt/postcard_company/*\n\n# Keeps\n!geography.csv\n!dashboard.json"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2023 Constantin Lungu\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# Portable Data Stack\n\nThis application is an Analytics suite for an imaginary company selling postcards. The company sells both directly but also through resellers in the majority of European countries.\n\n## Stack\n\n- Dagster\n- Docker (Docker Compose)\n- DuckDB\n- dbt core\n- Superset\n\n## Interested in the data model?\n\nGeneration of example data and the underlying dbt-core model is available in the [postcard-company-datamart](https://github.com/cnstlungu/postcard-company-datamart) project\n\n## For other stacks using the same dbt-core model, check the below:\n\n- [portable-data-stack-mage](https://github.com/cnstlungu/portable-data-stack-mage)\n- [portable-data-stack-airflow](https://github.com/cnstlungu/portable-data-stack-airflow)\n- [portable-data-stack-sqlmesh](https://github.com/cnstlungu/portable-data-stack-sqlmesh)\n\nImplementations with other tools with a different model:\n- [portable-data-stack-bruin](https://github.com/cnstlungu/portable-data-stack-bruin)\n- [postcard-company-dataform](https://github.com/cnstlungu/postcard-company-dataform)\n\n\nFor the legacy version involving OLTP, CSV and JSON sources, check out the `legacy-oltp` branch.\n\n### System requirements\n* [Docker](https://docs.docker.com/engine/install/)\n\n## Setup\n\n1. Rename `.env.example` file to `.env` and set your desired password. Remember to never commit files containing passwords or any other sensitive information.\n\n2. Rename `shared/db/datamart.duckdb.example` to `shared/db/datamart.duckdb` or init an empty database file there with that name.\n\n3. With **Docker Engine** installed, change directory to the root folder of the project (also the one that contains docker-compose.yml) and run\n\n    `docker compose up --build`\n\n    Note that this may take several minutes to completed. Check out the console to see when the Dagster interface is ready.\n\n4. Once the Docker suite has finished loading, open up [Dagster (dagit)](http://localhost:3000) , go to `Assets`, select all and click `Materialize selected`\n\n![Dagit](resources/dagit.png \"Dagit\")\n\n5. When the assets have been materialized, you can open the [Superset interface](http://localhost:8088)\n\n\n### Demo Credentials\n\nDemo credentials are set in the .env file mentioned above. \n\n### Ports exposed locally\n* Dagster (dagit): 3000\n* Superset: 8088\n\nGenerated parquet files are saved in the **shared** folder.\n\nThe data is fictional and automatically generated. Any similarities with existing persons, entities, products or businesses are purely coincidental.\n\n### General flow\n\n1. Generate test data as parquet files using Python\n2. Import data to the staging area in the Data Warehouse (DuckDB), orchestrated by Dagster\n3. Model data, build fact and dimension tables, load the Data Warehouse using dbt\n    - installs dbt dependencies\n    - seeds the database with static data (e.g. geography)\n    - runs the model\n    - tests the model\n4. Analyze and visually explore the data using Superset or directly query the Data Warehouse database instance\n\nFor superset, the default credentials are: user = admin, password = admin\n\n\n## Overview of architecture\n\nThe Docker process will begin building the application suite. The suite is made up of the following components, each within its own docker container:\n* **generator**: this is a collection of Python scripts that will generate, insert and export the example data\n* **dbt**: the data model, sourced from [postcard-company-datamart](https://github.com/cnstlungu/postcard-company-datamart) project\n* **dagster**: this is the orchestrator tool that will trigger the ETL tasks; its GUI is locally available on port 3000; \n* **superset**: this contains the web-based Business Intelligence application we will use to explore the data; exposed on port 8088.\n\nOnce the Docker building process has completed, we may open the Dagster GUI (locally: localhost:3000) to view and materialize our assets.\n\n\n\n![Dagster](resources/orchestration.png \"Orchestration with Dagster\")\n\n\nAfter the assets have been materialized you can either analyze the data using the querying and visualization tools provided by Superset (available locally on port 8088), or query the Data Warehouse (available as a DuckDB Database)\n\n![Apache Superset](resources/superset.png \"Superset\")\n\n\n## Credits\n\nInspired by:\n- [Build a poor man’s data lake from scratch with DuckDB](https://dagster.io/blog/duckdb-data-lake)\n- [Using dbt with Dagster software-defined assets](https://docs.dagster.io/integrations/dbt/using-dbt-with-dagster)\n"
  },
  {
    "path": "dagster/Dockerfile",
    "content": "FROM python:3.11-slim\n\nRUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/*\n\nRUN pip install uv \n\nRUN uv pip install --system dagster==1.11.13 \\\n    dagster-dbt==0.27.13 \\\n    duckdb==1.4.0 \\\n    dbt-core==1.10.13 \\\n    dbt-duckdb==1.9.6 \\\n    dagster-duckdb==0.27.13 \\\n    dagster-webserver==1.11.13 \\\n    \"pydantic<2.9.0\" \\\n    \"watchdog<5\"\n\nWORKDIR /\n\nCOPY dagster/definitions.py /definitions.py\nCOPY dagster/entrypoint.sh /entrypoint.sh\n\nRUN chmod +x /entrypoint.sh\n\nENTRYPOINT [\"/entrypoint.sh\"]\nCMD [\"-h\", \"0.0.0.0\", \"-f\", \"/definitions.py\"]"
  },
  {
    "path": "dagster/definitions.py",
    "content": "from pathlib import Path\n\nfrom dagster import Definitions, AssetExecutionContext\nfrom dagster_dbt import DbtCliResource, dbt_assets\n\n# Paths inside the container\nDBT_PROJECT_DIR = Path(\"/postcard_company\")\nDBT_PROFILES_DIR = Path(\"/postcard_company\")  # you have profiles.yml there\n\n# dbt CLI resource Dagster will use to run dbt\ndbt_resource = DbtCliResource(\n    project_dir=str(DBT_PROJECT_DIR),\n    profiles_dir=str(DBT_PROFILES_DIR),\n)\n\n# dbt manifest produced by `dbt compile` or `dbt build`\nMANIFEST_PATH = DBT_PROJECT_DIR / \"target\" / \"manifest.json\"\n\n\n@dbt_assets(manifest=MANIFEST_PATH)\ndef postcard_company_dbt_assets(\n    context: AssetExecutionContext,\n    dbt: DbtCliResource,\n):\n    # You can change to [\"run\"], [\"test\"], etc.\n    yield from dbt.cli([\"build\"], context=context).stream()\n\n\ndefs = Definitions(\n    assets=[postcard_company_dbt_assets],\n    resources={\"dbt\": dbt_resource},\n)"
  },
  {
    "path": "dagster/entrypoint.sh",
    "content": "#!/bin/bash\nset -e\n\necho \"Running dbt setup...\"\n\ncd /postcard_company\ndbt deps\ndbt seed\ndbt compile\n\necho \"Starting Dagster...\"\n\n# Forward CMD arguments to Dagster\nexec dagster dev \"$@\""
  },
  {
    "path": "dbt/Dockerfile",
    "content": "FROM python:3.11-slim\n\nRUN apt-get update && apt-get install -y git make automake gcc g++ subversion && rm -rf /var/lib/apt/lists/*\n\nRUN git clone -n --depth=1 --filter=tree:0 https://github.com/cnstlungu/postcard-company-datamart.git /datamart\n\nWORKDIR /datamart\n\nRUN git sparse-checkout set --no-cone postcard_company\n\nCMD [\"git\", \"checkout\"]"
  },
  {
    "path": "docker-compose.yml",
    "content": "services:\n  generator:\n    build:\n      context: .\n      dockerfile: ./generator/Dockerfile\n    volumes:\n      - ./shared:/shared\n    environment:\n      INPUT_FILES_PATH: /shared/parquet\n  dbt:\n    build:\n      context: .\n      dockerfile: ./dbt/Dockerfile\n    volumes:\n      - ./shared:/shared\n      - ./dbt/postcard_company:/datamart/postcard_company\n    environment:\n      INPUT_FILES_PATH: /shared/parquet  \n  dagster:\n    build:\n      context: .\n      dockerfile: ./dagster/Dockerfile\n    restart: always\n    environment:\n        DUCKDB_FILE_PATH: /shared/db/datamart.duckdb\n        INPUT_FILES_PATH: /shared/parquet\n    volumes:\n        - ./shared:/shared\n        - ./dbt/postcard_company:/postcard_company\n    ports:\n        - \"3000:3000\"\n    depends_on:\n      generator:\n        condition: service_completed_successfully\n      dbt:\n        condition: service_completed_successfully\n  superset:\n    build:\n      context: .\n      dockerfile: ./superset/Dockerfile\n      args:\n        SUPERSET_ADMIN: $SUPERSET_ADMIN\n        SUPERSET_PASSWORD: $SUPERSET_PASSWORD\n        SUPERSET_SECRET_KEY: ${SUPERSET_SECRET_KEY}\n    environment:\n      SUPERSET_SECRET_KEY: ${SUPERSET_SECRET_KEY}\n    ports:\n        - \"8088:8088\"\n    command:     gunicorn --bind  \"0.0.0.0:8088\" --access-logfile '-' --error-logfile '-' --workers 1 --worker-class gthread --threads 20 --timeout 60 --limit-request-line 0 --limit-request-field_size 0 \"superset.app:create_app()\"\n    post_start:\n      - command: \"superset import-dashboards -p ./dashboard.zip -u ${SUPERSET_ADMIN}\"\n    volumes:\n      - ./shared/db:/app/superset_home/db\n    depends_on:\n      - dagster\n"
  },
  {
    "path": "generator/Dockerfile",
    "content": "FROM python:3.11-slim\n\nRUN apt-get update && apt-get install -y git make automake gcc g++ subversion\n\nRUN git clone -n --depth=1 --filter=tree:0 https://github.com/cnstlungu/postcard-company-datamart.git /generator\n\nWORKDIR /generator\n\nRUN git sparse-checkout set --no-cone generator && git checkout\n\nWORKDIR /generator/generator\n\nRUN pip install uv\n\nRUN uv pip install --system -r requirements.txt\n\nCMD [\"python3\", \"generate.py\"]"
  },
  {
    "path": "shared/db/.keep",
    "content": ""
  },
  {
    "path": "shared/parquet/.keep",
    "content": ""
  },
  {
    "path": "superset/Dockerfile",
    "content": "FROM apache/superset:4.1.1\n\nARG SUPERSET_ADMIN\nARG SUPERSET_PASSWORD\nARG SUPERSET_SECRET_KEY\n# Switching to root to install the required packages\nUSER root\n\nRUN pip install uv\n\nCOPY --chown=superset:superset ./superset/assets .\nRUN uv pip install --system duckdb-engine==0.17.0 duckdb==1.4.4\nUSER superset\nRUN superset fab create-admin \\\n              --username ${SUPERSET_ADMIN} \\\n              --firstname Superset \\\n              --lastname Admin \\\n              --email admin@example.com \\\n              --password ${SUPERSET_PASSWORD}\nRUN superset db upgrade\nRUN superset init\nRUN superset set_database_uri -d DW  -u duckdb:///superset_home/db/datamart.duckdb"
  }
]