add more caching to speed up agent table loading
This commit is contained in:
@@ -18,6 +18,7 @@ from nats.errors import TimeoutError
|
|||||||
from packaging import version as pyver
|
from packaging import version as pyver
|
||||||
|
|
||||||
from agents.utils import get_agent_url
|
from agents.utils import get_agent_url
|
||||||
|
from checks.models import CheckResult
|
||||||
from core.models import TZ_CHOICES
|
from core.models import TZ_CHOICES
|
||||||
from core.utils import get_core_settings, send_command_with_mesh
|
from core.utils import get_core_settings, send_command_with_mesh
|
||||||
from logs.models import BaseAuditModel, DebugLog, PendingAction
|
from logs.models import BaseAuditModel, DebugLog, PendingAction
|
||||||
@@ -25,6 +26,8 @@ from tacticalrmm.constants import (
|
|||||||
AGENT_STATUS_OFFLINE,
|
AGENT_STATUS_OFFLINE,
|
||||||
AGENT_STATUS_ONLINE,
|
AGENT_STATUS_ONLINE,
|
||||||
AGENT_STATUS_OVERDUE,
|
AGENT_STATUS_OVERDUE,
|
||||||
|
AGENT_TBL_CHECKS_CACHE_PREFIX,
|
||||||
|
AGENT_TBL_PEND_ACTION_CNT_CACHE_PREFIX,
|
||||||
ONLINE_AGENTS,
|
ONLINE_AGENTS,
|
||||||
AgentHistoryType,
|
AgentHistoryType,
|
||||||
AgentMonType,
|
AgentMonType,
|
||||||
@@ -214,48 +217,58 @@ class Agent(BaseAuditModel):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def checks(self) -> Dict[str, Any]:
|
def checks(self) -> Dict[str, Any]:
|
||||||
from checks.models import CheckResult
|
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
|
total, passing, failing, warning, info = 0, 0, 0, 0, 0
|
||||||
|
for check in self.get_checks_with_policies(exclude_overridden=True):
|
||||||
for check in self.get_checks_with_policies(exclude_overridden=True):
|
total += 1
|
||||||
total += 1
|
if (
|
||||||
if (
|
not hasattr(check.check_result, "status")
|
||||||
not hasattr(check.check_result, "status")
|
or isinstance(check.check_result, CheckResult)
|
||||||
or isinstance(check.check_result, CheckResult)
|
and check.check_result.status == CheckStatus.PASSING
|
||||||
and check.check_result.status == CheckStatus.PASSING
|
):
|
||||||
):
|
passing += 1
|
||||||
passing += 1
|
elif (
|
||||||
elif (
|
isinstance(check.check_result, CheckResult)
|
||||||
isinstance(check.check_result, CheckResult)
|
and check.check_result.status == CheckStatus.FAILING
|
||||||
and check.check_result.status == CheckStatus.FAILING
|
):
|
||||||
):
|
alert_severity = (
|
||||||
alert_severity = (
|
check.check_result.alert_severity
|
||||||
check.check_result.alert_severity
|
if check.check_type
|
||||||
if check.check_type
|
in (
|
||||||
in (
|
CheckType.MEMORY,
|
||||||
CheckType.MEMORY,
|
CheckType.CPU_LOAD,
|
||||||
CheckType.CPU_LOAD,
|
CheckType.DISK_SPACE,
|
||||||
CheckType.DISK_SPACE,
|
CheckType.SCRIPT,
|
||||||
CheckType.SCRIPT,
|
)
|
||||||
|
else check.alert_severity
|
||||||
)
|
)
|
||||||
else check.alert_severity
|
if alert_severity == AlertSeverity.ERROR:
|
||||||
)
|
failing += 1
|
||||||
if alert_severity == AlertSeverity.ERROR:
|
elif alert_severity == AlertSeverity.WARNING:
|
||||||
failing += 1
|
warning += 1
|
||||||
elif alert_severity == AlertSeverity.WARNING:
|
elif alert_severity == AlertSeverity.INFO:
|
||||||
warning += 1
|
info += 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
|
return ret
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
@@ -137,10 +137,6 @@ class GetAgents(APIView):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
.annotate(
|
.annotate(
|
||||||
pending_actions_count=Count(
|
|
||||||
"pendingactions",
|
|
||||||
filter=Q(pendingactions__status=PAStatus.PENDING),
|
|
||||||
),
|
|
||||||
has_patches_pending=Exists(
|
has_patches_pending=Exists(
|
||||||
WinUpdate.objects.filter(
|
WinUpdate.objects.filter(
|
||||||
agent_id=OuterRef("pk"), action="approve", installed=False
|
agent_id=OuterRef("pk"), action="approve", installed=False
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ from django.http import FileResponse
|
|||||||
from meshctrl.utils import get_auth_token
|
from meshctrl.utils import get_auth_token
|
||||||
|
|
||||||
from tacticalrmm.constants import (
|
from tacticalrmm.constants import (
|
||||||
|
AGENT_TBL_CHECKS_CACHE_PREFIX,
|
||||||
|
AGENT_TBL_PEND_ACTION_CNT_CACHE_PREFIX,
|
||||||
CORESETTINGS_CACHE_KEY,
|
CORESETTINGS_CACHE_KEY,
|
||||||
ROLE_CACHE_PREFIX,
|
ROLE_CACHE_PREFIX,
|
||||||
AgentPlat,
|
AgentPlat,
|
||||||
@@ -29,6 +31,8 @@ class CoreSettingsNotFound(Exception):
|
|||||||
|
|
||||||
def clear_entire_cache() -> None:
|
def clear_entire_cache() -> None:
|
||||||
cache.delete_many_pattern(f"{ROLE_CACHE_PREFIX}*")
|
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(CORESETTINGS_CACHE_KEY)
|
||||||
cache.delete_many_pattern("site_*")
|
cache.delete_many_pattern("site_*")
|
||||||
cache.delete_many_pattern("agent_*")
|
cache.delete_many_pattern("agent_*")
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ class MeshAgentIdent(Enum):
|
|||||||
|
|
||||||
CORESETTINGS_CACHE_KEY = "core_settings"
|
CORESETTINGS_CACHE_KEY = "core_settings"
|
||||||
ROLE_CACHE_PREFIX = "role_"
|
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_ONLINE = "online"
|
||||||
AGENT_STATUS_OFFLINE = "offline"
|
AGENT_STATUS_OFFLINE = "offline"
|
||||||
|
|||||||
Reference in New Issue
Block a user