-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard_handlers.py
43 lines (33 loc) · 1.38 KB
/
dashboard_handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import json
import logging
from auth_handlers import BaseHandler
logger = logging.getLogger(__name__)
class DashboardHandler(BaseHandler):
def get(self):
try:
host = self.get_argument('host', None)
with open("dashboard.html", "r") as file:
dashboard_html = file.read()
with self.db.get_cursor() as cursor:
cursor.execute("SELECT hostname, tags FROM hosts")
hosts = {row['hostname']: {'tags': row['tags']} for row in cursor.fetchall()}
selected_host = None
if host and host in hosts:
selected_host = host
elif host:
logger.warning(f"Requested host '{host}' not found")
template_data = {
"hosts": json.dumps(hosts),
"selected_host": json.dumps(selected_host)
}
for key, value in template_data.items():
placeholder = f"{{{{ {key} }}}}"
dashboard_html = dashboard_html.replace(placeholder, value)
self.write(dashboard_html)
except Exception as e:
logger.error(f"Error in DashboardHandler: {str(e)}")
self.set_status(500)
self.write({"error": "Internal server error"})
def post(self):
self.set_status(405)
self.write({"error": "POST method not supported for dashboard"})