bkyryliuk commented on a change in pull request #10605:
URL: 
https://github.com/apache/incubator-superset/pull/10605#discussion_r476651301



##########
File path: superset/models/alerts.py
##########
@@ -100,3 +119,187 @@ class AlertLog(Model):
     @property
     def duration(self) -> int:
         return (self.dttm_end - self.dttm_start).total_seconds()
+
+
+class Observer(Model):
+
+    __tablename__ = "alert_observers"
+
+    id = Column(Integer, primary_key=True)
+    name = Column(String(150), nullable=False)
+    observer_type = Column(Enum(AlertObserverType))
+    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=backref("alert_observers", cascade="all, delete-orphan"),
+        )
+
+    @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("alert_observers", cascade="all, delete-orphan"),
+        )
+
+    __mapper_args__ = {
+        "polymorphic_identity": "base_observer",
+        "polymorphic_on": observer_type,
+    }
+
+
+class SQLObserver(Observer):
+
+    __tablename__ = "alert_observers"
+
+    sql = Column(Text, nullable=False)
+
+    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
+            Observation(dttm_ts=datetime.utcnow(), value=df.to_json())
+        )
+
+        db.session.commit()
+
+    def get_observations(self, observation_num: int) -> List[Any]:
+        return (
+            db.session.query(Observation)
+            .filter_by(observer_id=self.id)
+            .order_by(Observation.dttm_ts.desc())
+            .limit(observation_num)
+        )
+
+    __mapper_args__ = {"polymorphic_identity": AlertObserverType.sql}
+
+
+class Observation(Model):  # pylint: disable=too-few-public-methods
+
+    __tablename__ = "alert_observations"
+
+    id = Column(Integer, primary_key=True)
+    dttm_ts = Column(DateTime, default=datetime.utcnow)
+    observer_id = Column(Integer, ForeignKey("alert_observers.id"), 
nullable=False)
+    observer = relationship(
+        "Observer",
+        foreign_keys=[observer_id],
+        backref=backref("observations", cascade="all, delete-orphan"),
+    )
+    value = Column(Text)
+
+
+class Validator(Model):
+
+    __tablename__ = "alert_validators"

Review comment:
       no need for the validator table, probably move this class closer to the 
alerts logic




----------------------------------------------------------------
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]

Reply via email to