Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sensor reading performance improvement #4464

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you had me make a commit just to add this comment, why are you removing it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that change is gone now in favor of a better one - also, you merged your PR before it was reviewed/approved, so I couldn't make the change there.

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)
Comment on lines -82 to -83
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this needed anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already handled at the top, no need to do it twice

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