resStatisticsMixin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import debounce from 'lodash/debounce'
  2. import ResStatusTab from '@/sections/ResStatusTab'
  3. import { arrayToObj, sizestr } from '@/utils/utils'
  4. export default {
  5. components: {
  6. ResStatusTab,
  7. },
  8. data () {
  9. return {
  10. statisticsLoading: false,
  11. filterParams: {},
  12. statusOpts: [],
  13. statusArr: [],
  14. errorFilterStatus: [],
  15. otherFilterStatus: [],
  16. tableOverviewIndexs: [],
  17. }
  18. },
  19. created () {
  20. this.fetchResStatistics = debounce(this.fetchResStatisticsDebounce, 2000)
  21. this.$bus.$off('ListParamsChange')
  22. this.$bus.$on('ListParamsChange', (params) => {
  23. this.tableOverviewIndexs = []
  24. this.resetStatusOpts()
  25. if (params && params.details) {
  26. this.fetchResStatistics(params)
  27. }
  28. })
  29. },
  30. methods: {
  31. fetchResStatisticsDebounce (params = {}) {
  32. this.errorFilterStatus = []
  33. this.otherFilterStatus = []
  34. const m = new this.$Manager(`${this.resStaticsResource}/statistics`, this.apiVersion ? this.apiVersion : 'v2')
  35. this.statisticsLoading = true
  36. m.list({ params }).then(res => {
  37. const statusObj = arrayToObj(res.data.status_info || [], 'status')
  38. this.statusOpts = this.getStatusOpts(statusObj)
  39. this.statusArr = Object.keys(statusObj)
  40. this.generateTableOverviewIndexs(res.data)
  41. }).catch(() => {
  42. // console.error(err)
  43. this.statusOpts = []
  44. }).finally(() => {
  45. this.statisticsLoading = false
  46. })
  47. },
  48. getStatusOpts (data) {
  49. const { ready = {}, running = {} } = data
  50. // 统计
  51. let total = 0
  52. let error = 0
  53. let other = 0
  54. for (const k in data) {
  55. total += data[k].total_count
  56. if (new RegExp('fail').test(k)) {
  57. this.errorFilterStatus.push(k)
  58. error += data[k].total_count
  59. } else {
  60. if (!['running', 'ready'].includes(k)) {
  61. this.otherFilterStatus.push(k)
  62. other += data[k].total_count
  63. }
  64. }
  65. }
  66. const statusOpts = [
  67. { title: this.$t('compute.text_576'), type: 'total', num: total },
  68. { title: this.$t('compute.text_574'), type: 'running', num: running?.total_count || 0 },
  69. { title: this.$t('compute.text_273'), type: 'ready', num: ready?.total_count || 0 },
  70. { title: this.$t('common_623', [this.$t('scope.text_61')]), type: 'error', num: error },
  71. { title: this.$t('compute.text_674'), type: 'other', num: other },
  72. ]
  73. return statusOpts
  74. },
  75. statusClickHandle (obj) {
  76. let statusCheckArr = []
  77. if (obj.num > 0) {
  78. switch (obj.type) {
  79. case 'total':
  80. statusCheckArr = []
  81. break
  82. case 'error':
  83. statusCheckArr = [...this.errorFilterStatus]
  84. break
  85. case 'other':
  86. statusCheckArr = [...this.otherFilterStatus]
  87. break
  88. default:
  89. statusCheckArr = [obj.type]
  90. }
  91. this.filterParams = {
  92. statusCheckArr,
  93. statusArr: this.statusArr,
  94. }
  95. }
  96. },
  97. generateTableOverviewIndexs (resData) {
  98. const tableOverviewIndexs = []
  99. Object.keys(resData).forEach(v => {
  100. switch (v) {
  101. case 'total_cpu_count':
  102. tableOverviewIndexs.push({ key: 'CPU', value: `${resData[v]}${this.$t('common_60')}`, order: 1 })
  103. break
  104. case 'total_mem_size_mb':
  105. tableOverviewIndexs.push({ key: this.$t('compute.text_369'), value: `${sizestr(resData[v], 'M', 1024)}`, order: 2 })
  106. break
  107. case 'total_disk_size_mb':
  108. tableOverviewIndexs.push({ key: this.$t('compute.text_99'), value: sizestr(resData[v], 'M', 1024), order: 3 })
  109. break
  110. }
  111. })
  112. this.tableOverviewIndexs = tableOverviewIndexs.sort((a, b) => a.order - b.order)
  113. },
  114. resetStatusOpts () {
  115. if (this.statusOpts) {
  116. this.statusOpts.forEach(item => {
  117. item.num = 0
  118. })
  119. }
  120. },
  121. },
  122. }