fix(cmdb-ui):resource search common attrs (#397)

This commit is contained in:
dagongren 2024-02-22 16:19:12 +08:00 committed by GitHub
parent 013b116eb5
commit 082da5fade
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 49 additions and 61 deletions

View File

@ -206,7 +206,7 @@
import _ from 'lodash' import _ from 'lodash'
import SearchForm from '../../components/searchForm/SearchForm.vue' import SearchForm from '../../components/searchForm/SearchForm.vue'
import { searchCI } from '../../api/ci' import { searchCI } from '../../api/ci'
import { searchAttributes, getCITypeAttributesByTypeIds } from '../../api/CITypeAttr' import { searchAttributes, getCITypeAttributesByTypeIds, getCITypeAttributesById } from '../../api/CITypeAttr'
import { getCITypes } from '../../api/CIType' import { getCITypes } from '../../api/CIType'
import { getSubscribeAttributes } from '../../api/preference' import { getSubscribeAttributes } from '../../api/preference'
import { getCITableColumns } from '../../utils/helper' import { getCITableColumns } from '../../utils/helper'
@ -315,61 +315,59 @@ export default {
this.instanceList = [] this.instanceList = []
this.totalNumber = res['numfound'] this.totalNumber = res['numfound']
const oldData = res.result const { attributes: resAllAttributes } = await getCITypeAttributesByTypeIds({
type_ids: Object.keys(res.counter).join(','),
function allKeys(data) { })
const keys = {} const _columnsGroup = Object.keys(res.counter).map((key) => {
const ignoreAttr = ['_id', '_type', 'ci_type', 'ci_type_alias', 'unique', 'unique_alias'] const _find = this.ciTypes.find((item) => item.name === key)
data.forEach((item) => { return {
Object.keys(item).forEach((key) => { id: `parent-${_find.id}`,
if (!ignoreAttr.includes(key)) { value: key,
keys[key] = '' label: _find?.alias || _find?.name,
} isCiType: true,
}) }
})
const ciTypeAttribute = {}
const promises = _columnsGroup.map((item) => {
return getCITypeAttributesById(item.id.split('-')[1]).then((res) => {
ciTypeAttribute[item.label] = res.attributes
}) })
return keys })
} await Promise.all(promises)
function tidy(data) { const outputKeys = {}
const outputKeys = allKeys(data) resAllAttributes.forEach((attr) => {
const common = {} outputKeys[attr.name] = ''
data.forEach((item) => { })
const tmp = {}
Object.keys(outputKeys).forEach((j) => { const common = {}
if (j in item) { Object.keys(outputKeys).forEach((key) => {
tmp[j] = item[j] Object.entries(ciTypeAttribute).forEach(([type, attrs]) => {
// 提取common if (attrs.find((a) => a.name === key)) {
{ if (key in common) {
const key = item['ci_type_alias'] common[key][type] = ''
if (j in common) {
common[j][[key]] = ''
} else {
common[j] = { [key]: '' }
}
}
} else { } else {
tmp[j] = null common[key] = { [type]: '' }
}
})
})
const commonObject = {}
const commonKeys = []
// 整理common
Object.keys(common).forEach((key) => {
if (Object.keys(common[key]).length > 1) {
commonKeys.push(key)
const reverseKey = Object.keys(common[key]).join('&')
if (!commonObject[reverseKey]) {
commonObject[reverseKey] = [key]
} else {
commonObject[reverseKey].push(key)
} }
} }
}) })
return { commonObject, commonKeys } })
}
const commonObject = {}
const commonKeys = []
// 整理common
Object.keys(common).forEach((key) => {
if (Object.keys(common[key]).length > 1) {
commonKeys.push(key)
const reverseKey = Object.keys(common[key]).join('&')
if (!commonObject[reverseKey]) {
commonObject[reverseKey] = [key]
} else {
commonObject[reverseKey].push(key)
}
}
})
const { commonObject, commonKeys } = tidy(oldData)
const _commonColumnsGroup = Object.keys(commonObject).map((key) => { const _commonColumnsGroup = Object.keys(commonObject).map((key) => {
return { return {
id: `parent-${key}`, id: `parent-${key}`,
@ -385,24 +383,14 @@ export default {
} }
}) })
const _columnsGroup = Object.keys(res.counter).map((key) => { const promises1 = _columnsGroup.map((item) => {
const _find = this.ciTypes.find((item) => item.name === key)
return {
id: `parent-${_find.id}`,
value: key,
label: _find?.alias || _find?.name,
isCiType: true,
}
})
const promises = _columnsGroup.map((item) => {
return getSubscribeAttributes(item.id.split('-')[1]).then((res1) => { return getSubscribeAttributes(item.id.split('-')[1]).then((res1) => {
item.children = this.getColumns(res.result, res1.attributes).filter( item.children = this.getColumns(res.result, res1.attributes).filter(
(col) => !commonKeys.includes(col.field) (col) => !commonKeys.includes(col.field)
) )
}) })
}) })
await Promise.all(promises).then(() => { await Promise.all(promises1).then(() => {
this.columnsGroup = [..._commonColumnsGroup, ..._columnsGroup] this.columnsGroup = [..._commonColumnsGroup, ..._columnsGroup]
this.instanceList = res['result'] this.instanceList = res['result']
}) })