Fix: db fields conversion

Mod: separated update functions
This commit is contained in:
L-Nafaryus 2021-08-03 22:59:51 +05:00
parent 60d4e37749
commit 3d05f661b3
No known key found for this signature in database
GPG Key ID: C76D8DCD2727DBB7
2 changed files with 121 additions and 72 deletions

View File

@ -138,6 +138,7 @@ class Anisotropy(object):
from math import sqrt from math import sqrt
structures = deepcopy(self.env["structures"]) structures = deepcopy(self.env["structures"])
self.params = []
for structure in structures: for structure in structures:
_theta = structure["geometry"]["theta"] _theta = structure["geometry"]["theta"]
@ -194,7 +195,7 @@ class Anisotropy(object):
) )
geometry = dict( geometry = dict(
theta = theta, theta = theta,
direction = direction, direction = [ float(num) for num in direction ],
r0 = r0, r0 = r0,
L = L, L = L,
radius = radius, radius = radius,
@ -236,11 +237,109 @@ class Anisotropy(object):
]) ])
def _updateStructure(self, src: dict, queryMain) -> int:
raw = deepcopy(src["geometry"])
raw.update(
name = src["name"],
path = src["path"]
)
with self.db.atomic():
if not queryMain.exists():
tabID = Structure.create(**raw)
else:
req = queryMain.dicts().get()
tabID = req["id"]
query = (
Structure.update(**raw)
.where(
Structure.name == req["name"],
Structure.direction == req["direction"],
Structure.theta == req["theta"]
)
)
query.execute()
return tabID
def _updateMesh(self, src: dict, queryMain, structureID) -> int:
raw = deepcopy(src)
with self.db.atomic():
if not queryMain.exists():
tabID = Mesh.create(
structure_id = structureID,
**raw
)
else:
req = queryMain.dicts().get()
tabID = req["mesh_id"]
query = (
Mesh.update(**raw)
.where(
Mesh.structure_id == req["id"]
)
)
query.execute()
return tabID
def _updateSubMesh(self, srcs: list, queryMain, meshID) -> None:
for src in srcs:
raw = deepcopy(src)
with self.db.atomic():
if not queryMain.exists():
tabID = SubMesh.create(
mesh_id = meshID,
**raw
)
else:
req = queryMain.dicts().get()
tabID = req["mesh_id"]
query = (
SubMesh.update(**raw)
.where(
SubMesh.mesh_id == req["mesh_id"]
SubMesh.name == src["name"]
)
)
query.execute()
def _updateMeshResult(self, src: dict, queryMain, meshID) -> None:
raw = deepcopy(src)
with self.db.atomic():
if not queryMain.exists():
tabID = MeshResult.create(
mesh_id = meshID,
**raw
)
else:
req = queryMain.dicts().get()
tabID = req["mesh_id"]
query = (
Mesh.update(**raw)
.where(
Mesh.mesh_id == req["mesh_id"]
)
)
query.execute()
@timer @timer
def updateDB(self): def updateDB(self):
for entry in self.params: for entry in self.params:
query = (Structure query = (
.select() Structure
.select(Structure, Mesh)
.join(Mesh, JOIN.INNER, on = (Mesh.structure_id == Structure.id)) .join(Mesh, JOIN.INNER, on = (Mesh.structure_id == Structure.id))
.where( .where(
Structure.name == entry["name"], Structure.name == entry["name"],
@ -249,66 +348,18 @@ class Anisotropy(object):
) )
) )
s = deepcopy(entry["geometry"]) # TODO: empty entries
s.update( structureID = self._updateStructure(entry, query)
name = entry["name"], meshID = self._updateMesh(entry["mesh"], query, structureID)
path = entry["path"] self._updateSubMesh(entry.get("submesh", []), query, meshID)
) self._updateMeshResult(entry.get("meshresults", {}), query, meshID)
m = deepcopy(entry["mesh"])
sm = deepcopy(entry.get("submesh", []))
mr = deepcopy(entry.get("meshResult", {}))
if not query.exists():
with self.db.atomic():
stab = Structure.create(**s)
m.update(structure_id = stab)
mtab = Mesh.create(**m)
for item in sm:
smtab = SubMesh.create(**item, mesh_id = mtab)
mr.update(mesh_id = mtab)
mrtab = MeshResult.create(**mr)
else:
with self.db.atomic():
(Structure.update(**s)
.where(
Structure.name == entry["name"],
Structure.direction == str(entry["geometry"]["direction"]),
Structure.theta == entry["geometry"]["theta"]
)
.execute())
(Mesh.update(**m)
.where(
Mesh.structure_id == query.get().id
)
.execute())
for item in sm:
(SubMesh.update(**item)
.where(
SubMesh.mesh_id == query.get().mesh_id,
SubMesh.name == item["name"]
)
.execute())
(MeshResult.update(**mr)
.where(
MeshResult.mesh_id == query.get().mesh_id)
.execute())
@timer @timer
def updateFromDB(self): def updateFromDB(self):
squery = Structure.select().order_by(Structure.id) squery = Structure.select().order_by(Structure.id)
mquery = Mesh.select().order_by(Mesh.structure_id) mquery = Mesh.select().order_by(Mesh.structure_id)
self.params = []
for s, m in zip(squery.dicts(), mquery.dicts()): for s, m in zip(squery.dicts(), mquery.dicts()):
name = s.pop("name") name = s.pop("name")

View File

@ -9,15 +9,12 @@ class ListField(Field):
def python_value(self, value): def python_value(self, value):
pval = [] pval = []
for ch in value[1 : -1].split(","): for entry in value[1 : -1].split(","):
try: try:
pval.append(float(ch)) pval.append(float(entry))
except: except:
pass pval.append(entry.strip().replace("'", ""))
finally:
pval.append(ch.strip().replace("'", ""))
return pval return pval
@ -47,6 +44,7 @@ class Structure(BaseModel):
class Mesh(BaseModel): class Mesh(BaseModel):
mesh_id = PrimaryKeyField()
structure_id = ForeignKeyField(Structure, backref = "meshes") structure_id = ForeignKeyField(Structure, backref = "meshes")
maxSize = FloatField(null = True) maxSize = FloatField(null = True)
@ -106,13 +104,13 @@ class MeshResult(BaseModel):
surfaceArea = FloatField(null = True) surfaceArea = FloatField(null = True)
volume = FloatField(null = True) volume = FloatField(null = True)
elements = FloatField(null = True) elements = IntegerField(null = True)
edges = FloatField(null = True) edges = IntegerField(null = True)
faces = FloatField(null = True) faces = IntegerField(null = True)
volumes = FloatField(null = True) volumes = IntegerField(null = True)
tetrahedrons = FloatField(null = True) tetrahedrons = IntegerField(null = True)
prisms = FloatField(null = True) prisms = IntegerField(null = True)
pyramids = FloatField(null = True) pyramids = IntegerField(null = True)
calculationTime = TimeField(null = True) calculationTime = TimeField(null = True)