|
| 1 | +#include "PoolDashboard.h" |
| 2 | +#include "PoolDatabase.h" |
| 3 | +#include "PoolServer.h" |
| 4 | +#include <iostream> |
| 5 | +#include <sstream> |
| 6 | +#include <iomanip> |
| 7 | + |
| 8 | +#ifdef _WIN32 |
| 9 | +#include <windows.h> |
| 10 | +#include <conio.h> |
| 11 | +#endif |
| 12 | + |
| 13 | +// --- VT100 escape helpers --- (same subset ConsoleDashboard uses in main exe) |
| 14 | +#define ESC "\033[" |
| 15 | +#define CURSOR_HOME ESC "H" |
| 16 | +#define ERASE_SCREEN ESC "2J" |
| 17 | +#define ERASE_LINE ESC "2K" |
| 18 | +#define HIDE_CURSOR ESC "?25l" |
| 19 | +#define SHOW_CURSOR ESC "?25h" |
| 20 | +#define BOLD ESC "1m" |
| 21 | +#define DIM ESC "2m" |
| 22 | +#define RESET ESC "0m" |
| 23 | +#define FG_GREEN ESC "32m" |
| 24 | +#define FG_YELLOW ESC "33m" |
| 25 | +#define FG_RED ESC "31m" |
| 26 | +#define FG_CYAN ESC "36m" |
| 27 | +#define FG_BRIGHT_WHITE ESC "97m" |
| 28 | + |
| 29 | +namespace { |
| 30 | + std::string formatDuration(int64_t seconds) { |
| 31 | + int d = (int) (seconds / 86400); |
| 32 | + seconds %= 86400; |
| 33 | + int h = (int) (seconds / 3600); |
| 34 | + seconds %= 3600; |
| 35 | + int m = (int) (seconds / 60); |
| 36 | + int s = (int) (seconds % 60); |
| 37 | + std::ostringstream out; |
| 38 | + if (d > 0) out << d << "d "; |
| 39 | + if (d > 0 || h > 0) { |
| 40 | + out << std::setfill('0') << std::setw(2) << h << ":" |
| 41 | + << std::setfill('0') << std::setw(2) << m << ":" |
| 42 | + << std::setfill('0') << std::setw(2) << s; |
| 43 | + } else { |
| 44 | + out << m << " min " << s << " sec"; |
| 45 | + } |
| 46 | + return out.str(); |
| 47 | + } |
| 48 | + |
| 49 | + std::string formatNumber(uint64_t n) { |
| 50 | + std::string s = std::to_string(n); |
| 51 | + std::string out; |
| 52 | + int count = 0; |
| 53 | + for (auto it = s.rbegin(); it != s.rend(); ++it) { |
| 54 | + if (count > 0 && count % 3 == 0) out += ','; |
| 55 | + out += *it; |
| 56 | + count++; |
| 57 | + } |
| 58 | + std::reverse(out.begin(), out.end()); |
| 59 | + return out; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +PoolDashboard::PoolDashboard(PoolDatabase& db, PoolServer& server) |
| 64 | + : _db(db), _server(server), _startTime(std::chrono::system_clock::now()) { |
| 65 | +} |
| 66 | + |
| 67 | +PoolDashboard::~PoolDashboard() { |
| 68 | + stop(); |
| 69 | +} |
| 70 | + |
| 71 | +bool PoolDashboard::enableVT100() { |
| 72 | +#ifdef _WIN32 |
| 73 | + HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); |
| 74 | + if (hOut == INVALID_HANDLE_VALUE) return false; |
| 75 | + DWORD mode = 0; |
| 76 | + if (!GetConsoleMode(hOut, &mode)) return false; |
| 77 | + mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; |
| 78 | + return SetConsoleMode(hOut, mode) != 0; |
| 79 | +#else |
| 80 | + return true; |
| 81 | +#endif |
| 82 | +} |
| 83 | + |
| 84 | +void PoolDashboard::start() { |
| 85 | + if (_running.exchange(true)) return; |
| 86 | + std::cout << ESC "2J" << CURSOR_HOME << HIDE_CURSOR << std::flush; |
| 87 | + render(); |
| 88 | + _thread = std::thread([this]() { this->refreshLoop(); }); |
| 89 | +} |
| 90 | + |
| 91 | +void PoolDashboard::stop() { |
| 92 | + if (!_running.exchange(false)) return; |
| 93 | + if (_thread.joinable()) _thread.join(); |
| 94 | + std::cout << SHOW_CURSOR << std::flush; |
| 95 | +} |
| 96 | + |
| 97 | +void PoolDashboard::addLog(const std::string& line) { |
| 98 | + std::lock_guard<std::mutex> lk(_logMutex); |
| 99 | + _logLines.push_back(line); |
| 100 | + while (_logLines.size() > MAX_LOG_LINES) _logLines.pop_front(); |
| 101 | +} |
| 102 | + |
| 103 | +void PoolDashboard::refreshLoop() { |
| 104 | + while (_running.load()) { |
| 105 | + processInput(); |
| 106 | + render(); |
| 107 | + std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +void PoolDashboard::processInput() { |
| 112 | +#ifdef _WIN32 |
| 113 | + while (_kbhit()) { |
| 114 | + int ch = _getch(); |
| 115 | + if (ch == 'q' || ch == 'Q' || ch == 3 /* Ctrl+C */) { |
| 116 | + _quit.store(true); |
| 117 | + } else if (ch == 'p' || ch == 'P') { |
| 118 | + addLog("Pending payouts: 0.0000 DGB (Phase 3 not yet implemented)"); |
| 119 | + } else if (ch == 'e' || ch == 'E') { |
| 120 | + addLog("Execute payout: Phase 3 not yet implemented"); |
| 121 | + } else if (ch == 'n' || ch == 'N') { |
| 122 | + addLog("Registered nodes: " + std::to_string(_db.countTotalNodes())); |
| 123 | + } else if (ch == 'a' || ch == 'A') { |
| 124 | + addLog("Permanent assets: " + std::to_string(_db.countPermanentAssets()) + |
| 125 | + " across " + std::to_string(_db.countPermanentPages()) + " pages"); |
| 126 | + } else if (ch == 'h' || ch == 'H' || ch == '?') { |
| 127 | + addLog("Pool Server - keys: Q=Quit N=Node count A=Asset count P=Pending payouts E=Execute payout"); |
| 128 | + } |
| 129 | + } |
| 130 | +#endif |
| 131 | +} |
| 132 | + |
| 133 | +void PoolDashboard::render() { |
| 134 | + std::ostringstream out; |
| 135 | + out << HIDE_CURSOR << CURSOR_HOME; |
| 136 | + |
| 137 | + const int w = 90; |
| 138 | + auto separator = [&]() { out << ERASE_LINE << std::string(w, '-') << "\n"; }; |
| 139 | + |
| 140 | + // Header |
| 141 | + std::string title = "DigiAsset Pool Server (experimental) - Phase 1"; |
| 142 | + int pad = (w - (int) title.size()) / 2; |
| 143 | + out << BOLD << FG_BRIGHT_WHITE << ERASE_LINE |
| 144 | + << std::string(pad > 0 ? pad : 0, ' ') << title << RESET << "\n"; |
| 145 | + separator(); |
| 146 | + |
| 147 | + // Status rows |
| 148 | + auto now = std::chrono::system_clock::now(); |
| 149 | + int64_t uptime = std::chrono::duration_cast<std::chrono::seconds>(now - _startTime).count(); |
| 150 | + int64_t oneHourAgo = std::chrono::duration_cast<std::chrono::seconds>( |
| 151 | + now.time_since_epoch()).count() - 3600; |
| 152 | + |
| 153 | + unsigned int totalNodes = _db.countTotalNodes(); |
| 154 | + unsigned int activeNodes = _db.countNodesSeenSince(oneHourAgo); |
| 155 | + unsigned int permAssets = _db.countPermanentAssets(); |
| 156 | + unsigned int permPages = _db.countPermanentPages(); |
| 157 | + uint64_t requests = _server.getRequestCount(); |
| 158 | + |
| 159 | + out << ERASE_LINE << " " << "Listening: " << FG_GREEN << "Port " << _server.getPort() << RESET |
| 160 | + << " " << "Requests: " << FG_BRIGHT_WHITE << formatNumber(requests) << RESET << "\n"; |
| 161 | + |
| 162 | + out << ERASE_LINE << " " << "Registered: " << FG_BRIGHT_WHITE << totalNodes << " nodes" << RESET |
| 163 | + << " " << "Active (1h): " << FG_BRIGHT_WHITE << activeNodes << " nodes" << RESET << "\n"; |
| 164 | + |
| 165 | + out << ERASE_LINE << " " << "Permanent: " << FG_BRIGHT_WHITE << formatNumber(permAssets) |
| 166 | + << " assets" << RESET << " / " << FG_BRIGHT_WHITE << permPages << " pages" << RESET << "\n"; |
| 167 | + |
| 168 | + // Payout row. Shows the poolpayouts config state prominently so the |
| 169 | + // operator always knows what clients are seeing. If poolpayouts is |
| 170 | + // still disabled (Phase 1 default), say so in yellow. When Phase 3 |
| 171 | + // flips to enabled, switch to green. |
| 172 | + { |
| 173 | + bool payoutsEnabled = _server.getPayoutsEnabled(); |
| 174 | + out << ERASE_LINE << " " << "Payouts: "; |
| 175 | + if (payoutsEnabled) { |
| 176 | + out << FG_GREEN << "ENABLED" << RESET << DIM |
| 177 | + << " (Pending: 0.0000 DGB / Paid: 0.0000 DGB)" << RESET; |
| 178 | + } else { |
| 179 | + out << FG_YELLOW << "disabled" << RESET << DIM |
| 180 | + << " (clients see 'registered (no payouts yet)')" << RESET; |
| 181 | + } |
| 182 | + out << "\n"; |
| 183 | + } |
| 184 | + |
| 185 | + // Time row |
| 186 | + auto t = std::chrono::system_clock::to_time_t(now); |
| 187 | + std::tm tmBuf{}; |
| 188 | +#ifdef _WIN32 |
| 189 | + localtime_s(&tmBuf, &t); |
| 190 | +#else |
| 191 | + tmBuf = *std::localtime(&t); |
| 192 | +#endif |
| 193 | + char timeStr[16]; |
| 194 | + std::strftime(timeStr, sizeof(timeStr), "%H:%M:%S", &tmBuf); |
| 195 | + |
| 196 | + out << ERASE_LINE << " " << "Time: " << FG_BRIGHT_WHITE << timeStr << RESET |
| 197 | + << " " << "Uptime: " << FG_BRIGHT_WHITE << formatDuration(uptime) << RESET << "\n"; |
| 198 | + |
| 199 | + separator(); |
| 200 | + |
| 201 | + // Log area |
| 202 | + out << BOLD << ERASE_LINE << " Log:" << RESET << "\n"; |
| 203 | + { |
| 204 | + std::lock_guard<std::mutex> lk(_logMutex); |
| 205 | + for (const auto& line: _logLines) { |
| 206 | + out << ERASE_LINE << " " << line << "\n"; |
| 207 | + } |
| 208 | + // Pad to MAX_LOG_LINES so the key-hint row sits in a stable position. |
| 209 | + for (size_t i = _logLines.size(); i < MAX_LOG_LINES; i++) { |
| 210 | + out << ERASE_LINE << "\n"; |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + // Key hints row |
| 215 | + out << ERASE_LINE << DIM |
| 216 | + << " [Q] Quit [N] Nodes [A] Assets [P] Pending Payouts [E] Execute Payout [H] Help" |
| 217 | + << RESET; |
| 218 | + |
| 219 | + // Clear to end of screen to scrub any stale output from longer renders. |
| 220 | + out << ESC "J"; |
| 221 | + |
| 222 | + std::cout << out.str() << std::flush; |
| 223 | +} |
0 commit comments