From 45f1185a2e219923e26bd58d06a1b5130720fa74 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 7 Sep 2022 13:09:39 +0200 Subject: Implement Dashboard --- labbot/__main__.py | 21 +-- 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 ++++++++++ labbot/bot.py | 8 +- labbot/config.py | 4 + requirements.txt | 4 + 11 files changed, 484 insertions(+), 14 deletions(-) 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 diff --git a/labbot/__main__.py b/labbot/__main__.py index 39d8842..fa76c0f 100644 --- a/labbot/__main__.py +++ b/labbot/__main__.py @@ -12,7 +12,8 @@ import labbot.logger DEFAULT_ADDONS = [ "merge-label", "approve-merge", - "merge-stable" + "merge-stable", + "dashboard" ] @click.group() @@ -52,13 +53,12 @@ def config(name, **data): conf.update(data) labbot.config.write_instance_config(name, conf) click.echo("configured") - elif not print_config: - click.echo("run with `--help` to show usage") - - if print_config: - conf["access_token"] = "************" - conf["secret"] = "******" + elif print_config: + conf["access_token"] = (round(len(conf["access_token"]) / 4) * 4) * "*" + conf["secret"] = (round(len(conf["secret"]) / 4) * 4) * "*" click.echo(json.dumps(conf, indent=4)) + else: + click.echo("run with `--help` to show usage") else: click.echo(f"{name} is not an instance") pass @@ -82,11 +82,14 @@ def run(name, port: str, debug: bool): labbot.logger.init(logger_level) + access_token = conf.pop("access_token") + secret = conf.pop("secret", "") + instance = labbot.bot.Bot( name=name, config=conf, - secret=conf["secret"], - access_token=conf["access_token"] + access_token=access_token, + secret=secret, ) instance.run( 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 diff --git a/labbot/bot.py b/labbot/bot.py index ef13978..72d01ca 100644 --- a/labbot/bot.py +++ b/labbot/bot.py @@ -13,9 +13,9 @@ class Bot: def __init__(self, *args, **kwargs): self.name = kwargs.pop("name", "lab-bot") - self.access_token = kwargs.get("access_token") - self.secret = kwargs.get("secret", "") - self.config = kwargs.pop("config", labbot.config.read_instance_config(self.name)) + self.access_token = kwargs.pop("access_token") + self.secret = kwargs.pop("secret") + self.config = kwargs.pop("config") self.config_addons = self.config.get("addons", []) self.addons = [] @@ -43,8 +43,6 @@ class Bot: import_module(f"{addon}").setup(self) log.info(f"Loaded {addon}") self.addons.append(addon) - except ModuleNotFoundError: - log.error(f"No addon named `{addon}`") except Exception as e: log.exception(e) diff --git a/labbot/config.py b/labbot/config.py index 07c4d48..3574760 100644 --- a/labbot/config.py +++ b/labbot/config.py @@ -41,6 +41,10 @@ class Config: # write the hardcoded config data ontop of the loaded data self.settings["GLOBAL"].update(global_data) + + repo = self.settings.pop("REPO") + if repo: + self.settings["PROJECT"] = repo except (IOError, ValueError): pass diff --git a/requirements.txt b/requirements.txt index 12b02db..8f10d20 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,7 @@ gidgetlab[aiohttp] appdirs click + +# dashboard +jinja2 +aiohttp_jinja2 -- cgit v1.2.3