计算属性 触发计算 (#174)

This commit is contained in:
wang-liang0615 2023-09-11 19:16:05 +08:00 committed by GitHub
parent ebce839eaa
commit 3cb78f30d2
4 changed files with 70 additions and 15 deletions

View File

@ -153,3 +153,10 @@ export function canDefineComputed() {
method: 'HEAD', method: 'HEAD',
}) })
} }
export function calcComputedAttribute(attr_id) {
return axios({
url: `/v0.1/attributes/${attr_id}/calc_computed_attribute`,
method: 'PUT',
})
}

View File

@ -51,6 +51,9 @@
<a-space class="attribute-card-operation"> <a-space class="attribute-card-operation">
<a v-if="!isStore"><a-icon type="edit" @click="handleEdit"/></a> <a v-if="!isStore"><a-icon type="edit" @click="handleEdit"/></a>
<a-tooltip title="所有CI触发计算">
<a v-if="!isStore && property.is_computed"><a-icon type="redo" @click="handleCalcComputed"/></a>
</a-tooltip>
<a style="color:red;"><a-icon type="delete" @click="handleDelete"/></a> <a style="color:red;"><a-icon type="delete" @click="handleDelete"/></a>
</a-space> </a-space>
</div> </div>
@ -59,7 +62,7 @@
</template> </template>
<script> <script>
import { deleteCITypeAttributesById, deleteAttributesById } from '@/modules/cmdb/api/CITypeAttr' import { deleteCITypeAttributesById, deleteAttributesById, calcComputedAttribute } from '@/modules/cmdb/api/CITypeAttr'
import ValueTypeIcon from '@/components/CMDBValueTypeMapIcon' import ValueTypeIcon from '@/components/CMDBValueTypeMapIcon'
import { import {
ops_default_show, ops_default_show,
@ -165,6 +168,18 @@ export default {
openTrigger() { openTrigger() {
this.$refs.triggerForm.open(this.property) this.$refs.triggerForm.open(this.property)
}, },
handleCalcComputed() {
const that = this
this.$confirm({
title: '警告',
content: `确认触发所有CI的计算`,
onOk() {
calcComputedAttribute(that.property.id).then(() => {
that.$message.success('触发成功!')
})
},
})
},
}, },
} }
</script> </script>

View File

