mirror of
https://github.com/healthchecks/healthchecks.git
synced 2025-04-10 15:37:30 +00:00
Add a management command to notify admins about new log records
This commit is contained in:
parent
71f1a8d9a8
commit
c3b4100f3b
3 changed files with 34 additions and 0 deletions
hc/logs/management
0
hc/logs/management/__init__.py
Normal file
0
hc/logs/management/__init__.py
Normal file
0
hc/logs/management/commands/__init__.py
Normal file
0
hc/logs/management/commands/__init__.py
Normal file
34
hc/logs/management/commands/sendlogs.py
Normal file
34
hc/logs/management/commands/sendlogs.py
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import timedelta as td
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.mail import mail_admins
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
from django.urls import reverse
|
||||||
|
from django.utils.timezone import now
|
||||||
|
|
||||||
|
from hc.logs.models import Record
|
||||||
|
|
||||||
|
YEAR_AGO = now() - td(days=365)
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = """Send notification to admins about new log events."""
|
||||||
|
|
||||||
|
def handle(self, **options: Any) -> str:
|
||||||
|
threshold = now() - td(hours=24)
|
||||||
|
count = Record.objects.filter(created__gt=threshold).count()
|
||||||
|
if count > 0:
|
||||||
|
url = settings.SITE_ROOT + reverse("admin:logs_record_changelist")
|
||||||
|
s_maybe = "" if count == 1 else "s"
|
||||||
|
message = f"{count} new log record{s_maybe} in the last 24 hours"
|
||||||
|
html_message = f"""
|
||||||
|
{count} new log record{s_maybe} in the last 24 hours.<br />
|
||||||
|
<a href="{url}">Show log records</a>.
|
||||||
|
"""
|
||||||
|
mail_admins(message, message, html_message=html_message)
|
||||||
|
return f"Done, {count} new log record{s_maybe}."
|
||||||
|
|
||||||
|
return "Done, no new log records."
|
Loading…
Add table
Reference in a new issue