create.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('common.save')}}</div>
  4. <div slot="body">
  5. <a-form :form="form" v-bind="formLayout">
  6. <a-form-item :label="$t('compute.text_228')">
  7. <a-input v-decorator="decorators.name" :placeholder="$t('common.placeholder')" @change="handleNameChange" />
  8. </a-form-item>
  9. <a-form-item :label="$t('monitor.dashboard.title')">
  10. <base-select
  11. filterable
  12. v-decorator="decorators.dashboard_id"
  13. :loading="loading"
  14. resource="alertdashboards"
  15. :options="dashboards" />
  16. </a-form-item>
  17. </a-form>
  18. </div>
  19. <div slot="footer">
  20. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  21. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  22. </div>
  23. </base-dialog>
  24. </template>
  25. <script>
  26. import _ from 'lodash'
  27. import DialogMixin from '@/mixins/dialog'
  28. import WindowsMixin from '@/mixins/windows'
  29. export default {
  30. name: 'CreateMonitorDashboardChart',
  31. mixins: [DialogMixin, WindowsMixin],
  32. data () {
  33. this.handleNameChange = _.debounce(this._handleNameChange, 500)
  34. return {
  35. loading: false,
  36. scope: this.$store.getters.scope,
  37. form: this.$form.createForm(this),
  38. manager: new this.$Manager('alertdashboards', 'v1'),
  39. name: '',
  40. dashboards: [],
  41. decorators: {
  42. dashboard_id: [
  43. 'dashboard_id',
  44. {
  45. rules: [
  46. { required: true, message: this.$t('monitor.dashboard.create.chart.dashboard.tips') },
  47. ],
  48. },
  49. ],
  50. name: [
  51. 'name',
  52. {
  53. initialValue: this.params.name || '',
  54. rules: [
  55. { required: true, message: `${this.$t('common.placeholder')}${this.$t('common.name')}` },
  56. ],
  57. },
  58. ],
  59. },
  60. formLayout: {
  61. wrapperCol: {
  62. md: { span: 20 },
  63. xl: { span: 19 },
  64. xxl: { span: 21 },
  65. },
  66. labelCol: {
  67. md: { span: 4 },
  68. xl: { span: 5 },
  69. xxl: { span: 3 },
  70. },
  71. },
  72. }
  73. },
  74. created () {
  75. this.fetchDashboards()
  76. },
  77. methods: {
  78. async fetchDashboards () {
  79. this.loading = true
  80. this.dashboards = []
  81. try {
  82. const params = {
  83. scope: this.scope,
  84. }
  85. const { data: { data } } = await this.manager.list({ params })
  86. this.dashboards = data
  87. this.loading = false
  88. } catch (error) {
  89. throw error
  90. } finally {
  91. this.loading = false
  92. }
  93. },
  94. validateForm () {
  95. return new Promise((resolve, reject) => {
  96. this.form.validateFields((err, values) => {
  97. if (!err) {
  98. resolve(values)
  99. } else {
  100. reject(err)
  101. }
  102. })
  103. })
  104. },
  105. _handleNameChange (e) {
  106. this.name = e.target.value
  107. },
  108. async handleConfirm () {
  109. this.loading = true
  110. try {
  111. const values = await this.validateForm()
  112. const data = {
  113. dashboard_id: values.dashboard_id,
  114. name: values.name,
  115. metric_query: this.params.metric_query,
  116. interval: this.params.timeGroup,
  117. scope: this.$store.getters.scope,
  118. ...this.params.timeRangeParams,
  119. }
  120. if (!data.metric_query || !data.metric_query.length || !data.from || !data.dashboard_id) return
  121. await new this.$Manager('alertpanels', 'v1').create({ data })
  122. this.loading = false
  123. this.$message.success(this.$t('cloudenv.text_381'))
  124. this.cancelDialog()
  125. } catch (error) {
  126. this.loading = false
  127. throw error
  128. }
  129. },
  130. },
  131. }
  132. </script>