Skip to content

Commit

Permalink
Sensor reading performance improvement (#4464)
Browse files Browse the repository at this point in the history
Less queries, 40% faster
  • Loading branch information
axelstudios authored Jan 11, 2024
1 parent 13a83cc commit 036122d
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions seed/utils/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ def _usages_by_exact_times(self, page, per_page):
if self.showOnlyOccupiedReadings:
sensor_readings = sensor_readings.filter(is_occupied=True)

# order by id **greatly** speeds this up (cause of indexing, I think
timestamps = sensor_readings.distinct('timestamp').order_by("timestamp", "id").values_list("timestamp", flat=True)
timestamps = list(sensor_readings.distinct('timestamp').order_by('timestamp').values_list("timestamp", flat=True))
paginator = Paginator(timestamps, per_page)
timestamps_in_page = paginator.page(page)

Expand All @@ -78,9 +77,7 @@ def _usages_by_exact_times(self, page, per_page):
for sensor in self.sensors
}

sensor_readings = SensorReading.objects.filter(timestamp__range=[earliest_time, latest_time], sensor__in=self.sensors)
if self.showOnlyOccupiedReadings:
sensor_readings = sensor_readings.filter(is_occupied=True)
sensor_readings = sensor_readings.filter(timestamp__range=[earliest_time, latest_time])

for sensor_reading in sensor_readings.all():
timestamp = sensor_reading.timestamp.astimezone(tz=self.tz).strftime(time_format)
Expand Down

0 comments on commit 036122d

Please sign in to comment.