forked from TencentBlueKing/bk-job
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.vue
305 lines (279 loc) · 9.7 KB
/
node.vue
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<template>
<div
v-bkloading="{ isLoading }"
class="ip-selector-view-node">
<collapse-box
ref="collapseBoxRef"
name="node">
<template #title>
<span style="font-weight: bold;">【动态拓扑】</span>
<span>
- 共
<span class="bk-ip-selector-number">{{ tableData.length }}</span>
个
</span>
<span v-if="newNodeNum">
,新增
<span class="bk-ip-selector-number-success">{{ newNodeNum }}</span>
个
</span>
<span v-if="removedNodeList.length">
,删除
<span class="bk-ip-selector-number-error">{{ removedNodeList.length }}</span>
个
</span>
</template>
<template
v-if="!context.readonly"
#action>
<extend-action>
<div @click="handleRemoveAll">
清除所有
</div>
</extend-action>
</template>
<table>
<tr
v-for="row in renderData"
:key="row.id"
:class="diffMap[genNodeKey(row)]">
<td
style="width: 30%;"
@click="handleShowHostList(row)">
<div class="cell">
<div class="cell-text">
{{ nodeNamePathMap[genNodeKey(row)] || `#${row.instance_id}` }}
</div>
<diff-tag :value="diffMap[genNodeKey(row)]" />
</div>
</td>
<td @click="handleShowHostList(row)">
<render-agent-statistics
:data="nodeAgentStaticMap[genNodeKey(row)]"
:loading="isAgentStatisticsLoading" />
</td>
<td
v-if="!context.readonly"
style="width: 60px;">
<bk-button
text
theme="primary"
@click="handleRemove(row)">
移除
</bk-button>
</td>
</tr>
</table>
<div
v-if="isShowPagination"
style="padding: 0 10px 8px 0;">
<bk-pagination
style="margin-top: 8px;"
v-bind="pagination"
@change="handlePaginationCurrentChange"
@limit-change="handlePaginationLimitChange" />
</div>
</collapse-box>
<bk-dialog
v-model="isShowNodeHostList"
:draggable="false"
header-position="left"
:title="`【${nodeNamePathMap[genNodeKey(selectedNode)]}】动态拓扑主机预览`"
:width="dialogWidth">
<node-host-list
:is-show="isShowNodeHostList"
:node="selectedNode" />
<template #footer>
<bk-button
theme="primary"
@click="handleHideHostList">
关闭
</bk-button>
</template>
</bk-dialog>
</div>
</template>
<script setup>
import _ from 'lodash';
import {
ref,
shallowRef,
watch,
} from 'vue';
import DiffTag from '../common/diff-tag.vue';
import ExtendAction from '../common/extend-action.vue';
import useDialogSize from '../hooks/use-dialog-size';
import useIpSelector from '../hooks/use-ip-selector';
import useLocalPagination from '../hooks/use-local-pagination';
import Manager from '../manager';
import {
genNodeKey,
getInvalidNodeList,
getNodeDiffMap,
getRemoveNodeList,
groupNodeList,
} from '../utils';
import RenderAgentStatistics from './components/agent-statistics.vue';
import CollapseBox from './components/collapse-box.vue';
import NodeHostList from './components/node-host-list.vue';
const props = defineProps({
data: {
type: Array,
required: true,
},
});
const emits = defineEmits(['change']);
const collapseBoxRef = ref();
const isLoading = ref(false);
const isAgentStatisticsLoading = ref(false);
const tableData = shallowRef([]);
const nodeNamePathMap = shallowRef({});
const nodeAgentStaticMap = shallowRef({});
const validNodeList = shallowRef([]);
const removedNodeList = shallowRef([]);
const invalidNodeList = shallowRef([]);
const resultList = shallowRef([]);
const isShowNodeHostList = ref(false);
const selectedNode = shallowRef({});
const diffMap = shallowRef({});
const newNodeNum = ref(0);
let isInnerChange = false;
const context = useIpSelector();
const {
isShowPagination,
pagination,
data: renderData,
handlePaginationCurrentChange,
handlePaginationLimitChange,
} = useLocalPagination(tableData);
const {
width: dialogWidth,
} = useDialogSize();
const fetchData = () => {
isLoading.value = true;
const params = {
[Manager.nameStyle('nodeList')]: props.data.map(item => ({
[Manager.nameStyle('objectId')]: item.object_id,
[Manager.nameStyle('instanceId')]: item.instance_id,
[Manager.nameStyle('meta')]: item.meta,
})),
};
Manager.service.fetchNodesQueryPath(params)
.then((data) => {
const validData = [];
const namePathMap = {};
data.forEach((item) => {
const tailNode = _.last(item);
validData.push(tailNode);
namePathMap[genNodeKey(tailNode)] = item.map(nodeData => nodeData.instance_name).join(' / ');
});
nodeNamePathMap.value = namePathMap;
validNodeList.value = validData;
})
.finally(() => {
isLoading.value = false;
});
};
watch(() => props.data, () => {
if (isInnerChange) {
isInnerChange = false;
return;
}
if (props.data.length > 0) {
fetchData();
} else {
tableData.value = [];
}
}, {
immediate: true,
});
watch(renderData, _.debounce((currentPageData) => {
const params = {
[Manager.nameStyle('nodeList')]: currentPageData.map(item => ({
[Manager.nameStyle('objectId')]: item.object_id,
[Manager.nameStyle('instanceId')]: item.instance_id,
[Manager.nameStyle('meta')]: item.meta,
})),
};
isAgentStatisticsLoading.value = true;
Manager.service.fetchHostAgentStatisticsNodes(params)
.then((data) => {
const staticMap = {};
data.forEach((item) => {
staticMap[genNodeKey(item.node)] = item.agent_statistics;
});
nodeAgentStaticMap.value = staticMap;
})
.finally(() => {
isAgentStatisticsLoading.value = false;
});
}), 300);
watch([validNodeList, resultList], () => {
invalidNodeList.value = getInvalidNodeList(props.data, validNodeList.value);
removedNodeList.value = getRemoveNodeList(props.data, context.originalValue);
diffMap.value = getNodeDiffMap(props.data, context.originalValue, invalidNodeList.value);
const {
newList,
originalList,
} = groupNodeList(validNodeList.value, diffMap.value);
newNodeNum.value = newList.legnth;
tableData.value = [
...invalidNodeList.value,
...newList,
...removedNodeList.value,
...originalList,
];
pagination.count = tableData.value.length;
});
const triggerChange = () => {
isInnerChange = true;
emits('change', 'nodeList', resultList.value);
};
// 删除指定节点
const handleRemove = (nodeData) => {
resultList.value = props.data.reduce((result, item) => {
if (genNodeKey(item) !== genNodeKey(nodeData)) {
result.push(item);
}
return result;
}, []);
if (diffMap.value[genNodeKey(nodeData)] !== 'invalid') {
validNodeList.value = validNodeList.value.reduce((result, item) => {
if (genNodeKey(item) !== genNodeKey(nodeData)) {
result.push(item);
}
return result;
}, []);
}
triggerChange();
};
// 移除所有
const handleRemoveAll = () => {
resultList.value = [];
validNodeList.value = [];
triggerChange();
};
// 查看节点的主机列表
const handleShowHostList = (node) => {
isShowNodeHostList.value = true;
selectedNode.value = node;
};
const handleHideHostList = () => {
isShowNodeHostList.value = false;
};
defineExpose({
refresh () {
fetchData();
},
collapseToggle (toggle) {
collapseBoxRef.value.toggle(toggle);
},
});
</script>
<style lang="postcss">
@import url("../styles/table.mixin.css");
.ip-selector-view-node {
@include table;
@include view-table;
}
</style>