add more caching to speed up agent table loading

This commit is contained in:
wh1te909
2022-11-03 07:35:01 +00:00
parent 3c9e64de81
commit 13f544d2be
4 changed files with 59 additions and 44 deletions

View File

@@ -18,6 +18,7 @@ from nats.errors import TimeoutError
from packaging import version as pyver
from agents.utils import get_agent_url
from checks.models import CheckResult
from core.models import TZ_CHOICES
from core.utils import get_core_settings, send_command_with_mesh
from logs.models import BaseAuditModel, DebugLog, PendingAction
@@ -25,6 +26,8 @@ from tacticalrmm.constants import (
AGENT_STATUS_OFFLINE,
AGENT_STATUS_ONLINE,
AGENT_STATUS_OVERDUE,
AGENT_TBL_CHECKS_CACHE_PREFIX,
AGENT_TBL_PEND_ACTION_CNT_CACHE_PREFIX,
ONLINE_AGENTS,
AgentHistoryType,
AgentMonType,
@@ -214,48 +217,58 @@ class Agent(BaseAuditModel):
@property
def checks(self) -> Dict[str, Any]:
from checks.models import CheckResult
total, passing, failing, warning, info = 0, 0, 0, 0, 0
for check in self.get_checks_with_policies(exclude_overridden=True):
total += 1
if (
not hasattr(check.check_result, "status")
or isinstance(check.check_result, CheckResult)
and check.check_result.status == CheckStatus.PASSING
):
passing += 1
elif (
isinstance(check.check_result, CheckResult)
and check.check_result.status == CheckStatus.FAILING
):
alert_severity = (
check.check_result.alert_severity
if check.check_type
in (
CheckType.MEMORY,
CheckType.CPU_LOAD,
CheckType.DISK_SPACE,
CheckType.SCRIPT,
ret = cache.get(f"{AGENT_TBL_CHECKS_CACHE_PREFIX}{self.pk}")
if ret is None:
total, passing, failing, warning, info = 0, 0, 0, 0, 0
for check in self.get_checks_with_policies(exclude_overridden=True):
total += 1
if (
not hasattr(check.check_result, "status")
or isinstance(check.check_result, CheckResult)
and check.check_result.status == CheckStatus.PASSING
):
passing += 1
elif (
isinstance(check.check_result, CheckResult)
and check.check_result.status == CheckStatus.FAILING
):
alert_severity = (
check.check_result.alert_severity
if check.check_type
in (
CheckType.MEMORY,
CheckType.CPU_LOAD,
CheckType.DISK_SPACE,
CheckType.SCRIPT,
)
else check.alert_severity
)
else check.alert_severity
)
if alert_severity == AlertSeverity.ERROR:
failing += 1
elif alert_severity == AlertSeverity.WARNING:
warning += 1
elif alert_severity == AlertSeverity.INFO:
info += 1
if alert_severity == AlertSeverity.ERROR:
failing += 1
elif alert_severity == AlertSeverity.WARNING:
warning += 1
elif alert_severity == AlertSeverity.INFO:
info += 1
ret = {
"total": total,
"passing": passing,
"failing": failing,
"warning": warning,
"info": info,
"has_failing_checks": failing > 0 or warning > 0,
}
cache.set(f"{AGENT_TBL_CHECKS_CACHE_PREFIX}{self.pk}", ret, 300)
return ret
@property
def pending_actions_count(self) -> int:
ret = cache.get(f"{AGENT_TBL_PEND_ACTION_CNT_CACHE_PREFIX}{self.pk}")
if ret is None:
ret = self.pendingactions.filter(status=PAStatus.PENDING).count()
cache.set(f"{AGENT_TBL_PEND_ACTION_CNT_CACHE_PREFIX}{self.pk}", ret, 600)
ret = {
"total": total,
"passing": passing,
"failing": failing,
"warning": warning,
"info": info,
"has_failing_checks": failing > 0 or warning > 0,
}
return ret
@property

View File

@@ -137,10 +137,6 @@ class GetAgents(APIView):
),
)
.annotate(
pending_actions_count=Count(
"pendingactions",
filter=Q(pendingactions__status=PAStatus.PENDING),
),
has_patches_pending=Exists(
WinUpdate.objects.filter(
agent_id=OuterRef("pk"), action="approve", installed=False

View File

@@ -13,6 +13,8 @@ from django.http import FileResponse
from meshctrl.utils import get_auth_token
from tacticalrmm.constants import (
AGENT_TBL_CHECKS_CACHE_PREFIX,
AGENT_TBL_PEND_ACTION_CNT_CACHE_PREFIX,
CORESETTINGS_CACHE_KEY,
ROLE_CACHE_PREFIX,
AgentPlat,
@@ -29,6 +31,8 @@ class CoreSettingsNotFound(Exception):
def clear_entire_cache() -> None:
cache.delete_many_pattern(f"{ROLE_CACHE_PREFIX}*")
cache.delete_many_pattern(f"{AGENT_TBL_CHECKS_CACHE_PREFIX}*")
cache.delete_many_pattern(f"{AGENT_TBL_PEND_ACTION_CNT_CACHE_PREFIX}*")
cache.delete(CORESETTINGS_CACHE_KEY)
cache.delete_many_pattern("site_*")
cache.delete_many_pattern("agent_*")

View File

@@ -18,6 +18,8 @@ class MeshAgentIdent(Enum):
CORESETTINGS_CACHE_KEY = "core_settings"
ROLE_CACHE_PREFIX = "role_"
AGENT_TBL_CHECKS_CACHE_PREFIX = "agent_tbl_checks_"
AGENT_TBL_PEND_ACTION_CNT_CACHE_PREFIX = "agent_tbl_pendingactions_"
AGENT_STATUS_ONLINE = "online"
AGENT_STATUS_OFFLINE = "offline"