Alerts.vue
9.17 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
<!-- 告警:取数端离线、DMS 账号异常、补货流程失败等。同类合并,问题消失后自动恢复,也可以人工关闭(单条 / 勾选 / 一键处理)。
状态:待处理 / 已自动恢复(问题消失,系统关掉的)/ 已人工关闭(含一键处理)/ 全部;另可按大类、级别、关键词筛。
地址栏参数:status / group / severity / keyword(旧链接 status=resolved = 已恢复的全部)。 -->
<script setup lang="ts">
import { Refresh, Search } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { computed, onMounted, ref, watch } from 'vue'
import { api } from '@/api'
import EmptyState from '@/components/EmptyState.vue'
import PageHeader from '@/components/PageHeader.vue'
import Pager from '@/components/Pager.vue'
import Panel from '@/components/Panel.vue'
import StatusPill from '@/components/StatusPill.vue'
import { usePaged } from '@/composables/usePaged'
import { fmtShort, SEVERITY } from '@/format'
import { session } from '@/session'
const { rows, total, page, size, loading, filters, data, load } = usePaged('/api/alerts', {
size: 20, query: true, filters: { status: 'open', group: '', severity: '', keyword: '' },
})
const counts = computed<Record<string, number>>(() => data.value?.counts || {})
const groups = computed<Record<string, string>>(() => data.value?.groups || { edge: '取数端', restock: '补货流程', im: '消息通道', system: '系统运行' })
const narrowed = computed(() => !!(filters.group || filters.severity || filters.keyword))
// 菜单上的角标 = 全部待处理条数;只有没加其他筛选时这里的数才是全量
watch(counts, (c) => { if (!narrowed.value && c.open !== undefined) session.openAlerts = c.open })
onMounted(load)
// 关闭后刷新列表;带筛选时列表里的条数不是全量,角标单独再取一次
async function reload() {
await load()
if (narrowed.value) try { session.openAlerts = (await api.get('/api/alerts?status=open&size=1')).total } catch { /* 忽略 */ }
}
const STATUS_TABS = [
{ key: 'open', text: '待处理' },
{ key: 'recovered', text: '已自动恢复' },
{ key: 'closed', text: '已人工关闭' },
{ key: '', text: '全部' },
]
const kwd = ref(filters.keyword)
function search() { filters.keyword = kwd.value.trim() }
const selected = ref<any[]>([])
watch(rows, () => { selected.value = [] })
const openCount = computed(() => counts.value.open || 0)
function statusOf(r: any) {
if (r.status === 'open') return { text: '待处理', tone: 'err' as const }
return r.manual ? { text: '已人工关闭', tone: 'info' as const } : { text: '已自动恢复', tone: 'ok' as const }
}
async function resolve(a: any) {
let value = ''
try { value = (await ElMessageBox.prompt('关闭说明(选填)', `关闭告警:${a.title}`, { inputPlaceholder: '如:已人工处理' })).value } catch { return }
try { await api.post(`/api/alerts/${a.id}/resolve`, { note: value || '' }); ElMessage.success('已关闭'); reload() } catch (e: any) { ElMessage.error(e.message) }
}
// 一键处理:没勾选 = 当前筛选下所有待处理;勾了 = 只关勾选的
async function resolveMany(onlySelected: boolean) {
const n = onlySelected ? selected.value.length : openCount.value
if (!n) return
const scope = onlySelected ? `勾选的 ${n} 条告警` : `${narrowed.value ? '当前筛选下' : '全部'} ${n} 条待处理告警`
let note = ''
try {
note = (await ElMessageBox.prompt(
`将关闭${scope},关闭后不再重复提醒。问题如果还在,下次检测到时会重新发一条新告警。`,
onlySelected ? '关闭所选告警' : '一键处理',
{ inputPlaceholder: '处理说明(选填),如:已逐个确认', confirmButtonText: `关闭 ${n} 条`, cancelButtonText: '取消', type: 'warning' },
)).value || ''
} catch { return }
try {
const body: any = { note }
if (onlySelected) body.ids = selected.value.map((r) => r.id)
else Object.assign(body, { group: filters.group, severity: filters.severity, keyword: filters.keyword })
const r = await api.post('/api/alerts/resolve-all', body)
ElMessage.success(`已关闭 ${r.count} 条告警`)
reload()
} catch (e: any) { ElMessage.error(e.message) }
}
</script>
<template>
<div class="page">
<PageHeader title="告警" desc="同一类问题合并成一条,未恢复期间按间隔重复提醒;问题消失后系统自动恢复并通知,也可以人工关闭。
取数端告警的接收人在「系统设置 → 告警与通知」,其他告警发给用途含「告警通知」的接收对象。" />
<Panel flush title="告警列表">
<template #toolbar>
<div class="status-tabs">
<button v-for="t in STATUS_TABS" :key="t.key" type="button" class="st-tab" :class="{ on: filters.status === t.key, hot: t.key === 'open' && counts.open }"
@click="filters.status = t.key">
{{ t.text }}<span v-if="counts[t.key || 'all'] !== undefined" class="num">{{ counts[t.key || 'all'] }}</span>
</button>
</div>
<el-select v-model="filters.group" placeholder="全部大类" clearable size="small" style="width: 120px">
<el-option v-for="(name, k) in groups" :key="k" :label="name" :value="k" />
</el-select>
<el-select v-model="filters.severity" placeholder="全部级别" clearable size="small" style="width: 110px">
<el-option label="紧急" value="critical" />
<el-option label="异常及以上" value="error,critical" />
<el-option label="注意" value="warn" />
<el-option label="提示" value="info" />
</el-select>
<el-input v-model="kwd" size="small" placeholder="标题 / 详情关键词" clearable :prefix-icon="Search" style="width: 180px" @keyup.enter="search" @clear="search" />
</template>
<template #extra>
<el-button v-if="selected.length" type="primary" plain @click="resolveMany(true)">关闭所选({{ selected.length }})</el-button>
<el-button v-if="filters.status === 'open' && openCount" type="primary" @click="resolveMany(false)">一键处理({{ openCount }})</el-button>
<el-button :icon="Refresh" @click="load()">刷新</el-button>
</template>
<el-table v-loading="loading" :data="rows" row-key="id" @selection-change="(v: any[]) => (selected = v)">
<el-table-column v-if="filters.status === 'open'" type="selection" width="44" />
<el-table-column label="级别" width="90"><template #default="{ row }"><StatusPill :tone="SEVERITY[row.severity]?.tone">{{ SEVERITY[row.severity]?.text || row.severity }}</StatusPill></template></el-table-column>
<el-table-column label="告警" min-width="300">
<template #default="{ row }">
<div class="strong">{{ row.title }}</div>
<div class="muted small">{{ groups[row.group] || row.group }} · {{ row.kind_cn || row.kind }}</div>
<div class="detail small">{{ row.detail }}</div>
</template>
</el-table-column>
<el-table-column label="首次 / 最近" width="150"><template #default="{ row }"><span class="small">{{ fmtShort(row.first_at) }}<br />{{ fmtShort(row.last_at) }}</span></template></el-table-column>
<el-table-column label="次数" width="110"><template #default="{ row }"><span class="small">发生 {{ row.occur_count }}<br />通知 {{ row.notify_count }}</span></template></el-table-column>
<el-table-column label="状态" width="190">
<template #default="{ row }">
<StatusPill :tone="statusOf(row).tone">{{ statusOf(row).text }}</StatusPill>
<div v-if="row.resolved_at" class="muted small">{{ fmtShort(row.resolved_at) }}</div>
<div v-if="row.resolved_note" class="muted small">{{ row.resolved_note }}</div>
</template>
</el-table-column>
<el-table-column label="" width="80" align="right"><template #default="{ row }"><el-button v-if="row.status === 'open'" link type="primary" @click="resolve(row)">关闭</el-button></template></el-table-column>
<template #empty><EmptyState :title="filters.status === 'open' ? '没有待处理的告警' : '暂无告警'" :desc="narrowed ? '换个大类、级别或关键词试试' : ''" /></template>
</el-table>
<Pager v-model:page="page" v-model:size="size" :total="total" />
</Panel>
</div>
</template>
<style scoped>
.detail { color: var(--c-text-2); white-space: pre-wrap; margin-top: 4px; }
.status-tabs { display: inline-flex; gap: 2px; padding: 2px; border-radius: 8px; background: var(--c-bg, #f2f3f5); margin-right: var(--sp-2); flex-wrap: wrap; }
.st-tab { border: 0; background: transparent; padding: 4px 12px; border-radius: 6px; font-size: 13px; color: var(--c-text-2); cursor: pointer; display: inline-flex; align-items: center; gap: 6px; }
.st-tab:hover { color: var(--c-text); }
.st-tab.on { background: var(--c-surface); color: var(--c-text); font-weight: 600; box-shadow: 0 1px 2px rgba(0, 0, 0, .08); }
.st-tab .num { font-size: 12px; min-width: 18px; padding: 0 5px; border-radius: 9px; background: var(--c-border); color: var(--c-text-2); line-height: 18px; text-align: center; }
.st-tab.hot .num { background: var(--c-err, #e5484d); color: #fff; }
</style>