Mod: database improvements, new methods

Mod: conda netgen version changed to master
This commit is contained in:
L-Nafaryus 2021-12-20 16:16:10 +05:00
parent c166be7801
commit 6601525d22
6 changed files with 113 additions and 36 deletions

View File

@ -13,7 +13,7 @@ from anisotropy.core.utils import ParallelRunner, Timer
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
from anisotropy.database import database, tables as T from anisotropy.database import Database, tables as T
from anisotropy.shaping import Simple, BodyCentered, FaceCentered from anisotropy.shaping import Simple, BodyCentered, FaceCentered
from anisotropy.meshing import Mesh from anisotropy.meshing import Mesh
@ -56,8 +56,7 @@ class UltimateRunner(object):
def prepareDatabase(self): def prepareDatabase(self):
# NOTE: separate function in cause of unpicklability of connections (use after process is started) # NOTE: separate function in cause of unpicklability of connections (use after process is started)
self.database = database self.database = Database(path = self.config["database"])
self.database.setup(self.config["database"])
def createRow(self): def createRow(self):
# create a row in each table for the current case # create a row in each table for the current case

View File

@ -1,9 +1,8 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from .models import __database__, __models__ from .models import __models__
from .db import Database
database = __database__
class tables: class tables:
pass pass

View File

@ -3,41 +3,119 @@
# License: GNU GPL version 3, see the file "LICENSE" for details. # License: GNU GPL version 3, see the file "LICENSE" for details.
import os import os
from peewee import SqliteDatabase from peewee import SqliteDatabase, JOIN
from . import models
class Database(SqliteDatabase): class Database(SqliteDatabase):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.filepath = None self.filepath = kwargs.get("path", None)
self.pragmas_ = kwargs.get("pragmas", { "foreign_keys": 1 }) self.pragmas_ = kwargs.get("pragmas", { "foreign_keys": 1, "journal_mode": "wal" })
self.field_types_ = kwargs.get("field_types", { "list": "text" }) self.field_types_ = kwargs.get("field_types", { "list": "text" })
self.autoconnect_ = kwargs.get("autoconnect", False) self.autoconnect_ = kwargs.get("autoconnect", False)
SqliteDatabase.__init__( SqliteDatabase.__init__(
self, self,
None, None,
pragmas = kwargs.get("pragmas", { "foreign_keys": 1 }), pragmas = self.pragmas_,
field_types = kwargs.get("field_types", { "list": "text" }), field_types = self.field_types_,
autoconnect = kwargs.get("autoconnect", False) autoconnect = self.autoconnect_
) )
if self.filepath:
self.setup()
@property @property
def tables(self): def tables(self):
return models.__models__ return models.__models__
def setup(self, filename: str): def setup(self, filename: str = None):
if not self.filepath: #if not self.filepath:
self.filepath = os.path.abspath(filename) if filename else None self.filepath = os.path.abspath(filename or self.filepath)
self.init( self.init(
self.filepath, self.filepath,
pragmas = self.pragmas_, pragmas = self.pragmas_,
#field_types = self.field_types_, #field_types = self.field_types_,
#autoconnect = self.autoconnect_ #autoconnect = self.autoconnect_
) )
models.__database_proxy__.initialize(self)
self.connect() self.connect()
self.create_tables(self.tables) self.create_tables(self.tables)
self.close() self.close()
def getExecution(self, idn):
query = models.Execution.select().where(models.Execution.exec_id == idn)
self.connect()
table = query.get() if query.exists() else None
self.close()
# NOTE: avoid circular or partial import return table
from . import models
def getLatest(self):
query = models.Execution.select()
#self.connect()
with self:
table = query[-1] if query.exists() else None
#self.close()
return table
def getShape(self, label, direction, alpha, execution = None):
execution = execution or self.getLatest()
query = (
models.Shape
.select()
.join(models.Execution, JOIN.LEFT_OUTER)
.where(
models.Execution.exec_id == execution.exec_id,
models.Shape.label == label,
models.Shape.direction == direction,
models.Shape.alpha == alpha
)
)
self.connect()
table = query.get() if query.exists() else None
self.close()
return table
def getMesh(self, label, direction, alpha, execution = None):
execution = execution or self.getLatest()
query = (
models.Mesh
.select()
.join(models.Shape, JOIN.LEFT_OUTER)
.join(models.Execution, JOIN.LEFT_OUTER)
.where(
models.Execution.exec_id == execution.exec_id,
models.Shape.label == label,
models.Shape.direction == direction,
models.Shape.alpha == alpha
)
)
self.connect()
table = query.get() if query.exists() else None
self.close()
return table
def getFlowOnephase(self, label, direction, alpha, execution = None):
execution = execution or self.getLatest()
query = (
models.Mesh
.select()
.join(models.Mesh, JOIN.LEFT_OUTER)
.join(models.Shape, JOIN.LEFT_OUTER)
.join(models.Execution, JOIN.LEFT_OUTER)
.where(
models.Execution.exec_id == execution.exec_id,
models.Shape.label == label,
models.Shape.direction == direction,
models.Shape.alpha == alpha
)
)
self.connect()
table = query.get() if query.exists() else None
self.close()
return table

