Beispiel #1
0
    def get(self, alert_id):
        alert_id = int(alert_id)
        alert = Alert.get_by_id_and_org(alert_id, self.current_org)
        require_access(alert.groups, self.current_user, view_only)

        subscriptions = AlertSubscription.all(alert_id)
        return [s.to_dict() for s in subscriptions]
Beispiel #2
0
    def post(self):
        # 忽视mimetype类型,强制为Json类型

        req = request.get_json(True)

        # 判断请求参数
        require_fields(req, ('options', 'name', 'query_id'))

        #### 根据哪个query_id创建的alert
        query = Query.get_by_id_and_org(req['query_id'], self.current_org)

        ## 权限系统??????????????
        require_access(query.groups, self.current_user, view_only)

        # query_rel 是 relationShip, 传对方的行对象,需要提供
        # 但是不用提供外键对应的列
        alert = Alert(name=req['name'],
                      query_rel=query,
                      user=self.current_user,
                      rearm=req.get('rearm'),
                      options=req['options'])

        session.add(alert)
        session.flush()
        session.commit()

        self.record_event({
            'action': 'create',
            'timestamp': int(time.time()),
            'object_id': alert.id,
            'object_type': 'alert'
        })

        ### RESETFUL 规范, 不能只是响应个200,就完事了, 最好把创建好的东西返回给前端
        return serialize_alert(alert)
Beispiel #3
0
    def test_return_each_alert_only_once(self):
        group = self.factory.create_group()
        self.factory.data_source.add_group(group)

        alert = self.factory.create_alert()

        alerts = Alert.all(group_ids=[self.factory.default_group.id, group.id])
        self.assertEqual(1, len(list(alerts)))
        self.assertIn(alert, alerts)
Beispiel #4
0
    def test_return_each_alert_only_once(self):
        group = self.factory.create_group()
        self.factory.data_source.add_group(group)

        alert = self.factory.create_alert()

        alerts = Alert.all(groups=[self.factory.default_group, group])
        self.assertEqual(1, len(list(alerts)))
        self.assertIn(alert, alerts)
Beispiel #5
0
    def test_returns_all_alerts_for_given_groups(self):
        ds1 = self.factory.data_source
        group = self.factory.create_group()
        ds2 = self.factory.create_data_source(group=group)

        query1 = self.factory.create_query(data_source=ds1)
        query2 = self.factory.create_query(data_source=ds2)

        alert1 = self.factory.create_alert(query=query1)
        alert2 = self.factory.create_alert(query=query2)

        alerts = Alert.all(groups=[group, self.factory.default_group])
        self.assertIn(alert1, alerts)
        self.assertIn(alert2, alerts)

        alerts = Alert.all(groups=[self.factory.default_group])
        self.assertIn(alert1, alerts)
        self.assertNotIn(alert2, alerts)

        alerts = Alert.all(groups=[group])
        self.assertNotIn(alert1, alerts)
        self.assertIn(alert2, alerts)
Beispiel #6
0
    def post(self, alert_id):
        req = request.get_json(True)

        alert = Alert.get_by_id_and_org(alert_id, self.current_org)
        require_access(alert.groups, self.current_user, view_only)

        kwargs = {'alert': alert, 'user': self.current_user}

        if 'destination_id' in req:
            destination = NotificationDestination.get_by_id_and_org(
                req['destination_id'], self.current_org)
            kwargs['destination'] = destination

        # 用提供外键对应的列
        # kwargs = {'alert': alert, 'user': self.current_user, 'destination': destination}

        # AlertSubscription. 一个alert有对应多个订阅者,一订阅者订阅多个alert,多对多

        # alert
        # user
        # query
        # notification

        subscription = AlertSubscription(**kwargs)
        session.add(subscription)
        session.commit()

        self.record_event({
            'action': 'subscribe',
            'timestamp': int(time.time()),
            'object_id': alert_id,
            'object_type': 'alert',
            'destination': req.get('destination_id')
        })

        d = subscription.to_dict()
        return d
                schema = get_configuration_schema_for_destination_type('webhook')
                conf = {'url': settings.WEBHOOK_ENDPOINT}
                if settings.WEBHOOK_USERNAME:
                    conf['username'] = settings.WEBHOOK_USERNAME
                    conf['password'] = settings.WEBHOOK_PASSWORD
                options = ConfigurationContainer(conf, schema)

                webhook = NotificationDestination.create(
                    org=org,
                    user=user,
                    name="Webhook",
                    type="webhook",
                    options=options
                )

                for alert in Alert.select():
                    AlertSubscription.create(
                        user=user,
                        destination=webhook,
                        alert=alert
                    )

            if settings.HIPCHAT_API_TOKEN:
                # Have all existing alerts send to HipChat if already configured
                schema = get_configuration_schema_for_destination_type('hipchat')

                conf = {}

                if settings.HIPCHAT_API_URL:
                    conf['url'] = '{url}/room/{room_id}/notification?auth_token={token}'.format(
                        url=settings.HIPCHAT_API_URL, room_id=settings.HIPCHAT_ROOM_ID, token=settings.HIPCHAT_API_TOKEN)
Beispiel #8
0
                schema = get_configuration_schema_for_destination_type('webhook')
                conf = {'url': WEBHOOK_ENDPOINT}
                if WEBHOOK_USERNAME:
                    conf['username'] = WEBHOOK_USERNAME
                    conf['password'] = WEBHOOK_PASSWORD
                options = ConfigurationContainer(conf, schema)

                webhook = NotificationDestination.create(
                    org=org,
                    user=user,
                    name="Webhook",
                    type="webhook",
                    options=options
                )

                for alert in Alert.select():
                    AlertSubscription.create(
                        user=user,
                        destination=webhook,
                        alert=alert
                    )

            if HIPCHAT_API_TOKEN:
                # Have all existing alerts send to HipChat if already configured
                schema = get_configuration_schema_for_destination_type('hipchat')

                conf = {}

                if HIPCHAT_API_URL:
                    conf['url'] = '{url}/room/{room_id}/notification?auth_token={token}'.format(
                        url=HIPCHAT_API_URL, room_id=HIPCHAT_ROOM_ID, token=HIPCHAT_API_TOKEN)
from redash.models import db, Alert, AlertSubscription

if __name__ == '__main__':
    with db.database.transaction():
        Alert.create_table()
        AlertSubscription.create_table()

    db.close_db(None)
Beispiel #10
0
 def get(self):
     return [
         serialize_alert(alert)
         for alert in Alert.all(group_ids=self.current_user.group_ids)
     ]
from redash.models import db, Alert, AlertSubscription

if __name__ == '__main__':
    with db.database.transaction():
       Alert.create_table()
       AlertSubscription.create_table()

    db.close_db(None)