Skip to content

Commit 80595e7

Browse files
committed
cleanup orphaned checkhistory results fixes #1789
1 parent d49e687 commit 80595e7

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

api/tacticalrmm/core/tasks.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import nats
88
from django.conf import settings
9+
from django.db import transaction
910
from django.db.models import Prefetch
1011
from django.db.utils import DatabaseError
1112
from django.utils import timezone as djangotime
@@ -18,7 +19,7 @@
1819
from alerts.models import Alert
1920
from alerts.tasks import prune_resolved_alerts
2021
from autotasks.models import AutomatedTask, TaskResult
21-
from checks.models import Check, CheckResult
22+
from checks.models import Check, CheckHistory, CheckResult
2223
from checks.tasks import prune_check_history
2324
from clients.models import Client, Site
2425
from core.mesh_utils import (
@@ -66,6 +67,16 @@ def core_maintenance_tasks() -> None:
6667
remove_if_not_scheduled=True, expire_date__lt=djangotime.now()
6768
).delete()
6869

70+
with transaction.atomic():
71+
check_hist_agentids = CheckHistory.objects.values_list(
72+
"agent_id", flat=True
73+
).distinct()
74+
current_agentids = set(Agent.objects.values_list("agent_id", flat=True))
75+
orphaned_agentids = [
76+
i for i in check_hist_agentids if i not in current_agentids
77+
]
78+
CheckHistory.objects.filter(agent_id__in=orphaned_agentids).delete()
79+
6980
core = get_core_settings()
7081

7182
# remove old CheckHistory data

0 commit comments

Comments
 (0)