-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
210 lines (170 loc) · 7.96 KB
/
monitor.py
File metadata and controls
210 lines (170 loc) · 7.96 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python3
"""
NebulAI Mining Performance Monitor
Displays real-time statistics from the mining log file
"""
import time
import os
import re
from datetime import datetime
from collections import defaultdict
from typing import Dict, List, Tuple
import curses
class MiningMonitor:
def __init__(self, log_file: str = "nebulai_miner.log"):
self.log_file = log_file
self.stats = defaultdict(lambda: {"success": 0, "failure": 0, "last_seen": None})
self.overall_stats = {"total_success": 0, "total_failure": 0, "start_time": time.time()}
self.last_position = 0
def parse_log_line(self, line: str) -> Tuple[str, str, str]:
"""Parse a log line for relevant information"""
# Extract timestamp
timestamp_match = re.match(r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})', line)
timestamp = timestamp_match.group(1) if timestamp_match else ""
# Extract token (first 8 chars)
token_match = re.search(r'for ([a-zA-Z0-9]{8})\.\.\.', line)
token = token_match.group(1) if token_match else ""
# Determine event type
if "Results accepted" in line or "[✅]" in line:
event = "success"
elif "Results rejected" in line or "[❌]" in line:
event = "failure"
elif "Task received" in line or "[📥]" in line:
event = "task_received"
elif "Token refreshed" in line or "[🔄]" in line:
event = "token_refreshed"
else:
event = "other"
return timestamp, token, event
def update_stats(self):
"""Update statistics from log file"""
if not os.path.exists(self.log_file):
return
try:
with open(self.log_file, 'r') as f:
f.seek(self.last_position)
new_lines = f.readlines()
self.last_position = f.tell()
for line in new_lines:
timestamp, token, event = self.parse_log_line(line)
if token and event in ["success", "failure"]:
self.stats[token][event] += 1
self.stats[token]["last_seen"] = timestamp
if event == "success":
self.overall_stats["total_success"] += 1
else:
self.overall_stats["total_failure"] += 1
except Exception as e:
pass # Silently handle errors in monitoring
def get_success_rate(self, token: str) -> float:
"""Calculate success rate for a token"""
stats = self.stats[token]
total = stats["success"] + stats["failure"]
return (stats["success"] / total * 100) if total > 0 else 0
def get_overall_success_rate(self) -> float:
"""Calculate overall success rate"""
total = self.overall_stats["total_success"] + self.overall_stats["total_failure"]
return (self.overall_stats["total_success"] / total * 100) if total > 0 else 0
def format_uptime(self) -> str:
"""Format uptime as human-readable string"""
uptime = time.time() - self.overall_stats["start_time"]
hours = int(uptime // 3600)
minutes = int((uptime % 3600) // 60)
seconds = int(uptime % 60)
return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
def draw_dashboard(stdscr, monitor: MiningMonitor):
"""Draw the monitoring dashboard using curses"""
curses.curs_set(0) # Hide cursor
stdscr.nodelay(1) # Non-blocking input
# Color pairs
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_CYAN, curses.COLOR_BLACK)
while True:
try:
# Update statistics
monitor.update_stats()
# Clear screen
stdscr.clear()
height, width = stdscr.getmaxyx()
# Header
header = "🚀 NebulAI Mining Monitor 🚀"
stdscr.addstr(0, (width - len(header)) // 2, header, curses.A_BOLD | curses.color_pair(4))
# Warning
warning = "⚠️ WARNING: This violates NebulAI ToS! ⚠️"
stdscr.addstr(1, (width - len(warning)) // 2, warning, curses.color_pair(3))
# Overall statistics
y = 3
stdscr.addstr(y, 2, "━" * (width - 4), curses.A_DIM)
y += 1
stdscr.addstr(y, 2, "OVERALL STATISTICS", curses.A_BOLD)
y += 1
uptime = monitor.format_uptime()
overall_rate = monitor.get_overall_success_rate()
stdscr.addstr(y, 2, f"Uptime: {uptime}")
stdscr.addstr(y, 25, f"Success: {monitor.overall_stats['total_success']}", curses.color_pair(1))
stdscr.addstr(y, 45, f"Failure: {monitor.overall_stats['total_failure']}", curses.color_pair(2))
stdscr.addstr(y, 65, f"Rate: {overall_rate:.1f}%",
curses.color_pair(1) if overall_rate > 80 else curses.color_pair(2))
# Per-token statistics
y += 2
stdscr.addstr(y, 2, "━" * (width - 4), curses.A_DIM)
y += 1
stdscr.addstr(y, 2, "PER-TOKEN STATISTICS", curses.A_BOLD)
y += 2
# Table header
stdscr.addstr(y, 2, "Token", curses.A_UNDERLINE)
stdscr.addstr(y, 15, "Success", curses.A_UNDERLINE)
stdscr.addstr(y, 28, "Failure", curses.A_UNDERLINE)
stdscr.addstr(y, 40, "Rate", curses.A_UNDERLINE)
stdscr.addstr(y, 50, "Last Seen", curses.A_UNDERLINE)
y += 1
# Token data
for token, stats in sorted(monitor.stats.items()):
if y >= height - 3: # Leave room for footer
break
success_rate = monitor.get_success_rate(token)
color = curses.color_pair(1) if success_rate > 80 else curses.color_pair(2)
stdscr.addstr(y, 2, f"{token}...")
stdscr.addstr(y, 15, str(stats["success"]), curses.color_pair(1))
stdscr.addstr(y, 28, str(stats["failure"]), curses.color_pair(2))
stdscr.addstr(y, 40, f"{success_rate:.1f}%", color)
stdscr.addstr(y, 50, stats["last_seen"] or "Never")
y += 1
# Footer
stdscr.addstr(height - 2, 2, "━" * (width - 4), curses.A_DIM)
footer = "Press 'q' to quit | Updates every 2 seconds"
stdscr.addstr(height - 1, (width - len(footer)) // 2, footer, curses.A_DIM)
# Refresh display
stdscr.refresh()
# Check for quit command
key = stdscr.getch()
if key == ord('q') or key == ord('Q'):
break
# Update every 2 seconds
time.sleep(2)
except KeyboardInterrupt:
break
except Exception as e:
# Handle resize and other errors gracefully
pass
def main():
"""Main entry point"""
print("Starting NebulAI Mining Monitor...")
print("Press 'q' to quit")
time.sleep(1)
monitor = MiningMonitor()
try:
curses.wrapper(draw_dashboard, monitor)
except KeyboardInterrupt:
pass
print("\nMonitoring stopped.")
# Print final statistics
print(f"\nFinal Statistics:")
print(f"Total Runtime: {monitor.format_uptime()}")
print(f"Total Success: {monitor.overall_stats['total_success']}")
print(f"Total Failure: {monitor.overall_stats['total_failure']}")
print(f"Overall Success Rate: {monitor.get_overall_success_rate():.1f}%")
if __name__ == "__main__":
main()