fix: 拓扑取网元状态改ws读取

This commit is contained in:
TsMask
2024-01-29 18:37:00 +08:00
parent cf944a67bd
commit d5f2ca71cf
3 changed files with 109 additions and 59 deletions

View File

@@ -166,7 +166,8 @@ export class WS {
console.warn('websocket unavailable'); console.warn('websocket unavailable');
return; return;
} }
console.log(' readyState', this.ws.readyState);
// 非正常状态关闭
if ( if (
this.ws.readyState === WebSocket.CLOSED || this.ws.readyState === WebSocket.CLOSED ||
this.ws.readyState === WebSocket.CLOSING this.ws.readyState === WebSocket.CLOSING

View File

@@ -1,8 +1,11 @@
<script setup lang="ts"> <script setup lang="ts">
import { reactive, onMounted, ref, onBeforeUnmount } from 'vue'; import { reactive, onMounted, ref, onBeforeUnmount } from 'vue';
import { PageContainer } from 'antdv-pro-layout'; import { PageContainer } from 'antdv-pro-layout';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants'; import {
import { listNe, stateNe } from '@/api/ne/ne'; RESULT_CODE_ERROR,
RESULT_CODE_SUCCESS,
} from '@/constants/result-constants';
import { listNe } from '@/api/ne/ne';
import { message } from 'ant-design-vue/lib'; import { message } from 'ant-design-vue/lib';
import { getGraphData } from '@/api/monitor/topology'; import { getGraphData } from '@/api/monitor/topology';
import { parseDateToStr } from '@/utils/date-utils'; import { parseDateToStr } from '@/utils/date-utils';
@@ -20,8 +23,10 @@ import {
} from '../topologyBuild/hooks/registerNode'; } from '../topologyBuild/hooks/registerNode';
import useNeOptions from './useNeOptions'; import useNeOptions from './useNeOptions';
import useI18n from '@/hooks/useI18n'; import useI18n from '@/hooks/useI18n';
import { OptionsType, WS } from '@/plugins/ws-websocket';
const { t } = useI18n(); const { t } = useI18n();
const { fnNeRestart, fnNeStop, fnNeLogFile } = useNeOptions(); const { fnNeRestart, fnNeStop, fnNeLogFile } = useNeOptions();
const ws = new WS();
/**图DOM节点实例对象 */ /**图DOM节点实例对象 */
const graphG6Dom = ref<HTMLElement | undefined>(undefined); const graphG6Dom = ref<HTMLElement | undefined>(undefined);
@@ -39,7 +44,20 @@ const graphState = reactive<Record<string, any>>({
}); });
/**非网元元素 */ /**非网元元素 */
const notNeNodes = ['5GC', 'DN', 'UE', 'Base', "lan", "lan1", "lan2", "lan3", "lan4", "lan5", "lan6", "lan7"]; const notNeNodes = [
'5GC',
'DN',
'UE',
'Base',
'lan',
'lan1',
'lan2',
'lan3',
'lan4',
'lan5',
'lan6',
'lan7',
];
/**图实例对象 */ /**图实例对象 */
const graphG6 = ref<any>(null); const graphG6 = ref<any>(null);
@@ -247,7 +265,6 @@ function fnGraphDataLoad(reload: boolean = false) {
} }
}) })
.then(res => { .then(res => {
console.log('fnGraphDataLoad', res);
if (!res) return; if (!res) return;
const { combos, edges, nodes } = res.graphData; const { combos, edges, nodes } = res.graphData;
@@ -269,7 +286,6 @@ function fnGraphDataLoad(reload: boolean = false) {
return false; return false;
} }
); );
console.log(nf);
// 边过滤 // 边过滤
const ef: Record<string, any>[] = edges.filter( const ef: Record<string, any>[] = edges.filter(
@@ -320,52 +336,77 @@ const stateTimeout = ref<any>(null);
/**查询网元状态 */ /**查询网元状态 */
async function fnGetState() { async function fnGetState() {
clearTimeout(stateTimeout.value); clearTimeout(stateTimeout.value);
const { combos, edges, nodes } = graphState.data;
// console.log({ combos, edges, nodes })
// 获取节点状态 // 获取节点状态
for (const node of nodes) { for (const node of graphState.data.nodes) {
if (notNeNodes.includes(node.id)) continue; if (notNeNodes.includes(node.id)) continue;
const { neType, neId, neName } = node.neInfo; const { neType, neId } = node.neInfo;
if (!neType || !neId) continue; if (!neType || !neId) continue;
const result = await stateNe(neType, neId); ws.send({
if (result.code === RESULT_CODE_SUCCESS) { requestId: `${neType}_${neId}`,
// 更新网元状态 type: 'ne_state',
const newNeState = Object.assign(node.neState, result.data, { data: {
refreshTime: parseDateToStr(result.data.refreshTime, 'HH:mm:ss'), neType: neType,
}); neId: neId,
},
});
}
// 通过 ID 查询节点实例 stateTimeout.value = setTimeout(() => fnGetState(), 30_000);
const item = graphG6.value.findById(node.id); }
if (item) {
const stateColor = newNeState.online ? '#52c41a' : '#f5222d'; // 状态颜色 /**接收数据后回调 */
// 图片类型不能填充 function wsError(ev: any) {
if (node.type.startsWith('image')) { // 接收数据后回调
// 更新节点 console.error(ev);
graphG6.value.updateItem(item, { }
label: neName,
}); /**接收数据后回调 */
// 设置状态 function wsMessage(res: Record<string, any>) {
graphG6.value.setItemState(item, 'top-right-dot', stateColor); const { code, requestId, data } = res;
} else { if (code === RESULT_CODE_ERROR) {
// 更新节点 console.warn(res.msg);
graphG6.value.updateItem(item, { return;
label: neName, }
// neState: newNeState, if (!requestId) return;
style: { const [neType, neId] = requestId.split('_');
fill: stateColor, // 填充色 const { combos, edges, nodes } = graphState.data;
stroke: stateColor, // 填充色 const node = nodes.find((item: Record<string, any>) => item.id === neType);
},
// labelCfg: { // 更新网元状态
// style: { const newNeState = Object.assign(node.neState, data, {
// fill: '#ffffff', // 标签文本色 refreshTime: parseDateToStr(data.refreshTime, 'HH:mm:ss'),
// }, online: !!data.cpu,
// }, });
});
// 设置状态 // 通过 ID 查询节点实例
graphG6.value.setItemState(item, 'stroke', newNeState.online); const item = graphG6.value.findById(node.id);
} if (item) {
} const stateColor = newNeState.online ? '#52c41a' : '#f5222d'; // 状态颜色
// 图片类型不能填充
if (node.type.startsWith('image')) {
// 更新节点
graphG6.value.updateItem(item, {
label: newNeState.neName,
});
// 设置状态
graphG6.value.setItemState(item, 'top-right-dot', stateColor);
} else {
// 更新节点
graphG6.value.updateItem(item, {
label: newNeState.neName,
// neState: newNeState,
style: {
fill: stateColor, // 填充色
stroke: stateColor, // 填充色
},
// labelCfg: {
// style: {
// fill: '#ffffff', // 标签文本色
// },
// },
});
// 设置状态
graphG6.value.setItemState(item, 'stroke', newNeState.online);
} }
} }
@@ -383,8 +424,7 @@ async function fnGetState() {
// `${edgeSource} - ${edgeTarget}`, // `${edgeSource} - ${edgeTarget}`,
// neS.neState.online && neT.neState.online // neS.neState.online && neT.neState.online
// ); // );
const stateColor = // const stateColor = neS.neState.online && neT.neState.online ? '#000000' : '#ff4d4f'; // 状态颜色
neS.neState.online && neT.neState.online ? '#000000' : '#ff4d4f'; // 状态颜色
// 更新边 // 更新边
// graphG6.value.updateItem(item, { // graphG6.value.updateItem(item, {
// label: `${edgeSource} - ${edgeTarget}`, // label: `${edgeSource} - ${edgeTarget}`,
@@ -398,26 +438,35 @@ async function fnGetState() {
// }, // },
// }); // });
// 设置状态 // 设置状态
// graphG6.value.setItemState( graphG6.value.setItemState(
// item, item,
// 'line-dash', 'line-dash',
// neS.neState.online && neT.neState.online neS.neState.online && neT.neState.online
// ); );
} }
if (neS && notNeNodes.includes(edgeTarget)) { if (neS && notNeNodes.includes(edgeTarget)) {
graphG6.value.setItemState(item, 'line-dash', neT.neState.online);
} }
if (neT && notNeNodes.includes(edgeSource)) { if (neT && notNeNodes.includes(edgeSource)) {
graphG6.value.setItemState(item, 'line-dash', neT.neState.online);
} }
} }
stateTimeout.value = setTimeout(() => fnGetState(), 30_000);
} }
onMounted(() => { onMounted(() => {
fnGraphDataLoad(false); fnGraphDataLoad(false);
// 建立链接
const options: OptionsType = {
url: '/ws',
onmessage: wsMessage,
onerror: wsError,
};
ws.connect(options);
}); });
onBeforeUnmount(() => { onBeforeUnmount(() => {
ws.close();
clearTimeout(stateTimeout.value); clearTimeout(stateTimeout.value);
}); });
</script> </script>

View File

@@ -146,7 +146,7 @@ export function edgeLineAnimateState() {
} }
if (value) { if (value) {
// 第一个矩形边 // 第一个矩形边
const fillColor = model?.labelCfg?.style?.fill || '#1890ff'; const fillColor = typeof value === 'string' ? value : '#1890ff';
// 边缘路径的起始位置 // 边缘路径的起始位置
const startPoint = keyShape.getPoint(0); const startPoint = keyShape.getPoint(0);
back1 = group.addShape('circle', { back1 = group.addShape('circle', {
@@ -221,7 +221,7 @@ export function edgeLineAnimateState() {
index = 0; index = 0;
} }
return { return {
lineDash: [4, 2, 1, 2], lineDash: [4, 2, 2, 2],
lineDashOffset: -index, lineDashOffset: -index,
}; };
}, },