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/charts2/charts/npm/RelatedSessions.vue

144 lines
5.0 KiB
Vue
Raw Normal View History

2022-08-11 15:49:41 +08:00
<template>
<div class="npm-sessions">
2022-11-18 15:18:05 +08:00
<div class="npm-sessions-title">
{{$t('npm.relatedSessions')}}
<chart-error v-if="showError" small :content="errorMsg" />
</div>
<div class="npm-sessions-div">
<div class="npm-sessions-div-green" id="green" v-show="clientSessions > 0"></div>
<div class="npm-sessions-div-red" id="red" v-show="serverSessions > 0"></div>
2022-11-18 15:18:05 +08:00
<div class="npm-sessions-div-gray" id="gray" v-show="showError || isNoData"></div>
</div>
<div class="npm-sessions-body">
<div class="npm-sessions-body-left">
<div class="npm-sessions-as-clients right">
<div class="npm-sessions-as-client">
<div class="npm-sessions-as-client-green"></div>&nbsp;
<div class="npm-sessions-as-client-i18n">{{$t('npm.asClient')}}</div>
</div>
<div class="npm-sessions-as-client-percent">{{sessionData.clientSessions}}</div>
</div>
<div class="npm-sessions-as-clients">
<div class="npm-sessions-as-client">
<div class="npm-sessions-as-client-red"></div>&nbsp;
<div class="npm-sessions-as-client-i18n">{{$t('npm.asServer')}}</div>
</div>
<div class="npm-sessions-as-client-percent">{{sessionData.serverSessions}}</div>
</div>
</div>
<div class="npm-sessions-body-right">
<span class="npm-sessions-Progress-number">{{unitConvert(sessionData.sessionsRate, unitTypes.number)[0]}}</span>&nbsp;<span class="npm-sessions-Progress-unit">{{unitConvert(sessionData.sessionsRate, unitTypes.number)[1]}}Sessions/s</span>
</div>
</div>
</div>
2022-08-11 15:49:41 +08:00
</template>
<script>
import { get } from '@/utils/http'
import { api } from '@/utils/api'
import { getSecond } from '@/utils/date-util'
import unitConvert from '@/utils/unit-convert'
import { unitTypes } from '@/utils/constants'
import chartMixin from '@/views/charts2/chart-mixin'
2022-11-18 15:18:05 +08:00
import ChartError from '@/components/common/Error'
2022-08-11 15:49:41 +08:00
export default {
name: 'RelatedSessions',
2022-11-18 15:18:05 +08:00
components: { ChartError },
mixins: [chartMixin],
data () {
return {
sessionData: {},
unitConvert,
unitTypes,
clientSessions: 0,
2022-11-18 15:18:05 +08:00
serverSessions: 0,
showError: false,
errorMsg: '',
noData: false,
isNoData: false
}
},
2022-08-24 17:09:42 +08:00
watch: {
timeFilter: {
handler () {
2022-08-24 17:09:42 +08:00
this.relatedSessionsData()
}
}
},
methods: {
relatedSessionsData () {
const conditionStr = this.$route.query.queryCondition ? this.$route.query.queryCondition : ''
const condition = conditionStr.split(/["|'](.*?)["|']/)
const params = {
startTime: getSecond(this.timeFilter.startTime),
endTime: getSecond(this.timeFilter.endTime),
ip: condition[1]
}
const divGreen = document.getElementById('green')
2022-11-18 15:18:05 +08:00
const divRed = document.getElementById('red')
const divGray = document.getElementById('gray')
get(api.npm.overview.relatedSessions, params).then(res => {
if (res.code === 200) {
2022-11-18 15:18:05 +08:00
this.showError = false
this.isNoData = false
this.sessionData = res.data.result
const client = this.sessionData.clientSessions
const server = this.sessionData.serverSessions
// 如果返回数据为-,则不必计算比例
if (!isNaN(client) && !isNaN(server)) {
this.clientSessions = client / (client + server)
this.serverSessions = server / (client + server)
this.sessionData.clientSessions = unitConvert(this.clientSessions, unitTypes.percent).join('')
this.sessionData.serverSessions = unitConvert(this.serverSessions, unitTypes.percent).join('')
if (this.clientSessions === 1) {
divGreen.style.borderRadius = 4 + 'px'
} else if (this.serverSessions === 1) {
divRed.style.borderRadius = 4 + 'px'
} else if (this.clientSessions === 0 && this.serverSessions === 0) {
this.isNoData = true
divGray.style.borderRadius = 4 + 'px'
divGray.style.width = '100%'
}
divGreen.style.width = this.sessionData.clientSessions
divRed.style.width = this.sessionData.serverSessions
} else {
2022-11-18 15:18:05 +08:00
this.isNoData = true
this.changeByErrorOrNodata()
}
2022-11-18 15:18:05 +08:00
} else {
this.showError = true
this.errorMsg = res.message
this.changeByErrorOrNodata()
}
2022-11-18 15:18:05 +08:00
}).catch(error => {
this.showError = true
this.errorMsg = error.message
this.changeByErrorOrNodata()
}).finally(() => {
this.toggleLoading(false)
})
},
/**
* 当无数据或者报错时改变界面样式出现灰条
*/
changeByErrorOrNodata () {
const divGray = document.getElementById('gray')
divGray.style.borderRadius = 4 + 'px'
divGray.style.width = '100%'
this.sessionData.clientSessions = '—'
this.sessionData.serverSessions = '—'
this.clientSessions = 0
this.serverSessions = 0
}
},
mounted () {
this.relatedSessionsData()
}
2022-08-11 15:49:41 +08:00
}
</script>