generated from dopt-python/py311
prepare database interaction
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
# %%
|
# %%
|
||||||
import datetime
|
import datetime
|
||||||
import enum
|
import enum
|
||||||
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
import polars as pl
|
import polars as pl
|
||||||
|
import sqlalchemy as sql
|
||||||
|
|
||||||
|
from wattanalyse import db
|
||||||
|
|
||||||
# %%
|
# %%
|
||||||
PROJECT_BASE = Path(__file__).parents[1]
|
PROJECT_BASE = Path(__file__).parents[1]
|
||||||
@@ -408,6 +413,36 @@ tmp = tmp.with_columns(
|
|||||||
.alias("Durchlaufzeit_Anzahl_Tage"),
|
.alias("Durchlaufzeit_Anzahl_Tage"),
|
||||||
)
|
)
|
||||||
tmp
|
tmp
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
# // dump to database
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_to_json(value: Any) -> str:
|
||||||
|
if isinstance(value, (datetime.date, datetime.datetime)):
|
||||||
|
return value.isoformat()
|
||||||
|
else:
|
||||||
|
raise TypeError
|
||||||
|
|
||||||
|
|
||||||
|
parsed_lists = tmp.with_columns(
|
||||||
|
pl.col(pl.List)
|
||||||
|
.map_elements(
|
||||||
|
lambda x: json.dumps(x.to_list(), default=_parse_to_json) if x is not None else None,
|
||||||
|
return_dtype=pl.String,
|
||||||
|
)
|
||||||
|
.name.keep()
|
||||||
|
)
|
||||||
|
parsed_lists
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
parsed_lists["Import-Ist_Historie"].item(0)
|
||||||
|
|
||||||
|
# TODO make UPSERT with staging
|
||||||
|
|
||||||
|
########################################################
|
||||||
# %%
|
# %%
|
||||||
tmp_1 = tmp.select("Meldezeitpunkt_Historie")
|
tmp_1 = tmp.select("Meldezeitpunkt_Historie")
|
||||||
tmp_1 = tmp_1.with_columns(
|
tmp_1 = tmp_1.with_columns(
|
||||||
|
|||||||
27
prototypes/02_save_to_db.py
Normal file
27
prototypes/02_save_to_db.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# %%
|
||||||
|
import datetime
|
||||||
|
import json
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
# %%
|
||||||
|
dt = datetime.datetime.now()
|
||||||
|
date = dt.date()
|
||||||
|
|
||||||
|
# %%
|
||||||
|
val = [dt, date]
|
||||||
|
json.dumps(val)
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
def _parse_to_json(value: Any) -> str:
|
||||||
|
if isinstance(value, datetime.date):
|
||||||
|
return value.isoformat()
|
||||||
|
elif isinstance(value, datetime.datetime):
|
||||||
|
return value.isoformat()
|
||||||
|
else:
|
||||||
|
raise TypeError
|
||||||
|
|
||||||
|
|
||||||
|
# %%
|
||||||
|
json.dumps(val, default=_parse_to_json)
|
||||||
|
# %%
|
||||||
46
src/wattanalyse/db.py
Normal file
46
src/wattanalyse/db.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import sqlalchemy as sql
|
||||||
|
from sqlalchemy import Column, Table
|
||||||
|
|
||||||
|
from wattanalyse import constants
|
||||||
|
|
||||||
|
assert constants.Config.DB_PATH_INTERNAL.parent.exists(), (
|
||||||
|
"database parent folder does not exists"
|
||||||
|
)
|
||||||
|
|
||||||
|
ENGINE = sql.create_engine(f"sqlite:///{constants.Config.DB_PATH_INTERNAL}")
|
||||||
|
|
||||||
|
MD_INTERNAL = sql.MetaData()
|
||||||
|
|
||||||
|
|
||||||
|
intern_prod_order_t: Table = Table(
|
||||||
|
"Produktionsauftrag-Einzelsicht",
|
||||||
|
MD_INTERNAL,
|
||||||
|
Column("PA", sql.Integer, primary_key=True),
|
||||||
|
Column("PA_Pos", sql.Integer, primary_key=True),
|
||||||
|
Column("Konfektionär", sql.Text, nullable=False),
|
||||||
|
Column("Meldezeitpunkt_Historie", sql.Text, nullable=False),
|
||||||
|
Column("Liefertermin_Soll", sql.Date, nullable=False),
|
||||||
|
Column("Bestaetigter-Import_Historie", sql.Text, nullable=False),
|
||||||
|
Column("Liefertermin_Ist", sql.Date, nullable=True),
|
||||||
|
Column("Import-Ist_Historie", sql.Text, nullable=False),
|
||||||
|
Column("Import-Ist_letzter_Wert", sql.Date, nullable=True),
|
||||||
|
Column("Import-Ist_geaendert", sql.Boolean, nullable=False),
|
||||||
|
Column("Import-Ist_Anzahl_Aenderungen", sql.Integer, nullable=False),
|
||||||
|
Column("Tage_zu_letzter_PSM_Historie", sql.Text, nullable=False),
|
||||||
|
Column("Tage_zu_letzter_PSM_Durchschnitt", sql.Float, nullable=True),
|
||||||
|
Column("Prod-EP10_Historie", sql.Text, nullable=False),
|
||||||
|
Column("Prod-EP20_Historie", sql.Text, nullable=False),
|
||||||
|
Column("Prod-EP30_Historie", sql.Text, nullable=False),
|
||||||
|
Column("Prod-EP40_Historie", sql.Text, nullable=False),
|
||||||
|
Column("Prod-EP50_Historie", sql.Text, nullable=False),
|
||||||
|
Column("Prod-Qualitaet_Historie", sql.Text, nullable=False),
|
||||||
|
Column("Prod-Qualitaet_Durchschnitt", sql.Float, nullable=False),
|
||||||
|
Column("Prod-Start_Historie", sql.Text, nullable=False),
|
||||||
|
Column("Prod-Start", sql.Date, nullable=True),
|
||||||
|
Column("Terminabweichung_Anzahl_Tage", sql.Integer, nullable=True),
|
||||||
|
Column("Terminunterschreitung", sql.Boolean, nullable=True),
|
||||||
|
Column("Terminüberschreitung", sql.Boolean, nullable=True),
|
||||||
|
Column("Durchlaufzeit_Anzahl_Tage", sql.Float, nullable=True),
|
||||||
|
)
|
||||||
|
|
||||||
|
MD_INTERNAL.create_all(ENGINE)
|
||||||
Reference in New Issue
Block a user