Skip to content
This repository has been archived by the owner on Jun 21, 2018. It is now read-only.

Commit

Permalink
delete notification from id added (#48)
Browse files Browse the repository at this point in the history
* delete notification from id added

* flask errors fix

* moving average with exclude tests added

* unnecessary code removed
  • Loading branch information
melnikk authored and beevee committed May 3, 2017
1 parent 41195ed commit 0bc352d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
4 changes: 2 additions & 2 deletions moira/api/resources/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ def __init__(self, db):
def render_GET(self, request):
notifications, total = yield self.db.getNotifications(request.args.get('start')[0],
request.args.get('end')[0])
self.write_json(request, {"list": notifications, "total": total})
self.write_json(request, {"list": list(notifications), "total": total})

@delayed
@defer.inlineCallbacks
def render_DELETE(self, request):
result = yield self.db.removeNotification(request.args.get('json')[0])
result = yield self.db.removeNotification(request.args.get('id')[0])
self.write_json(request, {"result": result})

def getChild(self, path, request):
Expand Down
24 changes: 16 additions & 8 deletions moira/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def dec(obj):
return obj
return dec


audit_log = None


Expand Down Expand Up @@ -680,18 +681,25 @@ def getNotifications(self, start, end):

@defer.inlineCallbacks
@docstring_parameters(NOTIFIER_NOTIFICATIONS)
def removeNotification(self, json):
def removeNotification(self, id):
"""
removeNotification(self, json)
removeNotification(self, id)
Remove planning notification by given json from sorted set {0}
Remove planning notification by id string from sorted set {0}
:param json: notification json
:type json: string
:param id: notification id string
:type id: string
"""

result = yield self.rc.zrem(NOTIFIER_NOTIFICATIONS, json)
defer.returnValue(result)
notifications, total = yield self.getNotifications(0, -1)
for json in notifications:
notification = anyjson.loads(json)
timestamp = str(notification.get('timestamp'))
contact_id = notification.get('contact', {}).get('id')
sub_id = notification.get('event', {}).get('sub_id')
idstr = ''.join([timestamp, contact_id, sub_id])
if idstr == id:
result = yield self.rc.zrem(NOTIFIER_NOTIFICATIONS, json)
defer.returnValue(result)

@defer.inlineCallbacks
@docstring_parameters(TAG_TRIGGERS_PREFIX.format("<tag>"))
Expand Down
1 change: 1 addition & 0 deletions moira/metrics/spy.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ def get_metrics(self):
summary += batch['sum']
return {"sum": summary, "count": count}


TRIGGER_CHECK = Spy()
TRIGGER_CHECK_ERRORS = Spy()
1 change: 1 addition & 0 deletions moira/tools/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def run():

reactor.run()


if __name__ == '__main__':

run()
50 changes: 44 additions & 6 deletions tests/functional/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


class DataTests(WorkerTests):

@inlineCallbacks
def sendTrigger(self, trigger):
body = client.FileBodyProducer(StringIO(trigger))
Expand Down Expand Up @@ -192,13 +191,55 @@ def testExclude(self):
yield self.assert_trigger_metric(metric1, 1, state.OK)
yield self.assert_trigger_metric(metric2, None, None)

@trigger('test-trigger-exclude-with-moving-average')
@inlineCallbacks
def testExcludeWithMovingAverage(self):
pattern = 'MoiraFuncTest.supervisord.host.*.state'
metric1 = 'MoiraFuncTest.supervisord.host.one.state'
metric2 = 'MoiraFuncTest.supervisord.host.two.state'
yield self.sendTrigger('{"name": "test trigger", "targets": ["exclude(movingAverage(' + pattern +
', 3), \'two\')"], "warn_value": 20, "error_value": 30, "ttl":"600" }')

yield self.db.sendMetric(pattern, metric1, self.now - 180, 10)
yield self.db.sendMetric(pattern, metric1, self.now - 120, 20)
yield self.db.sendMetric(pattern, metric1, self.now - 60, 30)

yield self.db.sendMetric(pattern, metric2, self.now - 180, 30)
yield self.db.sendMetric(pattern, metric2, self.now - 120, 40)
yield self.db.sendMetric(pattern, metric2, self.now - 60, 50)

yield self.trigger.check(now=self.now)
yield self.assert_trigger_metric('movingAverage(' + metric1 + ',3)', 20, state.WARN)
yield self.assert_trigger_metric('movingAverage(' + metric2 + ',3)', None, None)

@trigger('test-trigger-moving-average-with-exclude')
@inlineCallbacks
def testMovingAverageWithExclude(self):
pattern = 'MoiraFuncTest.supervisord.host.*.state'
metric1 = 'MoiraFuncTest.supervisord.host.one.state'
metric2 = 'MoiraFuncTest.supervisord.host.two.state'
yield self.sendTrigger('{"name": "test trigger", "targets": ["movingAverage(exclude(' + pattern +
', \'two\'), 3)"], "warn_value": 20, "error_value": 30, "ttl":"600" }')

yield self.db.sendMetric(pattern, metric1, self.now - 180, 10)
yield self.db.sendMetric(pattern, metric1, self.now - 120, 20)
yield self.db.sendMetric(pattern, metric1, self.now - 60, 30)

yield self.db.sendMetric(pattern, metric2, self.now - 180, 30)
yield self.db.sendMetric(pattern, metric2, self.now - 120, 40)
yield self.db.sendMetric(pattern, metric2, self.now - 60, 50)

yield self.trigger.check(now=self.now)
yield self.assert_trigger_metric('movingAverage(' + metric1 + ',3)', 20, state.WARN)
yield self.assert_trigger_metric('movingAverage(' + metric2 + ',3)', None, None)

@trigger('test-trigger-transformNull')
@inlineCallbacks
def testTransformNull(self):
pattern = 'MoiraFuncTest.supervisord.host.*.state'
metric1 = 'MoiraFuncTest.supervisord.host.one.state'
yield self.sendTrigger(trigger='{"name": "test trigger", "targets": ["movingAverage(transformNull(' + pattern +
', 0),2)"], "warn_value": 20, "error_value": 50, "ttl":"600" }')
', 0),2)"], "warn_value": 20, "error_value": 50, "ttl":"600" }')
yield self.db.sendMetric(pattern, metric1, self.now - 180, 5)
yield self.db.sendMetric(pattern, metric1, self.now - 60, 5)
yield self.trigger.check()
Expand Down Expand Up @@ -791,7 +832,6 @@ def testComplexTriggerConservativeBehaviour(self):
yield self.trigger.check(now=self.now + 60)
yield self.assert_trigger_metric(metric, 100, state.ERROR)


