From a4ed66c86f5b641f4a87403222d9f78e4514fb80 2018-02-28 12:56:37 From: Kacper Krupa Date: 2018-02-28 12:56:37 Subject: [PATCH] Option to blacklist certain resources from AlertChannels --- diff --git a/backend/src/appenlight/migrations/versions/e9fcfbdd9498_connect_resources_to_alert_channels.py b/backend/src/appenlight/migrations/versions/e9fcfbdd9498_connect_resources_to_alert_channels.py new file mode 100644 index 0000000..08599aa --- /dev/null +++ b/backend/src/appenlight/migrations/versions/e9fcfbdd9498_connect_resources_to_alert_channels.py @@ -0,0 +1,30 @@ +"""connect resources to alert_channels + +Revision ID: e9fcfbdd9498 +Revises: 55b6e612672f +Create Date: 2018-02-28 13:52:50.717217 + +""" + +# revision identifiers, used by Alembic. +revision = 'e9fcfbdd9498' +down_revision = '55b6e612672f' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + op.create_table( + 'channels_resources', + sa.Column('channel_pkey', sa.Integer, + sa.ForeignKey('alert_channels.pkey', + ondelete='CASCADE', onupdate='CASCADE')), + sa.Column('resource_id', sa.Integer, + sa.ForeignKey('resources.resource_id', + ondelete='CASCADE', onupdate='CASCADE')) + ) + + +def downgrade(): + op.drop_table('channels_resources') diff --git a/backend/src/appenlight/models/alert_channel.py b/backend/src/appenlight/models/alert_channel.py index 6c0cf16..f298a46 100644 --- a/backend/src/appenlight/models/alert_channel.py +++ b/backend/src/appenlight/models/alert_channel.py @@ -34,6 +34,14 @@ channel_rules_m2m_table = sa.Table( sa.ForeignKey('alert_channels_actions.pkey')) ) +channel_resources_m2m_table = sa.Table( + 'channels_resources', Base.metadata, + sa.Column('channel_pkey', sa.Integer, + sa.ForeignKey('alert_channels.pkey')), + sa.Column('resource_id', sa.Integer, + sa.ForeignKey('resources.resource_id')) +) + DATE_FRMT = '%Y-%m-%dT%H:%M' @@ -70,6 +78,12 @@ class AlertChannel(Base, BaseModel): passive_updates=True, secondary=channel_rules_m2m_table, backref='channels') + resources = sa.orm.relationship('Resource', + cascade="all, delete-orphan", + passive_deletes=True, + passive_updates=True, + secondary=channel_resources_m2m_table, + backref='resources') @property def channel_visible_value(self): diff --git a/backend/src/appenlight/models/event.py b/backend/src/appenlight/models/event.py index b5c905e..1d01edc 100644 --- a/backend/src/appenlight/models/event.py +++ b/backend/src/appenlight/models/event.py @@ -92,7 +92,12 @@ class Event(Base, BaseModel): users = set([p.user for p in resource.users_for_perm('view')]) for user in users: for channel in user.alert_channels: - if not channel.channel_validated or not channel.send_alerts: + matches_resource = not channel.resources or resource in [r.resource_id for r in channel.resources] + if ( + not channel.channel_validated or + not channel.send_alerts or + not matches_resource + ): continue else: try: