Skip to content

Commit

Permalink
chore: optimize last_active_nodes fetch to last one_hour
Browse files Browse the repository at this point in the history
  • Loading branch information
thepsalmist committed Nov 21, 2024
1 parent e89323d commit 01fdff2
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions sensorsafrica/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.conf import settings
from django.utils import timezone
from django.db import connection
from django.db.models import ExpressionWrapper, F, FloatField, Max, Min, Sum, Avg, Q, Count
from django.db.models import ExpressionWrapper, F, FloatField, Max, Min, Sum, Avg, Q, Count, Prefetch
from django.db.models.functions import Cast, TruncHour, TruncDay, TruncMonth
from django.utils.decorators import method_decorator
from django.utils.text import slugify
Expand Down Expand Up @@ -138,13 +138,27 @@ class NodesView(viewsets.ViewSet):
@action(detail=False, methods=["get"], url_path="list-nodes", url_name="list_nodes", permission_classes=[AllowAny])
def list_nodes(self, request):
"""List all public nodes with active sensors."""
now = datetime.datetime.now()
one_hour_ago = now - datetime.timedelta(hours=1)

last_active_nodes = (
LastActiveNodes.objects.filter(last_data_received_at__gte=one_hour_ago)
.select_related("node", "location")
.prefetch_related(
Prefetch(
"node__sensors",
queryset=Sensor.objects.filter(public=True),
)
)
)

nodes = []
last_active_nodes = LastActiveNodes.objects.select_related("node", "location").iterator()

# Loop through the last active nodes
for last_active in last_active_nodes:
# Get the current node only if it has public sensors
node = last_active.node
if not node.sensors.filter(public=True).exists():
if not node.sensors.exists():
continue

# The last acive date
Expand Down

0 comments on commit 01fdff2

Please sign in to comment.