This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cyber-narrator-cn-ui/src/views/entityExplorer/entityList/entityListMixin.js

369 lines
12 KiB
JavaScript
Raw Normal View History

2021-12-16 18:22:47 +08:00
import _ from 'lodash'
import axios from 'axios'
2021-12-31 10:40:37 +08:00
import { api } from '@/utils/api'
import * as echarts from 'echarts'
2022-02-14 17:40:29 +08:00
import { entityListLineOption } from '@/views/charts/charts/chart-options'
import { riskLevelMapping, unitTypes } from '@/utils/constants'
import { getSecond } from '@/utils/date-util'
import { valueToRangeValue } from '@/utils/unit-convert'
2022-04-25 15:29:44 +08:00
import { shallowRef } from 'vue'
import { computeScore } from '@/utils/tools'
import i18n from '@/i18n'
2021-12-16 18:22:47 +08:00
export default {
props: {
2021-12-31 10:40:37 +08:00
entity: Object,
timeFilter: Object,
listMode: String
2021-12-16 18:22:47 +08:00
},
data () {
return {
2021-12-31 10:40:37 +08:00
entityData: {},
2022-03-27 13:04:47 +08:00
trafficUrl: '',
chartOption: null,
unitTypes,
valueToRangeValue,
echartsArray: [],
loadingNetworkQuality: false,
score: '-', // 网络质量评分
scoreUrl: '', // 网络质量评分url
eventNum: 0, // 性能事件和安全事件数量之和
loadingEvent: false, // event的loading
performanceEventUrl: '', // 性能事件接口url
2023-10-22 20:21:32 +08:00
securityEventUrl: '', // 安全事件接口url
performanceScoreData: {},
scoreDataState: false
2021-12-16 18:22:47 +08:00
}
},
computed: {
iconClass () {
let className
switch (this.entityData.entityType) {
case ('ip'): {
2022-03-18 17:42:02 +08:00
className = 'cn-icon cn-icon-ip2'
2021-12-16 18:22:47 +08:00
break
}
case ('domain'): {
2022-03-18 17:42:02 +08:00
className = 'cn-icon cn-icon-domain2'
2021-12-16 18:22:47 +08:00
break
}
case ('app'): {
2022-03-18 17:42:02 +08:00
className = 'cn-icon cn-icon-app2'
2021-12-16 18:22:47 +08:00
break
}
case ('subscriber_id'): {
className = 'cn-icon cn-icon-pedestrian'
break
}
2021-12-16 18:22:47 +08:00
default: break
}
return className
2021-12-31 10:40:37 +08:00
},
2022-03-27 13:04:47 +08:00
entityType () {
2023-07-07 17:22:51 +08:00
return this.entity.entityValue
2021-12-31 10:40:37 +08:00
},
2022-03-27 13:04:47 +08:00
entityName () {
2023-07-07 17:22:51 +08:00
return this.entity.entityValue
2021-12-31 10:40:37 +08:00
},
appRisk () {
return function (level) {
const m = riskLevelMapping.find(mapping => {
return mapping.value == level
})
return (m && m.name) || level
}
},
2021-12-31 10:40:37 +08:00
queryParams () {
let params
const now = window.$dayJs.tz().valueOf()
2023-07-07 17:22:51 +08:00
// eslint-disable-next-line prefer-const
params = {
startTime: this.timeFilter.startTime ? getSecond(this.timeFilter.startTime) : Math.floor(now / 1000 - 3600),
endTime: this.timeFilter.endTime ? getSecond(this.timeFilter.endTime) : Math.floor(now / 1000),
resource: this.entityData.entityValue
2021-12-31 10:40:37 +08:00
}
return params
2023-10-22 20:21:32 +08:00
},
scoreBaseState () {
return this.$store.getters.scoreBaseReady
},
scoreDot () {
const dots = []
if (this.score === '-') {
for (let i = 0; i < 6; i++) {
dots.push({
class: 'score-dot'
})
}
} else {
for (let i = 0; i < 6; i++) {
if (i < this.score) {
dots.push({
class: `score-dot ${handleClass(this.score)}`
})
} else {
dots.push({
class: 'score-dot'
})
}
}
}
return dots
function handleClass (score) {
if (score <= 2) {
return 'score-dot--red'
} else if (score <= 4) {
return 'score-dot--yellow'
} else if (score <= 6) {
return 'score-dot--green'
}
return ''
}
2021-12-16 18:22:47 +08:00
}
},
methods: {
showDetail () {
const { href } = this.$router.resolve({
2023-11-09 16:17:25 +08:00
path: '/entity/detail',
query: {
entityType: this.entityData.entityType,
entityName: this.entityData.entityValue,
range: this.timeFilter.dateRangeValue
}
})
window.open(href, '_blank')
2021-12-16 18:22:47 +08:00
},
showGraph () {
const { href } = this.$router.resolve({
2023-11-09 16:17:25 +08:00
path: '/entity/graph',
query: {
entityType: this.entityData.entityType,
2023-07-07 17:22:51 +08:00
entityName: this.entityData.entityValue
}
})
window.open(href, '_blank')
},
getQueryParams (dateRangeValue) {
if (dateRangeValue) {
// range取 config.js 中配置的值
const endTime = window.$dayJs.tz().valueOf()
const startTime = endTime - dateRangeValue * 60 * 1000
return {
startTime: getSecond(startTime),
endTime: getSecond(endTime),
resource: this.entityType
}
} else {
return {
startTime: getSecond(this.timeFilter.startTime),
endTime: getSecond(this.timeFilter.endTime),
resource: this.entityType
}
2022-03-27 13:04:47 +08:00
}
},
queryEntityDetailTraffic () {
this.loading = true
axios.get(this.trafficUrl, { params: this.getQueryParams(DEFAULT_TIME_FILTER_RANGE.entity.trafficLine) }).then(response => {
if (response.status === 200 && response.data.data.result && response.data.data.result.length > 0) {
2022-03-27 13:04:47 +08:00
let sentSeries
let receivedSeries
response.data.data.result.forEach(t => {
if (t.legend === 'rate') {
2022-03-27 13:04:47 +08:00
this.entityData.max = t.aggregation.max
this.entityData.avg = t.aggregation.avg
this.entityData.p50 = t.aggregation.p50
this.entityData.p90 = t.aggregation.p90
} else if (t.legend === 'sentRate') {
this.entityData.bytesSentRate = t.aggregation.avg
2022-03-27 13:04:47 +08:00
sentSeries = {
name: i18n.global.t('entities.sentThroughput'),
2022-03-27 13:04:47 +08:00
type: 'line',
legendHoverLink: false,
2021-12-31 10:40:37 +08:00
itemStyle: {
lineStyle: {
width: 1
2021-12-31 10:40:37 +08:00
}
},
2022-03-27 13:04:47 +08:00
color: '#69b072',
data: _.dropRight(t.values, 2).map(v => [Number(v[0]) * 1000, Number(v[1]), unitTypes.bps]),
2022-03-27 13:04:47 +08:00
showSymbol: false
2021-12-31 10:40:37 +08:00
}
} else if (t.legend === 'receivedRate') {
this.entityData.bytesReceivedRate = t.aggregation.avg
2022-03-27 13:04:47 +08:00
receivedSeries = {
name: i18n.global.t('entities.receivedThroughput'),
2022-03-27 13:04:47 +08:00
type: 'line',
legendHoverLink: false,
2021-12-31 10:40:37 +08:00
itemStyle: {
lineStyle: {
width: 1
2021-12-31 10:40:37 +08:00
}
},
2022-03-27 13:04:47 +08:00
color: '#7899c6',
data: t.values.map(v => [Number(v[0]) * 1000, Number(v[1]), unitTypes.bps]),
2022-03-27 13:04:47 +08:00
showSymbol: false
2021-12-31 10:40:37 +08:00
}
}
})
2022-03-27 13:04:47 +08:00
if (this.listMode === 'block') {
const chart = echarts.init(document.getElementById(`entityListChart${this.entityName}${this.listMode}`))
2022-04-25 15:29:44 +08:00
this.echartsArray.push(shallowRef(chart))
2022-03-27 13:04:47 +08:00
chart.setOption({
...this.chartOption,
series: [sentSeries, receivedSeries]
})
} else if (this.listMode === 'list') {
const sentChart = echarts.init(document.getElementById(`entityDetailSend${this.entityName}${this.listMode}`))
const receivedChart = echarts.init(document.getElementById(`entityDetailReceived${this.entityName}${this.listMode}`))
2022-04-25 15:29:44 +08:00
this.echartsArray.push(shallowRef(sentChart), shallowRef(receivedChart))
2022-03-27 13:04:47 +08:00
sentChart.setOption({
...this.chartOption,
series: [sentSeries]
})
receivedChart.setOption({
...this.chartOption,
series: [receivedSeries]
})
2021-12-31 10:40:37 +08:00
}
this.loading = false
2022-04-28 11:24:04 +08:00
} else {
this.loading = false
2021-12-31 10:40:37 +08:00
}
}).finally(() => {
2022-03-27 13:04:47 +08:00
setTimeout(() => {
try {
this.$nextTick(() => {
this.sentChart && this.sentChart.resize()
this.receivedChart && this.receivedChart.resize()
})
} catch (e) {}
}, 250)
2021-12-31 10:40:37 +08:00
})
2022-04-25 15:29:44 +08:00
},
resize () {
this.echartsArray.forEach(item => { item.value.resize() })
2023-07-07 17:22:51 +08:00
},
initUrl () {
if (this.entity.entityType) {
switch (this.entity.entityType) {
case 'ip': {
this.scoreUrl = api.entity.entityList.ipPerformance
2023-07-07 17:22:51 +08:00
this.trafficUrl = api.entity.entityList.ipThroughput
this.securityEventUrl = api.entity.entityList.ipSecurity
this.performanceEventUrl = api.entity.entityList.ipEventPerformance
2023-07-07 17:22:51 +08:00
break
}
case 'domain': {
this.scoreUrl = api.entity.entityList.domainPerformance
2023-07-07 17:22:51 +08:00
this.trafficUrl = api.entity.entityList.domainThroughput
this.securityEventUrl = api.entity.entityList.domainSecurity
this.performanceEventUrl = api.entity.entityList.domainEventPerformance
2023-07-07 17:22:51 +08:00
break
}
case 'app': {
this.scoreUrl = api.entity.entityList.appPerformance
2023-07-07 17:22:51 +08:00
this.trafficUrl = api.entity.entityList.appThroughput
this.securityEventUrl = api.entity.entityList.appSecurity
this.performanceEventUrl = api.entity.entityList.appEventPerformance
2023-07-07 17:22:51 +08:00
break
}
case 'subscriber_id': {
this.trafficUrl = api.entity.entityList.subscriberThroughput
break
}
2023-07-07 17:22:51 +08:00
default:
break
}
}
},
/** 获取网络评分 */
queryNetworkQuantity () {
this.loadingNetworkQuality = true
2023-10-22 20:21:32 +08:00
this.performanceScoreData = {}
this.scoreDataState = false
axios.get(this.scoreUrl, { params: this.getQueryParams(DEFAULT_TIME_FILTER_RANGE.entity.trafficLine) }).then(response => {
if (response.status === 200) {
2023-10-22 20:21:32 +08:00
this.performanceScoreData = {
establishLatencyMs: _.get(response, 'data.data.result.establishLatencyMsAvg', null),
httpResponseLatency: _.get(response, 'data.data.result.httpResponseLatencyAvg', null),
sslConLatency: _.get(response, 'data.data.result.sslConLatencyAvg', null),
tcpLostlenPercent: _.get(response, 'data.data.result.tcpLostlenPercentAvg', null),
pktRetransPercent: _.get(response, 'data.data.result.pktRetransPercentAvg', null)
}
}
}).finally(() => {
this.loadingNetworkQuality = false
2023-10-22 20:21:32 +08:00
this.scoreDataState = true
})
},
/** 获取事件数量 */
queryEventNum () {
this.loadingEvent = true
// const performance = axios.get(this.performanceEventUrl, { params: this.getQueryParams() })
// const security = axios.get(this.securityEventUrl, { params: this.getQueryParams() })
// performance接口修改暂时不能访问故只调用security的后续加上
this.eventNum = 0
const param = {
params: {
...this.getQueryParams(),
pageSize: -1
}
}
axios.get(this.securityEventUrl, param).then(res => {
if (res.status === 200) {
this.eventNum = res.data.data.result.length
}
}).finally(() => {
this.loadingEvent = false
})
// Promise.all([performance, security]).then(response => {
// this.eventNum = response[0].data.data.result.length + response[1].data.data.result.length
// }).catch(e => {
// this.eventNum = 0
// }).finally(() => {
// this.loadingEvent = false
// })
2023-10-22 20:21:32 +08:00
},
handleScoreData () {
this.score = computeScore(this.performanceScoreData, this.$store.getters.getScoreBase)
2021-12-16 18:22:47 +08:00
}
},
2022-03-27 13:04:47 +08:00
watch: {
entityData: {
deep: true,
handler (n) {
2023-07-07 17:22:51 +08:00
this.initUrl()
2022-03-27 13:04:47 +08:00
}
2023-10-22 20:21:32 +08:00
},
scoreBaseState (n) {
if (n && this.scoreDataState) {
this.handleScoreData()
}
},
scoreDataState (n) {
if (n && this.scoreBaseState) {
this.handleScoreData()
}
2022-03-27 13:04:47 +08:00
}
},
2021-12-16 18:22:47 +08:00
mounted () {
this.debounceFunc = this.$_.debounce(this.resize, 200)
window.addEventListener('resize', this.debounceFunc)
2022-03-27 13:04:47 +08:00
this.chartOption = _.cloneDeep(entityListLineOption)
2023-07-07 17:22:51 +08:00
this.entityData = { ...this.entityData, ...this.entity }
this.initUrl()
2022-03-27 13:04:47 +08:00
setTimeout(() => {
this.queryEntityDetailTraffic()
if (this.entity.entityType !== 'subscriber_id') {
this.queryNetworkQuantity()
this.queryEventNum()
}
2022-03-27 13:04:47 +08:00
})
2022-04-25 15:29:44 +08:00
},
beforeUnmount () {
window.removeEventListener('resize', this.debounceFunc)
2021-12-16 18:22:47 +08:00
}
}