From 45f1185a2e219923e26bd58d06a1b5130720fa74 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 7 Sep 2022 13:09:39 +0200 Subject: Implement Dashboard --- labbot/addons/dashboard/__init__.py | 110 +++++++++++++++ .../addons/dashboard/templates/addon_settings.html | 82 +++++++++++ labbot/addons/dashboard/templates/base.html | 153 +++++++++++++++++++++ labbot/addons/dashboard/templates/index.html | 7 + labbot/addons/dashboard/templates/log.html | 9 ++ labbot/addons/dashboard/templates/settings.html | 24 ++++ labbot/addons/dashboard/templates/sidebar.html | 76 ++++++++++ 7 files changed, 461 insertions(+) create mode 100644 labbot/addons/dashboard/__init__.py create mode 100644 labbot/addons/dashboard/templates/addon_settings.html create mode 100644 labbot/addons/dashboard/templates/base.html create mode 100644 labbot/addons/dashboard/templates/index.html create mode 100644 labbot/addons/dashboard/templates/log.html create mode 100644 labbot/addons/dashboard/templates/settings.html create mode 100644 labbot/addons/dashboard/templates/sidebar.html (limited to 'labbot/addons') diff --git a/labbot/addons/dashboard/__init__.py b/labbot/addons/dashboard/__init__.py new file mode 100644 index 0000000..9e63cbc --- /dev/null +++ b/labbot/addons/dashboard/__init__.py @@ -0,0 +1,110 @@ +""" +a web dashboard for lab-bot +""" +import os +import logging +import json + +import aiohttp +import jinja2 +import aiohttp_jinja2 # type: ignore + +from labbot import __version__ as labbot_version +from labbot.config import Config + +log = logging.getLogger(__name__) + +class BufferHandler(logging.Handler): + def __init__(self, dashboard, lines=-1): + super().__init__(level=logging.DEBUG) + self.dashboard = dashboard + self.lines = lines + + def emit(self, record): + + try: + msg = self.format(record) + return self.dashboard.log_buffer.append(msg) + finally: + if self.lines > 0: + while len(self.dashboard.log_buffer) > self.lines: + self.dashboard.log_buffer.remove(0) + +class Dashboard: + + def __init__(self, bot): + self.bot = bot + self.app = self.bot.instance.app + self.log_buffer = [] + + formatter = logging.Formatter( + "[{asctime}] [{levelname}] {name}: {message}", datefmt="%Y-%m-%d %H:%M:%S", style="{" + ) + + buffer_handler = BufferHandler(self) + buffer_handler.setFormatter(formatter) + + root_logger = logging.getLogger() + root_logger.addHandler(buffer_handler) + del root_logger + + dashboard_dir = os.path.join(os.path.dirname(__file__), "templates") + aiohttp_jinja2.setup( + self.app, + context_processors=[self.processor], + loader=jinja2.FileSystemLoader(dashboard_dir)) + + self.pages = [ + ["/", self.dashboard], + ["/log", self.log], + ["/settings", self.settings], + ["/settings/dashboard", self.addon_settings("dashboard")], + ] + + for addon in self.bot.addons: + self.pages.append([f"/settings/{addon}", self.addon_settings(addon)]) + + for page in self.pages: + endpoint, func = page + self.app.router.add_get(endpoint, func) + + @aiohttp_jinja2.template('index.html') + async def dashboard(self, request: aiohttp.web.Request) -> dict: + return {} + + @aiohttp_jinja2.template('log.html') + async def log(self, request: aiohttp.web.Request) -> dict: + return {} + + @aiohttp_jinja2.template('settings.html') + async def settings(self, request: aiohttp.web.Request) -> dict: + return {} + + def addon_settings(self, addon): + + @aiohttp_jinja2.template('addon_settings.html') + async def _settings(request: aiohttp.web.Request) -> dict: + c = Config(addon, self.bot.name) + return { + "addon_settings": c.settings + } + + return _settings + + async def processor(self, request) -> dict: + return { + "bot": { + "name": self.bot.name, + "version": labbot_version, + "addons": self.bot.addons, + "config": self.bot.config, + + "log": "\n".join(self.log_buffer) + } + } + + async def event_processor(self, *args, **kwargs): + self.event_counter += 1 + +def setup(bot): + Dashboard(bot) diff --git a/labbot/addons/dashboard/templates/addon_settings.html b/labbot/addons/dashboard/templates/addon_settings.html new file mode 100644 index 0000000..853cd15 --- /dev/null +++ b/labbot/addons/dashboard/templates/addon_settings.html @@ -0,0 +1,82 @@ +{% extends "base.html" %} +{% block content %} + +
+

Addon Settings

+
+ +
+
+

GLOBAL

+
+ + + + + + + + + + {% for k, v in addon_settings.GLOBAL.items() %} + + + + + {% endfor %} + +
KeyValue
{{k}}{{v}}
+
+ +
+
+

GROUP

+
+ + {% for group_id, setting in addon_settings.GROUP.items() %} +
{{ group_id }}
+ + + + + + + + + {% for k, v in setting.items() %} + + + + + {% endfor %} + +
KeyValue
{{k}}{{v}}
+ {% endfor %} +
+ +
+
+

PROJECT

+
+ + {% for project_id, setting in addon_settings.PROJECT.items() %} +
{{ project_id }}
+ + + + + + + + + {% for k, v in setting.items() %} + + + + + {% endfor %} + +
KeyValue
{{k}}{{v}}
+ {% endfor %} +
+{% endblock %} \ No newline at end of file diff --git a/labbot/addons/dashboard/templates/base.html b/labbot/addons/dashboard/templates/base.html new file mode 100644 index 0000000..03a5532 --- /dev/null +++ b/labbot/addons/dashboard/templates/base.html @@ -0,0 +1,153 @@ + + + + + + {{ bot.name }} - lab-bot {{ bot.version }} + + + + + + + + + + + + +
+
+ {% include "sidebar.html" %} + +
+ {% block content %}{% endblock %} +
+
+
+ + + + + + diff --git a/labbot/addons/dashboard/templates/index.html b/labbot/addons/dashboard/templates/index.html new file mode 100644 index 0000000..78ebebf --- /dev/null +++ b/labbot/addons/dashboard/templates/index.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} +{% block content %} + +
+

Dashboard

+
+{% endblock %} \ No newline at end of file diff --git a/labbot/addons/dashboard/templates/log.html b/labbot/addons/dashboard/templates/log.html new file mode 100644 index 0000000..441bdd3 --- /dev/null +++ b/labbot/addons/dashboard/templates/log.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} +{% block content %} + +
+

Log

+
+ +
{{bot.log}}
+{% endblock %} \ No newline at end of file diff --git a/labbot/addons/dashboard/templates/settings.html b/labbot/addons/dashboard/templates/settings.html new file mode 100644 index 0000000..9bf03fb --- /dev/null +++ b/labbot/addons/dashboard/templates/settings.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} +{% block content %} + +
+

Settings

+
+ + + + + + + + + + {% for k, v in bot.config.items() %} + + + + + {% endfor %} + +
KeyValue
{{k}}{{v}}
+{% endblock %} \ No newline at end of file diff --git a/labbot/addons/dashboard/templates/sidebar.html b/labbot/addons/dashboard/templates/sidebar.html new file mode 100644 index 0000000..f234f54 --- /dev/null +++ b/labbot/addons/dashboard/templates/sidebar.html @@ -0,0 +1,76 @@ + + + \ No newline at end of file -- cgit v1.2.3