alert_channel_action.py
84 lines
| 3.0 KiB
| text/x-python
|
PythonLexer
r0 | # -*- coding: utf-8 -*- | |||
r112 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |||
r0 | # | |||
r112 | # Licensed under the Apache License, Version 2.0 (the "License"); | |||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
r0 | # | |||
r112 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
r0 | # | |||
r112 | # Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
r0 | ||||
import sqlalchemy as sa | ||||
r135 | from ziggurat_foundations.models.services.resource import ResourceService | |||
r0 | from appenlight.models import Base, get_db_session | |||
from sqlalchemy.orm import validates | ||||
from ziggurat_foundations.models.base import BaseModel | ||||
class AlertChannelAction(Base, BaseModel): | ||||
""" | ||||
Stores notifications conditions for user's alert channels | ||||
This is later used for rule parsing like "alert if http_status == 500" | ||||
""" | ||||
r153 | __tablename__ = "alert_channels_actions" | |||
r0 | ||||
r153 | types = ["report", "chart"] | |||
owner_id = sa.Column( | ||||
sa.Integer, sa.ForeignKey("users.id", onupdate="CASCADE", ondelete="CASCADE") | ||||
) | ||||
r0 | resource_id = sa.Column(sa.Integer()) | |||
r153 | action = sa.Column(sa.Unicode(10), nullable=False, default="always") | |||
r0 | type = sa.Column(sa.Unicode(10), nullable=False) | |||
other_id = sa.Column(sa.Unicode(40)) | ||||
pkey = sa.Column(sa.Integer(), nullable=False, primary_key=True) | ||||
r153 | rule = sa.Column( | |||
sa.dialects.postgresql.JSON, | ||||
nullable=False, | ||||
default={"field": "http_status", "op": "ge", "value": "500"}, | ||||
) | ||||
r0 | config = sa.Column(sa.dialects.postgresql.JSON) | |||
name = sa.Column(sa.Unicode(255)) | ||||
r153 | @validates("notify_type") | |||
r0 | def validate_email(self, key, notify_type): | |||
r153 | assert notify_type in ["always", "only_first"] | |||
r0 | return notify_type | |||
def resource_name(self, db_session=None): | ||||
db_session = get_db_session(db_session) | ||||
if self.resource_id: | ||||
r135 | return ResourceService.by_resource_id( | |||
r153 | self.resource_id, db_session=db_session | |||
).resource_name | ||||
r0 | else: | |||
r153 | return "any resource" | |||
r0 | ||||
r153 | def get_dict(self, exclude_keys=None, include_keys=None, extended_info=False): | |||
r0 | """ | |||
Returns dictionary with required information that will be consumed by | ||||
angular | ||||
""" | ||||
instance_dict = super(AlertChannelAction, self).get_dict() | ||||
exclude_keys_list = exclude_keys or [] | ||||
include_keys_list = include_keys or [] | ||||
if extended_info: | ||||
r153 | instance_dict["channels"] = [ | |||
c.get_dict(extended_info=False) for c in self.channels | ||||
] | ||||
r0 | ||||
d = {} | ||||
for k in instance_dict.keys(): | ||||
r153 | if k not in exclude_keys_list and ( | |||
k in include_keys_list or not include_keys | ||||
): | ||||
r0 | d[k] = instance_dict[k] | |||
return d | ||||