Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cspell/custom-dictionary-workspace.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ kwargs
kwhb
labelcolor
labelnames
larr
linebreak
linestyle
loadml
Expand Down
14 changes: 12 additions & 2 deletions apps/predbat/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
from predbat import THIS_VERSION
from component_base import ComponentBase
from config import APPS_SCHEMA
from web_metrics_dashboard import get_metrics_dashboard_css, get_metrics_dashboard_body, FALLBACK_HTML
from web_metrics_dashboard import get_metrics_dashboard_css, get_metrics_dashboard_body
from predbat_metrics import metrics_handler, metrics_json_handler, metrics, PROMETHEUS_AVAILABLE

ROOT_YAML_KEY = "pred_bat"
Expand Down Expand Up @@ -4448,7 +4448,17 @@ async def html_metrics_dashboard(self, request):
self.default_page = "./metrics_dashboard"

if not PROMETHEUS_AVAILABLE:
return web.Response(text=FALLBACK_HTML, content_type="text/html")
text = self.get_header("Predbat Metrics", refresh=0)
text += "<body>\n"
text += """<div style="display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:60vh;text-align:center;padding:2rem;">
<h1>Metrics Dashboard Unavailable</h1>
<p><code>prometheus_client</code> is not installed.</p>
<p>Install it with: <code>pip install prometheus_client</code></p>
<p style="margin-top:1.5rem;"><a href="./dash" style="padding:10px 20px;background:#4CAF50;color:white;text-decoration:none;border-radius:4px;font-size:16px;">&larr; Back to Dashboard</a></p>
</div>
"""
text += "</body></html>\n"
return web.Response(text=text, content_type="text/html")

import json as _json

Expand Down
31 changes: 0 additions & 31 deletions apps/predbat/web_metrics_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
Dark/light mode is derived automatically from the ``body.dark-mode`` class used
by the rest of the PredBat web UI - no separate theme toggle is needed.
"""

from predbat_metrics import PROMETHEUS_AVAILABLE, metrics


def get_metrics_dashboard_css():
"""Return scoped CSS for the metrics dashboard component."""
return """<style>
Expand Down Expand Up @@ -363,30 +359,3 @@ def get_metrics_dashboard_body(data_json):
</script>
"""
)
FALLBACK_HTML = """<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>PredBat Metrics</title>
<style>body{font-family:sans-serif;display:flex;align-items:center;justify-content:center;min-height:100vh;background:#0f172a;color:#e2e8f0;text-align:center;}</style>
</head><body><div><h1>Metrics Dashboard Unavailable</h1>
<p><code>prometheus_client</code> is not installed.</p>
<p>Install it with: <code>pip install prometheus_client</code></p></div></body></html>"""


async def metrics_dashboard_handler(request):
"""Serve the self-contained metrics dashboard at ``/metrics_dashboard``."""
import json
from aiohttp import web

if not PROMETHEUS_AVAILABLE:
return web.Response(text=FALLBACK_HTML, content_type="text/html")

data_json = json.dumps(metrics().to_dict())
html = (
"<!DOCTYPE html><html lang='en'><head>"
"<meta charset='utf-8'><meta name='viewport' content='width=device-width, initial-scale=1'>"
"<title>PredBat Metrics</title>"
+ get_metrics_dashboard_css()
+ "</head><body style='background:var(--md-bg)'>"
+ get_metrics_dashboard_body(data_json)
+ "</body></html>"
)
return web.Response(text=html, content_type="text/html")
Loading