Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/github_actions/docker/build-pus…
Browse files Browse the repository at this point in the history
…h-action-6.10.0
  • Loading branch information
sh-kawakami authored Dec 13, 2024
2 parents ad591f0 + 6a2356f commit e9928c4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
17 changes: 17 additions & 0 deletions promgen/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,23 @@ def copy_to(self, content_type, object_id):

return self

# Custom logic before saving Rule to control the value of the annotation "rule":
# Format: annotations["rule"] = {domain}/rule/{id}
def save(self, *args, **kwargs):
with transaction.atomic():
if self.pk:
# When updating rule, we already have the primary key.
# Just set annotations["rule"] before saving to database.
self.annotations["rule"] = resolve_domain("rule-detail", pk=self.pk)
super().save(*args, **kwargs)
else:
# When creating a new rule, the primary key is typically not available until the
# instance is saved to the database.
# Therefore, we save it first then set annotations["rule"] and save again.
super().save(*args, **kwargs)
self.annotations["rule"] = resolve_domain("rule-detail", pk=self.pk)
super().save(update_fields=["annotations"])


class AlertLabel(models.Model):
alert = models.ForeignKey("Alert", on_delete=models.CASCADE)
Expand Down
37 changes: 36 additions & 1 deletion promgen/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Copyright (c) 2020 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE
from unittest import mock


from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError

from promgen import models, validators
from promgen.shortcuts import resolve_domain
from promgen.tests import PromgenTest


Expand Down Expand Up @@ -34,3 +36,36 @@ def test_validators(self):
validators.metricname(
"[[this.$el.ownerDocument.defaultView.alert(1337)]]",
)

@mock.patch("django.dispatch.dispatcher.Signal.send")
def test_rule_annotation(self, mock_post):
# Check if annotation["rule"] is automatically set to be {domain}/rule/{id} when creating a
# new rule
rule = models.Rule(
name="example-rule",
content_type=ContentType.objects.get_for_model(models.Site),
object_id=1,
clause="up==1",
duration="1s"
)
rule.save()
self.assertEqual(resolve_domain("rule-detail", rule.pk), rule.annotations["rule"])

# Check if annotation["rule"] is automatically set to be {domain}/rule/{id} when updating an
# existed rule
rule.name = "another-example-rule"
rule.annotations["rule"] = "another-annotation-value"
rule.save()
self.assertEqual(resolve_domain("rule-detail", rule.pk), rule.annotations["rule"])

# Check if annotation["rule"] is still set to be {domain}/rule/{id} when trying to remove
# annotation["rule"]
rule.annotations["rule"] = None
rule.save()
self.assertEqual(resolve_domain("rule-detail", rule.pk), rule.annotations["rule"])

# Check if annotation["rule"] of new rule is automatically set to be {domain}/rule/{id}
# when cloning an existed rule
new_rule = rule.copy_to(content_type="service", object_id=2)
self.assertEqual(resolve_domain("rule-detail", rule.pk), rule.annotations["rule"])
self.assertEqual(resolve_domain("rule-detail", new_rule.pk), new_rule.annotations["rule"])
13 changes: 12 additions & 1 deletion promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,18 @@ def get_queryset(self):

for key in self.FILTERS:
if key in self.request.GET:
obj = self.FILTERS[key].objects.get(pk=self.request.GET[key])
try:
obj = self.FILTERS[key].objects.get(pk=self.request.GET[key])
except self.FILTERS[key].DoesNotExist:
# If we can't find the object (maybe because it was deleted),
# we will search in the audit log by content_type and object_id
# and skip finding the related objects.
queryset = queryset.filter(
object_id=self.request.GET[key],
content_type_id=ContentType.objects.get_for_model(self.FILTERS.get(key)).id,
)
continue

# Get any log entries for the object itself
qset = Q(
object_id=obj.id,
Expand Down

0 comments on commit e9928c4

Please sign in to comment.