2021-04-21 16:47:18 +00:00
|
|
|
"""
|
|
|
|
This module defines the Alert database class.
|
|
|
|
"""
|
|
|
|
|
2021-05-07 17:46:14 +00:00
|
|
|
from ..base import ext
|
2021-05-17 13:29:08 +00:00
|
|
|
from .Enums import ConditionMode
|
2021-04-21 16:47:18 +00:00
|
|
|
|
|
|
|
|
2021-05-07 17:46:14 +00:00
|
|
|
class Alert(ext.Model):
|
2021-04-21 16:47:18 +00:00
|
|
|
__tablename__ = "alert"
|
2021-05-07 17:46:14 +00:00
|
|
|
id = ext.Column(ext.Integer, primary_key=True)
|
|
|
|
name = ext.Column(ext.String, nullable=False)
|
|
|
|
limit = ext.Column(ext.Integer, nullable=False)
|
|
|
|
window_size = ext.Column(ext.Integer, nullable=False)
|
2021-05-17 13:29:08 +00:00
|
|
|
evaluation_mode = ext.Column(ext.Enum(ConditionMode), nullable=False, default=ConditionMode.all_or)
|
2021-04-21 16:47:18 +00:00
|
|
|
# Foreign Keys
|
2021-05-07 17:46:14 +00:00
|
|
|
repository_id = ext.Column(ext.Integer, ext.ForeignKey("repository.id", ondelete="CASCADE"), nullable=False)
|
2021-04-21 16:47:18 +00:00
|
|
|
# Relationships
|
2021-05-07 17:46:14 +00:00
|
|
|
repository = ext.relationship("Repository", back_populates="alerts")
|
2021-05-11 16:45:40 +00:00
|
|
|
notifications = ext.relationship("Notification", back_populates="alert")
|
2021-05-17 13:29:08 +00:00
|
|
|
conditions = ext.relationship("MadeOf", back_populates="alert")
|
2021-05-07 17:15:14 +00:00
|
|
|
|
|
|
|
def to_json(self):
|
|
|
|
return {
|
|
|
|
'id': self.id,
|
|
|
|
'name': self.name,
|
|
|
|
'window_size': self.window_size,
|
|
|
|
'limit': self.limit,
|
|
|
|
'repository_id': self.repository_id,
|
2021-05-17 13:29:08 +00:00
|
|
|
'evaluation_mode': self.evaluation_mode.value,
|
2021-05-07 17:15:14 +00:00
|
|
|
'notifications': [notification.to_json() for notification in self.notifications],
|
2021-05-17 13:29:08 +00:00
|
|
|
'conditions': [c.condition.to_json() for c in self.conditions]
|
2021-05-07 17:15:14 +00:00
|
|
|
}
|