-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.py
More file actions
104 lines (78 loc) · 2.91 KB
/
Copy pathdashboard.py
File metadata and controls
104 lines (78 loc) · 2.91 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""Kavach-R — Live CLI dashboard.
Displays a continuously updating risk score and system status in the
terminal. Uses colorama for cross-platform ANSI colours when available;
falls back to plain text otherwise.
"""
import random
import signal
import sys
import time
from typing import Callable, Optional
from utils import clear_terminal
try:
from colorama import Fore, Style, init as colorama_init
colorama_init(autoreset=True)
HAS_COLOR = True
except ImportError:
HAS_COLOR = False
class _Stub:
def __getattr__(self, _):
return ""
Fore = _Stub()
Style = _Stub()
_RUNNING = True
def _handle_sigint(sig, frame):
global _RUNNING
_RUNNING = False
try:
signal.signal(signal.SIGINT, _handle_sigint)
except ValueError:
pass # not in main thread
def _status_label(score: float) -> str:
if score >= 0.8:
return f"{Fore.RED}██ CRITICAL ██{Style.RESET_ALL}"
if score >= 0.5:
return f"{Fore.YELLOW}▒▒ WARNING ▒▒{Style.RESET_ALL}"
return f"{Fore.GREEN}░░ SAFE ░░{Style.RESET_ALL}"
def _bar(score: float, width: int = 30) -> str:
filled = int(score * width)
empty = width - filled
if score >= 0.8:
colour = Fore.RED
elif score >= 0.5:
colour = Fore.YELLOW
else:
colour = Fore.GREEN
return f"{colour}{'█' * filled}{'░' * empty}{Style.RESET_ALL}"
def _default_risk() -> float:
return round(random.uniform(0.0, 1.0), 4)
def run_dashboard(
get_risk_score: Optional[Callable[[], float]] = None,
refresh_interval: float = 1.0,
) -> None:
"""Run the live CLI dashboard until Ctrl-C."""
global _RUNNING
_RUNNING = True
score_fn = get_risk_score or _default_risk
while _RUNNING:
score = score_fn()
clear_terminal()
header = f"{Fore.CYAN}╔══════════════════════════════════════════╗{Style.RESET_ALL}"
footer = f"{Fore.CYAN}╚══════════════════════════════════════════╝{Style.RESET_ALL}"
print(header)
print(f"{Fore.CYAN}║{Style.RESET_ALL} {Fore.MAGENTA}K A{Style.RESET_ALL} {Fore.WHITE}V A{Style.RESET_ALL} {Fore.GREEN}C H{Style.RESET_ALL} - R Dashboard {Fore.CYAN}║{Style.RESET_ALL}")
print(footer)
print()
print(f" Risk Score : {score:.4f} {_bar(score)}")
print(f" Status : {_status_label(score)}")
print(f" Timestamp : {time.strftime('%H:%M:%S')}")
print()
print(f" {Fore.CYAN}Press Ctrl+C to exit.{Style.RESET_ALL}")
try:
time.sleep(refresh_interval)
except KeyboardInterrupt:
break
clear_terminal()
print(f"{Fore.GREEN}Dashboard stopped.{Style.RESET_ALL}")
if __name__ == "__main__":
run_dashboard()