K8s.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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">
  6. {{ form.fd.name || $t('dashboard.text_6') }}<a-icon class="ml-2" type="loading" v-if="loading" />
  7. <span v-if="isResDeny" class="ml-2"><a-icon class="warning-color mr-1" type="warning" />{{ $t('common.permission.403') }}</span>
  8. <span v-if="isUsageKeyDeny" class="ml-2">
  9. <a-tooltip class="mr-2"><template slot="title">{{ $t('dashboard.usage_key_deny_tips') }}</template><icon type="help" /></a-tooltip>
  10. <a-icon class="warning-color mr-1" type="warning" />
  11. {{ $t('dashboard.usage_key_deny_tips_2') }}
  12. </span>
  13. </div>
  14. <div class="dashboard-card-header-right">
  15. <slot name="actions" :handle-edit="handleEdit" />
  16. </div>
  17. </div>
  18. <div class="dashboard-card-body align-items-center">
  19. <a-progress v-if="isRing" type="circle" :percent="percent" :strokeWidth="12" :status="status" :strokeColor="percentColor">
  20. <template v-slot:format><span class="percent-tips" :style="{ color: '#000' }">{{ percentTips }}</span></template>
  21. </a-progress>
  22. <liquid-fill v-else :value="decimalPercent" />
  23. <div class="flex-fill ml-4">
  24. <div class="d-flex bottomborder-box align-items-end">
  25. <div :class="`label-unit ${jumpParams.usedPath ? 'label-jump' : ''}`" @click="goJump('used')">{{ useLabel }}</div>
  26. <div class="flex-number mr-2 ml-1 text-right">{{isResDeny ? '-' : usage.usage}}</div>
  27. <div class="label-unit">{{ usage.unit }}</div>
  28. </div>
  29. <div class="d-flex bottomborder-box align-items-end">
  30. <div :class="`label-unit ${jumpParams.reservedPath ? 'label-jump' : ''}`" @click="goJump('reserved')">{{ unUseLabel }}</div>
  31. <div class="flex-number mr-2 ml-1 text-right">{{isResDeny ? '-' : displayUnUsage.usage}}</div>
  32. <div class="label-unit">{{displayUnUsage.unit}}</div>
  33. </div>
  34. <div class="d-flex bottomborder-box align-items-end">
  35. <div :class="`label-unit ${jumpParams.allPath ? 'label-jump' : ''}`" @click="goJump('all')">{{ $t('dashboard.text_44') }}</div>
  36. <div class="flex-number mr-2 ml-1 text-right">{{isResDeny ? '-' : allUsage.usage}}</div>
  37. <div class="label-unit">{{allUsage.unit}}</div>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. <base-drawer class="ring-drawer-wrapper" @update:visible="updateVisible" :visible="visible" :title="$t('dashboard.text_5')" @ok="handleSubmit">
  43. <slot />
  44. <a-form
  45. hideRequiredMark
  46. :form="form.fc"
  47. v-bind="formItemLayout">
  48. <a-form-item :label="$t('dashboard.text_6')">
  49. <a-input v-decorator="decorators.name" />
  50. </a-form-item>
  51. <k8s-config :fc="form.fc" :fd="form.fd" :decorators="decorators" />
  52. <a-form-item :label="$t('dashboard.chart_type')">
  53. <a-radio-group v-decorator="decorators.chart_type" @change="chartTypeChange">
  54. <a-radio-button value="ring">{{ $t('dashboard.ring') }}</a-radio-button>
  55. <a-radio-button value="liquidfill">{{ $t('dashboard.liquidfill') }}</a-radio-button>
  56. </a-radio-group>
  57. </a-form-item>
  58. <a-form-item v-if="isRing" :label="colorLabel || $t('dashboard.color.scheme')" class="mb-0">
  59. <a-select
  60. @change="colorChange"
  61. v-decorator="decorators.color">
  62. <a-select-option v-for="item in ringColors" :key="item.key" :value="item.key">
  63. <div>
  64. <a-progress :show-info="false" :stroke-color="{ '60%': item.percent60, '80%': item.percent80, '100%': item.percent100}" :percent="100" />
  65. </div>
  66. <div class="text-color-help">{{ item.label }}</div>
  67. </a-select-option>
  68. </a-select>
  69. </a-form-item>
  70. </a-form>
  71. </base-drawer>
  72. </div>
  73. </template>
  74. <script>
  75. import _ from 'lodash'
  76. import { mapGetters } from 'vuex'
  77. import BaseDrawer from '@Dashboard/components/BaseDrawer'
  78. import { K8S_USAGE_CONFIG, getTargetRangeUsageKey } from '@Dashboard/constants'
  79. import { load } from '@Dashboard/utils/cache'
  80. import { getRequestT } from '@/utils/utils'
  81. import K8sConfig from '@Dashboard/sections/K8sConfig'
  82. import { numerify } from '@/filters'
  83. import { chartColors } from '@/constants'
  84. import { hasPermission } from '@/utils/auth'
  85. import mixin from './mixin'
  86. export default {
  87. name: 'RingK8s',
  88. components: {
  89. BaseDrawer,
  90. K8sConfig,
  91. },
  92. mixins: [mixin],
  93. props: {
  94. dataRangeParams: {
  95. type: Object,
  96. },
  97. },
  98. data () {
  99. const initialNameValue = ((this.params && this.params.type === 'k8s') && this.params.name) || this.$t('dashboard.text_45')
  100. const initialAllUsageKeyValue = ((this.params && this.params.type === 'k8s') && this.params.all_usage_key) || 'all.cluster.node.memory.capacity'
  101. const initialUsageKeyValue = ((this.params && this.params.type === 'k8s') && this.params.usage_key) || 'all.cluster.node.memory.request'
  102. const initialColorValue = ((this.params && this.params.type !== 'k8s') && this.params.color) || 'default'
  103. const initialUsageLabelValue = ((this.params && this.params.type !== 'k8s') && this.params.usage_label && this.params.usage_label.length > 0) ? this.params.usage_label : this.$t('dashboard.text_33')
  104. const initialUnUsageLabelValue = ((this.params && this.params.type !== 'k8s') && this.params.un_usage_label && this.params.un_usage_label.length > 0) ? this.params.un_usage_label : this.$t('dashboard.text_34')
  105. const initialChartTypeValue = ((this.params && this.params.type !== 'k8s') && this.params.chart_type) || 'ring'
  106. return {
  107. data: {},
  108. loading: false,
  109. form: {
  110. fc: this.$form.createForm(this, { onValuesChange: this.onValuesChange }),
  111. fd: {
  112. name: initialNameValue,
  113. all_usage_key: initialAllUsageKeyValue,
  114. usage_key: initialUsageKeyValue,
  115. usage_label: initialUsageLabelValue,
  116. un_usage_label: initialUnUsageLabelValue,
  117. color: initialColorValue,
  118. chart_type: initialChartTypeValue,
  119. },
  120. },
  121. decorators: {
  122. name: [
  123. 'name',
  124. {
  125. initialValue: initialNameValue,
  126. rules: [
  127. { required: true, message: this.$t('dashboard.text_8') },
  128. ],
  129. },
  130. ],
  131. all_usage_key: [
  132. 'all_usage_key',
  133. {
  134. initialValue: initialAllUsageKeyValue,
  135. rules: [
  136. { required: true, message: this.$t('dashboard.text_22') },
  137. ],
  138. },
  139. ],
  140. usage_key: [
  141. 'usage_key',
  142. {
  143. initialValue: initialUsageKeyValue,
  144. rules: [
  145. { required: true, message: this.$t('dashboard.text_22') },
  146. ],
  147. },
  148. ],
  149. color: [
  150. 'color',
  151. {
  152. initialValue: initialColorValue,
  153. },
  154. ],
  155. usage_label: [
  156. 'usage_label',
  157. {
  158. initialValue: initialUsageLabelValue,
  159. rules: [
  160. { required: true, message: this.$t('dashboard.usage_label_title') },
  161. ],
  162. },
  163. ],
  164. un_usage_label: [
  165. 'un_usage_label',
  166. {
  167. initialValue: initialUnUsageLabelValue,
  168. rules: [
  169. { required: true, message: this.$t('dashboard.un_usage_label_title') },
  170. ],
  171. },
  172. ],
  173. chart_type: [
  174. 'chart_type',
  175. {
  176. initialValue: initialChartTypeValue,
  177. },
  178. ],
  179. },
  180. }
  181. },
  182. computed: {
  183. ...mapGetters(['userInfo', 'scope', 'isAdminMode', 'isDomainMode']),
  184. isUsageKeyDeny () {
  185. const usageConfig = getTargetRangeUsageKey(this.form.fd.usage_key, this.scope, this.dataRangeParams?.scope, K8S_USAGE_CONFIG)
  186. const allUsageConfig = getTargetRangeUsageKey(this.form.fd.all_usage_key, this.scope, this.dataRangeParams?.scope, K8S_USAGE_CONFIG)
  187. if (usageConfig.error || usageConfig.usageKey === 'null' || !usageConfig.usageKey || allUsageConfig.error || allUsageConfig.usageKey === 'null' || !allUsageConfig.usageKey) {
  188. return true
  189. }
  190. return false
  191. },
  192. isRing () {
  193. return this.form.fd.chart_type === 'ring'
  194. },
  195. allUsageNumber () {
  196. const usageKey = getTargetRangeUsageKey(this.form.fd.all_usage_key, this.scope, this.dataRangeParams?.scope, K8S_USAGE_CONFIG).usageKey
  197. if (!usageKey || usageKey === 'null') {
  198. return 0
  199. }
  200. return (this.data && _.get(this.data, usageKey)) || 0
  201. },
  202. usageNumber () {
  203. const usageKey = getTargetRangeUsageKey(this.form.fd.usage_key, this.scope, this.dataRangeParams?.scope, K8S_USAGE_CONFIG).usageKey
  204. if (!usageKey || usageKey === 'null') {
  205. return 0
  206. }
  207. return (this.data && _.get(this.data, usageKey)) || 0
  208. },
  209. allUsageConfig () {
  210. return K8S_USAGE_CONFIG[this.form.fd.all_usage_key]
  211. },
  212. usageConfig () {
  213. return K8S_USAGE_CONFIG[this.form.fd.usage_key]
  214. },
  215. allUsage () {
  216. let ret = this.allUsageNumber
  217. if (this.allUsageConfig && this.allUsageConfig.formatter) {
  218. ret = this.allUsageConfig.formatter(ret)
  219. }
  220. if (this.allUsageConfig && this.allUsageConfig.unit) {
  221. ret = `${ret} ${this.allUsageConfig.unit}`
  222. }
  223. return {
  224. usage: ret.toString().split(' ')[0],
  225. unit: ret.toString().split(' ')[1] || '',
  226. }
  227. },
  228. usage () {
  229. let ret = this.usageNumber
  230. if (this.usageConfig && this.usageConfig.formatter) {
  231. ret = this.usageConfig.formatter(ret)
  232. }
  233. if (this.usageConfig && this.usageConfig.unit) {
  234. ret = `${ret} ${this.usageConfig.unit}`
  235. }
  236. return {
  237. usage: ret.toString().split(' ')[0],
  238. unit: ret.toString().split(' ')[1] || '',
  239. }
  240. },
  241. unUsage () {
  242. const ret = this.allUsageNumber - this.usageNumber
  243. return ret < 0 ? 0 : ret
  244. },
  245. displayUnUsage () {
  246. let ret = this.unUsage
  247. if (
  248. (this.allUsageConfig && this.allUsageConfig.formatter) &&
  249. (this.usageConfig && this.usageConfig.formatter)
  250. ) {
  251. ret = this.usageConfig.formatter(this.unUsage)
  252. }
  253. if (
  254. (this.allUsageConfig && this.allUsageConfig.unit) &&
  255. (this.usageConfig && this.usageConfig.unit)
  256. ) {
  257. ret = `${ret} ${this.usageConfig.unit}`
  258. }
  259. return {
  260. usage: ret.toString().split(' ')[0],
  261. unit: ret.toString().split(' ')[1] || this.usage.unit,
  262. }
  263. },
  264. decimalPercent () {
  265. if (this.usageNumber === 0 || this.allUsageNumber === 0) return '0'
  266. const percent = this.usageNumber / this.allUsageNumber
  267. if (percent > 0 && percent < 0.01) {
  268. return '0.004'
  269. }
  270. return numerify(this.usageNumber / this.allUsageNumber, '0.00')
  271. },
  272. percent () {
  273. const data = parseFloat(this.decimalPercent)
  274. if (data > 0 && data < 0.01) {
  275. return 0.4
  276. }
  277. return numerify(data * 100, 0.0)
  278. },
  279. percentTips () {
  280. if (this.percent < 1) {
  281. return '< 1%'
  282. }
  283. return `${this.percent} %`
  284. },
  285. colorConfig () {
  286. return (this.params && this.params.color) || 'blue'
  287. },
  288. percentColor () {
  289. switch (this.colorConfig) {
  290. case 'default':
  291. if (this.percent < 60) {
  292. return chartColors[3]
  293. }
  294. if (this.percent < 80) {
  295. return chartColors[1]
  296. }
  297. return chartColors[2]
  298. case 'reverse':
  299. if (this.percent < 60) {
  300. return chartColors[2]
  301. }
  302. if (this.percent < 80) {
  303. return chartColors[1]
  304. }
  305. return chartColors[3]
  306. default:
  307. return chartColors[0]
  308. }
  309. },
  310. status () {
  311. let ret = 'normal'
  312. if (this.percent > 100) {
  313. ret = 'exception'
  314. }
  315. return ret
  316. },
  317. useLabel () {
  318. if (this.params) {
  319. return this.params.usage_label || this.$t('dashboard.text_43')
  320. }
  321. return this.$t('dashboard.text_43')
  322. },
  323. unUseLabel () {
  324. if (this.params) {
  325. return this.params.un_usage_label || this.$t('dashboard.text_34')
  326. }
  327. return this.$t('dashboard.text_34')
  328. },
  329. jumpParams () {
  330. const params = this.parseUsageKey(this.form.fd.all_usage_key || '', this.form.fd.usage_key || '')
  331. return params
  332. },
  333. isResDeny () {
  334. return !hasPermission({ key: 'k8s_usages_get' })
  335. },
  336. },
  337. watch: {
  338. 'form.fd' (val) {
  339. this.fetchUsage()
  340. for (const key in this.decorators) {
  341. let config = this.decorators[key][1] || {}
  342. config = {
  343. ...config,
  344. initialValue: val[key],
  345. }
  346. this.decorators[key][1] = config
  347. }
  348. },
  349. 'dataRangeParams.scope': {
  350. handler (val) {
  351. this.fetchUsage()
  352. },
  353. immediate: true,
  354. },
  355. 'dataRangeParams.domain': {
  356. handler (val) {
  357. this.fetchUsage()
  358. },
  359. immediate: true,
  360. },
  361. 'dataRangeParams.project': {
  362. handler (val) {
  363. this.fetchUsage()
  364. },
  365. immediate: true,
  366. },
  367. },
  368. created () {
  369. if (this.params && this.params.type === 'k8s') {
  370. this.form.fd = { chart_type: 'ring', ...this.params }
  371. }
  372. this.$emit('update', this.options.i, this.form.fd)
  373. },
  374. methods: {
  375. onValuesChange (props, values) {
  376. Object.keys(values).forEach((key) => {
  377. this.form.fd[key] = values[key]
  378. })
  379. },
  380. colorChange (color) {
  381. this.$emit('update:color', color)
  382. },
  383. chartTypeChange (type) {
  384. this.$emit('update:chart_type', type)
  385. },
  386. refresh () {
  387. return this.fetchUsage()
  388. },
  389. parseUsageKey (allKey, usageKey) {
  390. const str = allKey.replace(/^(all\.)|(domain\.)|(project\.)/, '')
  391. const list = str.split('.')
  392. const allFilterKey = list[1]
  393. const allFilterValue = list[2]
  394. const usageStr = usageKey.replace(/^(all\.)|(domain\.)|(project\.)/, '')
  395. const usageList = usageStr.split('.')
  396. // const usageResource = usageList[0]
  397. // const usageFilterKey = usageList[1]
  398. const usageFilterValue = usageList[2]
  399. // const usageFilterValue = usageList[2]
  400. let allPath = ''
  401. let usedPath = ''
  402. let reservedPath = ''
  403. const allParams = {}
  404. const usedParams = {}
  405. const reservedParams = {}
  406. switch (list[0]) {
  407. case 'cluster':
  408. if (allFilterKey === 'count') {
  409. allPath = '/k8s-cluster'
  410. usedPath = '/k8s-cluster'
  411. if (usageKey.startsWith('domain.')) {
  412. usedParams.domain = this.userInfo.projectDomain || ''
  413. }
  414. } else if (allFilterKey === 'node' && allFilterValue === 'count') {
  415. allPath = '/k8s-node'
  416. usedPath = '/k8s-node'
  417. reservedPath = '/k8s-node'
  418. if (usageFilterValue === 'ready_count') {
  419. usedParams.status = 'Ready'
  420. reservedParams.status = 'NotReady'
  421. } else if (usageFilterValue === 'not_ready_count') {
  422. usedParams.status = 'NotReady'
  423. reservedParams.status = 'Ready'
  424. }
  425. if (usageKey.startsWith('domain.')) {
  426. usedParams.domain = this.userInfo.projectDomain || ''
  427. }
  428. if (allKey.startsWith('domain.')) {
  429. allParams.domain = this.userInfo.projectDomain || ''
  430. }
  431. }
  432. }
  433. return {
  434. allPath,
  435. allParams,
  436. usedPath,
  437. usedParams,
  438. reservedPath,
  439. reservedParams,
  440. }
  441. },
  442. goJump (type) {
  443. this.$router.push({
  444. path: this.jumpParams[`${type}Path`],
  445. query: this.jumpParams[`${type}Params`],
  446. })
  447. },
  448. genUsageParams () {
  449. const params = {
  450. scope: this.$store.getters.scope,
  451. $t: getRequestT(),
  452. ignoreErrorStatusCode: [403],
  453. }
  454. if (this.isAdminMode) {
  455. if (this.dataRangeParams?.scope === 'domain' && this.dataRangeParams?.domain) {
  456. params.scope = 'domain'
  457. params.domain_id = this.dataRangeParams?.domain
  458. }
  459. if (this.dataRangeParams?.scope === 'project' && this.dataRangeParams?.project) {
  460. params.scope = 'project'
  461. params.project_id = this.dataRangeParams?.project
  462. }
  463. }
  464. if (this.isDomainMode) {
  465. if (this.dataRangeParams?.scope === 'project' && this.dataRangeParams?.project) {
  466. params.scope = 'project'
  467. params.project_id = this.dataRangeParams?.project
  468. }
  469. }
  470. return params
  471. },
  472. async fetchUsage () {
  473. this.loading = true
  474. try {
  475. const params = this.genUsageParams()
  476. const data = await load({
  477. res: 'usages',
  478. actionArgs: {
  479. url: '/v2/rpc/usages/k8s-usage',
  480. method: 'GET',
  481. params,
  482. },
  483. useManager: false,
  484. resPath: 'data',
  485. })
  486. this.data = data
  487. } finally {
  488. this.loading = false
  489. }
  490. },
  491. handleEdit () {
  492. this.updateVisible(true)
  493. },
  494. updateVisible (val) {
  495. this.$emit('update:visible', val)
  496. },
  497. async handleSubmit () {
  498. try {
  499. const values = await this.form.fc.validateFields()
  500. this.form.fd = values
  501. this.$emit('update', this.options.i, { type: 'k8s', ...values })
  502. this.updateVisible(false)
  503. } catch (error) {
  504. throw error
  505. }
  506. },
  507. },
  508. }
  509. </script>
  510. <style lang="less" scoped>
  511. .flex-number{
  512. font-size: 18px;
  513. line-height: 19px;
  514. flex: 1 1 auto;
  515. color: #000;
  516. font-weight: bold;
  517. }
  518. .label-unit{
  519. color: #837F89;
  520. }
  521. .label-jump{
  522. color: var(--antd-wave-shadow-color);
  523. cursor: pointer;
  524. }
  525. .percent-tips {
  526. font-size: 22px;
  527. font-weight: bold;
  528. }
  529. .ring-drawer-wrapper {
  530. &::v-deep.ant-drawer.ant-drawer-open .ant-drawer-mask {
  531. animation: none;
  532. }
  533. }
  534. .bottomborder-box{
  535. border-bottom: 1px solid #F2F2F2;
  536. margin: 10px 0;
  537. padding: 5px 0;
  538. }
  539. </style>