index.vue
6.71 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
<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 }">
<a-button type="link" class="ml-2" @click="handleViewAlarmDetails(record)">
查看告警详情
</a-button>
</template>
<template #toolbar>
<Button @click="handleBatchAck" type="primary" :disabled="getCanBatchAck">批量处理</Button>
</template>
</BasicTable>
<AlarmDetailDrawer @register="registerDetailDrawer" @success="handleSuccess" />
</div>
</template>
<script lang="ts">
import { defineComponent, nextTick, h, computed } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { alarmColumns, alarmSearchSchemas } from './config/detail.config';
import { doBatchAckAlarm, 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';
export default defineComponent({
name: 'AlarmCenter',
components: {
Button,
BasicTable,
TableAction,
AlarmDetailDrawer,
},
setup() {
const [registerTable, { reload, getSelectRows, clearSelectedRowKeys, getRowSelection }] =
useTable({
title: '告警记录列表',
api: getDeviceAlarm,
columns: alarmColumns,
useSearchForm: true,
formConfig: {
labelWidth: 120,
schemas: alarmSearchSchemas,
fieldMapToTime: [['alarmTime', ['startTime', 'endTime'], 'x']],
},
bordered: true,
showIndexColumn: false,
showTableSetting: true,
clickToRowSelect: false,
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 = () => {
reload();
};
const handleViewAlarmDetails = async (record: Recordable) => {
await nextTick();
const { details } = record;
const deviceIdKeys = Object.keys(details);
const detailObject = deviceIdKeys.map((key) => ({ label: key, value: details[key] }));
const dataFormat = await handleAlarmDetailFormat(deviceIdKeys);
const dataFormats = detailObject.reduce((acc: any, curr: any) => {
dataFormat.forEach((item) => {
if (item.tbDeviceId === curr.label) {
const findName = item.attribute.find(
(item) => item.identifier === curr.value.key
)?.name;
const findLogin = [
...operationNumber_OR_TIME,
...operationString,
...operationBoolean,
].find((item) => item.value === curr.value.logic)?.symbol;
const findAttribute = item.attribute.find(
(findItem) => findItem.identifier === curr.value.key
);
const value = {
['触发属性']: findName,
['触发条件']: `${findLogin}${curr.value.logicValue}`,
['触发值']: `${curr.value.realValue}${
findAttribute.detail?.dataType?.specs?.unit?.key ?? ''
}`,
};
const data = {
[item.name]: value,
};
acc.push(data);
}
});
return [...acc];
}, []);
const objectFormat = dataFormats.reduce((acc: any, curr: any) => {
return {
...acc,
...curr,
};
}, {});
Modal.info({
title: '告警详情',
width: 600,
centered: true,
maskClosable: true,
content: h(JsonPreview, { data: JSON.parse(JSON.stringify(objectFormat)) }),
});
};
const handleAlarmDetailFormat = async (keys: string[]) => {
const temp: any = [];
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: any = handleDataFormat(deviceDetailRes, attributeRes);
temp.push(dataFormat);
}
return temp;
};
const handleDataFormat = (deviceDetail: any, attributes: any) => {
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 getCanBatchAck = computed(() => {
const rowSelection = getRowSelection();
return !rowSelection.selectedRowKeys?.length;
});
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();
};
return {
registerTable,
registerDetailDrawer,
handleDetail,
handleSuccess,
handleViewAlarmDetails,
handleBatchAck,
getCanBatchAck,
};
},
});
</script>