index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <div class="mb-3" v-if="isShow">
  3. <a-alert type="info" class="mt-2">
  4. <div slot="message" class="d-flex">
  5. <div>{{ $t('common_567') }}:</div>
  6. <div>
  7. <a-tag
  8. v-for="(item, index) in statusErrorOpts"
  9. :key="index"
  10. :color="item.color"
  11. class="oc-pointer"
  12. @click="chooseStatusHandle(item)">
  13. {{ item.text}}({{ item.num }})
  14. </a-tag>
  15. </div>
  16. </div>
  17. </a-alert>
  18. </div>
  19. </template>
  20. <script>
  21. import { mapGetters } from 'vuex'
  22. import { uniqueOccurrences, uuid } from '@/utils/utils'
  23. export default {
  24. name: 'ServerErrorStatusTab',
  25. props: {
  26. resource: {
  27. type: String,
  28. required: true,
  29. },
  30. status: {
  31. type: String,
  32. required: true,
  33. },
  34. },
  35. data () {
  36. return {
  37. statusMap: {},
  38. statusErrorOpts: [],
  39. statusArr: [],
  40. isFirstLoad: true,
  41. }
  42. },
  43. computed: {
  44. ...mapGetters(['scope']),
  45. isShow () {
  46. const opts = this.statusErrorOpts.filter((item) => { return /fail/.test(item.val) })
  47. const sum = opts.reduce(function (acc, cur) {
  48. return acc + cur.num
  49. }, 0)
  50. return sum > 0
  51. },
  52. isHost () {
  53. return this.resource === 'hosts'
  54. },
  55. },
  56. created () {
  57. this.loadServersData()
  58. this.$bus.$on('ServerFilterChange', (val) => {
  59. const statusArr = val.status || []
  60. this.statusErrorOpts = this.statusErrorOpts.map((item) => {
  61. return {
  62. ...item,
  63. checked: statusArr.includes(item.val),
  64. color: statusArr.includes(item.val) ? 'red' : '',
  65. }
  66. })
  67. })
  68. },
  69. methods: {
  70. loadServersData () {
  71. const m = new this.$Manager(this.resource)
  72. const params = {
  73. scope: this.scope,
  74. limit: 0,
  75. $t: uuid(),
  76. }
  77. if (this.isHost) {
  78. params.baremetal = false
  79. } else {
  80. params.filter = 'hypervisor.notin(baremetal,container)'
  81. }
  82. m.list({ params }).then((res) => {
  83. const { data } = res.data
  84. const statusArr = data.map((item) => { return item.status })
  85. const statusMap = uniqueOccurrences(statusArr)
  86. this.statusErrorOpts = []
  87. this.statusArr = []
  88. for (const k in statusMap) {
  89. this.statusArr.push(k)
  90. if (statusMap.hasOwnProperty(k) && /fail/.test(k)) {
  91. const num = statusMap[k]
  92. this.statusErrorOpts.push({
  93. color: '',
  94. text: this.$t(`status.${this.status}.${k}`),
  95. num: num,
  96. val: k,
  97. checked: false,
  98. })
  99. }
  100. }
  101. const statusErrorOpts = this.statusErrorOpts.map((item) => {
  102. return {
  103. ...item,
  104. num: statusMap[item.val] || 0,
  105. }
  106. })
  107. this.statusErrorOpts = statusErrorOpts
  108. const statusCheckArr = [...statusErrorOpts].filter((item) => { return item.checked && item.num > 0 }).map((item) => { return item.val })
  109. this.$emit('getStatusCheckArr', statusCheckArr, this.statusArr, this.isFirstLoad)
  110. }).catch((err) => {
  111. console.log(err)
  112. throw err
  113. })
  114. },
  115. chooseStatusHandle (item) {
  116. this.isFirstLoad = false
  117. if (!item.checked) {
  118. item.checked = true
  119. item.color = 'red'
  120. } else {
  121. item.checked = false
  122. item.color = ''
  123. }
  124. const statusCheckArr = [...this.statusErrorOpts].filter((item) => { return item.checked && item.num > 0 }).map((item) => { return item.val })
  125. this.$emit('getStatusCheckArr', statusCheckArr, this.statusArr, this.isFirstLoad)
  126. },
  127. },
  128. }
  129. </script>