healthchecks_healthchecks/hc/logs/management/commands/sendlogs.py
Pēteris Caune 281ce65504
isort
2024-12-18 13:42:18 +02:00

33 lines
1.1 KiB
Python

from __future__ import annotations
from datetime import timedelta as td
from typing import Any
from django.core.mail import mail_admins
from django.core.management.base import BaseCommand
from django.utils.timezone import now
from hc.lib.urls import absolute_reverse
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 = absolute_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."