-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make aggregation a lot more performant, by skipping loads of steps to…
… the db. Cutting from 200ms to 20ms mean time on 100 facilities.
- Loading branch information
1 parent
320c35f
commit 1eac656
Showing
3 changed files
with
84 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module CountableReviews | ||
private | ||
|
||
def most_recent_facility_reviews_for(facility) | ||
@grouped_facility_reviews[facility.id] || [] | ||
end | ||
|
||
def group_recent_facility_reviews_by_facility(count: 5) | ||
@grouped_facility_reviews = @space.facility_reviews | ||
.order(created_at: :desc) | ||
.group_by(&:facility_id) | ||
.transform_values { |reviews| reviews.first(count) } | ||
end | ||
|
||
def facility_reviews_for(facility) | ||
@last_five_facility_reviews_grouped_by_facility.find { |r| r.facility_id == facility.id } | ||
end | ||
|
||
def count_of_positive(reviews) | ||
# These keep performance high for this service, by keeping db calls to | ||
# a minimum. We could use the review.positive scope, but that would | ||
# require a db call for each review, which would be slow. We're only | ||
# counting in the already fetched list of reviews. | ||
count_of_reviews_with_experience(reviews, "was_allowed") | ||
end | ||
|
||
def count_of_negative(reviews) | ||
count_of_reviews_with_experience(reviews, "was_not_allowed") | ||
end | ||
|
||
def count_of_impossible(reviews) | ||
count_of_reviews_with_experience(reviews, "was_not_available") | ||
end | ||
|
||
def count_of_reviews_with_experience(reviews, experience) | ||
reviews.count { |r| r.experience == experience } | ||
end | ||
end |