-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlModels.py
More file actions
71 lines (51 loc) · 1.96 KB
/
SqlModels.py
File metadata and controls
71 lines (51 loc) · 1.96 KB
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
from datetime import datetime
from sqlalchemy import PrimaryKeyConstraint, create_engine, Column, String, Float, DateTime, Integer, Float, Date
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlalchemy.types import PickleType
DATABASE_URL = "sqlite:///solar_alerts.db"
Base = declarative_base()
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(bind=engine)
#
# Define tables
#
class Configuration(Base):
__tablename__ = "configuration"
key = Column(String, primary_key=True)
value = Column(String) # JSON
class Site(Base):
__tablename__ = "sites"
site_id = Column(String, nullable=False)
history = Column(String, default="") # Track alert history
__table_args__ = (
PrimaryKeyConstraint('site_id'),
)
class Alert(Base):
__tablename__ = "alerts"
site_id = Column(String, nullable=False)
alert_type = Column(String, nullable=False)
details = Column(String, nullable=False)
severity = Column(Integer, nullable=False)
first_triggered = Column(DateTime, nullable=False)
resolved_date = Column(DateTime, nullable=True)
__table_args__ = (
PrimaryKeyConstraint('site_id', 'alert_type'),
)
class ProductionHistory(Base):
__tablename__ = "productionhistory"
production_day = Column(Date, primary_key=True)
# stores a set of SolarPlatform.ProductionRecord, one for each site
data = Column(PickleType, nullable=False)
total_noon_kw = Column(Float, nullable=False)
class Battery(Base):
__tablename__ = "batteries"
site_id = Column(String, nullable=False)
serial_number = Column(String, nullable=False)
model_number = Column(String, nullable=False)
state_of_energy = Column(Float, nullable=True)
last_updated = Column(DateTime, nullable = False)
__table_args__ = (
PrimaryKeyConstraint('site_id', 'serial_number'),
)
def init_fleet_db():
Base.metadata.create_all(engine)