pcpool/wwwroot/api/pool_stats.dspy

51 lines
3.4 KiB
Plaintext

env = request._run_ns
dbname = get_module_dbname('pcpool')
try:
async with DBPools().sqlorContext(dbname) as sor:
pools = await sor.R('compute_pool', {})
total_pools = len(pools or [])
active = sum(1 for p in (pools or []) if str(getattr(p,'status','')) == '1')
total_cpu = 0; total_gpu = 0; total_mem = 0
alloc_cpu = 0; alloc_gpu = 0; alloc_mem = 0
for p in (pools or []):
nodes = await sor.R('compute_node', {'pool_id': p.id})
for n in (nodes or []):
cpu = int(getattr(n,'cpu_cores',0) or 0)
gpu = int(getattr(n,'gpu_count',0) or 0)
mem = int(getattr(n,'memory_gb',0) or 0)
total_cpu += cpu; total_gpu += gpu; total_mem += mem
if getattr(n,'status','') == 'allocated':
alloc_cpu += cpu; alloc_gpu += gpu; alloc_mem += mem
return {
'widgettype': 'HBox',
'options': {'gap': '16px'},
'subwidgets': [
{'widgettype': 'VBox', 'options': {'bgcolor': '#eff6ff', 'padding': '16px', 'width': '25%', 'border': '1px solid #dbeafe', 'borderRadius': '8px'},
'subwidgets': [
{'widgettype': 'Text', 'options': {'otext': '算力池', 'cfontsize': 0.7, 'color': '#64748b'}},
{'widgettype': 'Text', 'options': {'otext': str(total_pools) + ' 个', 'cfontsize': 2, 'fontWeight': 'bold', 'color': '#2563eb'}},
{'widgettype': 'Text', 'options': {'otext': str(active) + ' 运行中', 'cfontsize': 0.7, 'color': '#94a3b8'}}
]},
{'widgettype': 'VBox', 'options': {'bgcolor': '#f5f3ff', 'padding': '16px', 'width': '25%', 'border': '1px solid #ede9fe', 'borderRadius': '8px'},
'subwidgets': [
{'widgettype': 'Text', 'options': {'otext': 'CPU', 'cfontsize': 0.7, 'color': '#64748b'}},
{'widgettype': 'Text', 'options': {'otext': str(total_cpu) + ' 核', 'cfontsize': 2, 'fontWeight': 'bold', 'color': '#7c3aed'}},
{'widgettype': 'Text', 'options': {'otext': '已分配 ' + str(alloc_cpu) + ' 核', 'cfontsize': 0.7, 'color': '#94a3b8'}}
]},
{'widgettype': 'VBox', 'options': {'bgcolor': '#ecfdf5', 'padding': '16px', 'width': '25%', 'border': '1px solid #d1fae5', 'borderRadius': '8px'},
'subwidgets': [
{'widgettype': 'Text', 'options': {'otext': 'GPU', 'cfontsize': 0.7, 'color': '#64748b'}},
{'widgettype': 'Text', 'options': {'otext': str(total_gpu) + ' 个', 'cfontsize': 2, 'fontWeight': 'bold', 'color': '#059669'}},
{'widgettype': 'Text', 'options': {'otext': '已分配 ' + str(alloc_gpu) + ' 个', 'cfontsize': 0.7, 'color': '#94a3b8'}}
]},
{'widgettype': 'VBox', 'options': {'bgcolor': '#fff7ed', 'padding': '16px', 'width': '25%', 'border': '1px solid #ffedd5', 'borderRadius': '8px'},
'subwidgets': [
{'widgettype': 'Text', 'options': {'otext': '内存', 'cfontsize': 0.7, 'color': '#64748b'}},
{'widgettype': 'Text', 'options': {'otext': str(total_mem) + ' GB', 'cfontsize': 2, 'fontWeight': 'bold', 'color': '#ea580c'}},
{'widgettype': 'Text', 'options': {'otext': '已分配 ' + str(alloc_mem) + ' GB', 'cfontsize': 0.7, 'color': '#94a3b8'}}
]}
]
}
except:
return {'widgettype': 'Text', 'options': {'otext': '加载失败', 'color': '#dc2626'}}