From c33351d968c6012da5d8b29341ee76f6f480a409 Mon Sep 17 00:00:00 2001 From: Stefan Stammberger Date: Sun, 25 Jul 2021 17:24:33 +0200 Subject: [PATCH] refactor: move some config vars to the .env file --- .env => .env_sample | 12 ++++++++++++ .gitignore | 1 + README.md | 2 +- app/auth/auth_handler.py | 3 ++- app/repositories/hardware_info.py | 11 ++++++----- 5 files changed, 22 insertions(+), 7 deletions(-) rename .env => .env_sample (63%) diff --git a/.env b/.env_sample similarity index 63% rename from .env rename to .env_sample index beabb54..0a8d35b 100644 --- a/.env +++ b/.env_sample @@ -1,5 +1,17 @@ secret=please_please_update_me_please algorithm=HS256 +# expiry time in seconds +jwt_expiry_time=300 + +# Amount of seconds the app will wait until it'll +# send another hardware update +gather_hw_info_interval = 2 +# Amount of seconds the app will gather CPU usage data for each update +# The resulting CPU usage is averaged over this period of time. +# To get realistic results at least 0.1 seconds is recommended +# must be less than and not equal to gather_hw_info_interval +cpu_usage_averaging_period = 0.5 + # Redis - uncomment if Redis runs with non standard values (i.e. in Docker etc) # redis_host=127.0.0.1 diff --git a/.gitignore b/.gitignore index bee8a64..6d17870 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__ +.env \ No newline at end of file diff --git a/README.md b/README.md index 3b456ff..a6853c7 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## Configuration -Update the `.env` file with your `bitcoind` and `lnd` configuration. +Create a `.env` file with your `bitcoind` and `lnd` configuration. See the `.env_sample` file for all configuration options. Please note that you will also need to install [Redis](https://redis.io/). diff --git a/app/auth/auth_handler.py b/app/auth/auth_handler.py index 2567def..cb56182 100644 --- a/app/auth/auth_handler.py +++ b/app/auth/auth_handler.py @@ -6,10 +6,11 @@ from decouple import config JWT_SECRET = config("secret") JWT_ALGORITHM = config("algorithm") +JWT_EXPIRY_TIME = config("jwt_expiry_time", default=300.0, cast=float) def signJWT() -> Dict[str, str]: - payload = {"user_id": "admin", "expires": time.time() + 300} + payload = {"user_id": "admin", "expires": time.time() + JWT_EXPIRY_TIME} token = jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM) return token_response(token) diff --git a/app/repositories/hardware_info.py b/app/repositories/hardware_info.py index 48f5050..b87ea9d 100644 --- a/app/repositories/hardware_info.py +++ b/app/repositories/hardware_info.py @@ -2,11 +2,12 @@ import asyncio import psutil from app.utils import SSE, send_sse_message +from decouple import config from fastapi import Request -SLEEP_TIME = 1 -CPU_USAGE_INTERVAL = 0.5 -HW_INFO_YIELD_TIME = SLEEP_TIME+CPU_USAGE_INTERVAL +SLEEP_TIME = config("gather_hw_info_interval", default=2, cast=float) +CPU_AVG_PERIOD = config("cpu_usage_averaging_period", default=0.5, cast=float) +HW_INFO_YIELD_TIME = SLEEP_TIME+CPU_AVG_PERIOD async def subscribe_hardware_info(request: Request): @@ -22,9 +23,9 @@ def get_hardware_info() -> map: info = {} info['cpu_overall_percent'] = psutil.cpu_percent( - interval=CPU_USAGE_INTERVAL) + interval=CPU_AVG_PERIOD) info['cpu_per_cpu_percent'] = psutil.cpu_percent( - interval=CPU_USAGE_INTERVAL, percpu=True) + interval=CPU_AVG_PERIOD, percpu=True) v = psutil.virtual_memory() info['vram_total_bytes'] = v.total