Skip to content

Commit

Permalink
perf(api): resource search supports recent searches and my favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
pycook committed Sep 2, 2024
1 parent 02235d8 commit 417e8fe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
5 changes: 3 additions & 2 deletions cmdb-api/api/lib/cmdb/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def get_choice_values(cls, attr_id, value_type, choice_web_hook, choice_other,
return []
choice_values = choice_table.get_by(fl=["value", "option"], attr_id=attr_id)

return [[ValueTypeMap.serialize[value_type](choice_value['value']), choice_value['option']]
return [[ValueTypeMap.serialize[value_type](choice_value['value']), choice_value['option'] or
{"label": ValueTypeMap.serialize[value_type](choice_value['value'])}]
for choice_value in choice_values]

@staticmethod
Expand Down Expand Up @@ -140,7 +141,7 @@ def get_enum_map(cls, _attr_id, _attr=None):
attr = AttributeCache.get(_attr_id) if _attr_id else _attr
if attr and attr.is_choice:
choice_values = cls.get_choice_values(attr.id, attr.value_type, None, None)
return {i[0]: i[1]['label'] for i in choice_values if i[1].get('label')}
return {i[0]: i[1]['label'] for i in choice_values if i[1] and i[1].get('label')}

return {}

Expand Down
9 changes: 8 additions & 1 deletion cmdb-api/api/lib/cmdb/ci_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,14 @@ def get_all_attributes(type_id):
for _type_id in parent_ids + [type_id]:
result.extend(CITypeAttributesCache.get2(_type_id))

return result
attr_ids = set()
result2 = []
for i in result:
if i[1].id not in attr_ids:
result2.append(i)
attr_ids.add(i[1].id)

return result2

@classmethod
def get_attr_names_by_type_id(cls, type_id):
Expand Down
24 changes: 16 additions & 8 deletions cmdb-api/api/lib/cmdb/preference.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,22 @@ def get_search_option(**kwargs):
def add_search_option(**kwargs):
kwargs['uid'] = current_user.uid

existed = PreferenceSearchOption.get_by(uid=current_user.uid,
name=kwargs.get('name'),
prv_id=kwargs.get('prv_id'),
ptv_id=kwargs.get('ptv_id'),
type_id=kwargs.get('type_id'),
)
if existed:
return abort(400, ErrFormat.preference_search_option_exists)
if kwargs['name'] in ('__recent__', '__favor__'):
if kwargs['name'] == '__recent__':
for i in PreferenceSearchOption.get_by(
only_query=True, name=kwargs['name'], uid=current_user.uid).order_by(
PreferenceSearchOption.id.desc()).offset(20):
i.delete()

else:
existed = PreferenceSearchOption.get_by(uid=current_user.uid,
name=kwargs.get('name'),
prv_id=kwargs.get('prv_id'),
ptv_id=kwargs.get('ptv_id'),
type_id=kwargs.get('type_id'),
)
if existed:
return abort(400, ErrFormat.preference_search_option_exists)

return PreferenceSearchOption.create(**kwargs)

Expand Down

0 comments on commit 417e8fe

Please sign in to comment.