From 889126130ce894cf928be26bdd356834dcab724a Mon Sep 17 00:00:00 2001 From: TatLead Date: Wed, 13 Mar 2024 05:38:13 +0000 Subject: [PATCH] Create Flask Monitoring Dashboard --- .env.example | 5 +++++ .gitignore | 4 ++++ Dockerfile | 4 +++- app.py | 6 ++++++ config.py | 52 +++++++++++++++++++++++++++++++++++++++++++++ config.template.cfg | 24 +++++++++++++++++++++ docker-compose.yml | 2 ++ requirements.txt | 22 +------------------ version.py | 2 +- 9 files changed, 98 insertions(+), 23 deletions(-) create mode 100644 config.py create mode 100644 config.template.cfg diff --git a/.env.example b/.env.example index a5fd1d5..58c30f3 100644 --- a/.env.example +++ b/.env.example @@ -3,3 +3,8 @@ PORT=8000 FACTORIO_USERNAME= FACTORIO_TOKEN= + +# Flask-MonitoringDashboard +USERNAME=admin +PASSWORD=admin +SECURITY_TOKEN=cc83733cb0af8b884ff6577086b87909 diff --git a/.gitignore b/.gitignore index b6e4761..8426347 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,7 @@ dmypy.json # Pyre type checker .pyre/ + +# Flask-MonitoringDashboard +config.cfg +flask_monitoringdashboard.db diff --git a/Dockerfile b/Dockerfile index 4341282..a112753 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,8 @@ FROM python:3.12-alpine WORKDIR /app COPY requirements.txt requirements.txt -RUN pip install -r requirements.txt +RUN apk add --no-cache gcc musl-dev linux-headers \ + && pip install -r requirements.txt \ + && apk del gcc musl-dev linux-headers COPY . . diff --git a/app.py b/app.py index 7b0cf6e..5b8af25 100644 --- a/app.py +++ b/app.py @@ -1,8 +1,10 @@ from flask import Flask, abort, request, jsonify from flasgger import Swagger +import flask_monitoringdashboard as dashboard import markdown +from config import build_config_file from protocols import MasterServer, BeamMP, Factorio, Palworld, Scum from version import __version__ @@ -200,5 +202,9 @@ def render_stats(): return collection_counts +build_config_file() +dashboard.config.init_from(file='config.cfg') +dashboard.bind(app) + if __name__ == '__main__': app.run(debug=True) diff --git a/config.py b/config.py new file mode 100644 index 0000000..0d9fe79 --- /dev/null +++ b/config.py @@ -0,0 +1,52 @@ +import configparser +import os + +from version import __version__ +from dotenv import load_dotenv + +load_dotenv() + + +class FlaskMonitoringDashboardConfigParser(configparser.ConfigParser): + def optionxform(self, optionstr): + return optionstr + + def write(self, fp): + if self._defaults: + fp.write("[%s]\n" % configparser.DEFAULTSECT) + for (key, value) in self._defaults.items(): + fp.write("%s=%s\n" % (key, str(value).replace('\n', '\n\t'))) + fp.write("\n") + for section in self._sections: + fp.write("[%s]\n" % section) + for (key, value) in self._sections[section].items(): + if key == "__name__": + continue + if (value is not None) or (self._optcre == self.OPTCRE): + key = "=".join((key, str(value).replace('\n', '\n\t'))) + fp.write("%s\n" % (key)) + fp.write("\n") + + +def build_config_file(): + # Initialize the ConfigParser object + config = FlaskMonitoringDashboardConfigParser() + + # Read the configuration file + config.read('config.template.cfg') + + # Update the values in the configuration file + def overwrite(section: str, options: list[str]): + for option in options: + config[section][option] = os.getenv( + option, config.get(section, option)) + + + overwrite('authentication', ['USERNAME', 'PASSWORD', 'SECURITY_TOKEN']) + + # Update the app version + config['dashboard']['APP_VERSION'] = __version__ + + # Write the updated values to the config file + with open('config.cfg', 'w') as configfile: + config.write(configfile) diff --git a/config.template.cfg b/config.template.cfg new file mode 100644 index 0000000..c4aa5e4 --- /dev/null +++ b/config.template.cfg @@ -0,0 +1,24 @@ +[dashboard] +APP_VERSION=1.0 +CUSTOM_LINK=dashboard +MONITOR_LEVEL=3 +OUTLIER_DETECTION_CONSTANT=2.5 +SAMPLING_PERIOD=20 +ENABLE_LOGGING=True +BRAND_NAME=OpenGSQ APIs Monitoring Dashboard +TITLE_NAME=OpenGSQ APIs Monitoring Dashboard +DESCRIPTION=Automatically monitor the ongoing performance improvements of OpenGSQ API services. +SHOW_LOGIN_BANNER=True +SHOW_LOGIN_FOOTER=True + +[authentication] +USERNAME=admin +PASSWORD=admin +SECURITY_TOKEN=cc83733cb0af8b884ff6577086b87909 + +[database] +TABLE_PREFIX= +DATABASE=sqlite:///flask_monitoringdashboard.db + +[visualization] +COLORS={'main':'[128,0,128]','static':'[255,255,0]'} diff --git a/docker-compose.yml b/docker-compose.yml index 89b1da1..26dd71b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,8 @@ services: ports: - ${PORT}:8000 restart: always + volumes: + - flask_monitoringdashboard.db:flask_monitoringdashboard.db schedule: build: . diff --git a/requirements.txt b/requirements.txt index 333a15f..80e9d6a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,30 +1,10 @@ -attrs==23.2.0 -blinker==1.7.0 -certifi==2024.2.2 -charset-normalizer==3.3.2 -click==8.1.7 -colorama==0.4.6 -dnspython==2.6.1 flasgger==0.9.7.1 Flask==3.0.2 +Flask_MonitoringDashboard==3.2.2 gunicorn==21.2.0 -idna==3.6 -itsdangerous==2.1.2 -Jinja2==3.1.3 -jsonschema==4.21.1 -jsonschema-specifications==2023.12.1 Markdown==3.5.2 -MarkupSafe==2.1.5 -mistune==3.0.2 -packaging==24.0 pymongo==4.6.2 python-dotenv==1.0.1 -PyYAML==6.0.1 -referencing==0.33.0 requests==2.31.0 -rpds-py==0.18.0 schedule==1.2.1 -six==1.16.0 tqdm==4.66.2 -urllib3==2.2.1 -Werkzeug==3.0.1 diff --git a/version.py b/version.py index 6c4c011..6df8ed0 100644 --- a/version.py +++ b/version.py @@ -1 +1 @@ -__version__ = '1.0.1' \ No newline at end of file +__version__ = '1.1.0' \ No newline at end of file