From 0ab72e33ff46625bec33f66f2a7570e58a8ae11c Mon Sep 17 00:00:00 2001 From: James Cranwell-Ward Date: Fri, 6 Aug 2021 23:00:54 +0200 Subject: [PATCH] add redis to improve caching --- .devcontainer/devcontainer.json | 47 ++++++++++--------- .devcontainer/docker-compose.yml | 38 +++++++++++++++ Dockerfile | 9 ++-- docker-compose.debug.yml | 14 ++++++ docker-compose.yml | 18 +++++++ requirements.txt | 1 + .../src/transmonee_dashboard/app.py | 6 +-- .../transmonee_dashboard/pages/base_page.py | 3 -- .../src/transmonee_dashboard/settings.py | 9 ++++ 9 files changed, 112 insertions(+), 33 deletions(-) create mode 100644 .devcontainer/docker-compose.yml create mode 100644 docker-compose.debug.yml create mode 100644 docker-compose.yml diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 80f1ad47..27fc4a94 100755 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,16 +1,20 @@ -// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at: -// https://github.com/microsoft/vscode-dev-containers/tree/v0.122.1/containers/python-3 +// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: +// https://github.com/microsoft/vscode-dev-containers/tree/v0.187.0/containers/docker-existing-docker-compose +// If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml. { - "name": "Python 3", - "build": { - "dockerfile": "Dockerfile", - "context": "..", - // Update 'VARIANT' to pick a Python version. Rebuild the container - // if it already exists to update. Available variants: 3, 3.6, 3.7, 3.8 - "args": { - "VARIANT": "3" - } - }, + "name": "Existing Docker Compose (Extend)", + // Update the 'dockerComposeFile' list if you have more compose files or use different names. + // The .devcontainer/docker-compose.yml file contains any overrides you need/want to make. + "dockerComposeFile": [ + "../docker-compose.yml", + "docker-compose.yml" + ], + // The 'service' property is the name of the service for the container that VS Code should + // use. Update this value and .devcontainer/docker-compose.yml to the real service name. + "service": "dash", + // The optional 'workspaceFolder' property is the path VS Code should open by default when + // connected. This is typically a file mount in .devcontainer/docker-compose.yml + "workspaceFolder": "/workspace", // Set *default* container specific settings.json values on container create. "settings": { "terminal.integrated.shell.linux": "/bin/bash", @@ -33,14 +37,15 @@ "extensions": [ "ms-python.python", "ms-azuretools.vscode-docker" - ], + ] // Use 'forwardPorts' to make a list of ports inside the container available locally. - "forwardPorts": [ - 8888, - 8050 - ], - // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "pip3 install --user -r requirements.txt", - // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root. - // "remoteUser": "vscode" + // "forwardPorts": [], + // Uncomment the next line if you want start specific services in your Docker Compose config. + // "runServices": [], + // Uncomment the next line if you want to keep your containers running after VS Code shuts down. + // "shutdownAction": "none", + // Uncomment the next line to run commands after the container is created - for example installing curl. + // "postCreateCommand": "apt-get update && apt-get install -y curl", + // Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root. + // "remoteUser": "appuser" } \ No newline at end of file diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml new file mode 100644 index 00000000..db8a83d5 --- /dev/null +++ b/.devcontainer/docker-compose.yml @@ -0,0 +1,38 @@ +version: '3.4' +services: + # Update this to the name of the service you want to work with in your docker-compose.yml file + dash: + # If you want add a non-root user to your Dockerfile, you can use the "remoteUser" + # property in devcontainer.json to cause VS Code its sub-processes (terminals, tasks, + # debugging) to execute as the user. Uncomment the next line if you want the entire + # container to run as this user instead. Note that, on Linux, you may need to + # ensure the UID and GID of the container user you create matches your local user. + # See https://aka.ms/vscode-remote/containers/non-root for details. + # + # user: vscode + + # Uncomment if you want to override the service's Dockerfile to one in the .devcontainer + # folder. Note that the path of the Dockerfile and context is relative to the *primary* + # docker-compose.yml file (the first in the devcontainer.json "dockerComposeFile" + # array). The sample below assumes your primary file is in the root of your project. + # + # build: + # context: . + # dockerfile: .devcontainer/Dockerfile + + volumes: + # Update this to wherever you want VS Code to mount the folder of your project + - .:/workspace:cached + + # Uncomment the next line to use Docker from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker-compose for details. + # - /var/run/docker.sock:/var/run/docker.sock + + # Uncomment the next four lines if you will use a ptrace-based debugger like C++, Go, and Rust. + # cap_add: + # - SYS_PTRACE + # security_opt: + # - seccomp:unconfined + + # Overrides default command so things don't shut down after the process ends. + command: /bin/sh -c "while sleep 1000; do :; done" + diff --git a/Dockerfile b/Dockerfile index 6b7fbc63..105a3db3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,10 +4,10 @@ FROM python:3.8-slim-buster EXPOSE 8000 # Keeps Python from generating .pyc files in the container -ENV PYTHONDONTWRITEBYTECODE=1 +# ENV PYTHONDONTWRITEBYTECODE=1 # Turns off buffering for easier container logging -ENV PYTHONUNBUFFERED=1 +# ENV PYTHONUNBUFFERED=1 # Install pip requirements # ADD requirements.txt . @@ -18,8 +18,9 @@ ADD . /app RUN pip install -r requirements.txt # Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights -RUN useradd appuser && chown -R appuser /app -USER appuser +# RUN useradd appuser && chown -R appuser /app +# USER appuser +# ENV HOME /home/appuser # During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug CMD ["run-transmonee_dashboard-dev"] diff --git a/docker-compose.debug.yml b/docker-compose.debug.yml new file mode 100644 index 00000000..7994a9e8 --- /dev/null +++ b/docker-compose.debug.yml @@ -0,0 +1,14 @@ +version: '3.4' + +services: + dash: + image: dash + build: + context: . + dockerfile: ./Dockerfile + command: ["sh", "-c", "pip install debugpy -t /tmp && python /tmp/debugpy --wait-for-client --listen 0.0.0.0:5678 -m flask run --no-debugger --no-reload --host 0.0.0.0 --port 8000"] + ports: + - 8000:8000 + - 5678:5678 + environment: + - FLASK_APP=transmonee_dashboard/src/transmonee_dashboard/app.py diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..6066504d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3.4' + +services: + dash: + image: dash + build: + context: . + dockerfile: ./Dockerfile + env_file: + - .env + ports: + - 8000:8000 + + redis: + image: redis + container_name: redis-container + ports: + - "6379:6379" diff --git a/requirements.txt b/requirements.txt index 957fa29b..8bbe76a0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -59,5 +59,6 @@ uritemplate==3.0.1 urllib3==1.26.5 wcwidth==0.2.5 Werkzeug==1.0.1 +redis sentry-sdk[flask] diff --git a/transmonee_dashboard/src/transmonee_dashboard/app.py b/transmonee_dashboard/src/transmonee_dashboard/app.py index 9a0f434d..92e6fd4c 100755 --- a/transmonee_dashboard/src/transmonee_dashboard/app.py +++ b/transmonee_dashboard/src/transmonee_dashboard/app.py @@ -23,11 +23,7 @@ app = create_dash(server) # define a cache instance -# TODO: Move configuration to settings -# TODO: for prod move to redis or similar -cache = Cache( - app.server, config={"CACHE_TYPE": "filesystem", "CACHE_DIR": "cache-directory"} -) +cache = Cache(app.server) # Push an application context so we can use Flask's 'current_app' diff --git a/transmonee_dashboard/src/transmonee_dashboard/pages/base_page.py b/transmonee_dashboard/src/transmonee_dashboard/pages/base_page.py index 48ed9ebb..3197c512 100644 --- a/transmonee_dashboard/src/transmonee_dashboard/pages/base_page.py +++ b/transmonee_dashboard/src/transmonee_dashboard/pages/base_page.py @@ -77,7 +77,6 @@ def get_base_layout(**kwargs): kwargs.get("hash") if kwargs.get("hash") else "#{}".format((next(iter(indicators_dict.items())))[0].lower()) - # else "#{}".format(next(iter(indicators_dict.values()))["NAME"].lower()) ) return html.Div( @@ -641,7 +640,6 @@ def indicator_card( ) else "None" ) - print(name) card = dbc.Card( [ dbc.CardBody( @@ -699,7 +697,6 @@ def get_card_popover_body(sources): for index, source_info in enumerate(sources): countries.append(f"- {source_info[0]}: {source_info[1]}") card_countries = "\n".join(countries) - print(card_countries) return card_countries diff --git a/transmonee_dashboard/src/transmonee_dashboard/settings.py b/transmonee_dashboard/src/transmonee_dashboard/settings.py index da6c4107..33cfb78f 100755 --- a/transmonee_dashboard/src/transmonee_dashboard/settings.py +++ b/transmonee_dashboard/src/transmonee_dashboard/settings.py @@ -2,6 +2,7 @@ # Flask instance used by the Dash app. Any values corresponding to Dash # keword arguments will be passed They must be in UPPER CASE in order to take effect. For more information see # http://flask.pocoo.org/docs/config. +import os # Your App's title. The value of this parameter will be propagated into # `app.title` @@ -61,3 +62,11 @@ # The ID of the element used to inject the navbar items into NAVBAR_CONTAINER_ID = "navbar-items" + +# CACHE settings +CACHE_TYPE = os.environ.get("CACHE_TYPE") +CACHE_REDIS_HOST = os.environ.get("CACHE_REDIS_HOST") +CACHE_REDIS_PORT = os.environ.get("CACHE_REDIS_PORT") +CACHE_REDIS_DB = os.environ("CACHE_REDIS_DB") +CACHE_REDIS_URL = os.environ("CACHE_REDIS_URL") +CACHE_DEFAULT_TIMEOUT = os.environ("CACHE_DEFAULT_TIMEOUT")