Skip to content

Commit

Permalink
Create Flask Monitoring Dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Mar 13, 2024
1 parent b5d72f0 commit 8891261
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ PORT=8000

FACTORIO_USERNAME=
FACTORIO_TOKEN=

# Flask-MonitoringDashboard
USERNAME=admin
PASSWORD=admin
SECURITY_TOKEN=cc83733cb0af8b884ff6577086b87909
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,7 @@ dmypy.json

# Pyre type checker
.pyre/

# Flask-MonitoringDashboard
config.cfg
flask_monitoringdashboard.db
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 . .
6 changes: 6 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -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__

Expand Down Expand Up @@ -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)
52 changes: 52 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -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)
24 changes: 24 additions & 0 deletions config.template.cfg
Original file line number Diff line number Diff line change
@@ -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]'}
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ services:
ports:
- ${PORT}:8000
restart: always
volumes:
- flask_monitoringdashboard.db:flask_monitoringdashboard.db

schedule:
build: .
Expand Down
22 changes: 1 addition & 21 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.0.1'
__version__ = '1.1.0'

0 comments on commit 8891261

Please sign in to comment.