From 63cf21c7e3e27d2804c00fc8c1160af4c41f75ab Mon Sep 17 00:00:00 2001 From: "Borrell.Guillem@bcg.com" Date: Mon, 18 Sep 2023 09:32:03 +0200 Subject: [PATCH] Copy all source code from the fundamentals course --- .pre-commit-config.yaml | 10 + pyproject.toml | 38 + src/retailtwin/__init__.py | 0 src/retailtwin/api/README.md | 30 + src/retailtwin/api/__init__.py | 0 src/retailtwin/api/composition.py | 0 src/retailtwin/api/db.py | 138 +++ src/retailtwin/api/main.py | 43 + src/retailtwin/api/settings.py | 7 + src/retailtwin/api/static/detail.html | 107 ++ src/retailtwin/api/static/index.html | 117 ++ src/retailtwin/api/static/search.html | 107 ++ src/retailtwin/bootstrap.py | 269 +++++ src/retailtwin/cli/__init__.py | 0 src/retailtwin/cli/data.py | 68 ++ src/retailtwin/cli/db.py | 109 ++ src/retailtwin/cli/pos/__init__.py | 0 src/retailtwin/cli/pos/main.py | 217 ++++ src/retailtwin/cli/pos/models.py | 61 + src/retailtwin/cli/stock.py | 166 +++ src/retailtwin/cli/tasks/__init__.py | 0 src/retailtwin/cli/tasks/main.py | 0 src/retailtwin/cli/tasks/models.py | 0 src/retailtwin/data/discounts.csv | 11 + src/retailtwin/data/products.csv | 451 ++++++++ src/retailtwin/data/random_people.csv | 1001 +++++++++++++++++ src/retailtwin/models.py | 160 +++ src/retailtwin/simulation.py | 44 + src/retailtwin/sql/__init__.py | 0 src/retailtwin/sql/add_item_to_cart.sql | 9 + src/retailtwin/sql/checkout_cart.sql | 6 + src/retailtwin/sql/fetch_from_shelf.sql | 15 + src/retailtwin/sql/price_on_location.sql | 14 + .../sql/random_available_on_location.sql | 27 + .../sql/random_cart_on_location.sql | 38 + .../sql/retire_batch_from_shelves.sql | 22 + src/retailtwin/sql/select_random_customer.sql | 7 + src/retailtwin/sql/select_random_location.sql | 8 + src/retailtwin/sql/stock_on_location.sql | 16 + src/retailtwin/sql/sync.py | 37 + src/retailtwin/sql/views/README.md | 3 + src/retailtwin/sql/views/inventory.sql | 18 + src/retailtwin/sql/views/stores.sql | 13 + src/retailtwin/sql/views/warehouse_stock.sql | 16 + src/retailtwin/utils.py | 5 + src/retailtwin/views.py | 36 + 46 files changed, 3444 insertions(+) create mode 100644 .pre-commit-config.yaml create mode 100644 src/retailtwin/__init__.py create mode 100644 src/retailtwin/api/README.md create mode 100644 src/retailtwin/api/__init__.py create mode 100644 src/retailtwin/api/composition.py create mode 100644 src/retailtwin/api/db.py create mode 100644 src/retailtwin/api/main.py create mode 100644 src/retailtwin/api/settings.py create mode 100644 src/retailtwin/api/static/detail.html create mode 100644 src/retailtwin/api/static/index.html create mode 100644 src/retailtwin/api/static/search.html create mode 100644 src/retailtwin/bootstrap.py create mode 100644 src/retailtwin/cli/__init__.py create mode 100644 src/retailtwin/cli/data.py create mode 100644 src/retailtwin/cli/db.py create mode 100644 src/retailtwin/cli/pos/__init__.py create mode 100644 src/retailtwin/cli/pos/main.py create mode 100644 src/retailtwin/cli/pos/models.py create mode 100644 src/retailtwin/cli/stock.py create mode 100644 src/retailtwin/cli/tasks/__init__.py create mode 100644 src/retailtwin/cli/tasks/main.py create mode 100644 src/retailtwin/cli/tasks/models.py create mode 100644 src/retailtwin/data/discounts.csv create mode 100644 src/retailtwin/data/products.csv create mode 100644 src/retailtwin/data/random_people.csv create mode 100644 src/retailtwin/models.py create mode 100644 src/retailtwin/simulation.py create mode 100644 src/retailtwin/sql/__init__.py create mode 100644 src/retailtwin/sql/add_item_to_cart.sql create mode 100644 src/retailtwin/sql/checkout_cart.sql create mode 100644 src/retailtwin/sql/fetch_from_shelf.sql create mode 100644 src/retailtwin/sql/price_on_location.sql create mode 100644 src/retailtwin/sql/random_available_on_location.sql create mode 100644 src/retailtwin/sql/random_cart_on_location.sql create mode 100644 src/retailtwin/sql/retire_batch_from_shelves.sql create mode 100644 src/retailtwin/sql/select_random_customer.sql create mode 100644 src/retailtwin/sql/select_random_location.sql create mode 100644 src/retailtwin/sql/stock_on_location.sql create mode 100644 src/retailtwin/sql/sync.py create mode 100644 src/retailtwin/sql/views/README.md create mode 100644 src/retailtwin/sql/views/inventory.sql create mode 100644 src/retailtwin/sql/views/stores.sql create mode 100644 src/retailtwin/sql/views/warehouse_stock.sql create mode 100644 src/retailtwin/utils.py create mode 100644 src/retailtwin/views.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..8b162db --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,10 @@ +repos: + - repo: https://github.com/psf/black + rev: 22.10.0 + hooks: + - id: black + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.0.278 + hooks: + - id: ruff diff --git a/pyproject.toml b/pyproject.toml index b771a71..a908c12 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,6 +7,44 @@ name = "retailtwin" authors = [{name = "Guillem Borrell", email = "borrell.guillem@bcg.com"}] readme = "README.md" dynamic = ["version", "description"] +dependencies = [ + "duckdb", + "pydantic", + "typer", + "pyyaml", + "pydantic-settings", + "polars", + "pyarrow", + "sqlalchemy[asyncio] > 2.0.13", + "adbc-driver-postgresql", + "adbc-driver-sqlite", + "prompt_toolkit", + "asyncpg", + "pydantic-settings" +] + [project.urls] Home = "https://github.com/Borrell-Guillem_bcgprod/retailtwin" + +[project.optional-dependencies] +doc = [ + "mkdocs-material", + "pymdown-extensions", + "mkdocstrings[python-legacy]>=0.18", + "mkdocs-gen-files", + "markdown-include", + "mkdocs-with-pdf", + "mkdocs-literate-nav"] + +dev = [ + "black", + "ruff", + "pre-commit" +] + +[project.scripts] +retailtwin = "retailtwin.cli.data:app" +stock = "retailtwin.cli.stock:app" +pos = "retailtwin.cli.pos.main:app" +tasks = "retailtwin.cli.tasks.main:app" \ No newline at end of file diff --git a/src/retailtwin/__init__.py b/src/retailtwin/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/retailtwin/api/README.md b/src/retailtwin/api/README.md new file mode 100644 index 0000000..ab0dfca --- /dev/null +++ b/src/retailtwin/api/README.md @@ -0,0 +1,30 @@ +# Super simple API-based stock terminal + +Run the backend with + +```bash +uvicorn dengfun.retail.api.main:app +``` + +Use Caddy to reverse proxy and the following Caddyfile. Paths are static so you have to run caddy from the root of the package + +``` +:80 { + handle_path /api/v1/* { + reverse_proxy localhost:8000 + } + file_server { + root src/dengfun/retail/api/static + } +} +``` + +If you execute + +```bash +caddy run -c Caddyfile +``` + +You should be able to browse the application at http://127.0.0.1, and reach the api docs at http://127.0.0.1/api/v1/docs + +Caddy can be installed in Windows with Chocolatey \ No newline at end of file diff --git a/src/retailtwin/api/__init__.py b/src/retailtwin/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/retailtwin/api/composition.py b/src/retailtwin/api/composition.py new file mode 100644 index 0000000..e69de29 diff --git a/src/retailtwin/api/db.py b/src/retailtwin/api/db.py new file mode 100644 index 0000000..fcfd473 --- /dev/null +++ b/src/retailtwin/api/db.py @@ -0,0 +1,138 @@ +import os +from operator import itemgetter +from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession +from sqlalchemy import select, desc +from dengfun.retail.views import get_inventory_view, get_stores_view +from dengfun.retail.api.settings import Settings + + +async def get_session(): + try: + engine = create_async_engine(Settings().db_uri.unicode_string()) + yield async_sessionmaker(engine, expire_on_commit=False) + except: # noqa + await engine.dispose() + + +def detail_button(batch: int): + return "".join( + [ + f'' + f'' + f"" + ] + ) + + +async def get_inventory( + store: int, + page: int, + pagesize: int, + sortby: str, + root_path: str, + async_session: async_sessionmaker[AsyncSession] = None, +) -> str: + """Get inventory for a given location + + Args: + store (int): Location id + page (int): Page number for pagination + pagesize (int): Page size for pagination + sortby (str): Column name used for sorting + root_path (str): Root path of the API + async_session (async_sessionmaker[AsyncSession], optional): + SQLAlchemy async session. Defaults to None. + + Returns: + str: HTML table rows with the content + """ + output = list() + inventory = get_inventory_view() + + # Selects the column from which to sort by, and if asc or desc + col_reversed = sortby.startswith("-") + col_selector = itemgetter(sortby.strip("-")) + + async with async_session() as session: + stmt = ( + select( + inventory.c[ + "upc", + "batch", + "name", + "package", + "received", + "best_until", + "quantity", + ] + ) + .where(inventory.c.location == store) + .limit(pagesize) + .offset(pagesize * page) + .order_by( + desc(col_selector(inventory.c)) + if col_reversed + else col_selector(inventory.c) + ) + ) + result = await session.execute(stmt) + + query_params = " ".join( + [ + f'js:{{page: "{page + 1}",' + f'pagesize: "{pagesize}",' + f'sortby: "{sortby}",' + 'store: document.getElementById("store").value}' + ] + ) + + # Turn the results in a sequence of HTML table rows + for i, record in enumerate(result.fetchall()): + line = "" + cells = ( + " ".join(f"{it}" for it in record) + + f" {detail_button(record[1])}" + ) + if i == (pagesize - 1): # Handle last row for infinite scrolling + line = " ".join( + [ + f'{cells}', + ] + ) + else: + line = f"{cells}" + + output.append(line) + + return os.linesep.join(output) + + +async def get_stores( + root_path: str, + async_session: async_sessionmaker[AsyncSession] = None, +): + # Build store options + store_rows = [''] + stores = get_stores_view() + async with async_session() as session: + stmt = select(stores) + result = await session.execute(stmt) + for record in result.fetchall(): + store_rows.append(f'') + + # Wrap with the select tag + query_params = ( + 'js:{page: "0", ' + 'pagesize: "20", ' + 'sortby: "upc", ' + 'store: document.getElementById("store").value}' + ) + return ( + f'