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__)
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.meshing import Mesh
@ -56,9 +56,8 @@ class UltimateRunner(object):
def prepareDatabase(self):
# NOTE: separate function in cause of unpicklability of connections (use after process is started)
self.database = database
self.database.setup(self.config["database"])
self.database = Database(path = self.config["database"])
def createRow(self):
# create a row in each table for the current case
with self.database:

View File

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

View File

@ -3,41 +3,119 @@
# License: GNU GPL version 3, see the file "LICENSE" for details.
import os
from peewee import SqliteDatabase
from peewee import SqliteDatabase, JOIN
from . import models
class Database(SqliteDatabase):
def __init__(self, *args, **kwargs):
self.filepath = None
self.pragmas_ = kwargs.get("pragmas", { "foreign_keys": 1 })
self.filepath = kwargs.get("path", None)
self.pragmas_ = kwargs.get("pragmas", { "foreign_keys": 1, "journal_mode": "wal" })
self.field_types_ = kwargs.get("field_types", { "list": "text" })
self.autoconnect_ = kwargs.get("autoconnect", False)
SqliteDatabase.__init__(
self,
None,
pragmas = kwargs.get("pragmas", { "foreign_keys": 1 }),
field_types = kwargs.get("field_types", { "list": "text" }),
autoconnect = kwargs.get("autoconnect", False)
pragmas = self.pragmas_,
field_types = self.field_types_,
autoconnect = self.autoconnect_
)
if self.filepath:
self.setup()
@property
def tables(self):
return models.__models__
def setup(self, filename: str):
if not self.filepath:
self.filepath = os.path.abspath(filename) if filename else None
self.init(
self.filepath,
pragmas = self.pragmas_,
#field_types = self.field_types_,
#autoconnect = self.autoconnect_
)
def setup(self, filename: str = None):
#if not self.filepath:
self.filepath = os.path.abspath(filename or self.filepath)
self.init(
self.filepath,
pragmas = self.pragmas_,
#field_types = self.field_types_,
#autoconnect = self.autoconnect_
)
models.__database_proxy__.initialize(self)
self.connect()
self.create_tables(self.tables)
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
from . import models
return table
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,
TextField, FloatField,
IntegerField, BooleanField,
TimeField, DateTimeField
TimeField, DateTimeField, Proxy
)
from .utils import JSONField
from .db import Database
__database__ = Database()
__database_proxy__ = Proxy()
class Execution(Model):
exec_id = AutoField()
@ -23,13 +21,13 @@ class Execution(Model):
executionTime = TimeField(null = True)
class Meta:
database = __database__
database = __database_proxy__
table_name = "executions"
class Shape(Model):
shape_id = AutoField()
exec_id = ForeignKeyField(Execution, backref = "executions")
exec_id = ForeignKeyField(Execution, backref = "executions", on_delete = "CASCADE")
shapeStatus = TextField(null = True, default = "idle")
shapeExecutionTime = TimeField(null = True)
@ -52,14 +50,14 @@ class Shape(Model):
porosityRounded = FloatField(null = True)
class Meta:
database = __database__
database = __database_proxy__
table_name = "shapes"
#depends_on = Execution
class Mesh(Model):
mesh_id = AutoField()
shape_id = ForeignKeyField(Shape, backref = "shapes")
shape_id = ForeignKeyField(Shape, backref = "shapes", on_delete = "CASCADE")
meshStatus = TextField(null = True, default = "idle")
meshExecutionTime = TimeField(null = True)
@ -74,14 +72,14 @@ class Mesh(Model):
class Meta:
database = __database__
database = __database_proxy__
table_name = "meshes"
#depends_on = Execution
class FlowOnephase(Model):
flow_id = AutoField()
mesh_id = ForeignKeyField(Mesh, backref = "meshes")
mesh_id = ForeignKeyField(Mesh, backref = "meshes", on_delete = "CASCADE")
flowStatus = TextField(null = True, default = "idle")
flowExecutionTime = TimeField(null = True)
@ -90,7 +88,7 @@ class FlowOnephase(Model):
permeability = FloatField(null = True)
class Meta:
database = __database__
database = __database_proxy__
table_name = "flows"
#depends_on = Execution

View File

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

View File

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