@ -10,7 +10,7 @@
:headerStyle="{ borderBottom: 'none' }" :headerStyle="{ borderBottom: 'none' }"
wrapClassName="attribute-edit-form" wrapClassName="attribute-edit-form"
> >
<a-form :form="form" :layout="formLayout" @submit="handleSubmit"> <a-form :form="form" :layout="formLayout">
<a-divider style="font-size:14px;margin-top:6px;">基础设置</a-divider> <a-divider style="font-size:14px;margin-top:6px;">基础设置</a-divider>
<a-col :span="12"> <a-col :span="12">
<a-form-item :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol" label="属性名(英文)"> <a-form-item :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol" label="属性名(英文)">
@ -343,7 +343,13 @@
name="is_password" name="is_password"
v-decorator="['is_computed', { rules: [], valuePropName: 'checked' }]" v-decorator="['is_computed', { rules: [], valuePropName: 'checked' }]"
/> />
<ComputedArea ref="computedArea" v-show="isShowComputedArea" :canDefineComputed="canDefineComputed" /> <ComputedArea
showCalcComputed
ref="computedArea"
v-show="isShowComputedArea"
@handleCalcComputed="handleCalcComputed"
:canDefineComputed="canDefineComputed"
/>
</a-form-item> </a-form-item>
</a-col> </a-col>
</a-row> </a-row>
@ -353,7 +359,7 @@
</a-form-item> </a-form-item>
<div class="custom-drawer-bottom-action"> <div class="custom-drawer-bottom-action">
<a-button @click="onClose">取消</a-button> <a-button @click="onClose">取消</a-button>
<a-button @click="handleSubmit" type="primary">确定</a-button> <a-button @click="handleSubmit(false)" type="primary">确定</a-button>
</div> </div>
</a-form> </a-form>
</CustomDrawer> </CustomDrawer>
@ -366,6 +372,7 @@ import {
updateAttributeById, updateAttributeById,
updateCITypeAttributesById, updateCITypeAttributesById,
canDefineComputed, canDefineComputed,
calcComputedAttribute,
} from '@/modules/cmdb/api/CITypeAttr' } from '@/modules/cmdb/api/CITypeAttr'
import { valueTypeMap } from '../../utils/const' import { valueTypeMap } from '../../utils/const'
import ComputedArea from './computedArea.vue' import ComputedArea from './computedArea.vue'
@ -576,15 +583,14 @@ export default {
}) })
}, },
handleSubmit(e) { async handleSubmit(isCalcComputed = false) {
e.preventDefault() await this.form.validateFields(async (err, values) => {
this.form.validateFields((err, values) => {
if (!err) { if (!err) {
console.log('Received values of form: ', values) console.log('Received values of form: ', values)
if (this.record.is_required !== values.is_required || this.record.default_show !== values.default_show) { if (this.record.is_required !== values.is_required || this.record.default_show !== values.default_show) {
console.log('changed is_required') console.log('changed is_required')
updateCITypeAttributesById(this.CITypeId, { await updateCITypeAttributesById(this.CITypeId, {
attributes: [ attributes: [
{ attr_id: this.record.id, is_required: values.is_required, default_show: values.default_show }, { attr_id: this.record.id, is_required: values.is_required, default_show: values.default_show },
], ],
@ -630,19 +636,21 @@ export default {
const fontOptions = this.$refs.fontArea.getData() const fontOptions = this.$refs.fontArea.getData()
if (values.id) { if (values.id) {
this.updateAttribute(values.id, { ...values, option: { fontOptions } }) await this.updateAttribute(values.id, { ...values, option: { fontOptions } }, isCalcComputed)
} else { } else {
// this.createAttribute(values) // this.createAttribute(values)
} }
} }
}) })
}, },
updateAttribute(attrId, data) { async updateAttribute(attrId, data, isCalcComputed = false) {
updateAttributeById(attrId, data).then((res) => { await updateAttributeById(attrId, data)
this.$message.success(`更新成功`) if (isCalcComputed) {
this.handleOk() await calcComputedAttribute(attrId)
this.onClose() }
}) this.$message.success(`更新成功`)
this.handleOk()
this.onClose()
}, },
handleOk() { handleOk() {
this.$emit('ok') this.$emit('ok')
@ -682,6 +690,9 @@ export default {
default_value: key, default_value: key,
}) })
}, },
async handleCalcComputed() {
await this.handleSubmit(true)
},
}, },
watch: {}, watch: {},
} }

View File

@ -8,6 +8,14 @@
<span style="font-size:12px;" slot="tab">代码</span> <span style="font-size:12px;" slot="tab">代码</span>
<codemirror style="z-index: 9999" :options="cmOptions" v-model="compute_script"></codemirror> <codemirror style="z-index: 9999" :options="cmOptions" v-model="compute_script"></codemirror>
</a-tab-pane> </a-tab-pane>
<template slot="tabBarExtraContent" v-if="showCalcComputed">
<a-button type="primary" size="small" @click="handleCalcComputed">
应用
</a-button>
<a-tooltip title="所有CI触发计算">
<a-icon type="question-circle" style="margin-left:5px" />
</a-tooltip>
</template>
</a-tabs> </a-tabs>
</template> </template>
@ -25,6 +33,10 @@ export default {
type: Boolean, type: Boolean,
default: true, default: true,
}, },
showCalcComputed: {
type: Boolean,
default: false,
}
}, },
data() { data() {
return { return {
@ -62,6 +74,16 @@ export default {
this.activeKey = 'expr' this.activeKey = 'expr'
} }
}, },
handleCalcComputed() {
const that = this
this.$confirm({
title: '警告',
content: `确认触发将保存当前配置及触发所有CI的计算`,
onOk() {
that.$emit('handleCalcComputed')
},
})
},
}, },
} }
</script> </script>