index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <a-row :gutter="gutter">
  3. <a-form-item :extra="extra" style="margin-bottom:0">
  4. <a-col :span="span" v-if="showResType">
  5. <a-form-item class="mr-1">
  6. <base-select
  7. class="metric-select"
  8. minWidth="192px"
  9. v-decorator="decorators.metric_res_type"
  10. :options="metricTypeOpts"
  11. filterable
  12. :disabled="disabled"
  13. :select-props="{ placeholder: $t('monitor.text_111'), loading }"
  14. @change="metricTypeChange" />
  15. </a-form-item>
  16. </a-col>
  17. <a-col :span="span">
  18. <a-form-item class="mr-1">
  19. <base-select
  20. class="metric-select"
  21. minWidth="192px"
  22. v-decorator="decorators.metric_key"
  23. :options="metricKeyOpts"
  24. filterable
  25. :disabled="disabled"
  26. :item.sync="metricKeyItem"
  27. :select-props="{ placeholder: $t('monitor.text_112'), loading }"
  28. @change="metricKeyChange" />
  29. </a-form-item>
  30. </a-col>
  31. <a-col :span="span">
  32. <a-form-item>
  33. <base-select
  34. class="metric-select"
  35. minWidth="192px"
  36. filterable
  37. v-decorator="decorators.metric_value"
  38. :item.sync="metricValueItem"
  39. :options="metricOpts"
  40. :labelFormat="metricValueLabelFormat"
  41. :disabled="disabled"
  42. @change="metricValueChange"
  43. :select-props="{ placeholder: $t('monitor.text_113'), allowClear: true, loading }" />
  44. </a-form-item>
  45. </a-col>
  46. </a-form-item>
  47. </a-row>
  48. </template>
  49. <script>
  50. import _ from 'lodash'
  51. import { metric_zh } from '@Monitor/constants'
  52. export default {
  53. name: 'ExplorerFormMetric',
  54. props: {
  55. decorators: {
  56. type: Object,
  57. required: true,
  58. },
  59. form: {
  60. type: Object,
  61. required: true,
  62. validator: val => val.fc,
  63. },
  64. disabled: {
  65. type: Boolean,
  66. default: false,
  67. },
  68. loading: {
  69. type: Boolean,
  70. default: false,
  71. },
  72. res_type_measurements: {
  73. type: Object,
  74. default: () => ({}),
  75. },
  76. res_types: {
  77. type: Array,
  78. default: () => [],
  79. },
  80. showResType: {
  81. type: Boolean,
  82. default: true,
  83. },
  84. },
  85. data () {
  86. return {
  87. metric_key: _.get(this.decorators.metric_key, '[1].initialValue'),
  88. metric_res_type: _.get(this.decorators.metric_res_type, '[1].initialValue'),
  89. metricValueItem: {},
  90. metricKeyItem: {},
  91. }
  92. },
  93. computed: {
  94. metricTypeOpts () {
  95. return this.res_types.map(val => {
  96. // let label = val
  97. // if (this.$te(`dictionary.${val}`)) label = this.$t(`dictionary.${val}`)
  98. const label = val === 'system' ? this.$t('common.system_service') : this.$t(`dictionary.${val}`)
  99. return {
  100. key: val,
  101. label,
  102. }
  103. })
  104. },
  105. metricKeyOpts () {
  106. return (this.res_type_measurements[this.metric_res_type] || []).map(val => {
  107. let label = val.measurement
  108. const displayName = val.measurement_display_name
  109. if (displayName && metric_zh[displayName]) {
  110. label = metric_zh[displayName]
  111. }
  112. return {
  113. ...val,
  114. metric_res_type: this.metric_res_type,
  115. key: val.measurement,
  116. label,
  117. }
  118. })
  119. },
  120. metricOpts () {
  121. const metricKeyItem = this.metricKeyOpts.find(item => item.key === this.metric_key)
  122. if (metricKeyItem && _.isArray(metricKeyItem.field_key)) {
  123. return metricKeyItem.field_key.map(val => {
  124. let label = val
  125. const fieldDes = metricKeyItem.field_descriptions
  126. let description = {}
  127. if (fieldDes) {
  128. description = fieldDes[val]
  129. const displayName = _.get(fieldDes, `${val}.display_name`)
  130. if (displayName && metric_zh[displayName]) label = metric_zh[displayName]
  131. }
  132. return {
  133. key: val,
  134. label,
  135. description,
  136. metric_res_type: metricKeyItem.metric_res_type,
  137. }
  138. })
  139. } else {
  140. return []
  141. }
  142. },
  143. span () {
  144. if (this.showResType) return 8
  145. return 12
  146. },
  147. gutter () {
  148. if (this.showResType) return 8
  149. return 0
  150. },
  151. extra () {
  152. // if (this.metricValueItem.id === 'balance' && this.metricValueItem.metric_res_type === 'cloudaccount') {
  153. // return this.$t('monitor.currency_select_tip')
  154. // }
  155. return ''
  156. },
  157. },
  158. watch: {
  159. metric_res_type (val) {
  160. if (!this.metricTypeOpts) {
  161. this.resetMetric()
  162. } else {
  163. const metricKey = this.form.fc.getFieldValue(this.decorators.metric_key[0])
  164. const metricValue = this.form.fc.getFieldValue(this.decorators.metric_value[0])
  165. if (metricKey) {
  166. const validMetric = this.metricTypeOpts.find(option => option.key === metricKey)
  167. if (!validMetric && !this.disabled) {
  168. this.resetMetric()
  169. } else {
  170. this.metricKeyChange(this.metric_key, false)
  171. this.$nextTick(() => {
  172. if (metricValue) this.metricValueChange(metricValue)
  173. })
  174. }
  175. }
  176. }
  177. },
  178. },
  179. mounted: function () {
  180. this.$nextTick(function () {
  181. const metricValue = this.form.fc.getFieldValue(this.decorators.metric_value[0])
  182. if (metricValue) this.metricValueChange(metricValue)
  183. })
  184. },
  185. methods: {
  186. metricValueLabelFormat (item) {
  187. return (<div>
  188. {item.label}<span class="text-black-50">({item.description.name})</span>
  189. </div>)
  190. },
  191. metricTypeChange (val) {
  192. this.metric_res_type = val
  193. },
  194. metricKeyChange (val, isNative = true) {
  195. this.metric_key = val
  196. if (this.form && this.form.fc && isNative) {
  197. this.form.fc.setFieldsValue({
  198. [this.decorators.metric_value[0]]: undefined,
  199. })
  200. }
  201. },
  202. metricValueChange (val) {
  203. if (!val) {
  204. this.$emit('metricClear')
  205. } else {
  206. let vItem = this.metricValueItem
  207. let kItem = this.metricKeyItem
  208. if (!vItem) {
  209. vItem = this.metricOpts.find((opt) => { return opt.key === val })
  210. }
  211. if (this.metricKeyItem) {
  212. kItem = this.metricKeyOpts.find((opt) => { return opt.key === this.metric_key })
  213. }
  214. this.$emit('metricChange', { metricKey: this.metric_key, mertric: val, mertricItem: vItem, metricKeyItem: kItem })
  215. }
  216. },
  217. resetMetric () {
  218. this.form.fc.setFieldsValue({
  219. [this.decorators.metric_key[0]]: undefined,
  220. [this.decorators.metric_value[0]]: undefined,
  221. })
  222. this.$emit('metricClear')
  223. },
  224. },
  225. }
  226. </script>
  227. <style lang="less" scoped>
  228. .metric-select {
  229. min-width: 192px !important;
  230. flex: 1 1 auto;
  231. width: 100%;
  232. ::v-deep .ant-select {
  233. width: 100%;
  234. min-width: 192px;
  235. }
  236. }
  237. </style>