Skip to content

Commit

Permalink
cleanup orphaned checkhistory results fixes #1789
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1te909 committed Mar 9, 2024
1 parent d49e687 commit 80595e7
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion api/tacticalrmm/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import nats
from django.conf import settings
from django.db import transaction
from django.db.models import Prefetch
from django.db.utils import DatabaseError
from django.utils import timezone as djangotime
Expand All @@ -18,7 +19,7 @@
from alerts.models import Alert
from alerts.tasks import prune_resolved_alerts
from autotasks.models import AutomatedTask, TaskResult
from checks.models import Check, CheckResult
from checks.models import Check, CheckHistory, CheckResult
from checks.tasks import prune_check_history
from clients.models import Client, Site
from core.mesh_utils import (
Expand Down Expand Up @@ -66,6 +67,16 @@ def core_maintenance_tasks() -> None:
remove_if_not_scheduled=True, expire_date__lt=djangotime.now()
).delete()

with transaction.atomic():
check_hist_agentids = CheckHistory.objects.values_list(
"agent_id", flat=True
).distinct()
current_agentids = set(Agent.objects.values_list("agent_id", flat=True))
orphaned_agentids = [
i for i in check_hist_agentids if i not in current_agentids
]
CheckHistory.objects.filter(agent_id__in=orphaned_agentids).delete()

core = get_core_settings()

# remove old CheckHistory data
Expand Down

0 comments on commit 80595e7

Please sign in to comment.