index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <div class="d-flex">
  3. <a-form-item :extra="extra">
  4. <a-radio-group v-decorator="decorator" @change="change" :disabled="disabled">
  5. <a-radio-button v-show="showUnlimited" :key="0" :value="0">{{ $t('compute.unlimited') }}</a-radio-button>
  6. <a-radio-button v-for="item in realOptions" :value="item" :key="item" v-show="item < max || !showMore" :disabled="disableOptionHandle(item)">{{$t('compute.text_120', [ item ])}}</a-radio-button>
  7. <a-radio-button v-if="showMore" @click="showMore = !showMore">...</a-radio-button>
  8. </a-radio-group>
  9. </a-form-item>
  10. <a-form-item v-if="showCpuSockets" :extra="cpuSocketsExtra">
  11. <a-tooltip :title="isServerRunning ? $t('compute.hot_action_notsupport') : ''">
  12. <base-select :disabled="isServerRunning" :value="cpuSockets" class="ml-1" :options="getCpuSocketsOptions(cpuSocketsOptions, cpu)" @change="cpuSocketsChangeHandle" />
  13. </a-tooltip>
  14. </a-form-item>
  15. <a-button v-if="isVMware && !isServerRunning" class="mt-1" type="link" @click="showCpuSocketsHandle">{{ showCpuSockets ? $t('common.cancel') : $t('compute.set_cpu_sockets') }}</a-button>
  16. </div>
  17. </template>
  18. <script>
  19. import * as R from 'ramda'
  20. import { HYPERVISORS_MAP } from '@/constants'
  21. export default {
  22. name: 'CpuRadio',
  23. props: {
  24. decorator: {
  25. type: Array,
  26. required: true,
  27. },
  28. options: {
  29. type: Array,
  30. required: true,
  31. },
  32. disableOptions: {
  33. type: Array,
  34. default: () => [],
  35. },
  36. max: {
  37. type: Number,
  38. default: 32,
  39. },
  40. disabled: {
  41. type: Boolean,
  42. default: false,
  43. },
  44. extra: {
  45. type: String,
  46. default: '',
  47. },
  48. showUnlimited: {
  49. type: Boolean,
  50. default: false,
  51. },
  52. form: {
  53. type: Object,
  54. require: true,
  55. },
  56. hypervisor: {
  57. validator: val => {
  58. if (val) return R.is(String, val)
  59. return true
  60. },
  61. },
  62. serverStatus: {
  63. type: String,
  64. },
  65. cpuSocketsInit: {
  66. type: Number,
  67. },
  68. showCpuSocketsInit: {
  69. type: Boolean,
  70. },
  71. },
  72. data () {
  73. const max = Math.max.apply(null, this.options)
  74. const showMore = max > this.max
  75. return {
  76. showMore,
  77. opta: this.options,
  78. cpu: this.decorator[1].initialValue,
  79. cpuSockets: 0,
  80. cpuSocketsOptions: [
  81. { label: '1', value: 1 },
  82. { label: '2', value: 2 },
  83. { label: '4', value: 4 },
  84. ],
  85. showCpuSockets: this.showCpuSocketsInit || false,
  86. }
  87. },
  88. computed: {
  89. isVMware () {
  90. return this.hypervisor === HYPERVISORS_MAP.esxi.key
  91. },
  92. cpuSocketsExtra () {
  93. if (this.isServerRunning) {
  94. return `${this.$t('compute.core_per_sockets')}: ` + (this.cpuSocketsInit)
  95. }
  96. return `${this.$t('compute.core_per_sockets')}: ` + (this.cpu / (this.cpuSockets || 1))
  97. },
  98. isServerRunning () {
  99. return this.serverStatus === 'running'
  100. },
  101. realOptions () {
  102. if (this.isServerRunning && this.showCpuSockets) {
  103. return this.options.filter(v => v % this.cpuSocketsInit === 0)
  104. }
  105. return this.options
  106. },
  107. },
  108. watch: {
  109. options () {
  110. const max = Math.max.apply(null, this.options)
  111. this.showMore = max > this.max
  112. },
  113. cpuSockets (v) {
  114. if (this.form?.fi) {
  115. this.form.fi.cpuSockets = v
  116. }
  117. },
  118. showCpuSocketsInit (v) {
  119. this.showCpuSockets = v
  120. },
  121. cpuSocketsInit: {
  122. handler (v) {
  123. if (this.isServerRunning) {
  124. this.cpuSockets = this.cpu / v
  125. } else {
  126. this.cpuSockets = v
  127. }
  128. },
  129. immediate: true,
  130. },
  131. },
  132. methods: {
  133. change (e) {
  134. const cpu = e.target.value
  135. this.cpuSockets = this.isServerRunning ? cpu / this.cpuSocketsInit : 1
  136. this.cpu = cpu
  137. this.$emit('change', e.target.value)
  138. },
  139. disableOptionHandle (item) {
  140. return this.disableOptions.includes(item)
  141. },
  142. showCpuSocketsHandle () {
  143. this.showCpuSockets = !this.showCpuSockets
  144. this.form.fi.showCpuSockets = this.showCpuSockets
  145. },
  146. cpuSocketsChangeHandle (v) {
  147. this.cpuSockets = v
  148. },
  149. getCpuSocketsOptions (cpuSocketsOptions, cpu) {
  150. return cpuSocketsOptions.filter(item => {
  151. return cpu % item.value === 0
  152. })
  153. },
  154. },
  155. }
  156. </script>