-
Notifications
You must be signed in to change notification settings - Fork 30
/
data_sources.py
186 lines (146 loc) · 5.94 KB
/
data_sources.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from __future__ import annotations
import json
from typing import TYPE_CHECKING, Any
from sqlalchemy.ext.mutable import MutableDict
import timely_beliefs as tb
from flexmeasures.data import db
from flask import current_app
import hashlib
if TYPE_CHECKING:
from flexmeasures.data.models.user import User
class DataGeneratorMixin:
_data_source: DataSource | None = None
@classmethod
def get_data_source_info(cls: type) -> dict:
"""
Create and return the data source info, from which a data source lookup/creation is possible.
See for instance get_data_source_for_job().
"""
source_info = dict(
name=current_app.config.get("FLEXMEASURES_DEFAULT_DATASOURCE")
) # default
from flexmeasures.data.models.planning import Scheduler
from flexmeasures.data.models.reporting import Reporter
if issubclass(cls, Reporter):
source_info["type"] = "reporter"
elif issubclass(cls, Scheduler):
source_info["type"] = "scheduler"
else:
source_info["type"] = "undefined"
return source_info
@property
def data_source(self):
from flexmeasures.data.services.data_sources import get_or_create_source
if self._data_source is None:
data_source_info = self.get_data_source_info()
self._data_source = get_or_create_source(
source=data_source_info.get("name"),
source_type=data_source_info.get("type"),
)
return self._data_source
class DataSource(db.Model, tb.BeliefSourceDBMixin):
"""Each data source is a data-providing entity."""
__tablename__ = "data_source"
__table_args__ = (db.UniqueConstraint("name", "user_id", "model", "version"),)
# The type of data source (e.g. user, forecaster or scheduler)
type = db.Column(db.String(80), default="")
# The id of the user source (can link e.g. to fm_user table)
user_id = db.Column(
db.Integer, db.ForeignKey("fm_user.id"), nullable=True, unique=True
)
user = db.relationship("User", backref=db.backref("data_source", lazy=True))
attributes = db.Column(MutableDict.as_mutable(db.JSON), nullable=False, default={})
attributes_hash = db.Column(db.LargeBinary(length=256))
# The model and version of a script source
model = db.Column(db.String(80), nullable=True)
version = db.Column(
db.String(17), # length supports up to version 999.999.999dev999
nullable=True,
)
sensors = db.relationship(
"Sensor",
secondary="timed_belief",
backref=db.backref("data_sources", lazy="dynamic"),
viewonly=True,
)
def __init__(
self,
name: str | None = None,
type: str | None = None,
user: User | None = None,
attributes: dict | None = None,
**kwargs,
):
if user is not None:
name = user.username
type = "user"
self.user = user
elif user is None and type == "user":
raise TypeError("A data source cannot have type 'user' but no user set.")
self.type = type
if attributes is not None:
self.attributes = attributes
self.attributes_hash = hashlib.sha256(
json.dumps(attributes).encode("utf-8")
).digest()
tb.BeliefSourceDBMixin.__init__(self, name=name)
db.Model.__init__(self, **kwargs)
@property
def label(self):
"""Human-readable label (preferably not starting with a capital letter, so it can be used in a sentence)."""
if self.type == "user":
return f"data entered by user {self.user.username}" # todo: give users a display name
elif self.type == "forecaster":
return f"forecast by {self.name}" # todo: give DataSource an optional db column to persist versioned models separately to the name of the data source?
elif self.type == "scheduler":
return f"schedule by {self.name}"
elif self.type == "reporter":
return f"report by {self.name}"
elif self.type == "crawling script":
return f"data retrieved from {self.name}"
elif self.type in ("demo script", "CLI script"):
return f"demo data entered by {self.name}"
else:
return f"data from {self.name}"
@property
def description(self):
"""Extended description
For example:
>>> DataSource("Seita", type="forecaster", model="naive", version="1.2").description
<<< "Seita's naive model v1.2.0"
"""
descr = self.name
if self.model:
descr += f"'s {self.model} model"
if self.version:
descr += f" v{self.version}"
return descr
def __repr__(self) -> str:
return "<Data source %r (%s)>" % (self.id, self.description)
def __str__(self) -> str:
return self.description
def to_dict(self) -> dict:
model_incl_version = self.model if self.model else ""
if self.model and self.version:
model_incl_version += f" (v{self.version})"
return dict(
id=self.id,
name=self.name,
model=model_incl_version,
type=self.type if self.type in ("forecaster", "scheduler") else "other",
description=self.description,
)
def get_attribute(self, attribute: str, default: Any = None) -> Any:
"""Looks for the attribute on the DataSource.
If not found, returns the default.
"""
if hasattr(self, attribute):
return getattr(self, attribute)
if attribute in self.attributes:
return self.attributes[attribute]
return default
def has_attribute(self, attribute: str) -> bool:
return attribute in self.attributes
def set_attribute(self, attribute: str, value):
if self.has_attribute(attribute):
self.attributes[attribute] = value