K8s.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <div class="h-100 position-relative">
  3. <div class="dashboard-card-wrap">
  4. <div class="dashboard-card-header">
  5. <div class="dashboard-card-header-left">{{ form.fd.name || $t('dashboard.text_6') }}<a-icon class="ml-2" type="loading" v-if="loading" /></div>
  6. <div class="dashboard-card-header-right"><slot name="actions" :handle-edit="handleEdit" /></div>
  7. </div>
  8. <div class="dashboard-card-body d-flex align-items-center justify-content-center">
  9. <div class="d-flex align-items-baseline">
  10. <div class="number-card-number mr-1">{{ this.usage.usage }}</div>
  11. <div class="number-card-unit">{{ this.usage.unit }}</div>
  12. </div>
  13. </div>
  14. </div>
  15. <base-drawer class="drawer-wrapper" @update:visible="updateVisible" :visible="visible" :title="$t('dashboard.text_5')" @ok="handleSubmit" @cancel="cancel">
  16. <slot />
  17. <a-form
  18. hideRequiredMark
  19. :form="form.fc"
  20. v-bind="formItemLayout">
  21. <a-form-item :label="$t('dashboard.text_6')">
  22. <a-input v-decorator="decorators.name" />
  23. </a-form-item>
  24. <k8s-config :fc="form.fc" :decorators="decorators" :usage-label="$t('dashboard.text_20')" @update:usage_key="setDefaultName" />
  25. <a-form-item v-if="canShowUnitConfig" :label="$t('common_250')">
  26. <base-select
  27. v-decorator="decorators.unit"
  28. isDefaultSelect
  29. :filterable="true"
  30. :options="unitOpts"
  31. :select-props="{ placeholder: $t('common_618') }" />
  32. </a-form-item>
  33. </a-form>
  34. </base-drawer>
  35. </div>
  36. </template>
  37. <script>
  38. import _ from 'lodash'
  39. import BaseDrawer from '@Dashboard/components/BaseDrawer'
  40. import { K8S_USAGE_CONFIG } from '@Dashboard/constants'
  41. import { load } from '@Dashboard/utils/cache'
  42. import { getRequestT } from '@/utils/utils'
  43. import K8sConfig from '@Dashboard/sections/K8sConfig'
  44. import mixin from './mixin'
  45. export default {
  46. name: 'NumberCardK8s',
  47. components: {
  48. BaseDrawer,
  49. K8sConfig,
  50. },
  51. mixins: [mixin],
  52. data () {
  53. const initialNameValue = ((this.params && this.params.type === 'k8s') && this.params.name) || this.$t('dashboard.text_21')
  54. const initialUsageKeyValue = ((this.params && this.params.type === 'k8s') && this.params.usage_key) || ''
  55. const initUnitValue = (this.params && this.params.unit) || 'auto'
  56. return {
  57. data: {},
  58. loading: false,
  59. form: {
  60. fc: this.$form.createForm(this),
  61. fd: {
  62. name: initialNameValue,
  63. usage_key: initialUsageKeyValue,
  64. },
  65. fi: {
  66. nameTouched: false,
  67. },
  68. },
  69. decorators: {
  70. name: [
  71. 'name',
  72. {
  73. initialValue: initialNameValue,
  74. rules: [
  75. { required: true, message: this.$t('dashboard.text_8') },
  76. ],
  77. },
  78. ],
  79. usage_key: [
  80. 'usage_key',
  81. {
  82. initialValue: initialUsageKeyValue,
  83. rules: [
  84. { required: true, message: this.$t('dashboard.text_22') },
  85. ],
  86. },
  87. ],
  88. unit: [
  89. 'unit',
  90. {
  91. initialValue: initUnitValue,
  92. },
  93. ],
  94. },
  95. unitOpts: [
  96. { id: 'auto', name: this.$t('common_563') },
  97. { id: 'K', name: 'KB' },
  98. { id: 'M', name: 'MB' },
  99. { id: 'G', name: 'GB' },
  100. { id: 'T', name: 'TB' },
  101. ],
  102. currentUsageKey: initialUsageKeyValue,
  103. }
  104. },
  105. computed: {
  106. usage () {
  107. const usage = this.usageNumber
  108. const ret = {
  109. usage,
  110. }
  111. const config = K8S_USAGE_CONFIG[this.form.fd.usage_key]
  112. if (config && config.formatter) {
  113. const fVal = config.formatter(usage)
  114. const fValArr = fVal.split(' ')
  115. ret.usage = fValArr[0]
  116. ret.unit = fValArr[1]
  117. }
  118. if (config && config.unit) {
  119. ret.unit = config.unit
  120. }
  121. // 使用用户配置的单位
  122. if (config && config.userUnitFormatter) {
  123. if (this.params && this.params.unit && this.params.unit !== 'auto') {
  124. const fVal = config.userUnitFormatter(usage, this.params.unit)
  125. const fValArr = fVal.split(' ')
  126. ret.usage = fValArr[0]
  127. ret.unit = fValArr[1]
  128. }
  129. }
  130. return ret
  131. },
  132. usageNumber () {
  133. return (this.data && _.get(this.data, this.form.fd.usage_key)) || 0
  134. },
  135. canShowUnitConfig () {
  136. const config = K8S_USAGE_CONFIG[this.currentUsageKey]
  137. if (config && config.canUseUserUnit) {
  138. return true
  139. }
  140. return false
  141. },
  142. },
  143. watch: {
  144. 'form.fd' (val) {
  145. this.fetchUsage()
  146. for (const key in this.decorators) {
  147. let config = this.decorators[key][1] || {}
  148. config = {
  149. ...config,
  150. initialValue: val[key],
  151. }
  152. this.decorators[key][1] = config
  153. }
  154. },
  155. },
  156. created () {
  157. if (this.params && this.params.type === 'k8s') {
  158. this.form.fd = this.params
  159. }
  160. this.$bus.$on('DashboardCardRefresh', args => {
  161. this.fetchUsage()
  162. }, this)
  163. },
  164. methods: {
  165. refresh () {
  166. return this.fetchUsage()
  167. },
  168. genUsageParams () {
  169. const params = {
  170. scope: this.$store.getters.scope,
  171. $t: getRequestT(),
  172. }
  173. return params
  174. },
  175. async fetchUsage () {
  176. this.loading = true
  177. try {
  178. const params = this.genUsageParams()
  179. const data = await load({
  180. res: 'usages',
  181. actionArgs: {
  182. url: '/v2/rpc/usages/k8s-usage',
  183. method: 'GET',
  184. params,
  185. },
  186. useManager: false,
  187. resPath: 'data',
  188. })
  189. this.data = data
  190. } finally {
  191. this.loading = false
  192. }
  193. },
  194. handleEdit () {
  195. this.updateVisible(true)
  196. },
  197. updateVisible (val) {
  198. this.$emit('update:visible', val)
  199. },
  200. async handleSubmit () {
  201. try {
  202. const values = await this.form.fc.validateFields()
  203. this.form.fd = values
  204. this.$emit('update', this.options.i, { type: 'k8s', ...values })
  205. this.updateVisible(false)
  206. } catch (error) {
  207. throw error
  208. }
  209. },
  210. },
  211. }
  212. </script>
  213. <style lang="less" scoped>
  214. .number-card-number {
  215. font-size: 50px;
  216. color: #000;
  217. }
  218. .number-card-unit {
  219. font-size: 14px;
  220. color: #4F4B53;
  221. margin-left: 5px;
  222. }
  223. .drawer-wrapper {
  224. &::v-deep.ant-drawer.ant-drawer-open .ant-drawer-mask {
  225. animation: none;
  226. }
  227. }
  228. </style>