Esempio n. 1
0
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()
Esempio n. 2
0
def ci_relation_delete(parent_id, child_id):
    children = rd.get([parent_id], REDIS_PREFIX_CI_RELATION)[0]
    children = json.loads(children) if children is not None else {}

    if str(child_id) in children:
        children.pop(str(child_id))

    rd.create_or_update({parent_id: json.dumps(children)},
                        REDIS_PREFIX_CI_RELATION)

    current_app.logger.info("DELETE ci relation cache: {0} -> {1}".format(
        parent_id, child_id))
Esempio n. 3
0
def ci_cache(ci_id):
    time.sleep(0.01)
    db.session.close()

    m = api.lib.cmdb.ci.CIManager()
    ci = m.get_ci_by_id_from_db(ci_id, need_children=False, use_master=False)
    if current_app.config.get("USE_ES"):
        es.create_or_update(ci_id, ci)
    else:
        rd.create_or_update({ci_id: json.dumps(ci)}, REDIS_PREFIX_CI)

    current_app.logger.info("{0} flush..........".format(ci_id))
Esempio n. 4
0
def ci_relation_cache(parent_id, child_id):
    children = rd.get([parent_id], REDIS_PREFIX_CI_RELATION)[0]
    children = json.loads(children) if children is not None else {}

    cr = CIRelation.get_by(first_ci_id=parent_id,
                           second_ci_id=child_id,
                           first=True,
                           to_dict=False)
    if str(child_id) not in children:
        children[str(child_id)] = cr.second_ci.type_id

    rd.create_or_update({parent_id: json.dumps(children)},
                        REDIS_PREFIX_CI_RELATION)

    current_app.logger.info("ADD ci relation cache: {0} -> {1}".format(
        parent_id, child_id))