SummaryCards.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <div>
  3. <a-icon type="sync" spin v-if="loading" />
  4. <a-row v-else-if="cards.length > 0" type="flex" style="margin-left: 128px;">
  5. <a-col v-for="card in cards" :key="card.title" :span="8" style="width: 400px" class="mt-4">
  6. <overview-summary-card :card="card" @resourceClick="handleResClick" />
  7. </a-col>
  8. </a-row>
  9. <a-row v-else type="flex" style="justify-content:center">
  10. <data-empty :description="emptyContent" />
  11. </a-row>
  12. </div>
  13. </template>
  14. <script>
  15. import i18n from '@/locales'
  16. import OverviewSummaryCard from '../../../components/MonitorCard/sections/card'
  17. const cardsTypeMap = {
  18. guest: {
  19. icon: 'res-vminstance',
  20. index: 1,
  21. unit: i18n.t('common_62'),
  22. },
  23. host: {
  24. icon: 'res-host',
  25. index: 2,
  26. unit: i18n.t('common_62'),
  27. },
  28. cloudaccount: {
  29. icon: 'res-cloudaccount',
  30. index: 3,
  31. unit: i18n.t('dashboard.each'),
  32. },
  33. oss: {
  34. icon: 'res-bucket',
  35. index: 4,
  36. unit: i18n.t('dashboard.each'),
  37. },
  38. storage: {
  39. icon: 'res-blockstorage',
  40. index: 5,
  41. unit: i18n.t('compute.text_131'),
  42. },
  43. rds: {
  44. icon: 'res-rds',
  45. index: 6,
  46. unit: i18n.t('dashboard.each'),
  47. },
  48. redis: {
  49. icon: 'res-redis',
  50. index: 7,
  51. unit: i18n.t('dashboard.each'),
  52. },
  53. }
  54. export default {
  55. name: 'SummaryCards',
  56. components: { OverviewSummaryCard },
  57. props: {
  58. scope: {
  59. type: String,
  60. },
  61. scopeId: {
  62. type: String,
  63. },
  64. },
  65. data () {
  66. return {
  67. manager: new this.$Manager('monitorresources', 'v1'),
  68. loading: false,
  69. data: {},
  70. emptyContent: {
  71. default: this.$t('common.notData'),
  72. },
  73. }
  74. },
  75. computed: {
  76. cards () {
  77. const cards = []
  78. for (const k in this.data) {
  79. const items = Object.assign({ alerting: 0, attach: 0, init: 0 }, this.data[k])
  80. const total = items.total || 0
  81. delete items.total
  82. // 项目视图不需要展示宿主机信息
  83. if (this.scope === 'project' && k === 'host') {
  84. continue
  85. }
  86. if (total === 0) {
  87. continue
  88. }
  89. const icon = cardsTypeMap[k]?.icon || `res-${k}`
  90. cards.push({
  91. title: this.$t(`dictionary.${k}`),
  92. icon: icon,
  93. total: total,
  94. items: items,
  95. index: cardsTypeMap[k]?.index || 10,
  96. unit: cardsTypeMap[k]?.unit,
  97. resType: k,
  98. })
  99. }
  100. cards.sort((a, b) => { // 虚拟机和宿主机优先展示
  101. return a.index - b.index
  102. })
  103. return cards
  104. },
  105. },
  106. watch: {
  107. scopeId (val, oldVal) {
  108. if (val !== oldVal) {
  109. this.fetchData()
  110. }
  111. },
  112. },
  113. created () {
  114. this.fetchData()
  115. },
  116. methods: {
  117. async fetchData () {
  118. this.loading = true
  119. try {
  120. const params = { scope: this.scope }
  121. if (this.scope === 'project' && this.scopeId) {
  122. params.project = this.scopeId
  123. }
  124. if (this.scope === 'domain' && this.scopeId) {
  125. params.domain = this.scopeId
  126. }
  127. const res = await this.manager.get({
  128. id: 'alert',
  129. params,
  130. })
  131. this.data = res.data
  132. this.loading = false
  133. } catch (e) {
  134. this.loading = false
  135. throw e
  136. }
  137. },
  138. handleResClick (res) {
  139. if (['guest', 'host'].includes(res.resType)) {
  140. this.$router.push({
  141. path: `/monitorresources-${res.resType}`,
  142. query: { defaultFilter: { alert_state: [res.alert_state] } },
  143. })
  144. }
  145. },
  146. },
  147. }
  148. </script>
  149. <style scoped>
  150. </style>