def add(cls, **kwargs): choice_value = kwargs.pop("choice_value", []) kwargs.pop("is_choice", None) is_choice = True if choice_value else False name = kwargs.pop("name") alias = kwargs.pop("alias", "") alias = name if not alias else alias Attribute.get_by(name=name, first=True) and abort(400, "attribute {0} is already existed".format(name)) attr = Attribute.create(flush=True, name=name, alias=alias, is_choice=is_choice, **kwargs) if choice_value: cls._add_choice_values(attr.id, attr.value_type, choice_value) try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error("add attribute error, {0}".format(str(e))) return abort(400, "add attribute <{0}> failed".format(name)) AttributeCache.clean(attr) return attr.id
def update(self, _id, **kwargs): attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id)) if kwargs.get("name"): other = Attribute.get_by(name=kwargs['name'], first=True, to_dict=False) if other and other.id != attr.id: return abort(400, "Attribute name <{0}> cannot be duplicate!".format(kwargs['name'])) if kwargs.get("alias"): other = Attribute.get_by(alias=kwargs['alias'], first=True, to_dict=False) if other and other.id != attr.id: return abort(400, "Attribute alias <{0}> cannot be duplicate!".format(kwargs['alias'])) choice_value = kwargs.pop("choice_value", False) is_choice = True if choice_value else False kwargs['is_choice'] = is_choice attr.update(flush=True, **kwargs) if is_choice: self._add_choice_values(attr.id, attr.value_type, choice_value) else: self._del_choice_values(attr.id, attr.value_type) try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error("update attribute error, {0}".format(str(e))) return abort(400, "update attribute <{0}> failed".format(_id)) AttributeCache.clean(attr) return attr.id
def get(cls, key): if key is None: return attr = cache.get('Field::Name::{0}'.format(key)) \ or cache.get('Field::ID::{0}'.format(key)) \ or cache.get('Field::Alias::{0}'.format(key)) if attr is None: attr = Attribute.get_by(name=key, first=True, to_dict=False) \ or Attribute.get_by_id(key) \ or Attribute.get_by(alias=key, first=True, to_dict=False) if attr is not None: cls.set(attr) return attr
def get(cls, key): if key is None: return attr = cache.get(cls.PREFIX_NAME.format(key)) attr = attr or cache.get(cls.PREFIX_ID.format(key)) attr = attr or cache.get(cls.PREFIX_ALIAS.format(key)) if attr is None: attr = Attribute.get_by(name=key, first=True, to_dict=False) attr = attr or Attribute.get_by_id(key) attr = attr or Attribute.get_by( alias=key, first=True, to_dict=False) if attr is not None: cls.set(attr) return attr
def add(cls, **kwargs): choice_value = kwargs.pop("choice_value", []) kwargs.pop("is_choice", None) is_choice = True if choice_value else False name = kwargs.pop("name") alias = kwargs.pop("alias", "") alias = name if not alias else alias Attribute.get_by(name=name, first=True) and abort( 400, "attribute name <{0}> is duplicated".format(name)) Attribute.get_by(alias=alias, first=True) and abort( 400, "attribute alias <{0}> is duplicated".format(name)) attr = Attribute.create(flush=True, name=name, alias=alias, is_choice=is_choice, **kwargs) if choice_value: cls._add_choice_values(attr.id, attr.value_type, choice_value) try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error("add attribute error, {0}".format(str(e))) return abort(400, "add attribute <{0}> failed".format(name)) AttributeCache.clean(attr) if current_app.config.get("USE_ES"): from api.extensions import es other = dict() other['index'] = True if attr.is_index else False if attr.value_type == ValueTypeEnum.TEXT: other['analyzer'] = 'ik_max_word' other['search_analyzer'] = 'ik_smart' if attr.is_index: other["fields"] = { "keyword": { "type": "keyword", "ignore_above": 256 } } es.update_mapping(name, ValueTypeMap.es_type[attr.value_type], other) return attr.id
def get_attribute_by_alias(self, alias): attr = Attribute.get_by(alias=alias, first=True) if attr and attr["is_choice"]: attr.update( dict(choice_value=self.get_choice_values( attr["id"], attr["value_type"]))) return attr
def get_attribute_by_name(self, name): attr = Attribute.get_by(name=name, first=True) if attr and attr["is_choice"]: attr.update( dict(choice_value=self.get_choice_values( attr["id"], attr["value_type"]))) return attr
def search_attributes(cls, name=None, alias=None, page=1, page_size=None): """ :param name: :param alias: :param page: :param page_size: :return: attribute, if name is None, then return all attributes """ if name is not None: attrs = Attribute.get_by_like(name=name) elif alias is not None: attrs = Attribute.get_by_like(alias=alias) else: attrs = Attribute.get_by() numfound = len(attrs) attrs = attrs[(page - 1) * page_size:][:page_size] res = list() for attr in attrs: attr["is_choice"] and attr.update( dict(choice_value=cls.get_choice_values( attr["id"], attr["value_type"]))) res.append(attr) return numfound, res
def init_cache(): db.session.remove() if current_app.config.get("USE_ES"): from api.extensions import es from api.models.cmdb import Attribute from api.lib.cmdb.utils import ValueTypeMap attributes = Attribute.get_by(to_dict=False) for attr in attributes: other = dict() other['index'] = True if attr.is_index else False if attr.value_type == ValueTypeEnum.TEXT: other['analyzer'] = 'ik_max_word' other['search_analyzer'] = 'ik_smart' if attr.is_index: other["fields"] = { "keyword": { "type": "keyword", "ignore_above": 256 } } try: es.update_mapping(attr.name, ValueTypeMap.es_type[attr.value_type], other) except Exception as e: print(e) cis = CI.get_by(to_dict=False) for ci in cis: if current_app.config.get("USE_ES"): res = es.get_index_id(ci.id) if res: continue else: res = rd.get([ci.id], REDIS_PREFIX_CI) if res and list(filter(lambda x: x, res)): continue m = api.lib.cmdb.ci.CIManager() ci_dict = m.get_ci_by_id_from_db(ci.id, need_children=False, use_master=False) if current_app.config.get("USE_ES"): es.create(ci_dict) else: rd.create_or_update({ci.id: json.dumps(ci_dict)}, REDIS_PREFIX_CI) ci_relations = CIRelation.get_by(to_dict=False) relations = dict() for cr in ci_relations: relations.setdefault(cr.first_ci_id, {}).update( {cr.second_ci_id: cr.second_ci.type_id}) for i in relations: relations[i] = json.dumps(relations[i]) if relations: rd.create_or_update(relations, REDIS_PREFIX_CI_RELATION) db.session.remove()
def get_attributes(self, name=None): """ :param name: :return: attribute, if name is None, then return all attributes """ attrs = Attribute.get_by_like( name=name) if name is not None else Attribute.get_by() res = list() for attr in attrs: attr["is_choice"] and attr.update( dict(choice_value=self.get_choice_values( attr["id"], attr["value_type"]))) res.append(attr) return res