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

330 lines
9.9 KiB
JavaScript
Raw Normal View History

2021-12-16 18:22:47 +08:00
import _ from 'lodash'
2021-12-31 10:40:37 +08:00
import { get } from '@/utils/http'
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'
2022-03-27 13:04:47 +08:00
import unitConvert from '@/utils/unit-convert'
2022-04-25 15:29:44 +08:00
import { shallowRef } from 'vue'
2021-12-16 18:22:47 +08:00
export default {
props: {
2021-12-31 10:40:37 +08:00
entity: Object,
timeFilter: {},
2022-03-27 13:04:47 +08:00
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,
2022-04-25 15:29:44 +08:00
unitConvert,
echartsArray: []
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
}
default: break
}
return className
2021-12-31 10:40:37 +08:00
},
2022-03-27 13:04:47 +08:00
entityType () {
let type
2021-12-31 10:40:37 +08:00
switch (this.entityData.entityType) {
2022-03-27 13:04:47 +08:00
case 'ip': {
type = this.entityData.ipAddr
2021-12-31 10:40:37 +08:00
break
}
2022-03-27 13:04:47 +08:00
case 'domain': {
type = this.entityData.domainName
2021-12-31 10:40:37 +08:00
break
}
2022-03-27 13:04:47 +08:00
case 'app': {
type = this.entityData.appName
2021-12-31 10:40:37 +08:00
break
}
2022-03-27 13:04:47 +08:00
default:
break
2021-12-31 10:40:37 +08:00
}
2022-03-27 13:04:47 +08:00
return type
2021-12-31 10:40:37 +08:00
},
2022-03-27 13:04:47 +08:00
entityName () {
let name
2021-12-31 10:40:37 +08:00
switch (this.entityData.entityType) {
case ('ip'): {
2022-03-27 13:04:47 +08:00
name = this.entity.ipAddr
2021-12-31 10:40:37 +08:00
break
}
case ('domain'): {
2022-03-27 13:04:47 +08:00
name = this.entity.domainName
2021-12-31 10:40:37 +08:00
break
}
case ('app'): {
2022-03-27 13:04:47 +08:00
name = this.entity.appName
2021-12-31 10:40:37 +08:00
break
}
default: break
}
2022-03-27 13:04:47 +08:00
return name
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()
2021-12-31 10:40:37 +08:00
switch (this.entityData.entityType) {
case ('ip'): {
params = {
startTime: this.timeFilter.startTime ? parseInt(this.timeFilter.startTime / 1000) : Math.floor(now / 1000 - 3600),
endTime: this.timeFilter.endTime ? parseInt(this.timeFilter.endTime / 1000) : Math.floor(now / 1000),
ip: this.entityData.ipAddr
2021-12-31 10:40:37 +08:00
}
break
}
case ('domain'): {
params = {
startTime: this.timeFilter.startTime ? parseInt(this.timeFilter.startTime / 1000) : Math.floor(now / 1000 - 3600),
endTime: this.timeFilter.endTime ? parseInt(this.timeFilter.endTime / 1000) : Math.floor(now / 1000),
2021-12-31 10:40:37 +08:00
domain: this.entityData.domainName
}
break
}
case ('app'): {
params = {
startTime: this.timeFilter.startTime ? parseInt(this.timeFilter.startTime / 1000) : Math.floor(now / 1000 - 3600),
endTime: this.timeFilter.endTime ? parseInt(this.timeFilter.endTime / 1000) : Math.floor(now / 1000),
2021-12-31 10:40:37 +08:00
appName: this.entityData.appName
}
break
}
default: break
}
return params
2021-12-16 18:22:47 +08:00
}
},
methods: {
showDetail () {
const { href } = this.$router.resolve({
path: '/entityDetail',
query: {
entityType: this.entityData.entityType,
name: this.entityData.ipAddr || this.entityData.domainName || this.entityData.appName
}
})
window.open(href, '_blank')
2021-12-16 18:22:47 +08:00
},
querySecurity () {
const queryParams = {
startTime: getSecond(this.timeFilter.startTime),
endTime: getSecond(this.timeFilter.endTime)
}
let url
switch (this.entityData.entityType) {
case ('ip'): {
url = api.entityIpDetailSecurity
queryParams.ip = this.entityName
break
}
case ('domain'): {
url = api.entityDomainDetailSecurity
queryParams.domain = this.entityName
break
}
case ('app'): {
url = api.entityAppDetailSecurity
queryParams.appName = this.entityName
break
}
default: break
}
get(url, queryParams).then(response => {
2021-12-31 10:40:37 +08:00
if (response.code === 200) {
2022-03-27 13:04:47 +08:00
this.entityData.securityCount = response.data.result ? response.data.result.length : 0
2021-12-31 10:40:37 +08:00
}
})
2021-12-16 18:22:47 +08:00
},
queryPerformance () {
const queryParams = {
startTime: getSecond(this.timeFilter.startTime),
endTime: getSecond(this.timeFilter.endTime)
}
let url
switch (this.entityData.entityType) {
case ('ip'): {
url = api.entityIpDetailPerformance
queryParams.ip = this.entityName
break
}
case ('domain'): {
url = api.entityDomainDetailPerformance
queryParams.domain = this.entityName
break
}
case ('app'): {
url = api.entityAppDetailPerformance
queryParams.appName = this.entityName
break
}
default: break
}
get(url, queryParams).then(response => {
2021-12-31 10:40:37 +08:00
if (response.code === 200) {
2022-03-27 13:04:47 +08:00
this.entityData.performanceCount = response.data.result ? response.data.result.length : 0
2021-12-31 10:40:37 +08:00
}
})
},
2022-03-27 13:04:47 +08:00
getQueryParams () {
return {
startTime: getSecond(this.timeFilter.startTime),
endTime: getSecond(this.timeFilter.endTime),
appName: this.entityType,
domain: this.entityType,
ip: this.entityType
}
},
queryEntityDetailTraffic () {
get(this.trafficUrl, this.getQueryParams()).then(response => {
if (response.code === 200 && response.data.result && response.data.result.length > 0) {
let sentSeries
let receivedSeries
response.data.result.forEach(t => {
if (t.legend === 'bytesRate') {
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 === 'bytesSentRate') {
this.entityData.bytesSentRate = _.nth(t.values, -3)[1]
sentSeries = {
name: this.$t('entities.sentThroughput'),
type: 'line',
legendHoverLink: false,
2021-12-31 10:40:37 +08:00
itemStyle: {
normal: {
lineStyle: {
2022-03-27 13:04:47 +08:00
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.byte]),
showSymbol: false
2021-12-31 10:40:37 +08:00
}
2022-03-27 13:04:47 +08:00
} else if (t.legend === 'bytesReceivedRate') {
this.entityData.bytesReceivedRate = t.aggregation.last
receivedSeries = {
name: this.$t('entities.receivedThroughput'),
type: 'line',
legendHoverLink: false,
2021-12-31 10:40:37 +08:00
itemStyle: {
normal: {
lineStyle: {
2022-03-27 13:04:47 +08:00
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.byte]),
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
}
}
}).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() })
2021-12-16 18:22:47 +08:00
}
},
2022-03-27 13:04:47 +08:00
watch: {
entityData: {
deep: true,
handler (n) {
if (n.entityType) {
switch (n.entityType) {
case 'ip': {
this.trafficUrl = api.entityIpDetailTraffic
break
}
case 'domain': {
this.trafficUrl = api.entityDomainDetailTraffic
break
}
case 'app': {
this.trafficUrl = api.entityAppDetailTraffic
break
}
default:
break
}
}
}
}
},
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)
2021-12-16 18:22:47 +08:00
this.entityData = _.cloneDeep(this.entity)
2022-03-27 13:04:47 +08:00
setTimeout(() => {
this.querySecurity()
this.queryEntityDetailTraffic()
this.queryPerformance()
})
2022-04-25 15:29:44 +08:00
},
beforeUnmount () {
window.removeEventListener('resize', this.debounceFunc)
2021-12-16 18:22:47 +08:00
}
}