opencode-statboard 2.9.5 → 2.10.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/fix-port-final.js +86 -0
- package/fix-port-final10.js +79 -0
- package/fix-port-final11.js +79 -0
- package/fix-port-final12.js +79 -0
- package/fix-port-final13.js +79 -0
- package/fix-port-final14.js +79 -0
- package/fix-port-final15.js +79 -0
- package/fix-port-final16.js +79 -0
- package/fix-port-final17.js +79 -0
- package/fix-port-final18.js +79 -0
- package/fix-port-final19.js +79 -0
- package/fix-port-final2.js +79 -0
- package/fix-port-final20.js +79 -0
- package/fix-port-final21.js +79 -0
- package/fix-port-final22.js +79 -0
- package/fix-port-final23.js +79 -0
- package/fix-port-final3.js +79 -0
- package/fix-port-final5.js +79 -0
- package/fix-port-final7.js +79 -0
- package/fix-port-final8.js +79 -0
- package/fix-port-final9.js +79 -0
- package/fix-port-v2.js +58 -0
- package/fix-port-v3.js +79 -0
- package/fix-port.js +32 -0
- package/package.json +11 -5
- package/public/index.html +72 -13
- package/server.js +9 -20
- package/tui-dashboard.js +615 -0
package/tui-dashboard.js
ADDED
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* TUI Dashboard for opencode-statboard
|
|
4
|
+
* Terminal-based dashboard using blessed
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const blessed = require('blessed');
|
|
8
|
+
const http = require('http');
|
|
9
|
+
const { spawn } = require('child_process');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
const pkg = require('./package.json');
|
|
12
|
+
|
|
13
|
+
class TUIServer {
|
|
14
|
+
constructor(options = {}) {
|
|
15
|
+
this.port = options.port || 4868;
|
|
16
|
+
this.host = options.host || '127.0.0.1';
|
|
17
|
+
this.serverProcess = null;
|
|
18
|
+
this.screen = null;
|
|
19
|
+
this.layout = {};
|
|
20
|
+
this.data = {
|
|
21
|
+
overview: null,
|
|
22
|
+
projects: [],
|
|
23
|
+
models: [],
|
|
24
|
+
trend: null,
|
|
25
|
+
loading: true,
|
|
26
|
+
error: null
|
|
27
|
+
};
|
|
28
|
+
this.currentView = 'overview';
|
|
29
|
+
this.views = ['overview', 'projects', 'models', 'trend', 'heatmap'];
|
|
30
|
+
this.pollInterval = null;
|
|
31
|
+
this.lastUpdate = null;
|
|
32
|
+
this._earlyLogs = [];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async start() {
|
|
36
|
+
this.startServer();
|
|
37
|
+
await this.sleep(1500);
|
|
38
|
+
this.initTUI();
|
|
39
|
+
this.startPolling();
|
|
40
|
+
process.on('SIGINT', () => this.shutdown());
|
|
41
|
+
process.on('SIGTERM', () => this.shutdown());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
startServer() {
|
|
45
|
+
const serverPath = path.join(__dirname, 'server.js');
|
|
46
|
+
this.serverProcess = spawn('node', [serverPath], {
|
|
47
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
48
|
+
env: { ...process.env, OC_PORT: String(this.port) }
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
this.serverProcess.stdout.on('data', (data) => {
|
|
52
|
+
const msg = data.toString().trim();
|
|
53
|
+
if (msg) this.log('[SERVER] ' + msg);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
this.serverProcess.stderr.on('data', (data) => {
|
|
57
|
+
const msg = data.toString().trim();
|
|
58
|
+
if (msg) this.log('[SERVER ERR] ' + msg);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
this.serverProcess.on('close', (code) => {
|
|
62
|
+
this.log('Server exited with code ' + code);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async fetchData(endpoint) {
|
|
67
|
+
return new Promise((resolve, reject) => {
|
|
68
|
+
http.get('http://' + this.host + ':' + this.port + endpoint, (res) => {
|
|
69
|
+
let data = '';
|
|
70
|
+
res.on('data', chunk => data += chunk);
|
|
71
|
+
res.on('end', () => {
|
|
72
|
+
try {
|
|
73
|
+
const raw = res.headers['content-type']?.includes('utf-16')
|
|
74
|
+
? Buffer.from(data, 'utf16le').toString('utf8')
|
|
75
|
+
: data;
|
|
76
|
+
resolve(JSON.parse(raw));
|
|
77
|
+
} catch (e) {
|
|
78
|
+
reject(e);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}).on('error', reject);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async pollData() {
|
|
86
|
+
try {
|
|
87
|
+
const [stats, report, models, trend] = await Promise.all([
|
|
88
|
+
this.fetchData('/api/stats?days=7'),
|
|
89
|
+
this.fetchData('/api/report?days=7'),
|
|
90
|
+
this.fetchData('/api/stats?days=7').then(r => r.models || []),
|
|
91
|
+
this.fetchData('/api/stats?days=90')
|
|
92
|
+
]);
|
|
93
|
+
|
|
94
|
+
// Parse stats response
|
|
95
|
+
const statsData = stats.data || stats;
|
|
96
|
+
const overview = statsData.overview || {};
|
|
97
|
+
const cost = statsData.cost || {};
|
|
98
|
+
const tools = statsData.tools || [];
|
|
99
|
+
|
|
100
|
+
// Parse report for projects (from agents)
|
|
101
|
+
const reportData = report.report || report;
|
|
102
|
+
const agents = reportData.agents || [];
|
|
103
|
+
const projects = (agents || [])
|
|
104
|
+
.filter(a => a.sessions > 0)
|
|
105
|
+
.map(a => ({
|
|
106
|
+
name: a.agent,
|
|
107
|
+
mainSessionCount: a.sessions,
|
|
108
|
+
sessionCount: a.sessions,
|
|
109
|
+
tokens: { total: (a.tok_in || 0) + (a.tok_out || 0) + (a.tok_reasoning || 0) + (a.cache_read || 0) },
|
|
110
|
+
cost: a.cost || 0
|
|
111
|
+
}));
|
|
112
|
+
|
|
113
|
+
// Parse trend
|
|
114
|
+
const trendData = trend.trend || trend;
|
|
115
|
+
|
|
116
|
+
// Handle overview.sessions being a number or object
|
|
117
|
+
const sessionsValue = overview.sessions;
|
|
118
|
+
const sessionsObj = typeof sessionsValue === 'number'
|
|
119
|
+
? { main: sessionsValue, total: sessionsValue }
|
|
120
|
+
: (sessionsValue || { main: 0, total: 0 });
|
|
121
|
+
// Projects count from agents in report
|
|
122
|
+
const projectsCount = projects.length;
|
|
123
|
+
|
|
124
|
+
this.data = {
|
|
125
|
+
overview: {
|
|
126
|
+
sessions: sessionsObj,
|
|
127
|
+
projects: projectsCount,
|
|
128
|
+
days: overview.days || 0,
|
|
129
|
+
messages: overview.messages || 0
|
|
130
|
+
},
|
|
131
|
+
projects: Array.isArray(projects) ? projects : [],
|
|
132
|
+
models: Array.isArray(models) ? models : [],
|
|
133
|
+
trend: trendData,
|
|
134
|
+
tokens: {
|
|
135
|
+
input: cost.input || 0,
|
|
136
|
+
output: cost.output || 0,
|
|
137
|
+
reasoning: cost.reasoning || 0,
|
|
138
|
+
cacheRead: cost.cacheRead || 0,
|
|
139
|
+
cacheWrite: cost.cacheWrite || 0,
|
|
140
|
+
total: (cost.input || 0) + (cost.output || 0) + (cost.reasoning || 0) + (cost.cacheRead || 0) + (cost.cacheWrite || 0)
|
|
141
|
+
},
|
|
142
|
+
cost: {
|
|
143
|
+
total: cost.total || 0,
|
|
144
|
+
marketValue: cost.marketTotal || 0,
|
|
145
|
+
saved: cost.savedTotal || 0,
|
|
146
|
+
savedPct: cost.proxyShare || 0,
|
|
147
|
+
avgCostPerDay: cost.avgDay || 0,
|
|
148
|
+
avgTokensPerDay: cost.avgTokensDay || 0
|
|
149
|
+
},
|
|
150
|
+
loading: false,
|
|
151
|
+
error: null,
|
|
152
|
+
lastUpdate: new Date()
|
|
153
|
+
};
|
|
154
|
+
this.lastUpdate = new Date();
|
|
155
|
+
this.render();
|
|
156
|
+
} catch (e) {
|
|
157
|
+
this.data.error = e.message;
|
|
158
|
+
this.data.loading = false;
|
|
159
|
+
this.render();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
startPolling() {
|
|
164
|
+
this.pollData();
|
|
165
|
+
this.pollInterval = setInterval(() => this.pollData(), 15000);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
initTUI() {
|
|
169
|
+
this.screen = blessed.screen({
|
|
170
|
+
smartCSR: true,
|
|
171
|
+
title: 'opencode-statboard TUI v' + pkg.version,
|
|
172
|
+
fullUnicode: true,
|
|
173
|
+
autoPadding: false
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
this.screen.key(['q', 'C-c', 'escape'], () => this.shutdown());
|
|
177
|
+
this.screen.key(['1'], () => this.switchView('overview'));
|
|
178
|
+
this.screen.key(['2'], () => this.switchView('projects'));
|
|
179
|
+
this.screen.key(['3'], () => this.switchView('models'));
|
|
180
|
+
this.screen.key(['4'], () => this.switchView('trend'));
|
|
181
|
+
this.screen.key(['5'], () => this.switchView('heatmap'));
|
|
182
|
+
this.screen.key(['tab'], () => this.nextView());
|
|
183
|
+
this.screen.key(['S-tab'], () => this.prevView());
|
|
184
|
+
this.screen.key(['r'], () => this.pollData());
|
|
185
|
+
this.screen.key(['w'], () => this.openWeb());
|
|
186
|
+
|
|
187
|
+
this.createLayout();
|
|
188
|
+
this.render();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
createLayout() {
|
|
192
|
+
this.layout.header = blessed.box({
|
|
193
|
+
top: 0,
|
|
194
|
+
left: 0,
|
|
195
|
+
width: '100%',
|
|
196
|
+
height: 2,
|
|
197
|
+
tags: true,
|
|
198
|
+
border: { type: 'line' },
|
|
199
|
+
style: { border: { fg: 'cyan' } },
|
|
200
|
+
content: '{center}{bold}opencode-statboard v' + pkg.version + '{/bold} TUI | [1]Overview [2]Projects [3]Models [4]Trend [5]Heatmap [W]Web [R]Refresh [Q]Quit{/center}'
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
this.layout.main = blessed.box({
|
|
204
|
+
top: 2,
|
|
205
|
+
bottom: 2,
|
|
206
|
+
left: 0,
|
|
207
|
+
width: '100%',
|
|
208
|
+
height: '100%-4',
|
|
209
|
+
tags: true,
|
|
210
|
+
border: { type: 'line' },
|
|
211
|
+
style: { border: { fg: 'blue' } },
|
|
212
|
+
scrollable: true,
|
|
213
|
+
alwaysScroll: true,
|
|
214
|
+
keys: true,
|
|
215
|
+
vi: true,
|
|
216
|
+
mouse: true
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
this.layout.footer = blessed.box({
|
|
220
|
+
bottom: 0,
|
|
221
|
+
left: 0,
|
|
222
|
+
width: '100%',
|
|
223
|
+
height: 2,
|
|
224
|
+
tags: true,
|
|
225
|
+
border: { type: 'line' },
|
|
226
|
+
style: { border: { fg: 'green' } },
|
|
227
|
+
content: '{center}Last update: {bold}never{/bold} | View: {bold}overview{/bold} | W:Web R:Refresh Q:Quit{/center}'
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
this.screen.append(this.layout.header);
|
|
231
|
+
this.screen.append(this.layout.main);
|
|
232
|
+
this.screen.append(this.layout.footer);
|
|
233
|
+
|
|
234
|
+
this.render();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
switchView(view) {
|
|
238
|
+
if (this.views.includes(view)) {
|
|
239
|
+
this.currentView = view;
|
|
240
|
+
this.selectedIndex = 0;
|
|
241
|
+
this.updateFooter();
|
|
242
|
+
this.render();
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
nextView() {
|
|
247
|
+
const idx = this.views.indexOf(this.currentView);
|
|
248
|
+
this.currentView = this.views[(idx + 1) % this.views.length];
|
|
249
|
+
this.selectedIndex = 0;
|
|
250
|
+
this.updateFooter();
|
|
251
|
+
this.render();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
prevView() {
|
|
255
|
+
const idx = this.views.indexOf(this.currentView);
|
|
256
|
+
this.currentView = this.views[(idx - 1 + this.views.length) % this.views.length];
|
|
257
|
+
this.selectedIndex = 0;
|
|
258
|
+
this.updateFooter();
|
|
259
|
+
this.render();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
updateFooter() {
|
|
263
|
+
const time = this.lastUpdate ? this.lastUpdate.toLocaleTimeString() : 'never';
|
|
264
|
+
const viewLabels = {
|
|
265
|
+
overview: 'Overview',
|
|
266
|
+
projects: 'Projects',
|
|
267
|
+
models: 'Models',
|
|
268
|
+
trend: 'Trend',
|
|
269
|
+
heatmap: 'Heatmap'
|
|
270
|
+
};
|
|
271
|
+
this.layout.footer.setContent(
|
|
272
|
+
'{center}Last update: {bold}' + time + '{/bold} | View: {bold}' + (viewLabels[this.currentView] || this.currentView) + '{/bold} | W:Web R:Refresh Q:Quit{/center}'
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
openWeb() {
|
|
277
|
+
const url = 'http://' + this.host + ':' + this.port;
|
|
278
|
+
const opener = process.platform === 'win32' ? 'start' : process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
279
|
+
spawn(opener, [url], { detached: true, stdio: 'ignore' }).unref();
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
getContentWidth() {
|
|
283
|
+
const mainWidth = this.layout.main.width;
|
|
284
|
+
return typeof mainWidth === 'number' ? mainWidth - 2 : this.screen.width - 4;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
getContentHeight() {
|
|
288
|
+
const mainHeight = this.layout.main.height;
|
|
289
|
+
return typeof mainHeight === 'number' ? mainHeight - 2 : this.screen.height - 6;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
render() {
|
|
293
|
+
if (this.data.loading) {
|
|
294
|
+
this.layout.main.setContent('{center}{bold}Loading data...{/bold}{/center}');
|
|
295
|
+
} else if (this.data.error) {
|
|
296
|
+
this.layout.main.setContent('{center}{red-fg}{bold}Error: ' + this.escape(this.data.error) + '{/bold}{/red-fg}{/center}');
|
|
297
|
+
} else {
|
|
298
|
+
this.layout.main.setContent(this.renderCurrentView());
|
|
299
|
+
}
|
|
300
|
+
this.updateFooter();
|
|
301
|
+
this.screen.render();
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
renderCurrentView() {
|
|
305
|
+
switch (this.currentView) {
|
|
306
|
+
case 'overview': return this.renderOverview();
|
|
307
|
+
case 'projects': return this.renderProjects();
|
|
308
|
+
case 'models': return this.renderModels();
|
|
309
|
+
case 'trend': return this.renderTrend();
|
|
310
|
+
case 'heatmap': return this.renderHeatmap();
|
|
311
|
+
default: return '{center}Unknown view{/center}';
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
renderOverview() {
|
|
316
|
+
const ov = this.data;
|
|
317
|
+
if (!ov || ov.loading) return '{center}{bold}Loading data...{/bold}{/center}';
|
|
318
|
+
|
|
319
|
+
const tokens = ov.tokens || {};
|
|
320
|
+
const cost = ov.cost || {};
|
|
321
|
+
|
|
322
|
+
const lines = [
|
|
323
|
+
'{center}{bold}\u2501\u2501\u2501 OVERVIEW \u2501\u2501\u2501{/bold}{/center}',
|
|
324
|
+
'',
|
|
325
|
+
'{bold}Sessions:{/bold} ' + (ov.overview?.sessions?.main || 0) + ' main / ' + (ov.overview?.sessions?.total || 0) + ' total',
|
|
326
|
+
'{bold}Projects:{/bold} ' + (ov.overview?.projects || 0),
|
|
327
|
+
'{bold}Days:{/bold} ' + (ov.overview?.days || 0),
|
|
328
|
+
'{bold}Messages:{/bold} ' + (ov.overview?.messages || 0),
|
|
329
|
+
'',
|
|
330
|
+
'{bold}Tokens:{/bold}',
|
|
331
|
+
' Input: ' + this.formatNumber(tokens.input || 0),
|
|
332
|
+
' Output: ' + this.formatNumber(tokens.output || 0),
|
|
333
|
+
' Reasoning: ' + this.formatNumber(tokens.reasoning || 0),
|
|
334
|
+
' Cache Read: ' + this.formatNumber(tokens.cacheRead || 0),
|
|
335
|
+
' Cache Write: ' + this.formatNumber(tokens.cacheWrite || 0),
|
|
336
|
+
' {bold}Total: ' + this.formatNumber(tokens.total || 0) + '{/bold}',
|
|
337
|
+
'',
|
|
338
|
+
'{bold}Cost:{/bold} ' + this.formatCost(cost.total || 0),
|
|
339
|
+
'{bold}Market Value:{/bold} ' + this.formatCost(cost.marketValue || 0),
|
|
340
|
+
'{bold}Saved:{/bold} ' + this.formatCost(cost.saved || 0) + (cost.savedPct ? ' (' + cost.savedPct + '%)' : ''),
|
|
341
|
+
'',
|
|
342
|
+
'{bold}Avg Cost/Day:{/bold} ' + this.formatCost(cost.avgCostPerDay || 0),
|
|
343
|
+
'{bold}Avg Tokens/Day:{/bold} ' + this.formatNumber(cost.avgTokensPerDay || 0)
|
|
344
|
+
];
|
|
345
|
+
return lines.join('\n');
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
renderProjects() {
|
|
349
|
+
const projects = this.data.projects || [];
|
|
350
|
+
if (projects.length === 0) return '{center}No projects{/center}';
|
|
351
|
+
|
|
352
|
+
const lines = ['{center}{bold}\u2501\u2501\u2501 PROJECTS \u2501\u2501\u2501{/bold}{/center}', ''];
|
|
353
|
+
lines.push('{bold}{Project:<45} {Sessions:>10} {Tokens:>18} {Cost:>14}{/bold}');
|
|
354
|
+
lines.push('{gray}\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500{/gray}');
|
|
355
|
+
|
|
356
|
+
for (const p of projects.slice(0, 50)) {
|
|
357
|
+
const name = p.name.length > 43 ? p.name.slice(0, 40) + '...' : p.name;
|
|
358
|
+
const tokens = this.formatNumber(p.tokens?.total || 0);
|
|
359
|
+
const cost = this.formatCost(p.cost || 0);
|
|
360
|
+
const sessions = (p.mainSessionCount || 0) + '/' + (p.sessionCount || 0);
|
|
361
|
+
const namePad = name.padEnd(45);
|
|
362
|
+
const sessPad = sessions.padStart(10);
|
|
363
|
+
const tokPad = this.formatNumber(p.tokens?.total || 0).padStart(18);
|
|
364
|
+
const costPad = this.formatCost(p.cost || 0).padStart(14);
|
|
365
|
+
lines.push(namePad + ' ' + sessPad + ' ' + tokPad + ' ' + costPad);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (projects.length > 50) {
|
|
369
|
+
lines.push('');
|
|
370
|
+
lines.push('{gray}... and ' + (projects.length - 50) + ' more projects{/gray}');
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return lines.join('\n');
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
renderModels() {
|
|
377
|
+
const models = this.data.models || [];
|
|
378
|
+
if (models.length === 0) return '{center}No models{/center}';
|
|
379
|
+
|
|
380
|
+
const lines = ['{center}{bold}\u2501\u2501\u2501 MODELS \u2501\u2501\u2501{/bold}{/center}', ''];
|
|
381
|
+
lines.push('{bold}{Model:<40} {Provider:<14} {Tokens:>18} {Cost:>12} {Speed:>10}{/bold}');
|
|
382
|
+
lines.push('{gray}\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500{/gray}');
|
|
383
|
+
|
|
384
|
+
for (const m of models.slice(0, 30)) {
|
|
385
|
+
const name = m.name.length > 38 ? m.name.slice(0, 35) + '...' : m.name;
|
|
386
|
+
const provider = (m.provider || 'unknown').slice(0, 12);
|
|
387
|
+
const tokens = this.formatNumber(m.tokens?.total || m.tokens || 0);
|
|
388
|
+
const cost = this.formatCost(m.cost || 0);
|
|
389
|
+
const speed = m.speed ? m.speed.toFixed(1) + ' tok/s' : '\u2014';
|
|
390
|
+
const namePad = name.padEnd(40);
|
|
391
|
+
const provPad = provider.padEnd(14);
|
|
392
|
+
const tokPad = tokens.padStart(18);
|
|
393
|
+
const costPad = cost.padStart(12);
|
|
394
|
+
const speedPad = speed.padStart(10);
|
|
395
|
+
lines.push(namePad + ' ' + provPad + ' ' + tokPad + ' ' + costPad + ' ' + speedPad);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return lines.join('\n');
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
renderTrend() {
|
|
402
|
+
const trend = this.data.trend;
|
|
403
|
+
if (!trend || !trend.labels || !trend.labels.length) {
|
|
404
|
+
return '{center}No trend data{/center}';
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const labels = trend.labels;
|
|
408
|
+
const input = trend.input || [];
|
|
409
|
+
const output = trend.output || [];
|
|
410
|
+
const cost = trend.cost || [];
|
|
411
|
+
|
|
412
|
+
const maxVal = Math.max(...input, ...output, 1);
|
|
413
|
+
const height = this.getContentHeight() - 4;
|
|
414
|
+
const width = this.getContentWidth();
|
|
415
|
+
|
|
416
|
+
let out = '{center}{bold}\u2501\u2501\u2501 TREND (90 days) \u2501\u2501\u2501{/bold}{/center}\n\n';
|
|
417
|
+
out += '{bold}Input (cyan) / Output (green) / Cost (yellow){/bold}\n\n';
|
|
418
|
+
|
|
419
|
+
for (let row = height - 1; row >= 0; row--) {
|
|
420
|
+
let line = '';
|
|
421
|
+
const threshold = (row / height) * maxVal;
|
|
422
|
+
for (let i = 0; i < width; i++) {
|
|
423
|
+
const idx = Math.floor(i * labels.length / width);
|
|
424
|
+
const inVal = input[idx] || 0;
|
|
425
|
+
const outVal = output[idx] || 0;
|
|
426
|
+
if (inVal >= threshold && outVal >= threshold) line += '{magenta-fg}\u2588{/magenta-fg}';
|
|
427
|
+
else if (inVal >= threshold) line += '{cyan-fg}\u2588{/cyan-fg}';
|
|
428
|
+
else if (outVal >= threshold) line += '{green-fg}\u2588{/green-fg}';
|
|
429
|
+
else line += ' ';
|
|
430
|
+
}
|
|
431
|
+
out += line + '\n';
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
let xAxis = '';
|
|
435
|
+
const labelStep = Math.max(1, Math.ceil(width / 20));
|
|
436
|
+
for (let i = 0; i < width; i++) {
|
|
437
|
+
if (i % Math.ceil(width / 20) === 0) {
|
|
438
|
+
const idx = Math.floor(i * labels.length / width);
|
|
439
|
+
xAxis += (labels[idx] || ' ').padEnd(Math.ceil(width / 20), ' ').slice(0, Math.ceil(width / 20));
|
|
440
|
+
} else {
|
|
441
|
+
xAxis += ' ';
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
out += '\n{gray}' + xAxis + '{/gray}\n';
|
|
445
|
+
|
|
446
|
+
const lastIdx = labels.length - 1;
|
|
447
|
+
out += '\n{bold}Today:{/bold} Input: ' + this.formatNumber(input[lastIdx] || 0) + ' | Output: ' + this.formatNumber(output[lastIdx] || 0) + ' | Cost: ' + this.formatCost(cost[lastIdx] || 0);
|
|
448
|
+
|
|
449
|
+
return out;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
renderHeatmap() {
|
|
453
|
+
const trend = this.data.trend;
|
|
454
|
+
if (!trend || !trend.dates || !trend.dates.length) {
|
|
455
|
+
return '{center}No heatmap data{/center}';
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const dates = trend.dates;
|
|
459
|
+
const input = trend.input || [];
|
|
460
|
+
const cache = trend.cacheRead || [];
|
|
461
|
+
const vals = dates.map((d, i) => (input[i] || 0) + (cache[i] || 0));
|
|
462
|
+
const max = Math.max(1, ...vals);
|
|
463
|
+
|
|
464
|
+
const firstDate = new Date(dates[0]);
|
|
465
|
+
const lead = firstDate.getDay();
|
|
466
|
+
const cells = Array(lead).fill(null);
|
|
467
|
+
dates.forEach((d, i) => cells.push({ date: d, value: vals[i] }));
|
|
468
|
+
|
|
469
|
+
const weeks = [];
|
|
470
|
+
for (let i = 0; i < cells.length; i += 7) weeks.push(cells.slice(i, i + 7));
|
|
471
|
+
while (weeks[weeks.length - 1]?.length < 7) weeks[weeks.length - 1].push(null);
|
|
472
|
+
|
|
473
|
+
const monthNames = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
|
|
474
|
+
|
|
475
|
+
let out = '{center}{bold}\u2501\u2501\u2501 HEATMAP (90 days) \u2501\u2501\u2501{/bold}{/center}\n\n';
|
|
476
|
+
|
|
477
|
+
let monthHeader = ' ';
|
|
478
|
+
let lastMonth = -1;
|
|
479
|
+
for (let w = 0; w < weeks.length; w++) {
|
|
480
|
+
const firstCell = weeks[w].find(c => c);
|
|
481
|
+
if (firstCell) {
|
|
482
|
+
const m = new Date(firstCell.date).getMonth();
|
|
483
|
+
if (m !== lastMonth) {
|
|
484
|
+
monthHeader += monthNames[m].padStart(7, ' ');
|
|
485
|
+
lastMonth = m;
|
|
486
|
+
} else {
|
|
487
|
+
monthHeader += ' ';
|
|
488
|
+
}
|
|
489
|
+
} else {
|
|
490
|
+
monthHeader += ' ';
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
out = '{gray}' + monthHeader + '{/gray}\n';
|
|
494
|
+
|
|
495
|
+
const dow = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
496
|
+
for (let d = 0; d < 7; d++) {
|
|
497
|
+
let line = '{bold}' + dow[d].padEnd(4) + '{/bold} ';
|
|
498
|
+
for (let w = 0; w < weeks.length; w++) {
|
|
499
|
+
const cell = weeks[w][d];
|
|
500
|
+
if (!cell) {
|
|
501
|
+
line += ' \u00B7 ';
|
|
502
|
+
} else {
|
|
503
|
+
const level = this.getHeatLevel(cell.value, max);
|
|
504
|
+
const color = this.getHeatColor(level);
|
|
505
|
+
const char = level === 0 ? '\u00B7' : level <= 4 ? '\u2591' : level <= 8 ? '\u2592' : level <= 12 ? '\u2593' : '\u2588';
|
|
506
|
+
line += '{' + color + '-fg}' + char + ' {/}';
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
out += line + '\n';
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
out += '\n{gray}Less \u2591\u2591\u2592\u2592\u2593\u2593\u2588\u2588 More (max: ' + this.formatNumber(max) + '){/gray}';
|
|
513
|
+
return out;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
getHeatLevel(value, max) {
|
|
517
|
+
if (!value || value <= 0) return 0;
|
|
518
|
+
const r = value / max;
|
|
519
|
+
if (r >= 0.95) return 20;
|
|
520
|
+
if (r >= 0.90) return 19;
|
|
521
|
+
if (r >= 0.85) return 18;
|
|
522
|
+
if (r >= 0.80) return 17;
|
|
523
|
+
if (r >= 0.75) return 16;
|
|
524
|
+
if (r >= 0.70) return 15;
|
|
525
|
+
if (r >= 0.65) return 14;
|
|
526
|
+
if (r >= 0.60) return 13;
|
|
527
|
+
if (r >= 0.55) return 12;
|
|
528
|
+
if (r >= 0.50) return 11;
|
|
529
|
+
if (r >= 0.45) return 10;
|
|
530
|
+
if (r >= 0.40) return 9;
|
|
531
|
+
if (r >= 0.35) return 9;
|
|
532
|
+
if (r >= 0.30) return 8;
|
|
533
|
+
if (r >= 0.25) return 7;
|
|
534
|
+
if (r >= 0.20) return 6;
|
|
535
|
+
if (r >= 0.15) return 5;
|
|
536
|
+
if (r >= 0.10) return 4;
|
|
537
|
+
if (r >= 0.05) return 3;
|
|
538
|
+
if (r >= 0.02) return 2;
|
|
539
|
+
return 1;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
getHeatColor(level) {
|
|
543
|
+
if (level === 0) return 'gray';
|
|
544
|
+
if (level <= 4) return 'blue';
|
|
545
|
+
if (level <= 8) return 'cyan';
|
|
546
|
+
if (level <= 12) return 'green';
|
|
547
|
+
if (level <= 16) return 'yellow';
|
|
548
|
+
return 'red';
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
formatNumber(n) {
|
|
552
|
+
if (n >= 1e12) return (n / 1e12).toFixed(2) + 'T';
|
|
553
|
+
if (n >= 1e9) return (n / 1e9).toFixed(2) + 'B';
|
|
554
|
+
if (n >= 1e6) return (n / 1e6).toFixed(2) + 'M';
|
|
555
|
+
if (n >= 1e3) return (n / 1e3).toFixed(2) + 'K';
|
|
556
|
+
return String(n);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
formatCost(c) {
|
|
560
|
+
if (c == null) return '\u2014';
|
|
561
|
+
if (c >= 1e6) return '$' + (c / 1e6).toFixed(2) + 'M';
|
|
562
|
+
if (c >= 1e3) return '$' + (c / 1e3).toFixed(2) + 'K';
|
|
563
|
+
return '$' + c.toFixed(2);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
escape(str) {
|
|
567
|
+
return String(str).replace(/[{}]/g, '');
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
log(msg) {
|
|
571
|
+
const time = new Date().toLocaleTimeString();
|
|
572
|
+
console.log('[' + time + '] ' + this.escape(msg));
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
getContentWidth() {
|
|
576
|
+
const mainWidth = this.layout.main.width;
|
|
577
|
+
return typeof mainWidth === 'number' ? mainWidth - 2 : this.screen.width - 4;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
getContentHeight() {
|
|
581
|
+
const mainHeight = this.layout.main.height;
|
|
582
|
+
return typeof mainHeight === 'number' ? mainHeight - 2 : this.screen.height - 6;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
sleep(ms) {
|
|
586
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
shutdown() {
|
|
590
|
+
if (this.pollInterval) clearInterval(this.pollInterval);
|
|
591
|
+
if (this.serverProcess) this.serverProcess.kill();
|
|
592
|
+
this.screen.destroy();
|
|
593
|
+
process.exit(0);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
if (require.main === module) {
|
|
598
|
+
const args = process.argv.slice(2);
|
|
599
|
+
const port = parseInt(process.env.OC_PORT || '4868', 10);
|
|
600
|
+
|
|
601
|
+
const webOnly = args.includes('--web') || args.includes('-w');
|
|
602
|
+
const bothMode = args.includes('--both') || args.includes('-b');
|
|
603
|
+
|
|
604
|
+
if (webOnly) {
|
|
605
|
+
require('./server.js');
|
|
606
|
+
} else if (bothMode) {
|
|
607
|
+
const tui = new TUIServer({ port });
|
|
608
|
+
tui.start();
|
|
609
|
+
} else {
|
|
610
|
+
const tui = new TUIServer({ port });
|
|
611
|
+
tui.start();
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
module.exports = { TUIServer };
|