View File

@ -8,13 +8,11 @@ from peewee import (
AutoField, ForeignKeyField, AutoField, ForeignKeyField,
TextField, FloatField, TextField, FloatField,
IntegerField, BooleanField, IntegerField, BooleanField,
TimeField, DateTimeField TimeField, DateTimeField, Proxy
) )
from .utils import JSONField from .utils import JSONField
from .db import Database
__database_proxy__ = Proxy()
__database__ = Database()
class Execution(Model): class Execution(Model):
exec_id = AutoField() exec_id = AutoField()
@ -23,13 +21,13 @@ class Execution(Model):
executionTime = TimeField(null = True) executionTime = TimeField(null = True)
class Meta: class Meta:
database = __database__ database = __database_proxy__
table_name = "executions" table_name = "executions"
class Shape(Model): class Shape(Model):
shape_id = AutoField() shape_id = AutoField()
exec_id = ForeignKeyField(Execution, backref = "executions") exec_id = ForeignKeyField(Execution, backref = "executions", on_delete = "CASCADE")
shapeStatus = TextField(null = True, default = "idle") shapeStatus = TextField(null = True, default = "idle")
shapeExecutionTime = TimeField(null = True) shapeExecutionTime = TimeField(null = True)
@ -52,14 +50,14 @@ class Shape(Model):
porosityRounded = FloatField(null = True) porosityRounded = FloatField(null = True)
class Meta: class Meta:
database = __database__ database = __database_proxy__
table_name = "shapes" table_name = "shapes"
#depends_on = Execution #depends_on = Execution
class Mesh(Model): class Mesh(Model):
mesh_id = AutoField() mesh_id = AutoField()
shape_id = ForeignKeyField(Shape, backref = "shapes") shape_id = ForeignKeyField(Shape, backref = "shapes", on_delete = "CASCADE")
meshStatus = TextField(null = True, default = "idle") meshStatus = TextField(null = True, default = "idle")
meshExecutionTime = TimeField(null = True) meshExecutionTime = TimeField(null = True)
@ -74,14 +72,14 @@ class Mesh(Model):
class Meta: class Meta:
database = __database__ database = __database_proxy__
table_name = "meshes" table_name = "meshes"
#depends_on = Execution #depends_on = Execution
class FlowOnephase(Model): class FlowOnephase(Model):
flow_id = AutoField() flow_id = AutoField()
mesh_id = ForeignKeyField(Mesh, backref = "meshes") mesh_id = ForeignKeyField(Mesh, backref = "meshes", on_delete = "CASCADE")
flowStatus = TextField(null = True, default = "idle") flowStatus = TextField(null = True, default = "idle")
flowExecutionTime = TimeField(null = True) flowExecutionTime = TimeField(null = True)
@ -90,7 +88,7 @@ class FlowOnephase(Model):
permeability = FloatField(null = True) permeability = FloatField(null = True)
class Meta: class Meta:
database = __database__ database = __database_proxy__
table_name = "flows" table_name = "flows"
#depends_on = Execution #depends_on = Execution

View File

@ -26,6 +26,9 @@ class ListField(TextField):
class JSONField(TextField): class JSONField(TextField):
# TODO: fix double quotes when use __eq__ in 'where' method
field_type = "TEXT"
def db_value(self, value): def db_value(self, value):
if isinstance(value, ndarray): if isinstance(value, ndarray):
formatted = list(value) formatted = list(value)

View File

@ -8,4 +8,4 @@ dependencies:
- poetry - poetry
- sqlite - sqlite
- occt - occt
- netgen - netgen==master