-
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.
Merge pull request #236 from lnu-norge/fix-aggregation-destroying-unr…
…elated-stuff Fix aggregation destroying unrelated stuff + terrible performance
- Loading branch information
Showing
10 changed files
with
258 additions
and
40 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
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 |
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
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
require "benchmark" | ||
class PerformanceBenchmarkHighest | ||
def initialize(disabled: false) | ||
@start_times = {} | ||
@scores = [] | ||
@disabled = disabled | ||
end | ||
|
||
def track_score_for(title:, &block) | ||
return yield block if @disabled | ||
|
||
time = Benchmark.measure do | ||
yield block | ||
end | ||
@scores << { title:, time: time.real, ms: (time.real * 1000).round(2) } | ||
end | ||
|
||
def start_timer_for(title) | ||
return if @disabled | ||
|
||
@start_times[title] = Time.zone.now | ||
end | ||
|
||
def end_timer_for(title) | ||
return if @disabled | ||
|
||
time = Time.zone.now - @start_times[title] | ||
add_time(title:, time:) | ||
|
||
@start_times[title] = nil | ||
end | ||
|
||
def highest_scores | ||
return if @disabled | ||
|
||
high_scores = @scores.group_by { |s| s[:title] }.map do |title, scores| | ||
highest = "#{scores.max_by { |s| s[:time] }[:ms]}ms" | ||
{ "#{title}": highest, count: scores.count, total: "#{scores.sum { |s| s[:ms] }.round(2)}ms" } | ||
end | ||
puts "Highest times for each benchmark:" | ||
puts high_scores | ||
end | ||
|
||
private | ||
|
||
def add_time(title:, time:) | ||
@scores << { title:, time:, ms: (time * 1000).round(2) } | ||
end | ||
|
||
def filter_callers_to_only_project_files | ||
caller.select { |c| c.include?("/app/") } | ||
end | ||
end |
Oops, something went wrong.