import { scopeAlertHealth, summaryStatus } from '/assets/js/overview-health.js';
import { state } from '/assets/js/state.js';
import { esc, pluginText as text } from '/assets/js/shared.js';
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 `
${esc(label(alert))}${esc(resource ? name(resource) : '')}
`;
}).join('');
const metrics = snapshot.metrics || [];
const value = (resource, key) => metrics.find(m => m.resource_id === resource.id && m.key === key)?.value;
const card = (title, content, status = '') => ``;
const status = (semantic, title) => `${esc(title)}`;
const cards = resources.flatMap(resource => {
if (resource.type === 'job') {
const enabled = value(resource, 'job_enabled') === 1;
const result = value(resource, 'last_run_success');
const unknown = (snapshot.alerts || []).some(a => a.id === `JOB_UNKNOWN:${resource.id.slice(4)}`);
const kind = !current || unknown || result === undefined ? 'unknown' : result === 1 ? 'normal' : 'error';
const stateLabel = kind === 'normal' ? text('正常', 'Normal') : kind === 'error' ? text('失败', 'Failed') : text('待确认', 'Unconfirmed');
const title = /轮播/.test(resource.name || '') ? text('轮播任务', 'Slideshow task') : resource.name || text('任务', 'Job');
return [card(title, `${resource.name || text('任务', 'Job')} · ${enabled ? text('启用', 'Enabled') : text('停用', 'Disabled')}`, status(kind, stateLabel))];
}
return metrics.filter(m => m.resource_id === resource.id).map(metric => {
const title = label(metric);
return card(title, metricValue(metric));
});
}).join('');
const frames = resources.filter(resource => resource.type === 'device');
return `${esc(entry.host.name || 'Frame Flow')}
${frames.map(frame => `${esc(frame.name || text('画框', 'Frame'))}`).join('')}
${current ? text('在线', 'Online') : text('状态未确认', 'Unconfirmed')} ${!current ? `${text('以下为上次采集数据', 'Showing the last collected data')}
` : ''}${cards}
${text('已读取范围内的告警', 'Alerts within monitored scope')}${summaryStatus(scopeAlertHealth(snapshot.alerts, snapshot.alerts_complete, current), state.LANG)}
${alertRows || (current && snapshot.alerts_complete ? text('无告警', 'No alerts') : text('状态未确认', 'Unconfirmed'))}
`;
}