Files
Gset/static/js/pages/Import.js
T
treeandClaude Opus 5.5 f8d1ccc526 初始提交:5G 基站退服时长智能分析与故障闭环管控系统
Flask + SQLite 后端,免构建 React + ECharts 前端,含合成样例数据。
测试参考数据(FOR-TEST-ONLY)与运行时数据库不入库。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 14:01:48 +08:00

162 lines
7.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {GlassButton, html, useState, useToast} from 'vendor';
import {api, download, useApp} from '../api.js';
import {DataTable} from '../components/DataTable.js';
import {Icon} from '../components/icons.js';
import {Loading, Section} from '../components/ui.js';
// 问题行表格:站点工参没有时间列
const ERROR_COLUMNS = [
{key: 'row', title: '文件行号'}, {key: 'key', title: '编号 / 名称'}, {key: 'start_time', title: '时间'},
{key: 'reason', title: '问题原因', render: v => html`<span className="text-red">${v}</span>`},
];
const SITE_ERROR_COLUMNS = [
{key: 'row', title: '文件行号'}, {key: 'key', title: '基站编号 / 名称'},
{key: 'reason', title: '问题原因', render: v => html`<span className="text-red">${v}</span>`},
];
// 合格数据预览表格,按文件类型显示不同的列
const PREVIEW_COLUMNS = {
outage: [
{key: 'record_id', title: '流水号'}, {key: 'ne_id', title: '基站编号'}, {key: 'start_time', title: '开始时间'},
{key: 'end_time', title: '恢复时间'}, {key: 'duration_min', title: '计算时长(分)'},
{key: 'reported_min', title: '上报时长(分)'}, {key: 'cause_category', title: '原因大类'}, {key: 'cause_code', title: '原因码'},
],
alarm: [
{key: 'alarm_id', title: '告警编号'}, {key: 'ne_id', title: '基站编号'}, {key: 'alarm_name', title: '告警名称'},
{key: 'alarm_level', title: '级别'}, {key: 'category', title: '归入类别'}, {key: 'start_time', title: '发生时间'},
{key: 'clear_time', title: '清除时间'}, {key: 'parent_alarm_id', title: '父告警'},
],
site: [
{key: 'action', title: '操作'}, {key: 'ne_id', title: '基站编号'}, {key: 'site_name', title: '基站名称'},
{key: 'district', title: '片区'}, {key: 'scene', title: '推断场景'}, {key: 'site_type', title: '站型'},
{key: 'vendor', title: '厂家'}, {key: 'cells', title: '小区数'}, {key: 'lng', title: '经度'}, {key: 'lat', title: '纬度'},
],
};
/**
* 导入分两步:上传预览(服务端识别类型并校验,不入库)→ 确认入库。
* 退服流水入库后对新记录做根因分析;告警入库后对全部流水重新关联;站点入库后刷新下拉选项。
*/
export default function Import() {
const {refreshMeta} = useApp();
const toast = useToast();
const [dragOver, setDragOver] = useState(false);
const [checking, setChecking] = useState(false);
const [saving, setSaving] = useState(false);
const [preview, setPreview] = useState(null); // 服务端校验结果
const [result, setResult] = useState(null); // 入库结果
const upload = async file => {
if (!file) return;
const body = new FormData();
body.append('file', file);
setChecking(true);
setPreview(null);
setResult(null);
try {
setPreview(await api('/api/import/preview', {body}));
} catch (e) {
toast({message: e.message, tone: 'error'});
}
setChecking(false);
};
const confirmImport = async () => {
setSaving(true);
try {
const r = await api('/api/import/confirm', {json: {token: preview.token}});
setResult({...r, label: preview.label});
setPreview(null);
toast({message: `${preview.label}入库完成`, tone: 'success'});
if (r.kind === 'site') {
await refreshMeta(); // 新站点可能带来新的片区、厂家,并改变站点总数
}
} catch (e) {
toast({message: e.message, tone: 'error'});
}
setSaving(false);
};
const templateButton = html`
<${GlassButton} variant="gray" onClick=${() => download('/api/template')}>下载退服流水模板</${GlassButton}>`;
return html`
<${Section} title="数据批量导入" extra=${templateButton}>
<label className="drop" data-over=${dragOver || undefined}
onDragOver=${e => { e.preventDefault(); setDragOver(true); }} onDragLeave=${() => setDragOver(false)}
onDrop=${e => { e.preventDefault(); setDragOver(false); upload(e.dataTransfer.files[0]); }}>
<input type="file" accept=".xlsx,.xls,.csv" hidden onChange=${e => { upload(e.target.files[0]); e.target.value = ''; }}/>
<div className="drop-icon" aria-hidden="true"><${Icon} name="import" size=${28}/></div>
<b>点击或拖拽 Excel / CSV 文件到此处,系统自动识别文件类型</b>
<p>
<b>退服流水</b>:必填 ne_id、start_time、end_time(支持中文表头),系统按起止时间重算退服时长并核对上报值。<br/>
<b>告警流</b>:本系统口径(含 alarm_code 列)或华为网管「当前告警浏览」导出 CSV,按告警名称自动归入根因类别。<br/>
<b>站点工参</b>:电信 / 移动 5G 工参表,按基站汇总小区并按站名推断通信场景,已有基站只更新不重复。
</p>
</label>
${checking && html`<${Loading}/>`}
${result && html`<${ImportResult} result=${result}/>`}
${preview && html`<${PreviewPanel} preview=${preview} saving=${saving} onConfirm=${confirmImport}/>`}
</${Section}>`;
}
/** 入库完成后的说明 */
function ImportResult({result}) {
let detail = `入库 ${result.n} 条`;
if (result.kind === 'site') {
detail = `新增站点 ${result.added} 个,更新站点 ${result.updated} 个`;
}
return html`
<div className="note">
${result.label}:${detail},耗时 ${result.elapsed}s。可到「退服流水管理」或「场景与站点分级」查看。
</div>`;
}
/** 校验结果:统计数字 + 问题行 + 合格数据预览 */
function PreviewPanel({preview, saving, onConfirm}) {
const isSite = preview.kind === 'site';
let errorColumns = ERROR_COLUMNS;
if (isSite) {
errorColumns = SITE_ERROR_COLUMNS;
}
let confirmText = `确认导入 ${preview.ok} 条合格数据`;
if (isSite) {
confirmText = `确认导入 ${preview.ok} 个站点`;
}
return html`
<div>
<div className="chips">
<div className="chip"><span>文件类型</span>${preview.label}</div>
<div className="chip"><span>文件行数</span>${preview.total}</div>
<div className="chip" data-tone="green"><span>${isSite ? '汇总站点' : '校验通过'}</span>${preview.ok}</div>
<div className="chip" data-tone="red"><span>问题行</span>${preview.bad}</div>
${preview.diff > 0 && html`<div className="chip" data-tone="orange"><span>上报时长差错(已纠正)</span>${preview.diff}</div>`}
${preview.kind === 'alarm' && html`<div className="chip"><span>匹配站点库</span>${preview.matched}</div>`}
${isSite && html`<div className="chip"><span>新增 / 更新</span>${preview.added} / ${preview.updated}</div>`}
<div className="chip"><span>校验耗时</span>${preview.elapsed}s</div>
<span className="grow"></span>
<${GlassButton} variant="glassProminent" disabled=${!preview.ok} loading=${saving} onClick=${onConfirm}>
${confirmText}
</${GlassButton}>
</div>
${preview.kind === 'alarm' && preview.matched < preview.ok && html`
<p className="hint">
有 ${preview.ok - preview.matched} 条告警的基站不在站点库中:可以入库留档,但不会参与本系统站点的根因分析。
可先导入对应的站点工参。
</p>`}
${preview.bad > 0 && html`
<div>
<h4 className="sub-title">问题数据(不会入库,最多显示 500 条)</h4>
<${DataTable} columns=${errorColumns} rows=${preview.errors} maxHeight=${320}/>
</div>`}
<h4 className="sub-title">合格数据预览(前 50 条)</h4>
<${DataTable} columns=${PREVIEW_COLUMNS[preview.kind] || PREVIEW_COLUMNS.outage} rows=${preview.preview}/>
</div>`;
}