Remove RHS cache of child groups ids

closes OUT-4774
flag=improved_outcomes_management

Test plan:
- As this PS doesn't add feature, but remove a cache,
  Assert the RHS works properly in all scenarios.
  You may take screen shots of the results in master and
  compare with this PS.

  * In the management screen
    * Selecting a group in LHS
    * Search for a group in RHS
    * Infinite scroll works properly

  * In Find Outcomes Modal
    * Assert the same as previous one

  * Repeat test for account and course

Change-Id: I073ebbf02ea7c2f75342a72ad700f1ef10a67dcc
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/273513
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Martin Yosifov <martin.yosifov@instructure.com>
Reviewed-by: Pat Renner <prenner@instructure.com>
Reviewed-by: Marcus Pompeu <marcus.pompeu@instructure.com>
QA-Review: Brian Watson <bwatson@instructure.com>
Product-Review: Pat Renner <prenner@instructure.com>
This commit is contained in:
Manoel Quirino Neto 2021-09-14 11:12:10 -03:00 committed by Pat Renner
parent 283ec7846e
commit fb67a476f8
3 changed files with 9 additions and 58 deletions

View File

@ -34,8 +34,6 @@ class LearningOutcomeGroup < ActiveRecord::Base
belongs_to :context, polymorphic: [:account, :course]
before_save :infer_defaults
after_create :clear_descendants_cache
after_update :clear_descendants_cache, if: -> { clear_descendants_cache? }
resolves_root_account through: -> (group) { group.context_id ? group.context.resolved_root_account_id : 0 }
validates :vendor_guid, length: { maximum: maximum_string_length, allow_nil: true }
validates_length_of :description, :maximum => maximum_text_length, :allow_nil => true, :allow_blank => true
@ -288,14 +286,6 @@ class LearningOutcomeGroup < ActiveRecord::Base
ancestor_ids.member?(id)
end
def clear_descendants_cache
Outcomes::LearningOutcomeGroupChildren.new(context).clear_descendants_cache
end
def clear_descendants_cache?
(previous_changes.keys & %w[learning_outcome_group_id workflow_state]).any?
end
private_class_method def self.title_order_by_clause(table = nil)
col = table ? "#{table}.title" : "title"
best_unicode_collation_key(col)

View File

@ -73,7 +73,7 @@ module Outcomes
end
def suboutcomes_by_group_id(learning_outcome_group_id, args={})
learning_outcome_groups_ids = children_ids(learning_outcome_group_id) << learning_outcome_group_id
learning_outcome_groups_ids = children_ids_with_self(learning_outcome_group_id)
relation = ContentTag.active.learning_outcome_links
.where(associated_asset_id: learning_outcome_groups_ids)
.joins(:learning_outcome_content)
@ -91,13 +91,6 @@ module Outcomes
end
end
def clear_descendants_cache
return unless improved_outcomes_management?
Rails.cache.delete(descendants_cache_key)
clear_total_outcomes_cache
end
def clear_total_outcomes_cache
Rails.cache.delete(context_timestamp_cache_key) if improved_outcomes_management?
end
@ -112,7 +105,7 @@ module Outcomes
private
def total_outcomes_for(learning_outcome_group_id, args={})
learning_outcome_groups_ids = children_ids(learning_outcome_group_id) << learning_outcome_group_id
learning_outcome_groups_ids = children_ids_with_self(learning_outcome_group_id)
relation = ContentTag.active.learning_outcome_links
.where(associated_asset_id: learning_outcome_groups_ids)
@ -162,34 +155,21 @@ module Outcomes
)
end
def children_ids(learning_outcome_group_id)
parent = data.find { |d| d['parent_id'] == learning_outcome_group_id }
parent&.dig('descendant_ids')&.tr('{}', '')&.split(',') || []
end
def data
if improved_outcomes_management?
Rails.cache.fetch(descendants_cache_key) do
LearningOutcomeGroup.connection.execute(learning_outcome_group_descendants_query).as_json
end
else
LearningOutcomeGroup.connection.execute(learning_outcome_group_descendants_query).as_json
end
end
def learning_outcome_group_descendants_query
<<-SQL.squish
def children_ids_with_self(learning_outcome_group_id)
sql = <<-SQL.squish
WITH RECURSIVE levels AS (
SELECT id, learning_outcome_group_id AS parent_id
FROM (#{LearningOutcomeGroup.active.where(context: context).to_sql}) AS data
SELECT id, id AS parent_id
FROM (#{LearningOutcomeGroup.active.where(id: learning_outcome_group_id).to_sql}) AS data
UNION ALL
SELECT child.id AS id, parent.parent_id AS parent_id
FROM #{LearningOutcomeGroup.quoted_table_name} child
INNER JOIN levels parent ON parent.id = child.learning_outcome_group_id
WHERE child.workflow_state <> 'deleted'
)
SELECT parent_id, array_agg(id) AS descendant_ids FROM levels WHERE parent_id IS NOT NULL GROUP BY parent_id
SELECT id FROM levels
SQL
LearningOutcomeGroup.connection.execute(sql).as_json.map {|r| r["id"]}
end
def context_timestamp_cache
@ -198,10 +178,6 @@ module Outcomes
end
end
def descendants_cache_key
['learning_outcome_group_descendants', context_asset_string].cache_key
end
def total_outcomes_cache_key(learning_outcome_group_id = nil)
['learning_outcome_group_total_outcomes',
context_asset_string,

View File

@ -417,21 +417,6 @@ describe Outcomes::LearningOutcomeGroupChildren do
end
end
describe '#clear_descendants_cache' do
it 'clears the cache' do
enable_cache do
expect(LearningOutcomeGroup.connection).to receive(:execute).and_call_original.twice
expect(ContentTag).to receive(:active).and_call_original.exactly(4).times
expect(subject.total_outcomes(g0.id)).to eq 12
expect(subject.total_outcomes(g1.id)).to eq 9
subject.clear_descendants_cache
instance = described_class.new(context)
expect(instance.total_outcomes(g0.id)).to eq 12
expect(instance.total_outcomes(g1.id)).to eq 9
end
end
end
describe '#clear_total_outcomes_cache' do
it 'clears the cache' do
enable_cache do