add command cmdb-index-table-upgrade

This commit is contained in:
pycook 2023-07-25 10:31:30 +08:00
parent 52574c64cc
commit 0655b0e9eb
5 changed files with 84 additions and 34 deletions

View File

@ -5,6 +5,9 @@ from flask.cli import with_appcontext
@click.command()
@with_appcontext
def init_acl():
"""
acl init
"""
from api.models.acl import Role
from api.models.acl import App
from api.tasks.acl import role_rebuild

View File

@ -29,6 +29,7 @@ from api.lib.perm.acl.role import RoleCRUD
from api.lib.perm.acl.user import UserCRUD
from api.models.acl import App
from api.models.acl import ResourceType
from api.models.cmdb import Attribute
from api.models.cmdb import CI
from api.models.cmdb import CIRelation
from api.models.cmdb import CIType
@ -200,6 +201,9 @@ def del_user(user):
@click.command()
@with_appcontext
def cmdb_counter():
"""
Dashboard calculations
"""
from api.lib.cmdb.cache import CMDBCounterCache
while True:
@ -217,6 +221,9 @@ def cmdb_counter():
@click.command()
@with_appcontext
def cmdb_trigger():
"""
Trigger execution
"""
current_day = datetime.datetime.today().strftime("%Y-%m-%d")
trigger2cis = dict()
trigger2completed = dict()
@ -259,3 +266,33 @@ def cmdb_trigger():
i += 1
time.sleep(10)
@click.command()
@with_appcontext
def cmdb_index_table_upgrade():
"""
Migrate data from tables c_value_integers, c_value_floats, and c_value_datetime
"""
for attr in Attribute.get_by(to_dict=False):
if attr.value_type not in {ValueTypeEnum.TEXT, ValueTypeEnum.JSON}:
attr.update(is_index=True)
from api.models.cmdb import CIValueInteger, CIIndexValueInteger
from api.models.cmdb import CIValueFloat, CIIndexValueFloat
from api.models.cmdb import CIValueDateTime, CIIndexValueDateTime
for i in CIValueInteger.get_by(to_dict=False):
CIIndexValueInteger.create(ci_id=i.ci_id, attr_id=i.attr_id, value=i.value, commit=False)
i.delete(commit=False)
db.session.commit()
for i in CIValueFloat.get_by(to_dict=False):
CIIndexValueFloat.create(ci_id=i.ci_id, attr_id=i.attr_id, value=i.value, commit=False)
i.delete(commit=False)
db.session.commit()
for i in CIValueDateTime.get_by(to_dict=False):
CIIndexValueDateTime.create(ci_id=i.ci_id, attr_id=i.attr_id, value=i.value, commit=False)
i.delete(commit=False)
db.session.commit()

View File

@ -19,7 +19,7 @@ class InitEmployee(object):
def import_user_from_acl(self):
"""
从ACL导入用户
Import users from ACL
"""
acl = ACLManager('acl')
@ -149,7 +149,7 @@ class InitDepartment(object):
@with_appcontext
def init_import_user_from_acl():
"""
从ACL导入用户
Import users from ACL
"""
InitEmployee().import_user_from_acl()
@ -158,7 +158,7 @@ def init_import_user_from_acl():
@with_appcontext
def init_department():
"""
初始化 部门
Department initialization
"""
InitDepartment().init()
InitDepartment().create_acl_role_with_department()

View File

@ -13,6 +13,8 @@ import api.models.cmdb as model
from api.lib.cmdb.cache import AttributeCache
from api.lib.cmdb.const import ValueTypeEnum
TIME_RE = re.compile(r"^(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$")
def string2int(x):
return int(float(x))
@ -32,8 +34,7 @@ class ValueTypeMap(object):
ValueTypeEnum.INT: string2int,
ValueTypeEnum.FLOAT: float,
ValueTypeEnum.TEXT: lambda x: escape(x).encode('utf-8').decode('utf-8'),
ValueTypeEnum.TIME: lambda x: re.compile(r'\d\d:\d\d:\d\d').findall(
escape(x).encode('utf-8').decode('utf-8'))[0],
ValueTypeEnum.TIME: lambda x: TIME_RE.findall(escape(x).encode('utf-8').decode('utf-8'))[0],
ValueTypeEnum.DATETIME: str2datetime,
ValueTypeEnum.DATE: str2datetime,
ValueTypeEnum.JSON: lambda x: json.loads(x) if isinstance(x, six.string_types) and x else x,

