bkyryliuk commented on a change in pull request #10605:
URL:
https://github.com/apache/incubator-superset/pull/10605#discussion_r470861176
##########
File path: superset/models/alerts.py
##########
@@ -100,3 +109,117 @@ class AlertLog(Model):
@property
def duration(self) -> int:
return (self.dttm_end - self.dttm_start).total_seconds()
+
+
+class Observer:
+
+ __tablename__ = "alert_observers"
+
+ id = Column(Integer, primary_key=True)
+ name = Column(String(150))
+ validation_type = Column(Enum(AlertValidationType))
+
+ @declared_attr
+ def alert_id(self) -> int:
+ return Column(Integer, ForeignKey("alerts.id"), nullable=False)
+
+ @declared_attr
+ def alert(self) -> RelationshipProperty:
+ return relationship(
+ "Alert", foreign_keys=[self.alert_id],
backref=self.__tablename__[:-1]
+ )
+
+ @declared_attr
+ def database_id(self) -> int:
+ return Column(Integer, ForeignKey("dbs.id"), nullable=False)
+
+ @declared_attr
+ def database(self) -> RelationshipProperty:
+ return relationship(
+ "Database",
+ foreign_keys=[self.database_id],
+ backref=backref(self.__tablename__, cascade="all, delete-orphan"),
+ )
+
+
+class SQLObserver(Model, Observer):
+
+ __tablename__ = "sql_alert_observers"
+
+ sql = Column(Text)
+
+ def query(self) -> None:
+ parsed_query = ParsedQuery(self.sql)
+ sql = parsed_query.stripped()
+ df = self.database.get_df(sql)
+
+ self.observations.append( # pylint: disable=no-member
+ SQLObservation(dttm_ts=datetime.utcnow(), value=df.to_json())
+ )
+
+ def get_observations(self, observation_num: int) -> List[Any]:
+ return (
+ db.session.query(SQLObservation)
+ .filter_by(observer_id=self.id)
+ .order_by(Observation.dttm_ts.desc())
+ .limit(observation_num)
+ )
+
+
+class Observation: # pylint: disable=too-few-public-methods
Review comment:
why does it have a table?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]