Server.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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" @click.alt="showDebuggerInfo = !showDebuggerInfo">{{ form.fd.name || $t('dashboard.text_6') }}
  6. <a-icon class="ml-2" type="loading" v-if="loading" />
  7. <a-tooltip v-if="isServersAnypool"><template slot="title">{{ $t('dashboard.server_tips') }}</template><icon type="help" /></a-tooltip>
  8. </div>
  9. <div class="dashboard-card-header-right">
  10. <span v-if="showDebuggerInfo">{{ `${$t('dashboard.text_20')}: ${form.fd.usage_key}` }}</span>
  11. <slot name="actions" :handle-edit="handleEdit" />
  12. <a class="ml-2" :style="{ color: isResDeny ? '#ccc' : '' }" v-if="!edit && canShowEdit && !isPageDeny" @click="goPage">
  13. <icon type="arrow-right" style="font-size:18px" />
  14. </a>
  15. </div>
  16. </div>
  17. <div class="dashboard-card-body d-flex align-items-center justify-content-center">
  18. <div class="d-flex">
  19. <div class="number-card-number mr-1">{{ this.usage.usage }}</div>
  20. <div class="number-card-unit">{{ this.usage.unit }}</div>
  21. </div>
  22. </div>
  23. </div>
  24. <base-drawer class="drawer-wrapper" @update:visible="updateVisible" :visible="visible" :title="$t('dashboard.text_5')" @ok="handleSubmit" @cancel="cancel">
  25. <a-form
  26. hideRequiredMark
  27. :form="form.fc"
  28. v-bind="formItemLayout">
  29. <slot />
  30. <a-form-item :label="$t('dashboard.text_6')">
  31. <a-input v-decorator="decorators.name" />
  32. </a-form-item>
  33. <quota-config :fc="form.fc" :decorators="decorators" :usage-label="$t('dashboard.text_20')" @update:usage_key="setDefaultName" />
  34. <a-form-item v-if="canShowUnitConfig" :label="$t('common_250')">
  35. <base-select
  36. v-decorator="decorators.unit"
  37. isDefaultSelect
  38. :filterable="true"
  39. :options="unitOpts"
  40. :select-props="{ placeholder: $t('common_618') }" />
  41. </a-form-item>
  42. </a-form>
  43. </base-drawer>
  44. </div>
  45. </template>
  46. <script>
  47. import { mapGetters } from 'vuex'
  48. import BaseDrawer from '@Dashboard/components/BaseDrawer'
  49. import QuotaConfig from '@Dashboard/sections/QuotaConfig'
  50. import { USAGE_CONFIG } from '@Dashboard/constants'
  51. import { load } from '@Dashboard/utils/cache'
  52. import { getRequestT } from '@/utils/utils'
  53. import { hasPermission } from '@/utils/auth'
  54. import routes from '@/router/routes'
  55. import mixin from './mixin'
  56. export default {
  57. name: 'NumberCardServer',
  58. components: {
  59. BaseDrawer,
  60. QuotaConfig,
  61. },
  62. mixins: [mixin],
  63. props: {
  64. parmas: Object,
  65. edit: Boolean,
  66. },
  67. data () {
  68. const usage_key = this.$store.getters.isAdminMode ? 'all.servers' : this.$store.getters.isDomainMode ? 'domain.servers' : 'servers'
  69. const initialUsageKeyValue = ((this.params && this.params.type !== 'k8s') && this.params.usage_key) || usage_key
  70. const initialNameValue = ((this.params && this.params.type !== 'k8s') && this.params.name) || this.$t('usage')[initialUsageKeyValue]
  71. const initialRegionAccountType = ((this.params && this.params.type !== 'k8s') && this.params.regionAccountType) || 'region'
  72. const initUnitValue = (this.params && this.params.unit) || 'auto'
  73. return {
  74. data: {},
  75. loading: false,
  76. form: {
  77. fc: this.$form.createForm(this),
  78. fd: {
  79. name: initialNameValue,
  80. usage_key: initialUsageKeyValue,
  81. },
  82. fi: {
  83. nameTouched: false,
  84. },
  85. },
  86. decorators: {
  87. name: [
  88. 'name',
  89. {
  90. initialValue: initialNameValue,
  91. rules: [
  92. { required: true, message: this.$t('dashboard.text_8') },
  93. ],
  94. },
  95. ],
  96. cloud_env: [
  97. 'cloud_env',
  98. {
  99. initialValue: this.params && this.params.cloud_env,
  100. },
  101. ],
  102. brand: [
  103. 'brand',
  104. {
  105. initialValue: this.params && this.params.brand,
  106. },
  107. ],
  108. region: [
  109. 'region',
  110. {
  111. initialValue: this.params && this.params.region,
  112. },
  113. ],
  114. regionAccountType: [
  115. 'regionAccountType',
  116. {
  117. initialValue: initialRegionAccountType,
  118. },
  119. ],
  120. account: [
  121. 'account',
  122. {
  123. initialValue: this.params && this.params.account,
  124. },
  125. ],
  126. usage_key: [
  127. 'usage_key',
  128. {
  129. initialValue: initialUsageKeyValue,
  130. rules: [
  131. { required: true, message: this.$t('dashboard.text_22') },
  132. ],
  133. },
  134. ],
  135. unit: [
  136. 'unit',
  137. {
  138. initialValue: initUnitValue,
  139. },
  140. ],
  141. },
  142. showDebuggerInfo: false,
  143. unitOpts: [
  144. { id: 'auto', name: this.$t('common_563') },
  145. { id: 'K', name: 'KB' },
  146. { id: 'M', name: 'MB' },
  147. { id: 'G', name: 'GB' },
  148. { id: 'T', name: 'TB' },
  149. ],
  150. currentUsageKey: initialUsageKeyValue,
  151. }
  152. },
  153. computed: {
  154. ...mapGetters(['permission']),
  155. usage () {
  156. const usage = (this.data && this.data[this.form.fd.usage_key]) || 0
  157. const ret = {
  158. usage,
  159. }
  160. const config = USAGE_CONFIG[this.form.fd.usage_key]
  161. // 使用默认单位
  162. if (config && config.formatter) {
  163. const fVal = config.formatter(usage)
  164. const fValArr = fVal.split(' ')
  165. ret.usage = fValArr[0]
  166. ret.unit = fValArr[1]
  167. }
  168. if (config && config.unit) {
  169. ret.unit = config.unit
  170. }
  171. // 使用用户配置的单位
  172. if (config && config.userUnitFormatter) {
  173. if (this.params && this.params.unit && this.params.unit !== 'auto') {
  174. const fVal = config.userUnitFormatter(usage, this.params.unit)
  175. const fValArr = fVal.split(' ')
  176. ret.usage = fValArr[0]
  177. ret.unit = fValArr[1]
  178. }
  179. }
  180. return ret
  181. },
  182. isServersAnypool () {
  183. if (!this.params) return false
  184. return ['all.servers.any_pool', 'domain.servers.any_pool', 'servers.any_pool', 'all.servers', 'domain.servers', 'servers'].includes(this.params.usage_key)
  185. },
  186. canShowEdit () {
  187. if (!this.params) return false
  188. return this.getPageUrl()
  189. },
  190. canShowUnitConfig () {
  191. const config = USAGE_CONFIG[this.currentUsageKey]
  192. if (config && config.canUseUserUnit) {
  193. return true
  194. }
  195. return false
  196. },
  197. isResDeny () {
  198. const usage_key = this.params?.usage_key || ''
  199. if (this.isContainerHostUsageKey(usage_key)) {
  200. return !hasPermission({ key: 'servers_list', permissionData: this.permission })
  201. }
  202. if (usage_key.endsWith('servers')) {
  203. return !hasPermission({ key: 'servers_list', permissionData: this.permission })
  204. } else if (usage_key.endsWith('hosts') || usage_key.endsWith('baremetals')) {
  205. return !hasPermission({ key: 'hosts_list', permissionData: this.permission })
  206. } else if (usage_key.endsWith('isolated_devices')) {
  207. return !hasPermission({ key: 'isolated_devices_list', permissionData: this.permission })
  208. }
  209. return false
  210. },
  211. isPageDeny () {
  212. const path = this.getPageUrl()
  213. if (!path) return false
  214. const target = routes.filter(item => item.path === path)
  215. if (target.length && target[0].meta?.hidden) {
  216. return target[0].meta.hidden()
  217. }
  218. return false
  219. },
  220. },
  221. watch: {
  222. 'form.fd' (val) {
  223. this.fetchUsage()
  224. for (const key in this.decorators) {
  225. let config = this.decorators[key][1] || {}
  226. config = {
  227. ...config,
  228. initialValue: val[key],
  229. }
  230. this.decorators[key][1] = config
  231. }
  232. },
  233. },
  234. created () {
  235. if (this.params && this.params.type !== 'k8s') {
  236. this.form.fd = this.params
  237. } else if (this.form.fd.usage_key) {
  238. this.$emit('update', this.options.i, this.form.fd)
  239. this.setDefaultName(this.form.fd.usage_key)
  240. this.refresh()
  241. }
  242. this.$bus.$on('DashboardCardRefresh', args => {
  243. this.fetchUsage()
  244. }, this)
  245. },
  246. methods: {
  247. isPendingDeleteContainerUsageKey (uk) {
  248. if (!uk) return false
  249. return uk.split('.').includes('pending_delete_containers')
  250. },
  251. isContainerHostUsageKey (uk) {
  252. if (!uk) return false
  253. const segs = uk.split('.')
  254. return segs.includes('containers') || segs.includes('running_containers') || segs.includes('ready_containers') || segs.includes('pending_delete_containers')
  255. },
  256. refresh () {
  257. return this.fetchUsage()
  258. },
  259. genUsageParams () {
  260. const params = {
  261. scope: this.$store.getters.scope,
  262. $t: getRequestT(),
  263. }
  264. const fd = this.form.fd
  265. if (fd.cloud_env) params.cloud_env = fd.cloud_env
  266. if (fd.region) {
  267. params.range_type = 'cloudregions'
  268. params.range_id = fd.region
  269. }
  270. if (fd.account) {
  271. params.range_type = 'cloudaccounts'
  272. params.range_id = fd.account
  273. }
  274. if (fd.brand) params.brand = fd.brand
  275. return params
  276. },
  277. async fetchUsage () {
  278. this.loading = true
  279. try {
  280. const params = this.genUsageParams()
  281. const data = await load({
  282. res: 'usages',
  283. action: 'rpc',
  284. actionArgs: {
  285. methodname: 'getGeneralUsage',
  286. params,
  287. },
  288. resPath: 'data',
  289. })
  290. this.data = data
  291. } finally {
  292. this.loading = false
  293. }
  294. },
  295. handleEdit (val) {
  296. this.updateVisible(true)
  297. },
  298. updateVisible (val) {
  299. this.$emit('update:visible', val)
  300. },
  301. async handleSubmit () {
  302. try {
  303. const values = await this.form.fc.validateFields()
  304. this.form.fd = values
  305. this.$emit('update', this.options.i, values)
  306. this.updateVisible(false)
  307. } catch (error) {
  308. throw error
  309. }
  310. },
  311. goPage () {
  312. if (!this.params || this.isResDeny) return
  313. const path = this.getPageUrl()
  314. if (path) this.$router.push(path)
  315. },
  316. getPageUrl () {
  317. const uk = this.params.usage_key
  318. if (uk && this.isPendingDeleteContainerUsageKey(uk)) {
  319. return '/serverrecovery'
  320. }
  321. if (uk && this.isContainerHostUsageKey(uk)) {
  322. return '/vminstance-container'
  323. }
  324. switch (this.params.usage_key) {
  325. case 'all.servers':
  326. return '/vminstance'
  327. case 'servers':
  328. return '/vminstance'
  329. case 'domain.servers':
  330. return '/vminstance'
  331. case 'baremetals':
  332. return '/physicalmachine'
  333. case 'domain.baremetals':
  334. return '/physicalmachine'
  335. case 'hosts':
  336. return '/host'
  337. case 'domain.hosts':
  338. return '/host'
  339. case 'all.buckets':
  340. return '/bucket'
  341. case 'domain.buckets':
  342. return '/bucket'
  343. case 'all.cache':
  344. return '/redis'
  345. case 'domain.cache':
  346. return '/redis'
  347. case 'all.rds':
  348. return '/rds'
  349. case 'domain.rds':
  350. return '/rds'
  351. case 'all.loadbalancer':
  352. return '/lb'
  353. case 'domain.loadbalancer':
  354. return '/lb'
  355. case 'all.eip.floating_ip':
  356. return '/eip'
  357. case 'domain.eip.floating_ip':
  358. return '/eip'
  359. case 'all.snapshot':
  360. return '/disk-snapshot'
  361. case 'domain.snapshot':
  362. return '/disk-snapshot'
  363. case 'all.disks.count':
  364. return '/disk'
  365. case 'domain.disks.count':
  366. return '/disk'
  367. case 'all.servers.isolated_devices':
  368. return '/gpu'
  369. case 'domain.servers.isolated_devices':
  370. return '/gpu'
  371. default:
  372. return ''
  373. }
  374. },
  375. },
  376. }
  377. </script>
  378. <style lang="less" scoped>
  379. .number-card-number {
  380. font-size: 60px;
  381. line-height: 48px;
  382. color: #000000;
  383. }
  384. .number-card-unit {
  385. font-size: 14px;
  386. color: #000000;
  387. margin-left: 5px;
  388. }
  389. .drawer-wrapper {
  390. &::v-deep.ant-drawer.ant-drawer-open .ant-drawer-mask {
  391. animation: none;
  392. }
  393. }
  394. </style>