Skip to content

Commit

Permalink
feat(cmdb): 添加操作历史表的唯一值列 (Add unique value column to operation histor…
Browse files Browse the repository at this point in the history
…y table)
  • Loading branch information
thexqn committed Sep 13, 2024
1 parent 383d4c8 commit ff98777
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
31 changes: 15 additions & 16 deletions cmdb-api/api/lib/cmdb/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from api.lib.cmdb.cache import AttributeCache
from api.lib.cmdb.cache import RelationTypeCache
from api.lib.cmdb.const import OperateType
from api.lib.cmdb.cache import CITypeCache
from api.lib.cmdb.perms import CIFilterPermsCRUD
from api.lib.cmdb.resp_format import ErrFormat
from api.lib.perm.acl.cache import UserCache
Expand All @@ -22,7 +23,7 @@
from api.models.cmdb import CITypeTrigger
from api.models.cmdb import CITypeUniqueConstraint
from api.models.cmdb import OperationRecord
from api.lib.cmdb.utils import ValueTypeMap
from api.lib.cmdb.utils import TableMap


class AttributeHistoryManger(object):
Expand Down Expand Up @@ -60,23 +61,21 @@ def get_records_for_attributes(start, end, username, page, page_size, operate_ty
total = len(records)

res = {}
unique_set = {}
from api.lib.cmdb.ci import CIManager
show_attr_set = {}
show_attr_cache = {}
for record in records:
record_id = record.OperationRecord.id
type_id = record.OperationRecord.type_id
ci_id = record.AttributeHistory.ci_id
if ci_id not in unique_set:
ci = CIManager.get_by_id(ci_id)
if ci and hasattr(ci, 'ci_type') and ci.ci_type:
unique_id = ci.ci_type.unique_id
unique_ci_type = AttributeCache.get(unique_id).value_type
value_table_name = ValueTypeMap.table_name.get(f"index_{unique_ci_type}")
value_table_list = getattr(ci, f"{value_table_name}.ci_id", None)
matched_items = [item for item in value_table_list if item.attr_id == unique_id] if value_table_list else []
if matched_items:
unique_set[ci_id] = matched_items[0].value
else:
unique_set[ci_id] = None
show_attr_set[ci_id] = None

ci_type = CITypeCache.get(type_id)
if ci_type:
show_attr = show_attr_cache.setdefault(type_id, AttributeCache.get(ci_type.show_id or ci_type.unique_id))
attr_table = TableMap(attr=show_attr).table
attr_record = attr_table.get_by(attr_id=show_attr.id, ci_id=ci_id, first=True, to_dict=False)
show_attr_set[ci_id] = attr_record.value if attr_record else None

attr_hist = record.AttributeHistory.to_dict()
attr_hist['attr'] = AttributeCache.get(attr_hist['attr_id'])
if attr_hist['attr']:
Expand All @@ -92,7 +91,7 @@ def get_records_for_attributes(start, end, username, page, page_size, operate_ty

if record_id not in res:
record_dict = record.OperationRecord.to_dict()
record_dict['unique_value'] = unique_set.get(ci_id)
record_dict['show_attr_value'] = show_attr_set.get(ci_id)
record_dict["user"] = UserCache.get(record_dict.get("uid"))
if record_dict["user"]:
record_dict['user'] = record_dict['user'].nickname
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</template>
</vxe-column>
<vxe-column field="type_id" width="100px" :title="$t('cmdb.ciType.ciType')"></vxe-column>
<vxe-column field="unique_value" width="100px" :title="$t('cmdb.ciType.uniqueKey')"></vxe-column>
<vxe-column field="show_attr_value" width="100px" :title="$t('cmdb.ciType.show')"></vxe-column>
<vxe-column field="operate_type" width="89px" :title="$t('operation')">
<template #header="{ column }">
<span>{{ column.title }}</span>
Expand Down Expand Up @@ -315,7 +315,7 @@ export default {
}
},
mergeRowMethod({ row, _rowIndex, column, visibleData }) {
const fields = ['created_at', 'user', 'type_id', 'unique_value']
const fields = ['created_at', 'user', 'type_id', 'show_attr_value']
const cellValue = row[column.property]
const created_at = row['created_at']
if (column.property === 'created_at') {
Expand Down Expand Up @@ -366,7 +366,7 @@ export default {
}
}
}
} else if (column.property === 'unique_value') {
} else if (column.property === 'show_attr_value') {
if (cellValue && fields.includes(column.property)) {
const prevRow = visibleData[_rowIndex - 1]
let nextRow = visibleData[_rowIndex + 1]
Expand Down

0 comments on commit ff98777

Please sign in to comment.