Compare commits
No commits in common. "eb2486afbabdd3b80bea4f7e779e4562aef5738c" and "6bfa0aa73b5e395b035728ba19df95acaaf61dd8" have entirely different histories.
eb2486afba
...
6bfa0aa73b
@ -306,9 +306,4 @@ urlpatterns = [
|
|||||||
notifications.RuleDelete.as_view(),
|
notifications.RuleDelete.as_view(),
|
||||||
name="rule_delete",
|
name="rule_delete",
|
||||||
),
|
),
|
||||||
path(
|
|
||||||
"rule/<str:type>/clear/<str:pk>/",
|
|
||||||
notifications.RuleClear.as_view(),
|
|
||||||
name="rule_clear",
|
|
||||||
),
|
|
||||||
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
|
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
|
||||||
|
@ -74,26 +74,14 @@ class NotificationSettingsForm(RestrictedFormMixin, ModelForm):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = NotificationSettings
|
model = NotificationSettings
|
||||||
fields = (
|
fields = (
|
||||||
"topic",
|
"ntfy_topic",
|
||||||
"url",
|
"ntfy_url",
|
||||||
"service",
|
|
||||||
)
|
)
|
||||||
help_texts = {
|
help_texts = {
|
||||||
"topic": "The topic to send notifications to.",
|
"ntfy_topic": "The topic to send notifications to.",
|
||||||
"url": "Custom NTFY server/webhook destination. Leave blank to use the default server for NTFY. For webhooks this field is required.",
|
"ntfy_url": "Custom NTFY server. Leave blank to use the default server.",
|
||||||
"service": "The service to use for notifications.",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def clean(self):
|
|
||||||
cleaned_data = super(NotificationSettingsForm, self).clean()
|
|
||||||
if "service" in cleaned_data:
|
|
||||||
if cleaned_data["service"] == "webhook":
|
|
||||||
if not cleaned_data.get("url"):
|
|
||||||
self.add_error(
|
|
||||||
"url",
|
|
||||||
"You must set a URL for webhooks.",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class NotificationRuleForm(RestrictedFormMixin, ModelForm):
|
class NotificationRuleForm(RestrictedFormMixin, ModelForm):
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -104,16 +92,12 @@ class NotificationRuleForm(RestrictedFormMixin, ModelForm):
|
|||||||
"interval",
|
"interval",
|
||||||
"window",
|
"window",
|
||||||
"priority",
|
"priority",
|
||||||
"service",
|
|
||||||
"url",
|
|
||||||
"topic",
|
"topic",
|
||||||
"enabled",
|
"enabled",
|
||||||
)
|
)
|
||||||
help_texts = {
|
help_texts = {
|
||||||
"name": "The name of the rule.",
|
"name": "The name of the rule.",
|
||||||
"priority": "The notification priority of the rule.",
|
"priority": "The notification priority of the rule.",
|
||||||
"url": "Custom NTFY server/webhook destination. Leave blank to use the default server for NTFY. For webhooks this field is required.",
|
|
||||||
"service": "The service to use for notifications",
|
|
||||||
"topic": "The topic to send notifications to. Leave blank for default.",
|
"topic": "The topic to send notifications to. Leave blank for default.",
|
||||||
"enabled": "Whether the rule is enabled.",
|
"enabled": "Whether the rule is enabled.",
|
||||||
"data": "The notification rule definition.",
|
"data": "The notification rule definition.",
|
||||||
@ -123,13 +107,6 @@ class NotificationRuleForm(RestrictedFormMixin, ModelForm):
|
|||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
cleaned_data = super(NotificationRuleForm, self).clean()
|
cleaned_data = super(NotificationRuleForm, self).clean()
|
||||||
if "service" in cleaned_data:
|
|
||||||
if cleaned_data["service"] == "webhook":
|
|
||||||
if not cleaned_data.get("url"):
|
|
||||||
self.add_error(
|
|
||||||
"url",
|
|
||||||
"You must set a URL for webhooks.",
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
# Passing db to avoid circular import
|
# Passing db to avoid circular import
|
||||||
parsed_data = NotificationRuleData(self.request.user, cleaned_data, db=db)
|
parsed_data = NotificationRuleData(self.request.user, cleaned_data, db=db)
|
||||||
|
@ -9,6 +9,8 @@ log = logs.get_logger(__name__)
|
|||||||
|
|
||||||
# Actual function to send a message to a topic
|
# Actual function to send a message to a topic
|
||||||
def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None):
|
def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None):
|
||||||
|
if url is None:
|
||||||
|
url = NTFY_URL
|
||||||
headers = {"Title": "Fisk"}
|
headers = {"Title": "Fisk"}
|
||||||
if title:
|
if title:
|
||||||
headers["Title"] = title
|
headers["Title"] = title
|
||||||
@ -30,20 +32,11 @@ def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None)
|
|||||||
def sendmsg(user, *args, **kwargs):
|
def sendmsg(user, *args, **kwargs):
|
||||||
notification_settings = user.get_notification_settings()
|
notification_settings = user.get_notification_settings()
|
||||||
|
|
||||||
# No custom topic specified
|
|
||||||
if "topic" not in kwargs:
|
if "topic" not in kwargs:
|
||||||
# No user topic set either
|
if notification_settings.ntfy_topic is None:
|
||||||
if notification_settings.topic is None:
|
|
||||||
# No topic set, so don't send
|
# No topic set, so don't send
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
kwargs["topic"] = notification_settings.topic
|
kwargs["topic"] = notification_settings.ntfy_topic
|
||||||
|
|
||||||
if "url" in kwargs:
|
raw_sendmsg(*args, **kwargs, url=notification_settings.ntfy_url)
|
||||||
url = kwargs["url"]
|
|
||||||
elif notification_settings.url is not None:
|
|
||||||
url = notification_settings.url
|
|
||||||
else:
|
|
||||||
url = NTFY_URL
|
|
||||||
|
|
||||||
raw_sendmsg(*args, **kwargs, url=url)
|
|
||||||
|
@ -83,63 +83,29 @@ class NotificationRuleData(object):
|
|||||||
self.object.save()
|
self.object.save()
|
||||||
log.debug(f"Stored match: {index} - {match}")
|
log.debug(f"Stored match: {index} - {match}")
|
||||||
|
|
||||||
def get_match(self, index):
|
|
||||||
"""
|
|
||||||
Get a match result for an index.
|
|
||||||
"""
|
|
||||||
if self.object.match is None:
|
|
||||||
return None
|
|
||||||
if not isinstance(self.object.match, dict):
|
|
||||||
return None
|
|
||||||
|
|
||||||
return self.object.match.get(index)
|
|
||||||
|
|
||||||
def format_aggs(self, aggs):
|
|
||||||
"""
|
|
||||||
Format aggregations for the query.
|
|
||||||
We have self.aggs, which contains:
|
|
||||||
{"avg_sentiment": (">", 0.5)}
|
|
||||||
and aggs, which contains:
|
|
||||||
{"avg_sentiment": {"value": 0.6}}
|
|
||||||
It's matched already, we just need to format it like so:
|
|
||||||
{"avg_sentiment": "0.06>0.5"}
|
|
||||||
"""
|
|
||||||
new_aggs = {}
|
|
||||||
for agg_name, agg in aggs.items():
|
|
||||||
# Already checked membership below
|
|
||||||
op, value = self.aggs[agg_name]
|
|
||||||
new_aggs[agg_name] = f"{agg['value']}{op}{value}"
|
|
||||||
|
|
||||||
return new_aggs
|
|
||||||
|
|
||||||
async def run_schedule(self):
|
async def run_schedule(self):
|
||||||
"""
|
"""
|
||||||
Run the schedule query.
|
Run the schedule query.
|
||||||
"""
|
"""
|
||||||
response = await self.db.schedule_query_results(self)
|
if self.db:
|
||||||
for index, (aggs, results) in response.items():
|
response = await self.db.schedule_query_results(self)
|
||||||
if not results:
|
for index, (aggs, results) in response.items():
|
||||||
|
if not results:
|
||||||
|
self.store_match(index, False)
|
||||||
|
|
||||||
|
aggs_for_index = []
|
||||||
|
for agg_name in self.aggs.keys():
|
||||||
|
if agg_name in aggs:
|
||||||
|
if "match" in aggs[agg_name]:
|
||||||
|
aggs_for_index.append(aggs[agg_name]["match"])
|
||||||
|
|
||||||
|
# All required aggs are present
|
||||||
|
if len(aggs_for_index) == len(self.aggs.keys()):
|
||||||
|
if all(aggs_for_index):
|
||||||
|
self.store_match(index, True)
|
||||||
|
continue
|
||||||
self.store_match(index, False)
|
self.store_match(index, False)
|
||||||
|
|
||||||
aggs_for_index = []
|
|
||||||
for agg_name in self.aggs.keys():
|
|
||||||
if agg_name in aggs:
|
|
||||||
if "match" in aggs[agg_name]:
|
|
||||||
aggs_for_index.append(aggs[agg_name]["match"])
|
|
||||||
|
|
||||||
# All required aggs are present
|
|
||||||
if len(aggs_for_index) == len(self.aggs.keys()):
|
|
||||||
if all(aggs_for_index):
|
|
||||||
# Ensure we only send notifications when the previous run
|
|
||||||
# did not return any matches
|
|
||||||
current_match = self.get_match(index)
|
|
||||||
if current_match is False:
|
|
||||||
formatted_aggs = self.format_aggs(aggs)
|
|
||||||
rule_matched(self.object, results[:5], formatted_aggs)
|
|
||||||
self.store_match(index, True)
|
|
||||||
continue
|
|
||||||
self.store_match(index, False)
|
|
||||||
|
|
||||||
def test_schedule(self):
|
def test_schedule(self):
|
||||||
"""
|
"""
|
||||||
Test the schedule query to ensure it is valid.
|
Test the schedule query to ensure it is valid.
|
||||||
|
@ -15,8 +15,6 @@ def process_rules(data):
|
|||||||
for index, index_messages in data.items():
|
for index, index_messages in data.items():
|
||||||
for message in index_messages:
|
for message in index_messages:
|
||||||
for rule in all_rules:
|
for rule in all_rules:
|
||||||
# Quicker helper to get the data without spinning
|
|
||||||
# up a NotificationRuleData object
|
|
||||||
parsed_rule = rule.parse()
|
parsed_rule = rule.parse()
|
||||||
matched = {}
|
matched = {}
|
||||||
if "index" not in parsed_rule:
|
if "index" not in parsed_rule:
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
# Generated by Django 4.1.5 on 2023-01-15 18:14
|
|
||||||
|
|
||||||
from django.db import migrations, models
|
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
|
||||||
|
|
||||||
dependencies = [
|
|
||||||
('core', '0019_alter_notificationrule_match'),
|
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
|
||||||
migrations.RenameField(
|
|
||||||
model_name='notificationsettings',
|
|
||||||
old_name='ntfy_topic',
|
|
||||||
new_name='topic',
|
|
||||||
),
|
|
||||||
migrations.RemoveField(
|
|
||||||
model_name='notificationsettings',
|
|
||||||
name='ntfy_url',
|
|
||||||
),
|
|
||||||
migrations.AddField(
|
|
||||||
model_name='notificationrule',
|
|
||||||
name='service',
|
|
||||||
field=models.CharField(blank=True, choices=[('ntfy', 'NTFY'), ('wehbook', 'Custom webhook')], max_length=255, null=True),
|
|
||||||
),
|
|
||||||
migrations.AddField(
|
|
||||||
model_name='notificationrule',
|
|
||||||
name='url',
|
|
||||||
field=models.CharField(blank=True, max_length=1024, null=True),
|
|
||||||
),
|
|
||||||
migrations.AddField(
|
|
||||||
model_name='notificationsettings',
|
|
||||||
name='service',
|
|
||||||
field=models.CharField(blank=True, choices=[('ntfy', 'NTFY'), ('wehbook', 'Custom webhook')], max_length=255, null=True),
|
|
||||||
),
|
|
||||||
migrations.AddField(
|
|
||||||
model_name='notificationsettings',
|
|
||||||
name='url',
|
|
||||||
field=models.CharField(blank=True, max_length=1024, null=True),
|
|
||||||
),
|
|
||||||
]
|
|
@ -35,11 +35,6 @@ INTERVAL_CHOICES = (
|
|||||||
(86400, "Every day"),
|
(86400, "Every day"),
|
||||||
)
|
)
|
||||||
|
|
||||||
SERVICE_CHOICES = (
|
|
||||||
("ntfy", "NTFY"),
|
|
||||||
("webhook", "Custom webhook"),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Plan(models.Model):
|
class Plan(models.Model):
|
||||||
name = models.CharField(max_length=255, unique=True)
|
name = models.CharField(max_length=255, unique=True)
|
||||||
@ -171,13 +166,11 @@ class NotificationRule(models.Model):
|
|||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255)
|
||||||
priority = models.IntegerField(choices=PRIORITY_CHOICES, default=1)
|
priority = models.IntegerField(choices=PRIORITY_CHOICES, default=1)
|
||||||
topic = models.CharField(max_length=255, null=True, blank=True)
|
topic = models.CharField(max_length=255, null=True, blank=True)
|
||||||
url = models.CharField(max_length=1024, null=True, blank=True)
|
|
||||||
interval = models.IntegerField(choices=INTERVAL_CHOICES, default=0)
|
interval = models.IntegerField(choices=INTERVAL_CHOICES, default=0)
|
||||||
window = models.CharField(max_length=255, null=True, blank=True)
|
window = models.CharField(max_length=255, null=True, blank=True)
|
||||||
enabled = models.BooleanField(default=True)
|
enabled = models.BooleanField(default=True)
|
||||||
data = models.TextField()
|
data = models.TextField()
|
||||||
match = models.JSONField(null=True, blank=True)
|
match = models.JSONField(null=True, blank=True)
|
||||||
service = models.CharField(choices=SERVICE_CHOICES, max_length=255, default="ntfy")
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.user} - {self.name}"
|
return f"{self.user} - {self.name}"
|
||||||
@ -189,20 +182,11 @@ class NotificationRule(models.Model):
|
|||||||
raise ValueError(f"Invalid YAML: {e}")
|
raise ValueError(f"Invalid YAML: {e}")
|
||||||
return parsed
|
return parsed
|
||||||
|
|
||||||
@property
|
|
||||||
def matches(self):
|
|
||||||
"""
|
|
||||||
Get the total number of matches for this rule.
|
|
||||||
"""
|
|
||||||
if isinstance(self.match, dict):
|
|
||||||
return f"{sum(list(self.match.values()))}/{len(self.match)}"
|
|
||||||
|
|
||||||
|
|
||||||
class NotificationSettings(models.Model):
|
class NotificationSettings(models.Model):
|
||||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||||
topic = models.CharField(max_length=255, null=True, blank=True)
|
ntfy_topic = models.CharField(max_length=255, null=True, blank=True)
|
||||||
url = models.CharField(max_length=1024, null=True, blank=True)
|
ntfy_url = models.CharField(max_length=255, null=True, blank=True)
|
||||||
service = models.CharField(choices=SERVICE_CHOICES, max_length=255, default="ntfy")
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"Notification settings for {self.user}"
|
return f"Notification settings for {self.user}"
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
<th>topic</th>
|
<th>topic</th>
|
||||||
<th>enabled</th>
|
<th>enabled</th>
|
||||||
<th>data length</th>
|
<th>data length</th>
|
||||||
<th>match</th>
|
|
||||||
<th>actions</th>
|
<th>actions</th>
|
||||||
</thead>
|
</thead>
|
||||||
{% for item in object_list %}
|
{% for item in object_list %}
|
||||||
@ -41,7 +40,6 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>{{ item.data|length }}</td>
|
<td>{{ item.data|length }}</td>
|
||||||
<td>{{ item.matches }}</td>
|
|
||||||
<td>
|
<td>
|
||||||
<div class="buttons">
|
<div class="buttons">
|
||||||
<button
|
<button
|
||||||
@ -71,20 +69,6 @@
|
|||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
</button>
|
</button>
|
||||||
<button
|
|
||||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
|
||||||
hx-post="{% url 'rule_clear' type=type pk=item.id %}"
|
|
||||||
hx-trigger="click"
|
|
||||||
hx-target="#modals-here"
|
|
||||||
hx-swap="innerHTML"
|
|
||||||
hx-confirm="Are you sure you wish to clear matches for {{ item.name }}?"
|
|
||||||
class="button">
|
|
||||||
<span class="icon-text">
|
|
||||||
<span class="icon">
|
|
||||||
<i class="fa-solid fa-arrow-rotate-right"></i>
|
|
||||||
</span>
|
|
||||||
</span>
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
|
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
|
||||||
from django.shortcuts import render
|
|
||||||
from rest_framework.views import APIView
|
|
||||||
|
|
||||||
from core.forms import NotificationRuleForm, NotificationSettingsForm
|
from core.forms import NotificationRuleForm, NotificationSettingsForm
|
||||||
from core.models import NotificationRule, NotificationSettings
|
from core.models import NotificationRule, NotificationSettings
|
||||||
@ -63,24 +61,3 @@ class RuleUpdate(LoginRequiredMixin, PermissionRequiredMixin, ObjectUpdate):
|
|||||||
class RuleDelete(LoginRequiredMixin, PermissionRequiredMixin, ObjectDelete):
|
class RuleDelete(LoginRequiredMixin, PermissionRequiredMixin, ObjectDelete):
|
||||||
permission_required = "use_rules"
|
permission_required = "use_rules"
|
||||||
model = NotificationRule
|
model = NotificationRule
|
||||||
|
|
||||||
|
|
||||||
class RuleClear(LoginRequiredMixin, PermissionRequiredMixin, APIView):
|
|
||||||
permission_required = "use_rules"
|
|
||||||
|
|
||||||
def post(self, request, type, pk):
|
|
||||||
template_name = "partials/notify.html"
|
|
||||||
rule = NotificationRule.objects.get(pk=pk, user=request.user)
|
|
||||||
if isinstance(rule.match, dict):
|
|
||||||
for index in rule.match:
|
|
||||||
rule.match[index] = False
|
|
||||||
rule.save()
|
|
||||||
|
|
||||||
cleared_indices = ", ".join(rule.match)
|
|
||||||
context = {
|
|
||||||
"message": f"Cleared match status for indices: {cleared_indices}",
|
|
||||||
"class": "success",
|
|
||||||
}
|
|
||||||
response = render(request, template_name, context)
|
|
||||||
response["HX-Trigger"] = "notificationruleEvent"
|
|
||||||
return response
|
|
||||||
|
Loading…
Reference in New Issue
Block a user