Skip to content

Commit

Permalink
figured out we had a unbounded query with the recent cases, so gettin…
Browse files Browse the repository at this point in the history
…g rid of that case scope
  • Loading branch information
epugh committed Jan 19, 2024
1 parent 5598d4f commit c332d7e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 45 deletions.
2 changes: 1 addition & 1 deletion app/controllers/analytics/sparkline_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def vega_specification

def vega_data
@scores = []
@current_user.cases_involved_with.not_archived.recent.limit(8).each do |kase|
recent_cases(8).each do |kase|
@scores << kase.scores.sampled(kase.id, 100)
end
@scores.flatten!
Expand Down
32 changes: 1 addition & 31 deletions app/controllers/api/v1/cases/dropdown_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,11 @@ module Api
module V1
module Cases
class DropdownController < Api::ApiController
# rubocop:disable Metrics/MethodLength
def index
# Using joins/includes will not return the proper list in the
# correct order because rails refuses to include the
# `case_metadata`.`last_viewed_at` column in the SELECT statement
# which will then cause the ordering not to work properly.
# So instead, we have this beauty!
sql = "
SELECT DISTINCT `cases`.`id`, `case_metadata`.`last_viewed_at`
FROM `cases`
LEFT OUTER JOIN `case_metadata` ON `case_metadata`.`case_id` = `cases`.`id`
LEFT OUTER JOIN `teams_cases` ON `teams_cases`.`case_id` = `cases`.`id`
LEFT OUTER JOIN `teams` ON `teams`.`id` = `teams_cases`.`team_id`
LEFT OUTER JOIN `teams_members` ON `teams_members`.`team_id` = `teams`.`id`
LEFT OUTER JOIN `users` ON `users`.`id` = `teams_members`.`member_id`
WHERE (`teams_members`.`member_id` = #{current_user.id} OR `cases`.`owner_id` = #{current_user.id})
AND (`cases`.`archived` = false OR `cases`.`archived` IS NULL)
ORDER BY `case_metadata`.`last_viewed_at` DESC, `cases`.`id` DESC
LIMIT 3
"

results = ActiveRecord::Base.connection.execute(sql)

case_ids = []
results.each do |row|
case_ids << row.first.to_i
end

# map to objects
@cases = Case.includes(:tries).where(id: [ case_ids ])
@cases = @cases.sort_by { |x| case_ids.index x.id }
@cases = recent_cases 3

respond_with @cases
end
# rubocop:enable Metrics/MethodLength
end
end
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ApplicationController < ActionController::Base
before_action :set_current_user
before_action :require_login
before_action :check_current_user_locked!
before_action :set_recent_cases

# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
Expand Down
44 changes: 44 additions & 0 deletions app/controllers/concerns/authentication/current_case_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,50 @@ def find_case
set_case
end

def set_recent_cases
@recent_cases = recent_cases(3)
end

# rubocop:disable Metrics/MethodLength
def recent_cases count
if current_user
# Using joins/includes will not return the proper list in the
# correct order because rails refuses to include the
# `case_metadata`.`last_viewed_at` column in the SELECT statement
# which will then cause the ordering not to work properly.
# So instead, we have this beauty!
sql = "
SELECT DISTINCT `cases`.`id`, `case_metadata`.`last_viewed_at`
FROM `cases`
LEFT OUTER JOIN `case_metadata` ON `case_metadata`.`case_id` = `cases`.`id`
LEFT OUTER JOIN `teams_cases` ON `teams_cases`.`case_id` = `cases`.`id`
LEFT OUTER JOIN `teams` ON `teams`.`id` = `teams_cases`.`team_id`
LEFT OUTER JOIN `teams_members` ON `teams_members`.`team_id` = `teams`.`id`
LEFT OUTER JOIN `users` ON `users`.`id` = `teams_members`.`member_id`
WHERE (`teams_members`.`member_id` = #{current_user.id} OR `cases`.`owner_id` = #{current_user.id})
AND (`cases`.`archived` = false OR `cases`.`archived` IS NULL)
ORDER BY `case_metadata`.`last_viewed_at` DESC, `cases`.`id` DESC
LIMIT #{count}
"

results = ActiveRecord::Base.connection.execute(sql)

case_ids = []
results.each do |row|
case_ids << row.first.to_i
end

# map to objects
# cases = Case.includes(:tries).where(id: [ case_ids ])
cases = Case.where(id: [ case_ids ])
cases = cases.sort_by { |x| case_ids.index x.id }
else
cases = []
end
cases
end
# rubocop:enable Metrics/MethodLength

def case_with_all_the_bells_whistles
if current_user
@case = current_user
Expand Down
7 changes: 2 additions & 5 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# frozen_string_literal: true

class HomeController < ApplicationController

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def show
@cases = @current_user.cases_involved_with.not_archived.with_counts

@most_recent_cases = @current_user.cases_involved_with.not_archived.recent.limit(4).with_counts.sort_by(&:case_name)
@most_recent_cases = recent_cases(4).sort_by { |c| c.case_name.downcase }

# Run the prophet!
@prophet_case_data = {}
Expand Down Expand Up @@ -61,10 +59,9 @@ def show

# Homepage is too slow so we have to cut some stuff out ;-(
# candidate_cases = @cases.select { |kase| kase.scores.scored.count.positive? }
@cases
# @grouped_cases = candidate_cases.group_by { |kase| kase.case_name.split(':').first }
# @grouped_cases = @grouped_cases.select { |_key, value| value.count > 1 }
end
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/CyclomaticComplexity
Expand Down
6 changes: 0 additions & 6 deletions app/models/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ class Case < ApplicationRecord

scope :public_cases, -> { where(public: true) }

# return cases sorted by recently either updated or viewed by the user
scope :recent, -> {
left_outer_joins(:metadata)
.order(Arel.sql('`case_metadata`.`last_viewed_at` DESC, `cases`.`updated_at` DESC'))
}

# load up the queries count for the case, alternative to counter_cache
scope :with_counts, -> {
select <<~SQL.squish
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/_case.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<tr>
<th scope="row"><%= kase.case_name %></th>
<td><%= kase.queries_count %></td>
<td><%= kase.queries.count %></td>
<td>
<% unless kase.last_score.blank? %>
<%= number_with_precision(kase.last_score.score, precision: 2) %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
RECENT CASES
</li>
<li><hr class="dropdown-divider"></li>
<% @current_user.cases_involved_with.not_archived.recent.limit(3).each do |kase| %>
<% @recent_cases.each do |kase| %>
<li><%= link_to kase.case_name, case_core_path(kase, kase.last_try_number), class: 'cases-nav-listing' %></li>
<li><hr class="dropdown-divider"></li>
<% end %>
Expand Down

0 comments on commit c332d7e

Please sign in to comment.