| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <template>
- <div class="h-100 position-relative">
- <div class="dashboard-card-wrap">
- <div class="dashboard-card-header">
- <div class="dashboard-card-header-left">{{ form.fd.name || $t('dashboard.instance_count') }}<a-icon class="ml-2" type="loading" v-if="loading" /></div>
- <div class="dashboard-card-header-right">
- <slot name="actions" :handle-edit="handleEdit" />
- </div>
- </div>
- <div class="dashboard-card-body flex-column justify-content-center">
- <div class="flex-fill position-relative">
- <div class="dashboard-fco-wrap">
- <e-chart v-if="chartConfig.xAxis.data.length" :options="chartConfig" style="height: 100%; width: 100%;" autoresize />
- </div>
- </div>
- </div>
- </div>
- <base-drawer :visible.sync="visible" :title="$t('dashboard.text_5')" @ok="handleSubmit">
- <a-form
- hideRequiredMark
- :form="form.fc"
- v-bind="formItemLayout">
- <a-form-item :label="$t('dashboard.text_6')">
- <a-input v-decorator="decorators.name" />
- </a-form-item>
- </a-form>
- </base-drawer>
- </div>
- </template>
- <script>
- import { mapGetters } from 'vuex'
- import { chartColors } from '@/constants'
- import { load } from '@Dashboard/utils/cache'
- import BaseDrawer from '@Dashboard/components/BaseDrawer'
- import { getRequestT } from '@/utils/utils'
- export default {
- name: 'ResourceCount',
- components: {
- BaseDrawer,
- },
- props: {
- options: {
- type: Object,
- required: true,
- },
- params: Object,
- edit: Boolean,
- dataRangeParams: {
- type: Object,
- },
- },
- data () {
- const initialNameValue = (this.params && this.params.name) || this.$t('dashboard.instance_count')
- return {
- data: {},
- visible: false,
- loading: false,
- form: {
- fc: this.$form.createForm(this),
- fd: {
- name: initialNameValue,
- },
- },
- decorators: {
- name: [
- 'name',
- {
- initialValue: initialNameValue,
- rules: [
- { required: true, message: this.$t('dashboard.text_8') },
- ],
- },
- ],
- },
- formItemLayout: {
- wrapperCol: {
- span: 18,
- },
- labelCol: {
- span: 6,
- },
- },
- chartConfig: {
- legend: {
- show: false,
- },
- grid: {
- left: '3%',
- right: '4%',
- bottom: '2%',
- top: '8%',
- containLabel: true,
- },
- xAxis: {
- type: 'category',
- data: [],
- axisTick: {
- show: false,
- },
- axisLabel: {
- interval: 0,
- },
- },
- yAxis: {
- type: 'value',
- axisLine: {
- show: false,
- },
- },
- series: [
- {
- barMaxWidth: 30,
- type: 'bar',
- data: [],
- label: {
- show: true,
- position: 'top',
- },
- },
- ],
- color: chartColors,
- },
- usageList: [
- {
- label: this.$t('common_239'),
- key: ['all.servers', 'domain.servers', 'servers'],
- required: true,
- },
- {
- label: this.$t('compute.vminstance-container'),
- key: ['all.containers', 'domain.containers', 'containers'],
- required: false,
- },
- {
- label: this.$t('usage')['all.running_containers'],
- key: ['all.running_containers', 'domain.running_containers', 'running_containers'],
- required: false,
- },
- {
- label: 'RDS',
- key: ['all.rds', 'domain.rds', 'rds'],
- required: true,
- },
- {
- label: 'Redis',
- key: ['all.cache', 'domain.cache', 'cache'],
- required: true,
- },
- {
- label: 'OSS',
- key: ['all.buckets', 'domain.buckets', 'buckets'],
- required: true,
- },
- {
- label: this.$t('common_248'),
- key: ['all.loadbalancer', 'domain.loadbalancer', 'loadbalancer'],
- required: true,
- },
- {
- label: this.$t('scope.text_104'),
- key: ['baremetals', 'domain.baremetals', 'baremetals'],
- required: false,
- },
- {
- label: this.$t('common_305'),
- key: ['hosts', 'domain.hosts', 'hosts'],
- required: false,
- },
- {
- label: 'MongoDB',
- key: ['all.mongodb', 'domain.mongodb', 'mongodb'],
- required: false,
- },
- ],
- }
- },
- computed: {
- ...mapGetters(['isDomainMode', 'isAdminMode', 'isProjectMode']),
- },
- watch: {
- 'form.fd' (val) {
- this.fetchUsage()
- },
- 'dataRangeParams.scope': {
- handler (val) {
- this.fetchUsage()
- },
- immediate: true,
- },
- 'dataRangeParams.domain': {
- handler (val) {
- this.fetchUsage()
- },
- immediate: true,
- },
- 'dataRangeParams.project': {
- handler (val) {
- this.fetchUsage()
- },
- immediate: true,
- },
- },
- destroyed () {
- this.manager = null
- },
- created () {
- this.$emit('update', this.options.i, {
- ...this.form.fd,
- })
- this.refresh()
- },
- methods: {
- refresh () {
- return this.fetchUsage()
- },
- handleEdit () {
- this.visible = true
- },
- async handleSubmit () {
- try {
- const values = await this.form.fc.validateFields()
- this.form.fd = values
- this.$emit('update', this.options.i, values)
- this.visible = false
- } catch (error) {
- throw error
- }
- },
- genUsageParams () {
- const params = {
- scope: this.$store.getters.scope,
- $t: getRequestT(),
- }
- if (this.isAdminMode) {
- if (this.dataRangeParams?.scope === 'domain' && this.dataRangeParams?.domain) {
- params.scope = 'domain'
- params.domain_id = this.dataRangeParams?.domain
- }
- if (this.dataRangeParams?.scope === 'project' && this.dataRangeParams?.project) {
- params.scope = 'project'
- params.project_id = this.dataRangeParams?.project
- }
- }
- if (this.isDomainMode) {
- if (this.dataRangeParams?.scope === 'project' && this.dataRangeParams?.project) {
- params.scope = 'project'
- params.project_id = this.dataRangeParams?.project
- }
- }
- return params
- },
- async fetchUsage () {
- this.loading = true
- try {
- const params = this.genUsageParams()
- const data = await load({
- res: 'usages',
- actionArgs: {
- url: '/v2/rpc/usages/general-usage',
- method: 'GET',
- params,
- },
- useManager: false,
- resPath: 'data',
- })
- const xData = []
- const yData = []
- this.usageList.map(item => {
- let key = item.key[0]
- if (this.isDomainMode || (this.isAdminMode && this.dataRangeParams?.scope === 'domain')) {
- key = item.key[1]
- }
- if (this.isProjectMode || (this.isAdminMode && this.dataRangeParams?.scope === 'project') || (this.isDomainMode && this.dataRangeParams?.scope === 'project')) {
- key = item.key[2]
- }
- if (item.required || (data.hasOwnProperty(key) && data[key])) {
- xData.push(item.label)
- yData.push(data[key] || 0)
- }
- })
- this.chartConfig.xAxis.data = xData
- this.chartConfig.series[0].data = yData
- } finally {
- this.loading = false
- }
- },
- },
- }
- </script>
|