Fix Console language support and named frame monitoring cards
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"id": "pigway-frame-flow",
|
||||
"name": "PIGWay Frame Flow",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.1",
|
||||
"protocol_version": 1,
|
||||
"runtime": "python3",
|
||||
"entrypoint": "worker.py",
|
||||
@@ -30,5 +30,9 @@
|
||||
"en": "Frame Flow API token"
|
||||
}
|
||||
}
|
||||
},
|
||||
"ui": {
|
||||
"contract": "console-ui-v1",
|
||||
"entrypoint": "ui/index.js"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { state } from '/assets/js/state.js';
|
||||
import { esc, pluginText as text } from '/assets/js/shared.js';
|
||||
|
||||
const typeName = type => ({service: text('服务', 'Service'), device: text('画框', 'Frame'), job: text('任务', 'Job')}[type] || type);
|
||||
const metricValue = metric => {
|
||||
if (metric.unit === 'percent') return `${metric.value}%`;
|
||||
if (metric.unit === 'count') return `${metric.value} ${text('个', 'items')}`;
|
||||
const values = {
|
||||
service_ready: [text('未就绪', 'Not ready'), text('就绪', 'Ready')],
|
||||
device_online: [text('离线', 'Offline'), text('在线', 'Online')],
|
||||
job_enabled: [text('停用', 'Disabled'), text('启用', 'Enabled')],
|
||||
last_run_success: [text('失败或待确认', 'Failed or unconfirmed'), text('成功', 'Succeeded')],
|
||||
};
|
||||
return values[metric.key]?.[metric.value] ?? String(metric.value);
|
||||
};
|
||||
|
||||
export function hostCard(entry) {
|
||||
const snapshot = entry.snapshot || {};
|
||||
const resources = snapshot.resources || [];
|
||||
const current = entry.dataCurrent === true;
|
||||
const label = item => item.label?.[state.LANG] || item.key || item.id;
|
||||
const name = id => resources.find(r => r.id === id)?.name || id;
|
||||
const alertRows = (snapshot.alerts || []).map(alert => {
|
||||
const separator = alert.id.indexOf(':');
|
||||
const kind = alert.id.split(':')[0];
|
||||
const resource = separator < 0 ? '' : `${kind.startsWith('JOB_') ? 'job' : 'device'}:${alert.id.slice(separator + 1)}`;
|
||||
return `<div class="row"><span>${esc(label(alert))}</span><span>${esc(resource ? name(resource) : '')}</span></div>`;
|
||||
}).join('');
|
||||
return `<article class="panel host-panel"><div class="host-head"><h2>${esc(entry.host.name || 'Frame Flow')}</h2><span class="badge semantic-status" data-semantic="${current ? 'online' : 'unknown'}">${current ? text('在线', 'Online') : text('状态未确认', 'Unconfirmed')}</span></div>${!current ? `<p class="muted">${text('以下为上次采集数据', 'Showing the last collected data')}</p>` : ''}${resources.map(resource => `<div class="group"><h3>${esc(resource.name || resource.id)} · ${esc(typeName(resource.type))}</h3><div class="host-grid">${(snapshot.metrics || []).filter(m => m.resource_id === resource.id).map(metric => `<div class="metric"><div class="muted">${esc(label(metric))}</div><div class="value">${esc(metricValue(metric))}</div></div>`).join('')}</div></div>`).join('')}<div class="group"><h3>${text('当前告警', 'Current alerts')}</h3>${alertRows || text('无告警', 'No alerts')}</div></article>`;
|
||||
}
|
||||
+29
-2
@@ -16,6 +16,15 @@ OUTPUT_LOCK = threading.Lock()
|
||||
DEVICE_LOCKS = {}
|
||||
LOCK = threading.Lock()
|
||||
MAX_MESSAGE = 16 * 1024 * 1024
|
||||
METRIC_LABELS = {
|
||||
"service_ready": {"zh": "服务就绪", "en": "Service ready"},
|
||||
"device_online": {"zh": "画框在线", "en": "Frame online"},
|
||||
"battery_percent": {"zh": "电量", "en": "Battery"},
|
||||
"job_enabled": {"zh": "任务启用", "en": "Job enabled"},
|
||||
"last_run_success": {"zh": "最近运行成功", "en": "Last run succeeded"},
|
||||
"device_count": {"zh": "画框数量", "en": "Frame count"},
|
||||
"job_count": {"zh": "任务数量", "en": "Job count"},
|
||||
}
|
||||
|
||||
|
||||
class AdapterError(Exception):
|
||||
@@ -89,10 +98,28 @@ def collect(device):
|
||||
alerts = []
|
||||
|
||||
def metric(rid, key, value, unit="boolean"):
|
||||
metrics.append({"resource_id": rid, "key": key, "value": value, "unit": unit})
|
||||
metrics.append(
|
||||
{
|
||||
"resource_id": rid,
|
||||
"key": key,
|
||||
"value": value,
|
||||
"unit": unit,
|
||||
"label": METRIC_LABELS[key],
|
||||
}
|
||||
)
|
||||
|
||||
def alert(identifier):
|
||||
alerts.append({"id": identifier, "severity": 1})
|
||||
kind = identifier.split(":", 1)[0]
|
||||
labels = {
|
||||
"SERVICE_NOT_READY": {"zh": "服务未就绪", "en": "Service not ready"},
|
||||
"DEVICE_OFFLINE": {"zh": "画框离线", "en": "Frame offline"},
|
||||
"JOB_FAILED": {"zh": "任务最近运行失败", "en": "Last job run failed"},
|
||||
"JOB_UNKNOWN": {
|
||||
"zh": "任务最近结果待确认",
|
||||
"en": "Last job result unconfirmed",
|
||||
},
|
||||
}
|
||||
alerts.append({"id": identifier, "severity": 1, "label": labels[kind]})
|
||||
|
||||
metric("service", "service_ready", int(ready["status"] == "ready"))
|
||||
if ready["status"] != "ready":
|
||||
|
||||
Reference in New Issue
Block a user