mirror of
https://github.com/veops/cmdb.git
synced 2025-08-08 14:50:56 +08:00
Realize /api/v0.1/ci_relations/s [done]
This commit is contained in:
@@ -10,7 +10,11 @@ from flask.cli import with_appcontext
|
||||
import api.lib.cmdb.ci
|
||||
from api.extensions import db
|
||||
from api.extensions import rd
|
||||
from api.lib.cmdb.const import REDIS_PREFIX_CI
|
||||
from api.lib.cmdb.const import REDIS_PREFIX_CI_RELATION
|
||||
from api.lib.cmdb.const import ValueTypeEnum
|
||||
from api.models.cmdb import CI
|
||||
from api.models.cmdb import CIRelation
|
||||
|
||||
|
||||
@click.command()
|
||||
@@ -21,12 +25,12 @@ def init_cache():
|
||||
if current_app.config.get("USE_ES"):
|
||||
from api.extensions import es
|
||||
from api.models.cmdb import Attribute
|
||||
from api.lib.cmdb.const import type_map
|
||||
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 == Attribute.TEXT:
|
||||
if attr.value_type == ValueTypeEnum.TEXT:
|
||||
other['analyzer'] = 'ik_max_word'
|
||||
other['search_analyzer'] = 'ik_smart'
|
||||
if attr.is_index:
|
||||
@@ -37,7 +41,7 @@ def init_cache():
|
||||
}
|
||||
}
|
||||
try:
|
||||
es.update_mapping(attr.name, type_map['es_type'][attr.value_type], other)
|
||||
es.update_mapping(attr.name, ValueTypeMap.es_type[attr.value_type], other)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
@@ -48,7 +52,7 @@ def init_cache():
|
||||
if res:
|
||||
continue
|
||||
else:
|
||||
res = rd.get([ci.id])
|
||||
res = rd.get([ci.id], REDIS_PREFIX_CI)
|
||||
if res and list(filter(lambda x: x, res)):
|
||||
continue
|
||||
|
||||
@@ -58,7 +62,15 @@ def init_cache():
|
||||
if current_app.config.get("USE_ES"):
|
||||
es.create(ci_dict)
|
||||
else:
|
||||
rd.delete(ci.id)
|
||||
rd.add({ci.id: json.dumps(ci_dict)})
|
||||
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, []).append(cr.second_ci_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()
|
||||
|
Reference in New Issue
Block a user