Spaces:
Running

Wendy-Fly commited on
Commit
b06fcf2
·
verified ·
1 Parent(s): 4b15561

Upload copy.html with huggingface_hub

Browse files
Files changed (1) hide show
  1. copy.html +824 -0
copy.html ADDED
@@ -0,0 +1,824 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta name="viewport" content="width=device-width,initial-scale=1">
5
+ <style>
6
+ * { margin:0; padding:0; box-sizing:border-box; }
7
+ body { background:#0a0e17; color:#c0d8ef; font-family:system-ui; padding:20px; }
8
+ h1 { color:#00e5ff; font-size:18px; margin-bottom:12px; }
9
+ .btn {
10
+ display:block; width:100%; padding:16px;
11
+ background:#00e5ff; color:#000; border:none;
12
+ border-radius:10px; font-size:18px; font-weight:bold;
13
+ cursor:pointer; margin-bottom:16px;
14
+ }
15
+ .btn:active { background:#0097a7; }
16
+ .btn.ok { background:#00e676; }
17
+ .note { color:#4a5e78; font-size:13px; line-height:1.5; margin-bottom:12px; }
18
+ pre {
19
+ background:#111827; border:1px solid #1e3a5f; border-radius:8px;
20
+ padding:12px; font-size:10px; color:#8b9dc3;
21
+ overflow:auto; max-height:300px; white-space:pre;
22
+ -webkit-overflow-scrolling:touch;
23
+ }
24
+ </style>
25
+ </head>
26
+ <body>
27
+ <h1>// HANGAR MONITOR</h1>
28
+ <button class="btn" id="copyBtn" onclick="copyCode()">COPY TO CLIPBOARD</button>
29
+ <p class="note">
30
+ 1. 点上面按钮复制全部代码<br>
31
+ 2. 打开 Scriptable - 新建脚本 - 粘贴<br>
32
+ 3. 长按桌面 - 添加小组件 - Scriptable - 选 Medium
33
+ </p>
34
+ <pre id="code"></pre>
35
+ <script>
36
+ const CODE = `// ========================================================
37
+ // HANGAR MONITOR — Gundam-style GPU Server Dashboard
38
+ // Scriptable iOS — WebView live dashboard + widget fallback
39
+ // ========================================================
40
+
41
+ const REPO = "Wendy-Fly/Truck_2026";
42
+ const HF_TOKEN = "YOUR_HF_TOKEN_HERE";
43
+
44
+ // ============================================================
45
+ // DATA FETCH (shared by widget & webview)
46
+ // ============================================================
47
+ async function fetchServers() {
48
+ const headers = { Authorization: "Bearer " + HF_TOKEN };
49
+ try {
50
+ const treeReq = new Request(
51
+ \`https://huggingface.co/api/datasets/\${REPO}/tree/main/monitor\`
52
+ );
53
+ treeReq.headers = headers;
54
+ const files = await treeReq.loadJSON();
55
+ const servers = [];
56
+ for (const f of files.filter((x) => x.path.endsWith(".json"))) {
57
+ const req = new Request(
58
+ \`https://huggingface.co/datasets/\${REPO}/resolve/main/\${f.path}?t=\${Date.now()}\`
59
+ );
60
+ req.headers = headers;
61
+ try { servers.push(await req.loadJSON()); } catch (e) {}
62
+ }
63
+ return servers;
64
+ } catch (e) { return []; }
65
+ }
66
+
67
+ // ============================================================
68
+ // WEBVIEW DASHBOARD (in-app, full animation)
69
+ // ============================================================
70
+ function buildHTML(serversJSON) {
71
+ return \`<!DOCTYPE html>
72
+ <html>
73
+ <head>
74
+ <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
75
+ <style>
76
+ * { margin:0; padding:0; box-sizing:border-box; }
77
+ body {
78
+ background: #05080f;
79
+ font-family: 'Menlo', 'Courier New', monospace;
80
+ color: #c0d8ef;
81
+ height: 100vh; width: 100vw;
82
+ display: flex; flex-direction: column;
83
+ overflow: hidden;
84
+ position: relative;
85
+ }
86
+
87
+ /* Scan line effect */
88
+ body::after {
89
+ content: '';
90
+ position: fixed; top: 0; left: 0; right: 0;
91
+ height: 2px;
92
+ background: linear-gradient(90deg, transparent, #00e5ff88, transparent);
93
+ animation: scanline 4s linear infinite;
94
+ z-index: 100;
95
+ pointer-events: none;
96
+ }
97
+ @keyframes scanline {
98
+ 0% { top: -2px; opacity: 0; }
99
+ 10% { opacity: 1; }
100
+ 90% { opacity: 1; }
101
+ 100% { top: 100vh; opacity: 0; }
102
+ }
103
+
104
+ /* Vignette */
105
+ body::before {
106
+ content: '';
107
+ position: fixed; top:0;left:0;right:0;bottom:0;
108
+ background: radial-gradient(ellipse at center, transparent 50%, #05080fcc 100%);
109
+ z-index: 99; pointer-events: none;
110
+ }
111
+
112
+ /* Top bar */
113
+ .topbar {
114
+ display: flex; align-items: center; justify-content: space-between;
115
+ padding: 12px 16px 8px 16px;
116
+ border-bottom: 1px solid #1a274444;
117
+ flex-shrink: 0;
118
+ }
119
+ .topbar .logo {
120
+ color: #00e5ff; font-size: 13px; font-weight: bold;
121
+ text-shadow: 0 0 8px #00e5ff44;
122
+ }
123
+ .topbar .status {
124
+ font-size: 11px; font-weight: bold;
125
+ }
126
+ .topbar .status.ok { color: #00e676; text-shadow: 0 0 6px #00e67644; }
127
+ .topbar .status.warn { color: #ffab00; }
128
+ .topbar .status.off { color: #ff1744; }
129
+
130
+ /* Bay grid */
131
+ .bays {
132
+ flex: 1;
133
+ display: grid;
134
+ gap: 8px;
135
+ padding: 10px 12px;
136
+ /* adaptive: set via JS */
137
+ }
138
+
139
+ /* Single bay */
140
+ .bay {
141
+ background: #0b1120;
142
+ border: 1px solid #1e3a5f;
143
+ border-radius: 10px;
144
+ padding: 10px 12px;
145
+ display: flex; flex-direction: column;
146
+ position: relative;
147
+ overflow: hidden;
148
+ transition: all 0.5s;
149
+ }
150
+ .bay.offline {
151
+ background: #080b12;
152
+ border-color: #151e2e;
153
+ }
154
+
155
+ /* Corner accents */
156
+ .bay::before, .bay::after {
157
+ content: '';
158
+ position: absolute;
159
+ width: 12px; height: 12px;
160
+ border-color: #1e3a5f;
161
+ border-style: solid;
162
+ }
163
+ .bay::before {
164
+ top: -1px; left: -1px;
165
+ border-width: 2px 0 0 2px;
166
+ border-radius: 10px 0 0 0;
167
+ }
168
+ .bay::after {
169
+ bottom: -1px; right: -1px;
170
+ border-width: 0 2px 2px 0;
171
+ border-radius: 0 0 10px 0;
172
+ }
173
+ .bay.online::before, .bay.online::after {
174
+ border-color: #00e5ff44;
175
+ }
176
+ .bay.offline::before, .bay.offline::after {
177
+ border-color: #151e2e;
178
+ }
179
+
180
+ /* Bay glow for online */
181
+ .bay.online {
182
+ box-shadow: inset 0 0 30px #00e5ff08, 0 0 15px #00e5ff06;
183
+ }
184
+
185
+ /* Bay header */
186
+ .bay-hdr {
187
+ display: flex; align-items: center; gap: 6px;
188
+ margin-bottom: 6px;
189
+ }
190
+
191
+ /* Heartbeat light */
192
+ .hb-light {
193
+ width: 8px; height: 8px; border-radius: 50%;
194
+ flex-shrink: 0;
195
+ }
196
+ .hb-light.on {
197
+ background: #00e676;
198
+ box-shadow: 0 0 6px #00e676, 0 0 12px #00e67644;
199
+ animation: heartbeat 2s ease-in-out infinite;
200
+ }
201
+ .hb-light.off {
202
+ background: #151e2e;
203
+ }
204
+ @keyframes heartbeat {
205
+ 0%, 100% { opacity: 1; transform: scale(1); }
206
+ 15% { opacity: 1; transform: scale(1.4); box-shadow: 0 0 10px #00e676, 0 0 20px #00e67666; }
207
+ 30% { opacity: 0.8; transform: scale(1); }
208
+ 45% { opacity: 1; transform: scale(1.2); box-shadow: 0 0 8px #00e676, 0 0 16px #00e67644; }
209
+ 60% { opacity: 0.7; transform: scale(1); }
210
+ }
211
+
212
+ /* Stagger heartbeat per bay */
213
+ .bay:nth-child(2) .hb-light.on { animation-delay: -0.5s; }
214
+ .bay:nth-child(3) .hb-light.on { animation-delay: -1.0s; }
215
+ .bay:nth-child(4) .hb-light.on { animation-delay: -1.5s; }
216
+
217
+ .bay-label {
218
+ font-size: 10px; color: #4fc3f7; font-weight: bold;
219
+ text-shadow: 0 0 4px #4fc3f744;
220
+ }
221
+ .bay.offline .bay-label { color: #252e40; text-shadow: none; }
222
+
223
+ .bay-name {
224
+ font-size: 18px; font-weight: bold;
225
+ color: #e8edf5;
226
+ margin-bottom: 4px;
227
+ text-shadow: 0 0 8px #e8edf522;
228
+ }
229
+ .bay.offline .bay-name { color: #252e40; text-shadow: none; }
230
+
231
+ /* Offline state */
232
+ .offline-msg {
233
+ color: #252e40; font-size: 10px; font-weight: bold;
234
+ letter-spacing: 2px;
235
+ margin: auto 0;
236
+ text-align: center;
237
+ }
238
+
239
+ /* Stats row */
240
+ .stats {
241
+ display: flex; gap: 8px; align-items: center;
242
+ font-size: 10px; margin-bottom: 6px;
243
+ color: #2a3a52;
244
+ }
245
+ .stat-val { font-weight: bold; }
246
+ .stat-val.g { color: #00e676; }
247
+ .stat-val.y { color: #ffab00; }
248
+ .stat-val.r { color: #ff1744; }
249
+
250
+ .age { color: #2a3a52; font-size: 9px; }
251
+
252
+ /* GPU grid */
253
+ .gpu-grid {
254
+ display: grid;
255
+ grid-template-columns: repeat(4, 1fr);
256
+ gap: 3px;
257
+ flex: 1;
258
+ align-content: center;
259
+ }
260
+
261
+ .gpu-cell {
262
+ background: #151e2e;
263
+ border-radius: 4px;
264
+ padding: 3px 2px;
265
+ text-align: center;
266
+ border: 1px solid transparent;
267
+ transition: all 0.3s;
268
+ display: flex; flex-direction: column;
269
+ align-items: center; justify-content: center;
270
+ min-height: 28px;
271
+ }
272
+ .gpu-cell.busy {
273
+ border-color: currentColor;
274
+ animation: gpu-pulse 3s ease-in-out infinite;
275
+ }
276
+ .gpu-cell.g { color: #00e676; background: #00e67615; }
277
+ .gpu-cell.y { color: #ffab00; background: #ffab0015; }
278
+ .gpu-cell.r { color: #ff1744; background: #ff174415; }
279
+
280
+ @keyframes gpu-pulse {
281
+ 0%, 100% { opacity: 1; }
282
+ 50% { opacity: 0.7; }
283
+ }
284
+ .gpu-cell:nth-child(odd).busy { animation-delay: -1.5s; }
285
+
286
+ .gpu-id { font-size: 8px; font-weight: bold; }
287
+ .gpu-pct { font-size: 7px; }
288
+ .gpu-cell:not(.busy) .gpu-id,
289
+ .gpu-cell:not(.busy) .gpu-pct { color: #2a3a52; }
290
+
291
+ /* Dark placeholder cells for offline */
292
+ .gpu-cell.dark {
293
+ background: #0a0e17;
294
+ min-height: 28px;
295
+ }
296
+
297
+ /* Summary */
298
+ .summary {
299
+ display: flex; align-items: center; gap: 6px;
300
+ margin-top: 6px; font-size: 10px;
301
+ }
302
+ .summary .active { color: #00e676; font-weight: bold; }
303
+ .summary .idle { color: #2a3a52; }
304
+ .proc-badge {
305
+ background: #ffab0020; color: #ffab00;
306
+ padding: 1px 5px; border-radius: 3px;
307
+ font-size: 8px; font-weight: bold;
308
+ }
309
+
310
+ /* Bottom bar */
311
+ .botbar {
312
+ display: flex; align-items: center; justify-content: space-between;
313
+ padding: 6px 16px 10px 16px;
314
+ border-top: 1px solid #1a274444;
315
+ flex-shrink: 0;
316
+ font-size: 10px;
317
+ }
318
+ .botbar .scan { color: #2a3a52; }
319
+ .botbar .time { color: #00e5ff; font-weight: bold; }
320
+ .botbar .ver { color: #2a3a52; }
321
+
322
+ /* Refresh spinner */
323
+ .refreshing .topbar .logo::after {
324
+ content: ' \\\\25C9';
325
+ animation: spin 1s linear infinite;
326
+ display: inline-block;
327
+ }
328
+ @keyframes spin {
329
+ from { transform: rotate(0deg); }
330
+ to { transform: rotate(360deg); }
331
+ }
332
+ </style>
333
+ </head>
334
+ <body>
335
+ <div class="topbar">
336
+ <div class="logo">// HANGAR MONITOR</div>
337
+ <div class="status" id="topStatus"></div>
338
+ </div>
339
+ <div class="bays" id="baysGrid"></div>
340
+ <div class="botbar">
341
+ <span class="scan">SCAN <span class="time" id="scanTime"></span></span>
342
+ <span class="ver" id="sysStatus">SYS NOMINAL</span>
343
+ </div>
344
+
345
+ <script>
346
+ const HF_TOKEN = "\${HF_TOKEN}";
347
+ const REPO = "\${REPO}";
348
+
349
+ function pctClass(p) {
350
+ if (p >= 90) return 'r';
351
+ if (p >= 70) return 'y';
352
+ return 'g';
353
+ }
354
+
355
+ function gpuMemPct(g) {
356
+ return g.memory_total_mb > 0 ? (g.memory_used_mb / g.memory_total_mb) * 100 : 0;
357
+ }
358
+
359
+ function formatAge(ts) {
360
+ if (!ts) return '--';
361
+ const m = Math.floor((Date.now() - new Date(ts).getTime()) / 60000);
362
+ if (m < 1) return '<1m';
363
+ if (m < 60) return m + 'm';
364
+ const h = Math.floor(m / 60);
365
+ if (h < 24) return h + 'h';
366
+ return Math.floor(h / 24) + 'd';
367
+ }
368
+
369
+ function isOffline(ts) {
370
+ if (!ts) return true;
371
+ return Date.now() - new Date(ts).getTime() > 15 * 60000;
372
+ }
373
+
374
+ async function fetchServers() {
375
+ const headers = { Authorization: 'Bearer ' + HF_TOKEN };
376
+ try {
377
+ const resp = await fetch(
378
+ 'https://huggingface.co/api/datasets/' + REPO + '/tree/main/monitor',
379
+ { headers }
380
+ );
381
+ const files = await resp.json();
382
+ const servers = [];
383
+ for (const f of files.filter(x => x.path.endsWith('.json'))) {
384
+ try {
385
+ const r = await fetch(
386
+ 'https://huggingface.co/datasets/' + REPO + '/resolve/main/' + f.path + '?t=' + Date.now(),
387
+ { headers }
388
+ );
389
+ servers.push(await r.json());
390
+ } catch(e) {}
391
+ }
392
+ return servers;
393
+ } catch(e) { return []; }
394
+ }
395
+
396
+ function renderBay(server, online) {
397
+ const cls = online ? 'bay online' : 'bay offline';
398
+ const hostname = server ? server.hostname || '---' : '---';
399
+ const shortName = hostname.slice(-3) || '---';
400
+ const bayNum = String(arguments[2] + 1).padStart(2, '0');
401
+
402
+ let html = '<div class="' + cls + '">';
403
+ html += '<div class="bay-hdr">';
404
+ html += '<div class="hb-light ' + (online ? 'on' : 'off') + '"></div>';
405
+ html += '<span class="bay-label">BAY' + bayNum + '</span>';
406
+ html += '</div>';
407
+ html += '<div class="bay-name">' + shortName + '</div>';
408
+
409
+ if (!online) {
410
+ html += '<div class="offline-msg">OFFLINE</div>';
411
+ html += '<div class="gpu-grid">';
412
+ for (let i = 0; i < 8; i++) html += '<div class="gpu-cell dark"></div>';
413
+ html += '</div>';
414
+ html += '<div class="offline-msg" style="margin-top:6px;font-size:8px">PWR OFF</div>';
415
+ html += '</div>';
416
+ return html;
417
+ }
418
+
419
+ const hw = server.hardware || {};
420
+ const gpus = hw.gpus || [];
421
+ const cpuPct = Math.round(hw.cpu_percent || 0);
422
+ const memPct = Math.round(hw.memory_percent || 0);
423
+
424
+ html += '<div class="stats">';
425
+ html += '<span class="age">' + formatAge(server.timestamp) + '</span>';
426
+ html += '<span>CPU <span class="stat-val ' + pctClass(cpuPct) + '">' + cpuPct + '%</span></span>';
427
+ html += '<span>MEM <span class="stat-val ' + pctClass(memPct) + '">' + memPct + '%</span></span>';
428
+ html += '</div>';
429
+
430
+ html += '<div class="gpu-grid">';
431
+ const totalGpus = Math.max(gpus.length, 8);
432
+ let busyCount = 0;
433
+ for (let i = 0; i < totalGpus; i++) {
434
+ const g = i < gpus.length ? gpus[i] : null;
435
+ const mp = g ? gpuMemPct(g) : 0;
436
+ const busy = mp > 5;
437
+ if (busy) busyCount++;
438
+ const cc = busy ? pctClass(mp) : '';
439
+ html += '<div class="gpu-cell ' + (busy ? 'busy ' : '') + cc + '">';
440
+ if (g) {
441
+ html += '<div class="gpu-id">G' + g.id + '</div>';
442
+ html += '<div class="gpu-pct">' + Math.round(mp) + '%</div>';
443
+ }
444
+ html += '</div>';
445
+ }
446
+ html += '</div>';
447
+
448
+ html += '<div class="summary">';
449
+ if (busyCount > 0) {
450
+ html += '<span class="active">' + busyCount + '/' + gpus.length + ' ACTIVE</span>';
451
+ } else {
452
+ html += '<span class="idle">' + gpus.length + ' GPU IDLE</span>';
453
+ }
454
+ const tasks = server.tasks || [];
455
+ if (tasks.length > 0) {
456
+ html += '<span class="proc-badge">' + tasks.length + ' PROC</span>';
457
+ }
458
+ html += '</div>';
459
+ html += '</div>';
460
+ return html;
461
+ }
462
+
463
+ function render(servers) {
464
+ // Sort: online first, then by hostname
465
+ servers.sort((a, b) => {
466
+ const ao = isOffline(a.timestamp), bo = isOffline(b.timestamp);
467
+ if (ao !== bo) return ao ? 1 : -1;
468
+ return (a.hostname || '').localeCompare(b.hostname || '');
469
+ });
470
+
471
+ const onlineCount = servers.filter(s => !isOffline(s.timestamp)).length;
472
+ const total = servers.length;
473
+
474
+ // Top status
475
+ const topStatus = document.getElementById('topStatus');
476
+ if (onlineCount === total && total > 0) {
477
+ topStatus.textContent = total + '/' + total + ' ONLINE';
478
+ topStatus.className = 'status ok';
479
+ } else if (onlineCount > 0) {
480
+ topStatus.textContent = onlineCount + '/' + total + ' ONLINE';
481
+ topStatus.className = 'status warn';
482
+ } else {
483
+ topStatus.textContent = 'ALL OFFLINE';
484
+ topStatus.className = 'status off';
485
+ }
486
+
487
+ // Adaptive grid layout
488
+ const grid = document.getElementById('baysGrid');
489
+ const n = servers.length;
490
+ if (n <= 1) {
491
+ grid.style.gridTemplateColumns = '1fr';
492
+ grid.style.gridTemplateRows = '1fr';
493
+ } else if (n === 2) {
494
+ grid.style.gridTemplateColumns = '1fr 1fr';
495
+ grid.style.gridTemplateRows = '1fr';
496
+ } else if (n === 3) {
497
+ grid.style.gridTemplateColumns = '1fr 1fr 1fr';
498
+ grid.style.gridTemplateRows = '1fr';
499
+ } else {
500
+ grid.style.gridTemplateColumns = '1fr 1fr';
501
+ grid.style.gridTemplateRows = '1fr 1fr';
502
+ }
503
+
504
+ let html = '';
505
+ for (let i = 0; i < n; i++) {
506
+ html += renderBay(servers[i], !isOffline(servers[i].timestamp), i);
507
+ }
508
+ grid.innerHTML = html;
509
+
510
+ // Scan time
511
+ document.getElementById('scanTime').textContent =
512
+ new Date().toLocaleTimeString('en-GB', { hour12: false });
513
+
514
+ // System status
515
+ document.getElementById('sysStatus').textContent =
516
+ onlineCount > 0 ? 'SYS NOMINAL' : 'SYS ALERT';
517
+ document.getElementById('sysStatus').style.color =
518
+ onlineCount > 0 ? '#2a3a52' : '#ff1744';
519
+ }
520
+
521
+ async function refresh() {
522
+ document.body.classList.add('refreshing');
523
+ const servers = await fetchServers();
524
+ render(servers);
525
+ document.body.classList.remove('refreshing');
526
+ }
527
+
528
+ // Initial render with pre-loaded data
529
+ const initData = \${serversJSON};
530
+ render(initData);
531
+
532
+ // Auto refresh - follow system pace, no aggressive polling
533
+ // Refresh on visibility change (user opens app) + every 60s as fallback
534
+ document.addEventListener('visibilitychange', () => {
535
+ if (!document.hidden) refresh();
536
+ });
537
+ setInterval(refresh, 60000);
538
+ </script>
539
+ </body>
540
+ </html>\`;
541
+ }
542
+
543
+ // ============================================================
544
+ // WIDGET FALLBACK (static, for home screen)
545
+ // ============================================================
546
+ function isOff(ts) {
547
+ if (!ts) return true;
548
+ return Date.now() - new Date(ts).getTime() > 15 * 60000;
549
+ }
550
+
551
+ function fmtAge(ts) {
552
+ if (!ts) return "--";
553
+ const m = Math.floor((Date.now() - new Date(ts).getTime()) / 60000);
554
+ if (m < 1) return "<1m";
555
+ if (m < 60) return m + "m";
556
+ const h = Math.floor(m / 60);
557
+ return h < 24 ? h + "h" : Math.floor(h / 24) + "d";
558
+ }
559
+
560
+ function pc(p) {
561
+ if (p >= 90) return new Color("#ff1744");
562
+ if (p >= 70) return new Color("#ffab00");
563
+ return new Color("#00e676");
564
+ }
565
+
566
+ function buildWidget(servers) {
567
+ const w = new ListWidget();
568
+ w.backgroundColor = new Color("#05080f");
569
+ w.setPadding(6, 8, 6, 8);
570
+
571
+ const online = servers.filter(s => !isOff(s.timestamp));
572
+
573
+ // Header
574
+ const hdr = w.addStack();
575
+ hdr.centerAlignContent();
576
+ const logo = hdr.addText("// HANGAR");
577
+ logo.textColor = new Color("#00e5ff");
578
+ logo.font = Font.boldMonospacedSystemFont(9);
579
+ hdr.addSpacer();
580
+ const st = hdr.addText(\`\${online.length}/\${servers.length} ONLINE\`);
581
+ st.textColor = online.length > 0 ? new Color("#00e676") : new Color("#ff1744");
582
+ st.font = Font.boldMonospacedSystemFont(8);
583
+
584
+ w.addSpacer(4);
585
+
586
+ // Main content area — fill remaining space
587
+ if (servers.length <= 2) {
588
+ // Single row, each bay fills half width and full height
589
+ const row = w.addStack();
590
+ row.spacing = 6;
591
+ for (const s of servers) {
592
+ drawBayWidget(row, s, servers.length);
593
+ }
594
+ } else {
595
+ // 2x2 grid
596
+ for (let r = 0; r < 2; r++) {
597
+ const row = w.addStack();
598
+ row.spacing = 6;
599
+ for (let c = 0; c < 2; c++) {
600
+ const idx = r * 2 + c;
601
+ if (idx < servers.length) {
602
+ drawBayWidget(row, servers[idx], servers.length);
603
+ }
604
+ }
605
+ if (r === 0) w.addSpacer(3);
606
+ }
607
+ }
608
+
609
+ w.addSpacer();
610
+
611
+ // Footer
612
+ const bot = w.addStack();
613
+ bot.centerAlignContent();
614
+ const scanL = bot.addText("SCAN ");
615
+ scanL.textColor = new Color("#2a3a52");
616
+ scanL.font = Font.mediumMonospacedSystemFont(7);
617
+ const scanV = bot.addText(new Date().toLocaleTimeString("en-GB", { hour12: false }));
618
+ scanV.textColor = new Color("#00e5ff");
619
+ scanV.font = Font.boldMonospacedSystemFont(7);
620
+ bot.addSpacer();
621
+ const sysT = bot.addText("SYS NOMINAL");
622
+ sysT.textColor = online.length > 0 ? new Color("#00e676", 0.5) : new Color("#ff1744", 0.5);
623
+ sysT.font = Font.mediumMonospacedSystemFont(7);
624
+
625
+ return w;
626
+ }
627
+
628
+ function drawBayWidget(parent, server, totalCount) {
629
+ const on = !isOff(server.timestamp);
630
+ // For 2 servers in medium widget: each bay ~155pt wide
631
+ // For 4 servers: each bay ~75pt wide
632
+ const isWide = totalCount <= 2;
633
+
634
+ const bay = parent.addStack();
635
+ bay.layoutVertically();
636
+ bay.backgroundColor = new Color(on ? "#0b1120" : "#080b12");
637
+ bay.cornerRadius = 8;
638
+ bay.borderWidth = 1;
639
+ bay.borderColor = new Color(on ? "#1e3a5f" : "#151e2e");
640
+ bay.setPadding(8, 10, 8, 10);
641
+
642
+ // ---- Header: status light + hostname ----
643
+ const h = bay.addStack();
644
+ h.centerAlignContent();
645
+ const dot = h.addStack();
646
+ dot.size = new Size(8, 8);
647
+ dot.cornerRadius = 4;
648
+ dot.backgroundColor = new Color(on ? "#00e676" : "#151e2e");
649
+ h.addSpacer(5);
650
+ const bayLabel = h.addText("BAY");
651
+ bayLabel.textColor = new Color(on ? "#4fc3f7" : "#252e40");
652
+ bayLabel.font = Font.boldMonospacedSystemFont(isWide ? 9 : 7);
653
+ h.addSpacer();
654
+ const nm = h.addText(server.hostname.slice(-3));
655
+ nm.textColor = new Color(on ? "#e8edf5" : "#252e40");
656
+ nm.font = Font.boldMonospacedSystemFont(isWide ? 16 : 12);
657
+
658
+ if (!on) {
659
+ // Offline bay — fill remaining space with centered message
660
+ bay.addSpacer();
661
+ const offT = bay.addText("SYSTEMS OFFLINE");
662
+ offT.textColor = new Color("#252e40");
663
+ offT.font = Font.boldMonospacedSystemFont(isWide ? 9 : 7);
664
+ offT.centerAlignText();
665
+ bay.addSpacer();
666
+
667
+ // Dark GPU placeholder grid
668
+ const gpuPlaceholder = bay.addStack();
669
+ gpuPlaceholder.spacing = 2;
670
+ gpuPlaceholder.centerAlignContent();
671
+ for (let i = 0; i < (isWide ? 8 : 4); i++) {
672
+ const block = gpuPlaceholder.addStack();
673
+ block.size = new Size(isWide ? 14 : 10, isWide ? 10 : 7);
674
+ block.cornerRadius = 2;
675
+ block.backgroundColor = new Color("#0a0e17");
676
+ }
677
+ bay.addSpacer(4);
678
+ const pwrT = bay.addText("PWR OFF");
679
+ pwrT.textColor = new Color("#1a1a2e");
680
+ pwrT.font = Font.mediumMonospacedSystemFont(isWide ? 7 : 6);
681
+ pwrT.centerAlignText();
682
+ return;
683
+ }
684
+
685
+ // ---- Online bay ----
686
+ const hw = server.hardware || {};
687
+ const gpus = hw.gpus || [];
688
+
689
+ bay.addSpacer(3);
690
+
691
+ // Stats: age, CPU, MEM
692
+ const info = bay.addStack();
693
+ info.centerAlignContent();
694
+ const age = info.addText(fmtAge(server.timestamp));
695
+ age.textColor = new Color("#2a3a52");
696
+ age.font = Font.mediumMonospacedSystemFont(isWide ? 9 : 7);
697
+ info.addSpacer();
698
+ const cpu = info.addText("CPU " + Math.round(hw.cpu_percent || 0) + "%");
699
+ cpu.textColor = pc(hw.cpu_percent || 0);
700
+ cpu.font = Font.boldMonospacedSystemFont(isWide ? 9 : 7);
701
+ info.addSpacer(6);
702
+ const mem = info.addText("MEM " + Math.round(hw.memory_percent || 0) + "%");
703
+ mem.textColor = pc(hw.memory_percent || 0);
704
+ mem.font = Font.boldMonospacedSystemFont(isWide ? 9 : 7);
705
+
706
+ bay.addSpacer();
707
+
708
+ // GPU grid — fill the main area
709
+ let busy = 0;
710
+ const cellW = isWide ? 28 : 16;
711
+ const cellH = isWide ? 20 : 12;
712
+ const fontSize = isWide ? 7 : 5;
713
+
714
+ for (let r = 0; r < 2; r++) {
715
+ const row = bay.addStack();
716
+ row.spacing = isWide ? 3 : 2;
717
+ row.centerAlignContent();
718
+ for (let c = 0; c < 4; c++) {
719
+ const idx = r * 4 + c;
720
+ const g = idx < gpus.length ? gpus[idx] : null;
721
+ const mp = g ? (g.memory_total_mb > 0 ? g.memory_used_mb / g.memory_total_mb * 100 : 0) : 0;
722
+ const b = mp > 5;
723
+ if (b) busy++;
724
+
725
+ const block = row.addStack();
726
+ block.layoutVertically();
727
+ block.size = new Size(cellW, cellH);
728
+ block.cornerRadius = 3;
729
+ block.setPadding(1, 1, 1, 1);
730
+
731
+ if (b) {
732
+ block.backgroundColor = new Color(mp >= 90 ? "#ff1744" : mp >= 70 ? "#ffab00" : "#00e676", 0.15);
733
+ block.borderWidth = 1;
734
+ block.borderColor = pc(mp);
735
+ } else {
736
+ block.backgroundColor = new Color("#151e2e");
737
+ }
738
+
739
+ if (g) {
740
+ const id = block.addText("G" + g.id);
741
+ id.textColor = b ? pc(mp) : new Color("#2a3a52");
742
+ id.font = Font.boldMonospacedSystemFont(fontSize);
743
+ id.centerAlignText();
744
+ if (isWide) {
745
+ const pct = block.addText(Math.round(mp) + "%");
746
+ pct.textColor = b ? pc(mp) : new Color("#2a3a52");
747
+ pct.font = Font.mediumMonospacedSystemFont(fontSize - 1);
748
+ pct.centerAlignText();
749
+ }
750
+ }
751
+ }
752
+ bay.addSpacer(isWide ? 2 : 1);
753
+ }
754
+
755
+ bay.addSpacer(3);
756
+
757
+ // Summary line
758
+ const sumRow = bay.addStack();
759
+ sumRow.centerAlignContent();
760
+ const sum = sumRow.addText(busy + "/" + gpus.length + " ACTIVE");
761
+ sum.textColor = busy > 0 ? new Color("#00e676") : new Color("#2a3a52");
762
+ sum.font = Font.boldMonospacedSystemFont(isWide ? 9 : 7);
763
+
764
+ const tasks = server.tasks || [];
765
+ if (tasks.length > 0) {
766
+ sumRow.addSpacer(6);
767
+ const proc = sumRow.addText(tasks.length + " PROC");
768
+ proc.textColor = new Color("#ffab00");
769
+ proc.font = Font.boldMonospacedSystemFont(isWide ? 8 : 6);
770
+ }
771
+ }
772
+
773
+ // ============================================================
774
+ // MAIN
775
+ // ============================================================
776
+ async function main() {
777
+ const servers = await fetchServers();
778
+ servers.sort((a, b) => {
779
+ const ao = isOff(a.timestamp), bo = isOff(b.timestamp);
780
+ if (ao !== bo) return ao ? 1 : -1;
781
+ return (a.hostname || "").localeCompare(b.hostname || "");
782
+ });
783
+
784
+ if (config.runsInWidget) {
785
+ // Static widget
786
+ const w = buildWidget(servers);
787
+ w.url = "scriptable:///run/" + encodeURIComponent(Script.name());
788
+ Script.setWidget(w);
789
+ } else {
790
+ // In-app: full WebView dashboard with animations
791
+ const html = buildHTML(JSON.stringify(servers));
792
+ const wv = new WebView();
793
+ await wv.loadHTML(html);
794
+ await wv.present(true); // fullscreen
795
+ }
796
+ Script.complete();
797
+ }
798
+
799
+ await main();
800
+ `;
801
+ document.getElementById('code').textContent = CODE;
802
+ function copyCode() {
803
+ navigator.clipboard.writeText(CODE).then(() => {
804
+ const btn = document.getElementById('copyBtn');
805
+ btn.textContent = 'COPIED ✓';
806
+ btn.className = 'btn ok';
807
+ setTimeout(() => { btn.textContent = 'COPY TO CLIPBOARD'; btn.className = 'btn'; }, 2000);
808
+ }).catch(() => {
809
+ const ta = document.createElement('textarea');
810
+ ta.value = CODE;
811
+ ta.style.cssText = 'position:fixed;left:-9999px';
812
+ document.body.appendChild(ta);
813
+ ta.select();
814
+ document.execCommand('copy');
815
+ document.body.removeChild(ta);
816
+ const btn = document.getElementById('copyBtn');
817
+ btn.textContent = 'COPIED ✓';
818
+ btn.className = 'btn ok';
819
+ setTimeout(() => { btn.textContent = 'COPY TO CLIPBOARD'; btn.className = 'btn'; }, 2000);
820
+ });
821
+ }
822
+ </script>
823
+ </body>
824
+ </html>