-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
763 lines (649 loc) · 25.1 KB
/
app.py
File metadata and controls
763 lines (649 loc) · 25.1 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
import os
from datetime import datetime
import argparse
import copy
import pytz
from collections import defaultdict
from subprocess import STDOUT, check_output
from flask import Flask, Response, render_template_string
from slurm_gpustat import (
resource_by_type,
parse_all_gpus,
gpu_usage,
node_states,
INACCESSIBLE,
parse_cmd,
avail_stats_for_node,
parse_node_names,
get_gpu_partitions,
)
# import gradio as gr # Not used in this file
# from https://developer.nvidia.com/cuda-gpus
# sort gpu by computing power
# if fail to display other GPU types, add items in the following dictionaries.
CAPABILITY = {
"1g.10gb": 8.0, # MIG GPU slice
"h200": 8.9,
"a4500": 8.0, # not sure
"a100": 8.0,
"a40": 8.6,
"a30": 8.0,
"a10": 8.6,
"a16": 8.6,
"v100": 7.0,
"gv100gl": 7.0,
"v100s": 7.0,
"p40": 6.1,
"m40": 5.2,
"rtx6k": 7.5,
"rtx8k": 7.5,
}
GMEM = {
"mig": "[11g]",
"1g.10gb": "[10g]", # MIG GPU slice with 10GB memory
"h200": "[141g]",
"a4500": "[20g]",
"a6000": "[48g]",
"a40": "[48g]",
"a30": "[24g]",
"v100": "[16g]",
"gv100gl": "[32g]",
"v100s": "[32g]",
"p40": "[24g]",
"m40": "[12/24g]",
"rtx6k": "[24g]",
"rtx8k": "[48g]",
}
OLD_GPU_TYPES = ["p40", "m40"]
def get_resource_bar(avail, total, text="", long=False):
"""Create a long/short progress bar with text overlaid. Formatting handled in css."""
if long:
long_str = " class=long"
else:
long_str = ""
if total == 0:
total = 1 # avoid ZeroDivisionError Error
bar = (
f'<div class="progress" data-text="{text}">'
f'<progress{long_str} max="100" value="{avail/total*100}"></progress></div>'
)
return bar
def str_to_int(text):
"""Convert string with unit to int in GB. e.g. "30 GB" --> 30, "1.4 T" --> 1400."""
# Remove any whitespace
text = text.strip()
# Split into number and unit
parts = text.split()
if len(parts) != 2:
return 0
number = float(parts[0])
unit = parts[1].upper()
# Convert to GB
if unit == 'T':
return int(number * 1024)
elif unit == 'G':
return int(number)
elif unit == 'M':
return int(number / 1024)
else:
return 0
def parse_leaderboard(sum_by_gmem=[48]):
"""Request sinfo, parse the leaderboard in string."""
resources = parse_all_gpus()
usage = gpu_usage(
resources=resources,
) # partition='gpu'
aggregates = {}
for user, subdict in usage.items():
aggregates[user] = {}
aggregates[user]["n_gpu"] = {
key: sum([x["n_gpu"] for x in val.values()]) for key, val in subdict.items()
}
aggregates[user]["bash_gpu"] = {
key: sum([x["bash_gpu"] for x in val.values()])
for key, val in subdict.items()
}
out = ""
for user, subdict in sorted(
aggregates.items(), key=lambda x: sum(x[1]["n_gpu"].values()), reverse=True
):
total = f"total={str(sum(subdict['n_gpu'].values())):2s}"
user_summary = [
f"{key}={val}"
for key, val in sorted(
subdict["n_gpu"].items(),
key=lambda x: CAPABILITY.get(x[0], 10.0),
reverse=True,
)
]
summary_str = "".join([f"{i:12s}" for i in user_summary])
num_new_gpus = [
val for key, val in subdict["n_gpu"].items() if key not in OLD_GPU_TYPES
]
for gm in sum_by_gmem:
total += f"|{gm}g={str(sum([val for key, val in subdict['n_gpu'].items() if key in GMEM and GMEM[key] == f'[{gm}g]'])):2s}"
total += f"|newer={str(sum(num_new_gpus)):2s}"
total += f"|bash={str(sum(subdict['bash_gpu'].values())):2s}"
out += f"{user:12s}[{total}] {summary_str}\n"
return out
def parse_leaderboard_by_partition(sum_by_gmem=[48]):
"""Request sinfo, parse the leaderboard in string."""
resources = parse_all_gpus()
gpu_partitions = get_gpu_partitions()
out = "=" * 64 + "\n"
for i, part in enumerate(gpu_partitions):
usage = gpu_usage(resources=resources, partition=part) # partition='gpu'
aggregates = {}
for user, subdict in usage.items():
aggregates[user] = {}
aggregates[user]["n_gpu"] = {
key: sum([x["n_gpu"] for x in val.values()])
for key, val in subdict.items()
}
aggregates[user]["bash_gpu"] = {
key: sum([x["bash_gpu"] for x in val.values()])
for key, val in subdict.items()
}
if i != 0:
out += "-" * 64 + "\n"
out += f"PARTITION: {part}\n"
for user, subdict in sorted(
aggregates.items(), key=lambda x: sum(x[1]["n_gpu"].values()), reverse=True
):
total = f"total={str(sum(subdict['n_gpu'].values())):2s}"
user_summary = [
f"{key}={val}"
for key, val in sorted(
subdict["n_gpu"].items(),
key=lambda x: CAPABILITY.get(x[0], 10.0),
reverse=True,
)
]
summary_str = "".join([f"{i:12s}" for i in user_summary])
num_new_gpus = [
val for key, val in subdict["n_gpu"].items() if key not in OLD_GPU_TYPES
]
for gm in sum_by_gmem:
total += f"|{gm}g={str(sum([val for key, val in subdict['n_gpu'].items() if key in GMEM and GMEM[key] == f'[{gm}g]'])):2s}"
total += f"|newer={str(sum(num_new_gpus)):2s}"
total += f"|bash={str(sum(subdict['bash_gpu'].values())):2s}"
out += f"{user:12s}[{total}] {summary_str}\n"
out += "=" * 64 + "\n"
return out
def cpu_usage(resources, partition="compute"):
"""Build a data structure of the CPU resource usage, organised by user.
Args:
resources (dict :: None): a summary of cluster resources, organised by node name.
Returns:
(dict): a summary of resources organised by user (and also by node name).
"""
cmd = "squeue -O NumNodes:100,nodelist:100,username:100,jobid:100 --noheader"
if partition:
cmd += f" --partition={partition}"
rows = parse_cmd(cmd)
usage = defaultdict(dict)
for row in rows:
tokens = row.split()
# ignore pending jobs
if len(tokens) < 4:
continue
cpu_count_str, node_str, user, jobid = tokens
num_cpus = int(cpu_count_str.strip())
node_names = parse_node_names(node_str)
for node_name in node_names:
# If a node still has jobs running but is draining, it will not be present
# in the "available" resources, so we ignore it
if node_name not in resources:
continue
cpu_type = resources[node_name]["type"]
if cpu_type in usage[user]:
usage[user][cpu_type][node_name]["n_cpu"] += num_cpus
else:
usage[user][cpu_type] = defaultdict(
lambda: {
"n_cpu": 0,
}
)
usage[user][cpu_type][node_name]["n_cpu"] += num_cpus
return usage
def parse_cpu_usage_to_table(partition="compute", show_bar=True):
"""Request sinfo for cnode, parse the output to a html table."""
node_str = parse_cmd(f"sinfo -o '%1000N' --noheader --partition={partition}")
assert isinstance(node_str, list) and len(node_str) == 1
node_names = parse_node_names(node_str[0].strip())
resources = {k: {"type": k[0:6] + "xx", "count": 1} for k in node_names}
states = node_states(partition=partition)
res = {
key: val
for key, val in resources.items()
if states.get(key, "down") not in INACCESSIBLE
}
res_total = copy.deepcopy(res)
usage = cpu_usage(resources=res, partition=partition)
for subdict in usage.values():
for cpu_type, node_dicts in subdict.items():
for node_name, user_cpu_count in node_dicts.items():
count = res[node_name]["count"]
count = max(count - user_cpu_count["n_cpu"], 0)
res[node_name]["count"] = count
res_total_by_type = defaultdict(list)
for node, spec in res_total.items():
res_total_by_type[spec["type"]].append({"node": node, "count": spec["count"]})
res_usage_by_type = defaultdict(list)
for node, spec in res.items():
res_usage_by_type[spec["type"]].append({"node": node, "count": spec["count"]})
table_html = []
total_cpu_count = 0
avail_cpu_count = 0
# sort cpus from new to old
type_list = sorted(list(res_total_by_type.keys()), reverse=True)
# writing the html table
for cpu_type in type_list:
node_dicts = res_total_by_type[cpu_type]
node_names = sorted([i["node"] for i in node_dicts])
node_summaries = []
num_col = []
for node in node_names:
node_name = f"<td>{node}</td>"
users = [user for user in usage if node in usage[user].get(cpu_type, [])]
if len(users):
users = f"<td>user: {','.join(users)}</td>"
else:
users = f"<td> </td>"
detail_dict = avail_stats_for_node(node)
detail_dict = {k: v for k, v in detail_dict.items() if k in ["cpu", "mem"]}
if show_bar:
c_stat = detail_dict["cpu"].split("/")
c_stat = [int(i.strip()) for i in c_stat]
cpu_bar = get_resource_bar(*c_stat, text=detail_dict["cpu"])
m_stat = detail_dict["mem"].split("/")
m_stat = [str_to_int(i.strip()) for i in m_stat]
mem_bar = get_resource_bar(*m_stat, text=detail_dict["mem"], long=True)
total_cpu_count += c_stat[-1]
avail_cpu_count += c_stat[-1] - c_stat[0]
else:
cpu_bar = detail_dict["cpu"]
mem_bar = detail_dict["mem"]
cpu_stat = f"<td>cpu: {cpu_bar}</td>"
mem_stat = f"<td>mem: {mem_bar}</td>"
node_summary = (
f"<tr><td> </td>{node_name}{cpu_stat}{mem_stat}{users}</tr>"
)
node_summaries.append(node_summary)
num_col.append(5)
type_summary = (
f'<tr><td colspan="{max(num_col)}"><b>' f"{cpu_type}: </b></td></tr>"
)
table_html.append(type_summary)
table_html.extend(node_summaries)
if show_bar:
total_bar = get_resource_bar(
avail_cpu_count,
total_cpu_count,
text=f"{avail_cpu_count} / {total_cpu_count}",
)
total_summary = (
f'<tr><td colspan="{max(num_col)}"><h3>'
f"Summary: {total_bar} cpus available</h3></td></tr>"
)
else:
total_bar = f"{avail_cpu_count}/{total_cpu_count}"
total_summary = ""
table_html = f"<table>{total_summary}{''.join(table_html)}</table>"
return table_html
def parse_usage_to_table(show_bar=True):
"""Request sinfo, parse the output to a html table."""
resources = parse_all_gpus()
states = node_states()
res = {
key: val
for key, val in resources.items()
if states.get(key, "down") not in INACCESSIBLE
}
res_total = copy.deepcopy(res)
usage = gpu_usage(resources=res)
for subdict in usage.values():
for gpu_type, node_dicts in subdict.items():
for node_name, user_gpu_count in node_dicts.items():
resource_idx = [x["type"] for x in res[node_name]].index(gpu_type)
count = res[node_name][resource_idx]["count"]
count = max(count - user_gpu_count["n_gpu"], 0)
res[node_name][resource_idx]["count"] = count
res_total_by_type = resource_by_type(res_total)
res_usage_by_type = resource_by_type(res)
table_html = []
total_gpu_count = 0
avail_gpu_count = 0
# sort gpus from new to old
type_list = sorted(
list(res_total_by_type.keys()),
key=lambda x: CAPABILITY.get(x, 10.0),
reverse=True,
)
# writing the html table
num_col = None
for gpu_type in type_list:
node_dicts = res_total_by_type[gpu_type]
node_names = sorted([i["node"] for i in node_dicts])
gpu_count_total = {i["node"]: i["count"] for i in node_dicts}
gpu_count_avail = {i["node"]: i["count"] for i in res_usage_by_type[gpu_type]}
node_summaries = []
num_col = []
for node in node_names:
node_name = f"<td>{node}</td>"
if show_bar:
gpu_bar = get_resource_bar(
gpu_count_avail[node],
gpu_count_total[node],
text=f"{gpu_count_avail[node]} / {gpu_count_total[node]}",
)
else:
gpu_bar = f"{gpu_count_avail[node]}/{gpu_count_total[node]}"
gpu_stat = f"<td>gpu: {gpu_bar}</td>"
users = [user for user in usage if node in usage[user].get(gpu_type, [])]
if len(users):
users = f"<td>user: {','.join(users)}</td>"
else:
users = f"<td> </td>"
detail_dict = avail_stats_for_node(node)
detail_dict = {k: v for k, v in detail_dict.items() if k in ["cpu", "mem"]}
if show_bar:
c_stat = detail_dict["cpu"].split("/")
c_stat = [int(i.strip()) for i in c_stat]
cpu_bar = get_resource_bar(*c_stat, text=detail_dict["cpu"])
m_stat = detail_dict["mem"].split("/")
m_stat = [str_to_int(i.strip()) for i in m_stat]
mem_bar = get_resource_bar(*m_stat, text=detail_dict["mem"], long=True)
else:
cpu_bar = detail_dict["cpu"]
mem_bar = detail_dict["mem"]
cpu_stat = f"<td>cpu: {cpu_bar}</td>"
mem_stat = f"<td>mem: {mem_bar}</td>"
node_summary = f"<tr><td> </td>{node_name}{gpu_stat}{cpu_stat}{mem_stat}{users}</tr>"
node_summaries.append(node_summary)
num_col.append(6)
if show_bar:
type_bar = get_resource_bar(
sum(gpu_count_avail.values()),
sum(gpu_count_total.values()),
text=f"{sum(gpu_count_avail.values())} / {sum(gpu_count_total.values())}",
)
else:
type_bar = (
f"{sum(gpu_count_avail.values())}/{sum(gpu_count_total.values())}"
)
type_summary = (
f'<tr><td colspan="{max(num_col)}"><b>'
f'{gpu_type} {GMEM.get(gpu_type, "")}: {type_bar} gpus available</b></td></tr>'
)
table_html.append(type_summary)
table_html.extend(node_summaries)
total_gpu_count += sum(gpu_count_total.values())
avail_gpu_count += sum(gpu_count_avail.values())
if show_bar:
total_bar = get_resource_bar(
avail_gpu_count,
total_gpu_count,
text=f"{avail_gpu_count} / {total_gpu_count}",
)
else:
total_bar = f"{avail_gpu_count}/{total_gpu_count}"
if num_col is None:
num_col = [6] # avoid edge case: no GPUs available
total_summary = (
f'<tr><td colspan="{max(num_col)}"><h3>'
f"Summary: {total_bar} gpus available</h3></td></tr>"
)
table_html = f"<table>{total_summary}{''.join(table_html)}</table>"
return table_html
def parse_queue_to_table():
"""Request pending queue for uva_cv_lab, uva_cv_lab2, and dac_cheng accounts, returning combined output with raw formatting."""
# Command templates to fetch pending jobs for uva_cv_lab and uva_cv_lab2 accounts
cmd_uva_cv_lab = (
"squeue -t PENDING -A uva_cv_lab -o '%.18i %.9P %.8u %.8T %.10M %.9l %.6D %R'"
)
cmd_uva_cv_lab2 = (
"squeue -t PENDING -A uva_cv_lab2 -o '%.18i %.9P %.8u %.8T %.10M %.9l %.6D %R'"
)
cmd_dac_cheng = (
"squeue -t PENDING -A dac_cheng -o '%.18i %.9P %.8u %.8T %.10M %.9l %.6D %R'"
)
# Fetch and combine outputs
out_uva_cv_lab = parse_cmd(cmd_uva_cv_lab)
out_uva_cv_lab2 = parse_cmd(cmd_uva_cv_lab2)
out_dac_cheng = parse_cmd(cmd_dac_cheng)
# Combine all queues and format output as a single string
combined_output = "\n".join(out_uva_cv_lab + out_uva_cv_lab2 + out_dac_cheng)
return combined_output
def parse_disk_io():
"""Measure disk reading speed, parse the output to a html table.
Pre-requisite: create a byte file by running
`dd if=/dev/zero of=/your/path/test.img bs=512MB count=1 oflag=dsync`."""
# return '<p>Under maintenance. </p>'
try:
beegfs_ultra_read = check_output(
"dd if=/scratch/shared/beegfs/shared-datasets/test/test.img of=/dev/null bs=512MB count=1 oflag=dsync",
stderr=STDOUT,
shell=True,
).decode("utf-8")
beegfs_ultra_read = beegfs_ultra_read.split("\n")[-2].split(",")[-1].strip()
except:
beegfs_ultra_read = "N/A"
try:
beegfs_fast_read = check_output(
"dd if=/scratch/shared/beegfs/htd/DATA/tmp/test.img of=/dev/null bs=512MB count=1 oflag=dsync",
stderr=STDOUT,
shell=True,
).decode("utf-8")
beegfs_fast_read = beegfs_fast_read.split("\n")[-2].split(",")[-1].strip()
except:
beegfs_fast_read = "N/A"
try:
beegfs_normal_read = check_output(
"dd if=/scratch/shared/beegfs/htd/tmp/test.img of=/dev/null bs=512MB count=1 oflag=dsync",
stderr=STDOUT,
shell=True,
).decode("utf-8")
beegfs_normal_read = beegfs_normal_read.split("\n")[-2].split(",")[-1].strip()
except:
beegfs_normal_read = "N/A"
try:
work_normal_read = check_output(
"dd if=/work/htd/Desktop_tmp/tmp/test.img of=/dev/null bs=512MB count=1 oflag=dsync",
stderr=STDOUT,
shell=True,
).decode("utf-8")
work_normal_read = work_normal_read.split("\n")[-2].split(",")[-1].strip()
except:
work_normal_read = "N/A"
summary = (
"<tr> <td><b>{}</b></td> <td><b>{}</b></td> <td><b>{}</b></td> </tr>".format(
"Disk", "Type", "Read Speed"
)
)
summary += "<tr> <td>{}</td> <td>{}</td> <td>{}</td> </tr>".format(
"/beegfs/shared-datasets <i>[ultra-fast-layer]</i>",
"NVMe flash",
beegfs_ultra_read,
)
summary += "<tr> <td>{}</td> <td>{}</td> <td>{}</td> </tr>".format(
"/beegfs <i>[fast-layer]</i>", "SSD flash", beegfs_fast_read
)
summary += "<tr> <td>{}</td> <td>{}</td> <td>{}</td> </tr>".format(
"/beegfs <i>[normal-layer]</i>", "HDD", beegfs_normal_read
)
summary += "<tr> <td>{}</td> <td>{}</td> <td>{}</td></tr>".format(
"/work", "HDD", work_normal_read
)
table_html = f"<table>{summary}</table>"
return table_html
def parse_disk_quota():
"""Run 'hdquota' command and parse the output to an HTML table."""
try:
output = check_output("hdquota", shell=True).decode("utf-8")
lines = output.split('\n')
# Create HTML table with headers
headers = ["Storage Type", "Location", "Size", "Used", "Avail", "Use%"]
table_html = "<table><tr>" + "".join(f"<th>{h}</th>" for h in headers) + "</tr>"
# Add data rows
for line in lines[2:]: # Skip header and separator lines
if not line.strip():
continue
# Split the line into parts, handling spaces correctly
parts = line.split()
# Skip lines that don't have enough parts
if len(parts) < 9: # Changed from 10 to 9 to handle single-word storage types
continue
try:
# Handle storage type (could be one or two words)
if parts[1] in ["Directory", "Project", "Standard"]:
storage_type = f"{parts[0]} {parts[1]}"
location = parts[2]
size = f"{parts[3]} {parts[4]}"
used = f"{parts[5]} {parts[6]}"
avail = f"{parts[7]} {parts[8]}"
use_percent = parts[9]
else:
storage_type = parts[0]
location = parts[1]
size = f"{parts[2]} {parts[3]}"
used = f"{parts[4]} {parts[5]}"
avail = f"{parts[6]} {parts[7]}"
use_percent = parts[8]
table_html += "<tr>"
table_html += f"<td>{storage_type}</td>"
table_html += f"<td>{location}</td>"
table_html += f"<td>{size}</td>"
table_html += f"<td>{used}</td>"
table_html += f"<td>{avail}</td>"
table_html += f"<td>{use_percent}</td>"
table_html += "</tr>"
except IndexError:
continue
table_html += "</table>"
return table_html
except Exception as e:
error_msg = f"Error getting disk quota information: {str(e)}"
return f"<p>{error_msg}</p>"
def parse_allocations_to_table():
"""Run 'allocations -a uva_cv_lab' command and parse the output to an HTML table."""
cmd = "allocations -a uva_cv_lab"
output = parse_cmd(cmd)
if not output:
return "<p>No allocation information found.</p>"
# The output has a header, separator, and data rows
allocation_lines = []
separator_count = 0
for line in output:
if "------" in line:
separator_count += 1
continue
if separator_count == 0:
continue # Skip lines before the first separator
elif separator_count == 1:
if line.strip():
allocation_lines.append(line.rstrip("\n"))
else:
break # Stop after the allocation table
if not allocation_lines:
return "<p>No allocation data found.</p>"
columns = [
"Description",
"StartTime",
"EndTime",
"Allocated",
"Remaining",
"PercentUsed",
"Active",
]
# Create HTML table
table_html = "<table><tr>"
for col in columns:
table_html += f"<th>{col}</th>"
table_html += "</tr>"
# Parse each data row by splitting on whitespace
for row in allocation_lines:
parts = row.split()
if len(parts) < 7:
continue # Skip rows that don't have all columns
table_html += "<tr>"
# Description
table_html += f"<td>{parts[0]}</td>"
# StartTime (date and time)
table_html += f"<td>{parts[1]} {parts[2]}</td>"
# EndTime
table_html += f"<td>{parts[3]}</td>"
# Allocated
table_html += f"<td>{parts[4]}</td>"
# Remaining
table_html += f"<td>{parts[5]}</td>"
# PercentUsed
table_html += f"<td>{parts[6]}</td>"
# Active
table_html += f"<td>{parts[7]}</td>"
table_html += "</tr>"
table_html += "</table>"
return table_html
def main():
parser = argparse.ArgumentParser(description="launch web app")
parser.add_argument(
"--host", default="0.0.0.0", help="the host address for the website"
)
parser.add_argument(
"--port", default=2070, type=int, help="the port for the website"
)
args = parser.parse_args()
app = Flask(__name__)
@app.route("/")
def index():
return render_template_string(open("index.html").read(), hostname=args.host)
@app.route("/time_feed")
def time_feed():
def generate():
yield f'updated at: {datetime.now(pytz.timezone("America/New_York")).strftime("%Y.%m.%d | %H:%M:%S")}'
return Response(generate(), mimetype="text")
@app.route("/resource")
def resource():
def generate():
out = parse_usage_to_table()
yield out
return Response(generate(), mimetype="text")
@app.route("/queue")
def queue():
def generate():
out = parse_queue_to_table()
yield out
return Response(generate(), mimetype="text")
@app.route("/leaderboard")
def leaderboard():
def generate():
out = parse_leaderboard()
yield out
return Response(generate(), mimetype="text")
@app.route("/leaderboard_partition")
def leaderboard_partition():
def generate():
out = parse_leaderboard_by_partition()
yield out
return Response(generate(), mimetype="text")
@app.route("/disk_quota")
def disk_quota():
def generate():
out = parse_disk_quota()
yield out
return Response(generate(), mimetype="text")
@app.route("/allocations")
def allocations():
def generate():
out = parse_allocations_to_table()
yield out
return Response(generate(), mimetype="text")
# @app.route('/cpu_resource')
# def cpu_resource():
# def generate():
# out = parse_cpu_usage_to_table()
# yield out
# return Response(generate(), mimetype='text')
app.run(host=args.host, port=args.port)
if __name__ == "__main__":
main()