class SensorAnnotationRelationship(db.Model): """Links annotations to sensors.""" __tablename__ = "annotations_sensors" id = db.Column(db.Integer(), primary_key=True) sensor_id = db.Column(db.Integer, db.ForeignKey("sensor.id")) annotation_id = db.Column(db.Integer, db.ForeignKey("annotation.id")) __table_args__ = (db.UniqueConstraint( "annotation_id", "sensor_id", name="annotations_sensors_annotation_id_key", ), )
class GenericAssetAnnotationRelationship(db.Model): """Links annotations to generic assets.""" __tablename__ = "annotations_assets" id = db.Column(db.Integer(), primary_key=True) generic_asset_id = db.Column(db.Integer, db.ForeignKey("generic_asset.id")) annotation_id = db.Column(db.Integer, db.ForeignKey("annotation.id")) __table_args__ = (db.UniqueConstraint( "annotation_id", "generic_asset_id", name="annotations_assets_annotation_id_key", ), )
class AccountAnnotationRelationship(db.Model): """Links annotations to accounts.""" __tablename__ = "annotations_accounts" id = db.Column(db.Integer(), primary_key=True) account_id = db.Column(db.Integer, db.ForeignKey("account.id")) annotation_id = db.Column(db.Integer, db.ForeignKey("annotation.id")) __table_args__ = (db.UniqueConstraint( "annotation_id", "account_id", name="annotations_accounts_annotation_id_key", ), )
class Price(TimedValue, db.Model): """ All prices are stored in one slim table. This model is now considered legacy. See TimedBelief. """ sensor_id = db.Column(db.Integer(), db.ForeignKey("sensor.id"), primary_key=True, index=True) sensor = db.relationship("Sensor", backref=db.backref("prices", lazy=True)) @classmethod def make_query(cls, **kwargs) -> Query: """Construct the database query.""" return super().make_query(**kwargs) def __init__(self, use_legacy_kwargs: bool = True, **kwargs): # todo: deprecate the 'market_id' argument in favor of 'sensor_id' (announced v0.8.0) if "market_id" in kwargs and "sensor_id" not in kwargs: kwargs["sensor_id"] = tb_utils.replace_deprecated_argument( "market_id", kwargs["market_id"], "sensor_id", None, ) kwargs.pop("market_id", None) # todo: deprecate the 'Price' class in favor of 'TimedBelief' (announced v0.8.0) if use_legacy_kwargs is False: # Create corresponding TimedBelief belief = TimedBelief(**kwargs) db.session.add(belief) # Convert key names for legacy model kwargs["value"] = kwargs.pop("event_value") kwargs["datetime"] = kwargs.pop("event_start") kwargs["horizon"] = kwargs.pop("belief_horizon") kwargs["sensor_id"] = kwargs.pop("sensor").id kwargs["data_source_id"] = kwargs.pop("source").id else: import warnings warnings.warn( f"The {self.__class__} class is deprecated. Switch to using the TimedBelief class to suppress this warning.", FutureWarning, ) super(Price, self).__init__(**kwargs)
class Weather(TimedValue, db.Model): """ All weather measurements are stored in one slim table. This model is now considered legacy. See TimedBelief. """ sensor_id = db.Column( db.Integer(), db.ForeignKey("sensor.id"), primary_key=True, index=True ) sensor = db.relationship("Sensor", backref=db.backref("weather", lazy=True)) @classmethod def make_query(cls, **kwargs) -> Query: """Construct the database query.""" return super().make_query(**kwargs) def __init__(self, use_legacy_kwargs: bool = True, **kwargs): # todo: deprecate the 'Weather' class in favor of 'TimedBelief' (announced v0.8.0) if use_legacy_kwargs is False: # Create corresponding TimedBelief belief = TimedBelief(**kwargs) db.session.add(belief) # Convert key names for legacy model kwargs["value"] = kwargs.pop("event_value") kwargs["datetime"] = kwargs.pop("event_start") kwargs["horizon"] = kwargs.pop("belief_horizon") kwargs["sensor_id"] = kwargs.pop("sensor").id kwargs["data_source_id"] = kwargs.pop("source").id else: import warnings warnings.warn( f"The {self.__class__} class is deprecated. Switch to using the TimedBelief class to suppress this warning.", FutureWarning, ) super(Weather, self).__init__(**kwargs) def __repr__(self): return "<Weather %.5f on Sensor %s at %s by DataSource %s, horizon %s>" % ( self.value, self.sensor_id, self.datetime, self.data_source_id, self.horizon, )
class Power(TimedValue, db.Model): """ All measurements of power data are stored in one slim table. Negative values indicate consumption. This model is now considered legacy. See TimedBelief. """ sensor_id = db.Column( db.Integer(), db.ForeignKey("sensor.id", ondelete="CASCADE"), primary_key=True, index=True, ) sensor = db.relationship( "Sensor", backref=db.backref( "measurements", lazy=True, cascade="all, delete-orphan", passive_deletes=True, ), ) @classmethod def make_query( cls, **kwargs, ) -> Query: """Construct the database query.""" return super().make_query(**kwargs) def to_dict(self): return { "datetime": isodate.datetime_isoformat(self.datetime), "sensor_id": self.sensor_id, "value": self.value, "horizon": self.horizon, } def __init__(self, use_legacy_kwargs: bool = True, **kwargs): # todo: deprecate the 'asset_id' argument in favor of 'sensor_id' (announced v0.8.0) if "asset_id" in kwargs and "sensor_id" not in kwargs: kwargs["sensor_id"] = tb_utils.replace_deprecated_argument( "asset_id", kwargs["asset_id"], "sensor_id", None, ) kwargs.pop("asset_id", None) # todo: deprecate the 'Power' class in favor of 'TimedBelief' (announced v0.8.0) if use_legacy_kwargs is False: # Create corresponding TimedBelief belief = TimedBelief(**kwargs) db.session.add(belief) # Convert key names for legacy model kwargs["value"] = kwargs.pop("event_value") kwargs["datetime"] = kwargs.pop("event_start") kwargs["horizon"] = kwargs.pop("belief_horizon") kwargs["sensor_id"] = kwargs.pop("sensor").id kwargs["data_source_id"] = kwargs.pop("source").id else: import warnings warnings.warn( f"The {self.__class__} class is deprecated. Switch to using the TimedBelief class to suppress this warning.", FutureWarning, ) super(Power, self).__init__(**kwargs) def __repr__(self): return "<Power %.5f on Sensor %s at %s by DataSource %s, horizon %s>" % ( self.value, self.sensor_id, self.datetime, self.data_source_id, self.horizon, )