@trigger('test-trigger-on-multiple-metrics-does-not-produce-preliminary-real-time-alert')
@inlineCallbacks
def testComplexTriggerMultipleMetricsNoPreliminaryAlert(self):
Expand Down Expand Up @@ -829,13 +869,11 @@ def testComplexTriggerMultipleMetricsNoPreliminaryAlert(self):
yield self.trigger.check(now=self.now + 240)
yield self.assert_trigger_metric(targetMetric, 120., state.ERROR)


@trigger('test-moving-average-bootstrap-with-no-realtime-alerting')
@inlineCallbacks
def testMovingAverageBootstrapWithNoRealTimeAlerting(self):
yield self.movingAverageBootstrap(allowRealTimeAlerting=False)


@trigger('test-moving-average-bootstrap-with-realtime-alerting')
@inlineCallbacks
def testMovingAverageBootstrapWithRealTimeAlerting(self):
Expand All @@ -853,4 +891,4 @@ def movingAverageBootstrap(self, allowRealTimeAlerting):
rc = datalib.createRequestContext(fromTime, endTime, allowRealTimeAlerting)
result = yield self.trigger.get_timeseries(rc)
ts = result[1][0]
self.assertEqual(ts[0], 6.5)
self.assertEqual(ts[0], 6.5)

0 comments on commit 0bc352d

Please sign in to comment.