index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <div>
  3. <a-button v-if="!isTemplate" @click="handleRefresh" class="action-btn">
  4. <icon type="refresh" />
  5. </a-button>
  6. <loading-block v-if="loading && !isTemplate" />
  7. <!-- <detail
  8. v-else
  9. :data="data"
  10. :base-info="baseInfo"
  11. :on-manager="onManager"
  12. :showDesc="false"
  13. :hiddenKeys="['id', 'name', 'status', 'project_domain', 'tenant', 'created_at', 'updated_at']" /> -->
  14. <template v-else>
  15. <a-row :gutter="16">
  16. <a-col :span="isTemplate ? 8 : undefined" :md="isTemplate ? undefined : 12" :xl="isTemplate ? undefined : 8" :xxl="isTemplate ? undefined : 6" v-for="obj in baseInfo" :key="obj.field">
  17. <usage-quota :title="obj.title" :value="obj.value()" :is-template="isTemplate" />
  18. </a-col>
  19. </a-row>
  20. <a-row :gutter="16">
  21. <a-col :span="isTemplate ? 12 : undefined" :md="isTemplate ? undefined : 12" :xl="isTemplate ? undefined : 8" :xxl="isTemplate ? undefined : 6" v-for="obj in ringInfo" :key="obj.field">
  22. <usage-ring :title="obj.title" :field="obj.field" :options="obj.options" :is-template="isTemplate" />
  23. </a-col>
  24. </a-row>
  25. </template>
  26. </div>
  27. </template>
  28. <script>
  29. import * as R from 'ramda'
  30. import { USAGE_CONFIG_MAP, USAGE_RING_DATAS } from './constants'
  31. import UsageQuota from './components/UsageQuota'
  32. import UsageRing from './components/UsageRing'
  33. export default {
  34. name: 'CloudenvUsageSidepage',
  35. components: {
  36. UsageQuota,
  37. UsageRing,
  38. },
  39. props: {
  40. // 作为模板中的组件使用
  41. isTemplate: {
  42. type: Boolean,
  43. default: false,
  44. },
  45. scopeParams: {
  46. type: Object,
  47. default: () => ({}),
  48. },
  49. onManager: {
  50. type: Function,
  51. required: true,
  52. },
  53. resId: {
  54. type: String,
  55. required: true,
  56. },
  57. resource: {
  58. type: String,
  59. required: true,
  60. },
  61. },
  62. data () {
  63. return {
  64. data: {},
  65. loading: false,
  66. baseInfo: [],
  67. ringInfo: [],
  68. }
  69. },
  70. created () {
  71. this.fetchData()
  72. },
  73. methods: {
  74. initBaseInfo () {
  75. const isAdminMode = this.isTemplate ? this.scopeParams.scope === 'system' && !this.scopeParams.project_id && !this.scopeParams.domain_id : this.$store.getters.isAdminMode
  76. const isDomainMode = this.isTemplate ? this.scopeParams.scope === 'domain' && !this.scopeParams.project_id : this.$store.getters.isDomainMode
  77. const baseInfo = []
  78. Object.keys(USAGE_CONFIG_MAP).forEach((key) => {
  79. const item = USAGE_CONFIG_MAP[key] || {}
  80. if (!item.noPerfix) {
  81. if (isAdminMode) key = `all.${key}`
  82. if (isDomainMode) key = `domain.${key}`
  83. }
  84. baseInfo.push({
  85. field: key,
  86. title: item.zh_cn || key,
  87. value: () => {
  88. return R.is(Function, item.formatter) ? item.formatter(this.data[key]) : `${this.data[key] || 0} ${item.unit || ''}`
  89. },
  90. })
  91. })
  92. return baseInfo
  93. },
  94. initRingInfo () {
  95. const isAdminMode = this.isTemplate ? this.scopeParams.scope === 'system' && !this.scopeParams.project_id && !this.scopeParams.domain_id : this.$store.getters.isAdminMode
  96. const isDomainMode = this.isTemplate ? this.scopeParams.scope === 'domain' && !this.scopeParams.project_id : this.$store.getters.isDomainMode
  97. const ringInfo = []
  98. const getUnuse = (sum = 0, use = 0) => {
  99. return sum - use
  100. }
  101. USAGE_RING_DATAS.forEach((obj) => {
  102. let key = ''
  103. if (!obj.noPerfix) {
  104. if (isAdminMode) key = 'all'
  105. if (isDomainMode) key = 'domain'
  106. }
  107. const sum = key ? `${key}.${obj.sum}` : obj.sum
  108. const use = key ? `${key}.${obj.use}` : obj.use
  109. ringInfo.push({
  110. field: sum,
  111. title: obj.name,
  112. options: {
  113. label: {
  114. sum: obj.sum_label || this.$t('common_234'),
  115. use: obj.use_label,
  116. unuse: obj.unuse_label,
  117. },
  118. value: {
  119. sum: this.data[sum],
  120. use: this.data[use],
  121. unuse: getUnuse(this.data[sum], this.data[use]),
  122. },
  123. },
  124. })
  125. })
  126. return ringInfo
  127. },
  128. async fetchData () {
  129. try {
  130. this.loading = true
  131. const params = { scope: this.$store.getters.scope }
  132. if (this.isTemplate) {
  133. const { data } = await new this.$Manager('rpc', 'v2').get({
  134. resource: 'rpc',
  135. id: 'usages/general-usage',
  136. params: this.scopeParams,
  137. })
  138. this.data = data
  139. } else {
  140. const { data } = await new this.$Manager('usages', 'v2').getSpecific({
  141. id: this.resource,
  142. spec: this.resId,
  143. params,
  144. })
  145. this.data = data
  146. }
  147. this.baseInfo = this.initBaseInfo()
  148. this.ringInfo = this.initRingInfo()
  149. this.loading = false
  150. } catch (error) {
  151. this.loading = false
  152. }
  153. },
  154. handleRefresh () {
  155. this.fetchData()
  156. },
  157. },
  158. }
  159. </script>
  160. <style lang="less" scoped>
  161. ::v-deep .detail-item {
  162. .detail-item-title {
  163. width: 220px !important;
  164. }
  165. .detail-item-value {
  166. margin-left: 220px;
  167. }
  168. }
  169. .action-btn {
  170. margin-bottom: 10px;
  171. color: rgba(0, 0, 0, 0.65);
  172. &:hover {
  173. color: #40a9ff;
  174. }
  175. }
  176. </style>