View File

@ -249,26 +249,32 @@ class CIIndexValueDateTime(Model):
__table_args__ = (db.Index("datetime_attr_value_index", "attr_id", "value"),)
# class CIValueInteger(Model):
# __tablename__ = "c_value_integers"
#
# ci_id = db.Column(db.Integer, db.ForeignKey('c_cis.id'), nullable=False)
# attr_id = db.Column(db.Integer, db.ForeignKey('c_attributes.id'), nullable=False)
# value = db.Column(db.Integer, nullable=False)
#
# ci = db.relationship("CI", backref="c_value_integers.ci_id")
# attr = db.relationship("Attribute", backref="c_value_integers.attr_id")
#
#
# class CIValueFloat(Model):
# __tablename__ = "c_value_floats"
#
# ci_id = db.Column(db.Integer, db.ForeignKey('c_cis.id'), nullable=False)
# attr_id = db.Column(db.Integer, db.ForeignKey('c_attributes.id'), nullable=False)
# value = db.Column(DOUBLE, nullable=False)
#
# ci = db.relationship("CI", backref="c_value_floats.ci_id")
# attr = db.relationship("Attribute", backref="c_value_floats.attr_id")
class CIValueInteger(Model):
"""
Deprecated in a future version
"""
__tablename__ = "c_value_integers"
ci_id = db.Column(db.Integer, db.ForeignKey('c_cis.id'), nullable=False)
attr_id = db.Column(db.Integer, db.ForeignKey('c_attributes.id'), nullable=False)
value = db.Column(db.Integer, nullable=False)
ci = db.relationship("CI", backref="c_value_integers.ci_id")
attr = db.relationship("Attribute", backref="c_value_integers.attr_id")
class CIValueFloat(Model):
"""
Deprecated in a future version
"""
__tablename__ = "c_value_floats"
ci_id = db.Column(db.Integer, db.ForeignKey('c_cis.id'), nullable=False)
attr_id = db.Column(db.Integer, db.ForeignKey('c_attributes.id'), nullable=False)
value = db.Column(DOUBLE, nullable=False)
ci = db.relationship("CI", backref="c_value_floats.ci_id")
attr = db.relationship("Attribute", backref="c_value_floats.attr_id")
class CIValueText(Model):
@ -282,15 +288,18 @@ class CIValueText(Model):
attr = db.relationship("Attribute", backref="c_value_texts.attr_id")
# class CIValueDateTime(Model):
# __tablename__ = "c_value_datetime"
#
# ci_id = db.Column(db.Integer, db.ForeignKey('c_cis.id'), nullable=False)
# attr_id = db.Column(db.Integer, db.ForeignKey('c_attributes.id'), nullable=False)
# value = db.Column(db.DateTime, nullable=False)
#
# ci = db.relationship("CI", backref="c_value_datetime.ci_id")
# attr = db.relationship("Attribute", backref="c_value_datetime.attr_id")
class CIValueDateTime(Model):
"""
Deprecated in a future version
"""
__tablename__ = "c_value_datetime"
ci_id = db.Column(db.Integer, db.ForeignKey('c_cis.id'), nullable=False)
attr_id = db.Column(db.Integer, db.ForeignKey('c_attributes.id'), nullable=False)
value = db.Column(db.DateTime, nullable=False)
ci = db.relationship("CI", backref="c_value_datetime.ci_id")
attr = db.relationship("Attribute", backref="c_value_datetime.attr_id")
class CIValueJson(Model):