From 80595e76e7520b62161b3d4a07067a45895aae3f Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Sat, 9 Mar 2024 08:31:25 +0000 Subject: [PATCH] cleanup orphaned checkhistory results fixes #1789 --- api/tacticalrmm/core/tasks.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/api/tacticalrmm/core/tasks.py b/api/tacticalrmm/core/tasks.py index c427f0c976..c49f9530bf 100644 --- a/api/tacticalrmm/core/tasks.py +++ b/api/tacticalrmm/core/tasks.py @@ -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 @@ -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 ( @@ -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