From 83e91727227168a9f40fe5dd820e8cb533d49cee Mon Sep 17 00:00:00 2001 From: pycook Date: Mon, 24 Jul 2023 21:55:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=9F=E5=BC=833=E4=B8=AA=E8=A1=A8:=20c=5Fva?= =?UTF-8?q?lue=5Fdatetime=20=20c=5Fvalue=5Ffloats=20=20c=5Fvalue=5Finteger?= =?UTF-8?q?s,=20time=E7=B1=BB=E5=9E=8B=E5=B1=9E=E6=80=A7=E5=80=BC=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=86=99=E5=85=A5=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmdb-api/api/lib/cmdb/utils.py | 25 +++++++-------- cmdb-api/api/models/cmdb.py | 58 +++++++++++++++++----------------- 2 files changed, 41 insertions(+), 42 deletions(-) diff --git a/cmdb-api/api/lib/cmdb/utils.py b/cmdb-api/api/lib/cmdb/utils.py index 1ea82af..725a0f2 100644 --- a/cmdb-api/api/lib/cmdb/utils.py +++ b/cmdb-api/api/lib/cmdb/utils.py @@ -4,6 +4,7 @@ from __future__ import unicode_literals import datetime import json +import re import six from markupsafe import escape @@ -31,7 +32,8 @@ 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: 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.DATETIME: str2datetime, ValueTypeEnum.DATE: str2datetime, ValueTypeEnum.JSON: lambda x: json.loads(x) if isinstance(x, six.string_types) and x else x, @@ -61,15 +63,11 @@ class ValueTypeMap(object): ValueTypeEnum.INT: model.IntegerChoice, ValueTypeEnum.FLOAT: model.FloatChoice, ValueTypeEnum.TEXT: model.TextChoice, + ValueTypeEnum.TIME: model.TextChoice, } table = { - ValueTypeEnum.INT: model.CIValueInteger, ValueTypeEnum.TEXT: model.CIValueText, - ValueTypeEnum.DATETIME: model.CIValueDateTime, - ValueTypeEnum.DATE: model.CIValueDateTime, - ValueTypeEnum.TIME: model.CIValueText, - ValueTypeEnum.FLOAT: model.CIValueFloat, ValueTypeEnum.JSON: model.CIValueJson, 'index_{0}'.format(ValueTypeEnum.INT): model.CIIndexValueInteger, 'index_{0}'.format(ValueTypeEnum.TEXT): model.CIIndexValueText, @@ -81,12 +79,7 @@ class ValueTypeMap(object): } table_name = { - ValueTypeEnum.INT: 'c_value_integers', ValueTypeEnum.TEXT: 'c_value_texts', - ValueTypeEnum.DATETIME: 'c_value_datetime', - ValueTypeEnum.DATE: 'c_value_datetime', - ValueTypeEnum.TIME: 'c_value_texts', - ValueTypeEnum.FLOAT: 'c_value_floats', ValueTypeEnum.JSON: 'c_value_json', 'index_{0}'.format(ValueTypeEnum.INT): 'c_value_index_integers', 'index_{0}'.format(ValueTypeEnum.TEXT): 'c_value_index_texts', @@ -117,8 +110,11 @@ class TableMap(object): @property def table(self): attr = AttributeCache.get(self.attr_name) if not self.attr else self.attr - if self.is_index is None: + if attr.value_type != ValueTypeEnum.TEXT and attr.value_type != ValueTypeEnum.JSON: + self.is_index = True + elif self.is_index is None: self.is_index = attr.is_index + i = "index_{0}".format(attr.value_type) if self.is_index else attr.value_type return ValueTypeMap.table.get(i) @@ -126,8 +122,11 @@ class TableMap(object): @property def table_name(self): attr = AttributeCache.get(self.attr_name) if not self.attr else self.attr - if self.is_index is None: + if attr.value_type != ValueTypeEnum.TEXT and attr.value_type != ValueTypeEnum.JSON: + self.is_index = True + elif self.is_index is None: self.is_index = attr.is_index + i = "index_{0}".format(attr.value_type) if self.is_index else attr.value_type return ValueTypeMap.table_name.get(i) diff --git a/cmdb-api/api/models/cmdb.py b/cmdb-api/api/models/cmdb.py index e1693cb..e3433d6 100644 --- a/cmdb-api/api/models/cmdb.py +++ b/cmdb-api/api/models/cmdb.py @@ -249,26 +249,26 @@ 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): +# __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 CIValueText(Model): @@ -282,15 +282,15 @@ 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): +# __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):