some more django orm magic
@@ -4,6 +4,8 @@ from agents.models import Agent
from channels.db import database_sync_to_async
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from django.contrib.auth.models import AnonymousUser
+from django.utils import timezone as djangotime
+from django.db.models import F
class DashInfo(AsyncJsonWebsocketConsumer):
@@ -33,44 +35,41 @@ class DashInfo(AsyncJsonWebsocketConsumer):
@database_sync_to_async
def get_dashboard_info(self):
- total_agents = Agent.objects.filter_by_role(self.user).only(
- "pk",
- "last_seen",
- "overdue_time",
- "offline_time",
+ total_server_agents_count = (
+ Agent.objects.filter_by_role(self.user)
+ .filter(monitoring_type="server")
+ .count()
)
-
- server_offline_count = len(
- [
- agent
- for agent in total_agents
- if agent.monitoring_type == "server" and agent.status != "online"
- ]
+ offline_server_agents_count = (
+ Agent.objects.filter_by_role(self.user)
+ .filter(monitoring_type="server")
+ .filter(
+ last_seen__lt=djangotime.now()
+ - (djangotime.timedelta(minutes=1) * F("offline_time"))
)
-
- workstation_offline_count = len(
- [
- agent
- for agent in total_agents
- if agent.monitoring_type == "workstation" and agent.status != "online"
- ]
+ .count()
+ )
+ total_workstation_agents_count = (
+ Agent.objects.filter_by_role(self.user)
+ .filter(monitoring_type="workstation")
+ .count()
+ )
+ offline_workstation_agents_count = (
+ Agent.objects.filter_by_role(self.user)
+ .filter(monitoring_type="workstation")
+ .filter(
+ last_seen__lt=djangotime.now()
+ - (djangotime.timedelta(minutes=1) * F("offline_time"))
+ )
+ .count()
)
- ret = {
- "total_server_offline_count": server_offline_count,
- "total_workstation_offline_count": workstation_offline_count,
- "total_server_count": len(
- [agent for agent in total_agents if agent.monitoring_type == "server"]
- ),
- "total_workstation_count": len(
- [
- agent
- for agent in total_agents
- if agent.monitoring_type == "workstation"
- ]
- ),
+ return {
+ "total_server_offline_count": offline_server_agents_count,
+ "total_workstation_offline_count": offline_workstation_agents_count,
+ "total_server_count": total_server_agents_count,
+ "total_workstation_count": total_workstation_agents_count,
}
- return ret
async def send_dash_info(self):
while self.connected:
