index.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.instance_count') }}<a-icon class="ml-2" type="loading" v-if="loading" /></div>
  6. <div class="dashboard-card-header-right">
  7. <slot name="actions" :handle-edit="handleEdit" />
  8. </div>
  9. </div>
  10. <div class="dashboard-card-body flex-column justify-content-center">
  11. <div class="flex-fill position-relative">
  12. <div class="dashboard-fco-wrap">
  13. <e-chart v-if="chartConfig.xAxis.data.length" :options="chartConfig" style="height: 100%; width: 100%;" autoresize />
  14. </div>
  15. </div>
  16. </div>
  17. </div>
  18. <base-drawer :visible.sync="visible" :title="$t('dashboard.text_5')" @ok="handleSubmit">
  19. <a-form
  20. hideRequiredMark
  21. :form="form.fc"
  22. v-bind="formItemLayout">
  23. <a-form-item :label="$t('dashboard.text_6')">
  24. <a-input v-decorator="decorators.name" />
  25. </a-form-item>
  26. </a-form>
  27. </base-drawer>
  28. </div>
  29. </template>
  30. <script>
  31. import { mapGetters } from 'vuex'
  32. import { chartColors } from '@/constants'
  33. import { load } from '@Dashboard/utils/cache'
  34. import BaseDrawer from '@Dashboard/components/BaseDrawer'
  35. import { getRequestT } from '@/utils/utils'
  36. export default {
  37. name: 'ResourceCount',
  38. components: {
  39. BaseDrawer,
  40. },
  41. props: {
  42. options: {
  43. type: Object,
  44. required: true,
  45. },
  46. params: Object,
  47. edit: Boolean,
  48. dataRangeParams: {
  49. type: Object,
  50. },
  51. },
  52. data () {
  53. const initialNameValue = (this.params && this.params.name) || this.$t('dashboard.instance_count')
  54. return {
  55. data: {},
  56. visible: false,
  57. loading: false,
  58. form: {
  59. fc: this.$form.createForm(this),
  60. fd: {
  61. name: initialNameValue,
  62. },
  63. },
  64. decorators: {
  65. name: [
  66. 'name',
  67. {
  68. initialValue: initialNameValue,
  69. rules: [
  70. { required: true, message: this.$t('dashboard.text_8') },
  71. ],
  72. },
  73. ],
  74. },
  75. formItemLayout: {
  76. wrapperCol: {
  77. span: 18,
  78. },
  79. labelCol: {
  80. span: 6,
  81. },
  82. },
  83. chartConfig: {
  84. legend: {
  85. show: false,
  86. },
  87. grid: {
  88. left: '3%',
  89. right: '4%',
  90. bottom: '2%',
  91. top: '8%',
  92. containLabel: true,
  93. },
  94. xAxis: {
  95. type: 'category',
  96. data: [],
  97. axisTick: {
  98. show: false,
  99. },
  100. axisLabel: {
  101. interval: 0,
  102. },
  103. },
  104. yAxis: {
  105. type: 'value',
  106. axisLine: {
  107. show: false,
  108. },
  109. },
  110. series: [
  111. {
  112. barMaxWidth: 30,
  113. type: 'bar',
  114. data: [],
  115. label: {
  116. show: true,
  117. position: 'top',
  118. },
  119. },
  120. ],
  121. color: chartColors,
  122. },
  123. usageList: [
  124. {
  125. label: this.$t('common_239'),
  126. key: ['all.servers', 'domain.servers', 'servers'],
  127. required: true,
  128. },
  129. {
  130. label: this.$t('compute.vminstance-container'),
  131. key: ['all.containers', 'domain.containers', 'containers'],
  132. required: false,
  133. },
  134. {
  135. label: this.$t('usage')['all.running_containers'],
  136. key: ['all.running_containers', 'domain.running_containers', 'running_containers'],
  137. required: false,
  138. },
  139. {
  140. label: 'RDS',
  141. key: ['all.rds', 'domain.rds', 'rds'],
  142. required: true,
  143. },
  144. {
  145. label: 'Redis',
  146. key: ['all.cache', 'domain.cache', 'cache'],
  147. required: true,
  148. },
  149. {
  150. label: 'OSS',
  151. key: ['all.buckets', 'domain.buckets', 'buckets'],
  152. required: true,
  153. },
  154. {
  155. label: this.$t('common_248'),
  156. key: ['all.loadbalancer', 'domain.loadbalancer', 'loadbalancer'],
  157. required: true,
  158. },
  159. {
  160. label: this.$t('scope.text_104'),
  161. key: ['baremetals', 'domain.baremetals', 'baremetals'],
  162. required: false,
  163. },
  164. {
  165. label: this.$t('common_305'),
  166. key: ['hosts', 'domain.hosts', 'hosts'],
  167. required: false,
  168. },
  169. {
  170. label: 'MongoDB',
  171. key: ['all.mongodb', 'domain.mongodb', 'mongodb'],
  172. required: false,
  173. },
  174. ],
  175. }
  176. },
  177. computed: {
  178. ...mapGetters(['isDomainMode', 'isAdminMode', 'isProjectMode']),
  179. },
  180. watch: {
  181. 'form.fd' (val) {
  182. this.fetchUsage()
  183. },
  184. 'dataRangeParams.scope': {
  185. handler (val) {
  186. this.fetchUsage()
  187. },
  188. immediate: true,
  189. },
  190. 'dataRangeParams.domain': {
  191. handler (val) {
  192. this.fetchUsage()
  193. },
  194. immediate: true,
  195. },
  196. 'dataRangeParams.project': {
  197. handler (val) {
  198. this.fetchUsage()
  199. },
  200. immediate: true,
  201. },
  202. },
  203. destroyed () {
  204. this.manager = null
  205. },
  206. created () {
  207. this.$emit('update', this.options.i, {
  208. ...this.form.fd,
  209. })
  210. this.refresh()
  211. },
  212. methods: {
  213. refresh () {
  214. return this.fetchUsage()
  215. },
  216. handleEdit () {
  217. this.visible = true
  218. },
  219. async handleSubmit () {
  220. try {
  221. const values = await this.form.fc.validateFields()
  222. this.form.fd = values
  223. this.$emit('update', this.options.i, values)
  224. this.visible = false
  225. } catch (error) {
  226. throw error
  227. }
  228. },
  229. genUsageParams () {
  230. const params = {
  231. scope: this.$store.getters.scope,
  232. $t: getRequestT(),
  233. }
  234. if (this.isAdminMode) {
  235. if (this.dataRangeParams?.scope === 'domain' && this.dataRangeParams?.domain) {
  236. params.scope = 'domain'
  237. params.domain_id = this.dataRangeParams?.domain
  238. }
  239. if (this.dataRangeParams?.scope === 'project' && this.dataRangeParams?.project) {
  240. params.scope = 'project'
  241. params.project_id = this.dataRangeParams?.project
  242. }
  243. }
  244. if (this.isDomainMode) {
  245. if (this.dataRangeParams?.scope === 'project' && this.dataRangeParams?.project) {
  246. params.scope = 'project'
  247. params.project_id = this.dataRangeParams?.project
  248. }
  249. }
  250. return params
  251. },
  252. async fetchUsage () {
  253. this.loading = true
  254. try {
  255. const params = this.genUsageParams()
  256. const data = await load({
  257. res: 'usages',
  258. actionArgs: {
  259. url: '/v2/rpc/usages/general-usage',
  260. method: 'GET',
  261. params,
  262. },
  263. useManager: false,
  264. resPath: 'data',
  265. })
  266. const xData = []
  267. const yData = []
  268. this.usageList.map(item => {
  269. let key = item.key[0]
  270. if (this.isDomainMode || (this.isAdminMode && this.dataRangeParams?.scope === 'domain')) {
  271. key = item.key[1]
  272. }
  273. if (this.isProjectMode || (this.isAdminMode && this.dataRangeParams?.scope === 'project') || (this.isDomainMode && this.dataRangeParams?.scope === 'project')) {
  274. key = item.key[2]
  275. }
  276. if (item.required || (data.hasOwnProperty(key) && data[key])) {
  277. xData.push(item.label)
  278. yData.push(data[key] || 0)
  279. }
  280. })
  281. this.chartConfig.xAxis.data = xData
  282. this.chartConfig.series[0].data = yData
  283. } finally {
  284. this.loading = false
  285. }
  286. },
  287. },
  288. }
  289. </script>