oncall-engine/engine/apps/alerts/models/alert_manager_models.py
Michael Derynck 6b40f95033 World, meet OnCall!
Co-authored-by: Eve832 <eve.meelan@grafana.com>
    Co-authored-by: Francisco Montes de Oca <nevermind89x@gmail.com>
    Co-authored-by: Ildar Iskhakov <ildar.iskhakov@grafana.com>
    Co-authored-by: Innokentii Konstantinov <innokenty.konstantinov@grafana.com>
    Co-authored-by: Julia <ferril.darkdiver@gmail.com>
    Co-authored-by: maskin25 <kengurek@gmail.com>
    Co-authored-by: Matias Bordese <mbordese@gmail.com>
    Co-authored-by: Matvey Kukuy <motakuk@gmail.com>
    Co-authored-by: Michael Derynck <michael.derynck@grafana.com>
    Co-authored-by: Richard Hartmann <richih@richih.org>
    Co-authored-by: Robby Milo <robbymilo@fastmail.com>
    Co-authored-by: Timur Olzhabayev <timur.olzhabayev@grafana.com>
    Co-authored-by: Vadim Stepanov <vadimkerr@gmail.com>
    Co-authored-by: Yulia Shanyrova <yulia.shanyrova@grafana.com>
2022-06-03 08:09:47 -06:00

64 lines
2.1 KiB
Python

import hashlib
import json
from django.db import transaction
from apps.alerts.models import Alert, AlertGroup
class AlertGroupForAlertManager(AlertGroup):
def is_alert_a_resolve_signal(self, alert):
non_resolved_hashes = set()
hash = alert.get_integration_optimization_hash()
if alert.calculated_is_resolve_signal:
# Calculate leftover hashes
for alert in AlertForAlertManager.objects.filter(group=self).exclude(pk=alert.pk).all():
if alert.calculated_is_resolve_signal:
try:
non_resolved_hashes.remove(alert.get_integration_optimization_hash())
except KeyError:
pass
else:
non_resolved_hashes.add(alert.get_integration_optimization_hash())
# Remove last hash
try:
non_resolved_hashes.remove(hash)
except KeyError:
pass
return len(non_resolved_hashes) == 0
else:
return False
class Meta:
app_label = "alerts"
proxy = True
class AlertForAlertManager(Alert):
def get_integration_optimization_hash(self):
if self.integration_optimization_hash is None:
with transaction.atomic():
if self.id is not None:
alert = AlertForAlertManager.objects.filter(id=self.id).select_for_update().get()
else:
alert = self
_hash = dict(alert.raw_request_data.get("labels", {}))
_hash = json.dumps(_hash, sort_keys=True)
_hash = hashlib.md5(str(_hash).encode()).hexdigest()
alert.integration_optimization_hash = _hash
if self.id is not None:
alert.save()
return alert.integration_optimization_hash
else:
return self.integration_optimization_hash
@property
def calculated_is_resolve_signal(self):
return self.raw_request_data.get("status", "") == "resolved"
class Meta:
app_label = "alerts"
proxy = True