-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdebug_bar.py
77 lines (62 loc) · 2.03 KB
/
debug_bar.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import time
import psutil
from flask import request
from threading import Lock
import logging
class DebugBarPanel:
def __init__(self, name):
self.name = name
self.data = {}
def record(self, key, value):
self.data[key] = value
def get_data(self):
return self.data
class DebugBar:
def __init__(self):
self.panels = {}
self.enabled = False
self.start_time = None
self.lock = Lock()
try:
self.process = psutil.Process()
except Exception as e:
logging.error(f"Failed to initialize psutil Process: {e}")
self.process = None
def add_panel(self, name):
with self.lock:
if name not in self.panels:
self.panels[name] = DebugBarPanel(name)
def record(self, panel, key, value):
with self.lock:
if panel in self.panels:
self.panels[panel].record(key, value)
def start_request(self):
self.start_time = time.time()
def end_request(self):
if self.start_time:
duration = time.time() - self.start_time
self.record('request', 'duration', f"{duration:.2f}s")
self.start_time = None
def get_data(self):
with self.lock:
#self.update_performance_metrics()
return {name: panel.get_data() for name, panel in self.panels.items()}
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False
def remove(self, panel_name, key):
with self.lock:
if panel_name in self.panels:
panel = self.panels[panel_name]
if key in panel.data:
del panel.data[key]
debug_bar = DebugBar()
# Initialize default panels
debug_bar.add_panel('mqtt')
debug_bar.add_panel('request')
debug_bar.add_panel('performance')
def debug_bar_middleware():
debug_bar.start_request()
debug_bar.record('request', 'path', request.path)
debug_bar.record('request', 'method', request.method)