Skip to content

Commit

Permalink
add command cmdb-index-table-upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
pycook committed Jul 25, 2023
1 parent 13eac93 commit 005be47
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 34 deletions.
3 changes: 3 additions & 0 deletions cmdb-api/api/commands/click_acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
@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
Expand Down
37 changes: 37 additions & 0 deletions cmdb-api/api/commands/click_cmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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()
6 changes: 3 additions & 3 deletions cmdb-api/api/commands/init_common_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self):

def import_user_from_acl(self):
"""
从ACL导入用户
Import users from ACL
"""

acl = ACLManager('acl')
Expand Down Expand Up @@ -149,7 +149,7 @@ def create_acl_role_with_department(self):
@with_appcontext
def init_import_user_from_acl():
"""
从ACL导入用户
Import users from ACL
"""
InitEmployee().import_user_from_acl()

Expand All @@ -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()
5 changes: 3 additions & 2 deletions cmdb-api/api/lib/cmdb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
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))
Expand All @@ -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,
Expand Down
67 changes: 38 additions & 29 deletions cmdb-api/api/models/cmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 005be47

Please sign in to comment.