2023-03-12 12:02:01 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from sqlalchemy import create_engine, inspect
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
2023-05-15 18:19:04 +08:00
|
|
|
from sqlalchemy import (Column, Integer, DECIMAL, String, ForeignKey,
|
|
|
|
BigInteger, DateTime, PickleType, Boolean, Text)
|
2023-03-12 12:02:01 +08:00
|
|
|
from sqlalchemy.sql import func
|
|
|
|
from sqlalchemy.orm import relationship, Session
|
|
|
|
from sqlalchemy.pool import NullPool
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
with open('conf_db.yaml') as file:
|
|
|
|
dct_conf_db_all = yaml.full_load(file)
|
|
|
|
is_local_db = dct_conf_db_all['is_local_db']
|
|
|
|
if is_local_db:
|
|
|
|
dct_conf_db = dct_conf_db_all['local']
|
|
|
|
else:
|
|
|
|
dct_conf_db = dct_conf_db_all['remote']
|
|
|
|
|
|
|
|
with open('conf_db_prefix.yaml') as file:
|
|
|
|
dct_conf_db_prefix = yaml.full_load(file)
|
|
|
|
db_name_prefix = dct_conf_db_prefix['db_name_prefix']
|
|
|
|
|
|
|
|
|
2023-05-15 13:44:21 +08:00
|
|
|
str_login = 'mysql://{}:{}@{}:{}/{}'.format(dct_conf_db['user_name'],
|
|
|
|
dct_conf_db['password'],
|
|
|
|
dct_conf_db['address'],
|
|
|
|
dct_conf_db['port'],
|
|
|
|
dct_conf_db['db_name'])
|
2023-05-15 13:37:05 +08:00
|
|
|
print('DB is {}:{}/{}'.format(dct_conf_db['address'],
|
|
|
|
dct_conf_db['port'], dct_conf_db['db_name']))
|
2023-03-12 12:02:01 +08:00
|
|
|
|
2023-05-15 13:37:05 +08:00
|
|
|
# must be null pool to avoid connection lost error
|
|
|
|
engine = create_engine(str_login, poolclass=NullPool)
|
2023-03-12 12:02:01 +08:00
|
|
|
ins = inspect(engine)
|
|
|
|
|
2023-03-12 22:21:39 +08:00
|
|
|
Base = declarative_base()
|
2023-03-12 12:02:01 +08:00
|
|
|
|
|
|
|
db_session = Session(bind=engine)
|
|
|
|
|
|
|
|
|
|
|
|
class Experiment(Base):
|
|
|
|
__tablename__ = f"{db_name_prefix}_experiment"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
|
|
|
2023-05-15 16:19:05 +08:00
|
|
|
idx_scenario = Column(Integer, nullable=False)
|
|
|
|
idx_init_removal = Column(Integer, nullable=False)
|
2023-03-12 12:02:01 +08:00
|
|
|
|
|
|
|
# fixed parameters
|
|
|
|
n_sample = Column(Integer, nullable=False)
|
|
|
|
n_iter = Column(Integer, nullable=False)
|
|
|
|
|
|
|
|
# variables
|
2023-03-14 18:53:00 +08:00
|
|
|
dct_lst_init_remove_firm_prod = Column(PickleType, nullable=False)
|
2023-03-16 15:07:43 +08:00
|
|
|
g_bom = Column(Text(4294000000), nullable=False)
|
2023-05-15 18:18:41 +08:00
|
|
|
|
2023-05-15 16:19:05 +08:00
|
|
|
n_max_trial = Column(Integer, nullable=False)
|
2023-05-15 18:18:41 +08:00
|
|
|
crit_supplier = Column(DECIMAL(8, 4), nullable=False)
|
2023-06-10 19:51:50 +08:00
|
|
|
firm_req_prf_size = Column(DECIMAL(8, 4), nullable=False)
|
2023-06-10 20:56:34 +08:00
|
|
|
firm_req_prf_conn = Column(Boolean, nullable=False)
|
2023-06-10 19:51:50 +08:00
|
|
|
firm_acc_prf_size = Column(DECIMAL(8, 4), nullable=False)
|
2023-06-10 20:56:34 +08:00
|
|
|
firm_acc_prf_conn = Column(Boolean, nullable=False)
|
2023-06-10 23:23:44 +08:00
|
|
|
netw_sply_prf_n = Column(Integer, nullable=False)
|
2023-06-10 19:51:50 +08:00
|
|
|
netw_sply_prf_size = Column(DECIMAL(8, 4), nullable=False)
|
2023-06-10 21:26:35 +08:00
|
|
|
cap_limit_prob_type = Column(String(16), nullable=False)
|
|
|
|
cap_limit_level = Column(DECIMAL(8, 4), nullable=False)
|
2023-05-15 18:18:41 +08:00
|
|
|
diff_new_conn = Column(DECIMAL(8, 4), nullable=False)
|
|
|
|
diff_remove = Column(DECIMAL(8, 4), nullable=False)
|
2023-06-05 16:01:20 +08:00
|
|
|
proactive_ratio = Column(DECIMAL(8, 4), nullable=False)
|
2023-03-12 12:02:01 +08:00
|
|
|
|
2023-05-15 13:37:05 +08:00
|
|
|
sample = relationship(
|
|
|
|
'Sample', back_populates='experiment', lazy='dynamic')
|
2023-03-12 12:02:01 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'<Experiment: {self.id}>'
|
|
|
|
|
|
|
|
|
|
|
|
class Sample(Base):
|
|
|
|
__tablename__ = f"{db_name_prefix}_sample"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
2023-05-15 13:37:05 +08:00
|
|
|
e_id = Column(Integer, ForeignKey('{}.id'.format(
|
|
|
|
f"{db_name_prefix}_experiment")), nullable=False)
|
2023-03-12 12:02:01 +08:00
|
|
|
|
|
|
|
idx_sample = Column(Integer, nullable=False)
|
|
|
|
seed = Column(BigInteger, nullable=False)
|
2023-05-15 13:37:05 +08:00
|
|
|
# -1, waiting; 0, running; 1, done
|
|
|
|
is_done_flag = Column(Integer, nullable=False)
|
2023-03-12 12:02:01 +08:00
|
|
|
computer_name = Column(String(64), nullable=True)
|
|
|
|
ts_done = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
stop_t = Column(Integer, nullable=True)
|
|
|
|
|
2023-03-16 15:07:43 +08:00
|
|
|
g_firm = Column(Text(4294000000), nullable=True)
|
2023-05-15 13:37:05 +08:00
|
|
|
|
|
|
|
experiment = relationship(
|
|
|
|
'Experiment', back_populates='sample', uselist=False)
|
2023-03-13 19:47:25 +08:00
|
|
|
result = relationship('Result', back_populates='sample', lazy='dynamic')
|
2023-03-12 12:02:01 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'<Sample id: {self.id}>'
|
|
|
|
|
|
|
|
|
2023-03-13 19:47:25 +08:00
|
|
|
class Result(Base):
|
|
|
|
__tablename__ = f"{db_name_prefix}_result"
|
2023-03-12 12:02:01 +08:00
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
2023-05-15 13:37:05 +08:00
|
|
|
s_id = Column(Integer, ForeignKey('{}.id'.format(
|
|
|
|
f"{db_name_prefix}_sample")), nullable=False)
|
2023-03-12 12:02:01 +08:00
|
|
|
|
2023-03-13 21:53:50 +08:00
|
|
|
id_firm = Column(String(10), nullable=False)
|
|
|
|
id_product = Column(String(10), nullable=False)
|
2023-03-13 19:47:25 +08:00
|
|
|
ts = Column(Integer, nullable=False)
|
|
|
|
is_disrupted = Column(Boolean, nullable=True)
|
|
|
|
is_removed = Column(Boolean, nullable=True)
|
2023-03-12 12:02:01 +08:00
|
|
|
|
2023-03-13 19:47:25 +08:00
|
|
|
sample = relationship('Sample', back_populates='result', uselist=False)
|
2023-03-12 12:02:01 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
2023-03-13 19:47:25 +08:00
|
|
|
return f'<Product id: {self.id}>'
|
2023-03-12 12:02:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
Base.metadata.drop_all()
|
|
|
|
Base.metadata.create_all()
|