index.vue
8.06 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<template>
<div>
<BasicTable @register="registerTable">
<template #action="{ record }">
<TableAction
:actions="[
{
label: '详情',
auth: 'api:yt:alarm:get',
icon: 'ant-design:eye-outlined',
onClick: handleDetail.bind(null, record),
},
]"
/>
</template>
<template #details="{ record }">
<Button type="link" class="ml-2" @click="handleViewAlarmDetails(record)">
查看告警详情
</Button>
</template>
<template #toolbar>
<Authority value="api:yt:alarm:batch:clear">
<a-button type="danger" :disabled="getCanBatchClear" @click="handleBatchClear">
批量清除
</a-button>
</Authority>
<Authority value="api:yt:alarm:batch:handle">
<Button @click="handleBatchAck" type="primary" :disabled="getCanBatchAck">
<span>批量处理</span>
</Button>
</Authority>
</template>
</BasicTable>
<AlarmDetailDrawer @register="registerDetailDrawer" @success="handleSuccess" />
</div>
</template>
<script lang="ts" setup>
import { nextTick, h, computed } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { alarmColumns, getAlarmSearchSchemas } from './config/detail.config';
import { doBatchAckAlarm, doBatchClearAlarm, getDeviceAlarm } from '/@/api/device/deviceManager';
import { useDrawer } from '/@/components/Drawer';
import AlarmDetailDrawer from './cpns/AlarmDetailDrawer.vue';
import { Modal, Button } from 'ant-design-vue';
import { JsonPreview } from '/@/components/CodeEditor';
import { getDeviceDetail } from '/@/api/device/deviceManager';
import { getAttribute } from '/@/api/ruleengine/ruleengineApi';
import {
operationNumber_OR_TIME,
operationString,
operationBoolean,
} from '/@/views/rule/linkedge/config/formatData';
import { AlarmLogItem } from '/@/api/device/model/deviceConfigModel';
import { AlarmStatus } from '/@/enums/alarmEnum';
import { useMessage } from '/@/hooks/web/useMessage';
import { Authority } from '/@/components/Authority';
const props = defineProps<{
deviceId?: string;
}>();
const [registerTable, { reload, getSelectRows, clearSelectedRowKeys, getRowSelection }] =
useTable({
title: '告警记录列表',
api: getDeviceAlarm,
beforeFetch: (params) => {
const { status } = params;
const obj = {
...params,
...{
status: status ? status.at(-1) : null,
},
};
return obj;
},
columns: alarmColumns,
rowKey: 'id',
useSearchForm: true,
formConfig: {
labelWidth: 120,
schemas: getAlarmSearchSchemas(!!props.deviceId),
fieldMapToTime: [['alarmTime', ['startTime', 'endTime'], 'x']],
baseColProps: { span: 8 },
},
bordered: true,
showIndexColumn: false,
showTableSetting: true,
clickToRowSelect: false,
searchInfo: {
deviceId: props.deviceId,
},
rowSelection: {
type: 'checkbox',
getCheckboxProps: (record: AlarmLogItem) => {
return {
disabled: record.status === AlarmStatus.CLEARED_ACK,
};
},
},
actionColumn: {
width: 200,
title: '操作',
slots: { customRender: 'action' },
fixed: 'right',
},
});
const [registerDetailDrawer, { openDrawer }] = useDrawer();
const handleDetail = (record: Recordable) => {
openDrawer(true, record);
};
const handleSuccess = () => {
clearSelectedRowKeys();
reload();
};
const findName = (item: Recordable, curr: Recordable) => {
return item.attribute.find((item) => item.identifier === curr?.key)?.name;
};
const findLogin = (curr: Recordable) => {
return [...operationNumber_OR_TIME, ...operationString, ...operationBoolean].find(
(item) => item.value === curr?.logic
)?.symbol;
};
const findAttribute = (item: Recordable, curr: Recordable) => {
item.attribute.find((findItem) => findItem.identifier === curr?.key);
};
const findValue = (item: Recordable, curr: Recordable) => {
return {
['触发属性']: findName(item, curr),
['触发条件']: `${findLogin(curr)}${curr?.logicValue}`,
['触发值']: `${curr?.realValue}${
findAttribute(item, curr)?.detail?.dataType?.specs?.unit?.key ?? ''
}`,
};
};
const handleAlarmText = (text: string) => (text === 'triggerData' ? '触发器' : '执行条件');
const handleViewAlarmDetails = async (record: Recordable) => {
await nextTick();
const { details } = record;
if (!details) return;
const deviceIdKeys = Object.keys(details);
const dataFormat = await handleAlarmDetailFormat(deviceIdKeys);
const mapDataFormat = deviceIdKeys.map((deviceKey: string) => {
const findDataFormat = dataFormat.find(
(dataItem: Recordable) => dataItem.tbDeviceId === deviceKey
);
const dataKeys = Object.keys(details[deviceKey]);
const data: any = dataKeys.map((dataItem: string) => {
if (dataItem !== 'triggerData' && dataItem !== 'conditionData') {
return findValue(findDataFormat, details[deviceKey]);
} else {
return {
[handleAlarmText(dataItem)]: findValue(findDataFormat, details[deviceKey][dataItem]),
};
}
});
const objectDataFormat = data.reduce((acc: Recordable, curr: Recordable) => {
return {
...acc,
...curr,
};
});
return {
[findDataFormat.name]: objectDataFormat,
};
});
const objectDataFormats = mapDataFormat.reduce((acc: Recordable, curr: Recordable) => {
return {
...acc,
...curr,
};
});
Modal.info({
title: '告警详情',
width: 600,
centered: true,
maskClosable: true,
content: h(JsonPreview, { data: JSON.parse(JSON.stringify(objectDataFormats)), deep: 4 }),
});
};
const handleAlarmDetailFormat = async (keys: string[]) => {
const temp: Recordable = [];
for (let item of keys) {
if (item === 'key' || item === 'data') return []; //旧数据则终止
const deviceDetailRes = await getDeviceDetail(item);
const { deviceProfileId } = deviceDetailRes;
if (!deviceProfileId) return [];
const attributeRes = await getAttribute(deviceProfileId);
const dataFormat: Recordable = handleDataFormat(deviceDetailRes, attributeRes);
temp.push(dataFormat);
}
return temp;
};
const handleDataFormat = (deviceDetail: Recordable, attributes: Recordable) => {
const { name, tbDeviceId, alias } = deviceDetail;
const attribute = attributes.map((item) => ({
identifier: item.identifier,
name: item.name,
detail: item.detail,
}));
return {
name: alias || name,
tbDeviceId,
attribute,
};
};
const getCanBatchClear = computed(() => {
const rowSelection = getRowSelection();
const getRows: AlarmLogItem[] = getSelectRows();
return !rowSelection.selectedRowKeys?.length || !getRows.every((item) => !item.cleared);
});
const getCanBatchAck = computed(() => {
const rowSelection = getRowSelection();
const getRows: AlarmLogItem[] = getSelectRows();
return !rowSelection.selectedRowKeys?.length || !getRows.every((item) => !item.acknowledged);
});
const { createMessage } = useMessage();
const handleBatchAck = async () => {
const ids = getSelectRows<AlarmLogItem>().map((item) => item.id);
if (!ids.length) return;
await doBatchAckAlarm(ids);
createMessage.success('操作成功');
clearSelectedRowKeys();
reload();
};
const handleBatchClear = async () => {
const ids = getSelectRows<AlarmLogItem>().map((item) => item.id);
if (!ids.length) return;
await doBatchClearAlarm(ids);
createMessage.success('操作成功');
clearSelectedRowKeys();
reload();
};
</script>
<style>
.alarm-stauts-cascader {
.ant-cascader-menu {
height: fit-content;
}
}
</style>