-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
116 lines (93 loc) · 2.91 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from datetime import datetime
from pathlib import Path
from sqlalchemy import (
create_engine,
Column,
Integer,
Float,
DateTime,
Boolean,
desc,
JSON,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import (
sessionmaker,
scoped_session,
)
DB_PATH = Path("/media/data/stats.sqlite")
DB_BACKUP = Path("/media/data/stats.sqlite.bak")
DB_ENGINE = f"sqlite:///{DB_PATH}"
Base = declarative_base()
def move_old_database(file_size_limit: int):
"""
Move old database if filesize is bigger than
file_size_limit in MiB
"""
file_size_limit *= 1024 ** 2
if DB_PATH.exists() and DB_PATH.stat().st_size > file_size_limit:
DB_PATH.rename(DB_BACKUP)
DB_PATH.touch()
class Cycle(Base):
__tablename__ = "cycle"
id = Column(Integer, primary_key=True)
timestamp = Column("timestamp", DateTime, default=datetime.utcnow)
cycle = Column("cycle", Integer, nullable=False)
class Configuration(Base):
__tablename__ = "configuration"
id = Column(Integer, primary_key=True)
cycle = Column(Integer, nullable=False)
capacity = Column("capacity", Float)
dimension = Column(Integer)
settings = Column(Integer)
class State(Base):
__tablename__ = "state"
id = Column(Integer, primary_key=True)
cycle = Column(Integer, nullable=False)
row = Column(Integer, nullable=False)
timestamp = Column(DateTime, default=datetime.utcnow)
onoff = Column(Boolean, nullable=False)
class Error(Base):
__tablename__ = "error"
id = Column(Integer, primary_key=True)
row = Column(Integer, nullable=False)
timestamp = Column(DateTime, default=datetime.utcnow)
cycle = Column(Integer, nullable=False)
error = Column(Integer)
class Statistik(Base):
__tablename__ = "statistik"
id = Column(Integer, primary_key=True)
cycle = Column(Integer, nullable=False)
row = Column(Integer, nullable=False)
timestamp = Column(DateTime, default=datetime.utcnow)
voltage = Column(Float)
current = Column(Float)
charge = Column(Float)
cell_voltages = Column(JSON)
temperature = Column(Float)
# check if database is bigger as 20 MiB
# and move it
# todo: make it dynamic in 4.2
move_old_database(20)
engine = create_engine(DB_ENGINE, connect_args={"check_same_thread": False})
try:
Base.metadata.create_all(engine)
except Exception as e:
print(e)
DB_PATH.touch()
Base.metadata.create_all(engine)
Session = scoped_session(sessionmaker(bind=engine))
def to_dict(obj):
if not obj:
return {}
return {key: value for key, value in vars(obj).items() if not key.startswith("_")}
def get_cycle(session):
cycle = session.query(Cycle.cycle).order_by(desc("id")).first()
if cycle:
return cycle[0]
return 0
def set_cycle(session):
cycle_id = get_cycle(session) + 1
session.add(Cycle(cycle=cycle_id))
session.commit()
return cycle_id