canvas-lms/lib/outcomes/learning_outcome_group_chil...

184 lines
6.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
#
# Copyright (C) 2021 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
module Outcomes
Add cache to Outcomes::LearningOutcomeGroupChildren Updates Outcomes::LearningOutcomeGroupChildren by adding cache at the queries for getting the total subgroups and outcomes. closes OUT-4148 flag=improved_outcomes_management Test plan: - Create nested learning outcome groups - For each nested learning outcome group create learning outcomes > With FF improved_outcomes_management: OFF - On Rails console: calls to Outcomes::LearningOutcomeGroupChildren methods should return a default value - On web: when generating actions over ContentTag, LearningOutcome and LearningOutcomeGroup it should not lead to clear any cache > With FF improved_outcomes_management: ON - On Rails console: call Outcomes::LearningOutcomeGroupChildren methods for getting total subgroups and outcomes, queries to the DB should be made (it will need the root context) - Call again the same methods, it should return the values from cache - Create a new instance of the class and call the same methods, it should return the values from cache - Clear the cache with `Rails.cache.clear` - On web (or through GraphiQL) get the total subgroups and total outcomes multiple times; it should cache the data and should not run additional queries - On web: clear the cache by executing the following actions: - Add a Learning Outcome Group - Adopt a Learning Outcome Group - Copy a Learning Outcome Group from global - Remove a Learning Outcome Group - Add an Outcome - Remove an Outcome - Get the total subgroups and total outcomes, it should run new queries - Run the same tests for global context, it should generate and clear the cache in the same way Change-Id: I9b0bfc68b84b3e36869d69a926ef84d9989ea96d Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257257 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Reviewed-by: Augusto Callejas <acallejas@instructure.com> QA-Review: Chrystal Langston <chrystal.langston@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com>
2021-01-23 09:03:53 +08:00
class LearningOutcomeGroupChildren
attr_reader :context
SHORT_DESCRIPTION = "coalesce(learning_outcomes.short_description, '')"
# E'<[^>]+>' -> removes html tags
# E'&\\w+;' -> removes html entities
DESCRIPTION = "regexp_replace(regexp_replace(coalesce(learning_outcomes.description, ''), E'<[^>]+>', '', 'gi'), E'&\\w+;', ' ', 'gi')"
def initialize(context = nil)
@context = context
end
def total_subgroups(learning_outcome_group_id)
Add cache to Outcomes::LearningOutcomeGroupChildren Updates Outcomes::LearningOutcomeGroupChildren by adding cache at the queries for getting the total subgroups and outcomes. closes OUT-4148 flag=improved_outcomes_management Test plan: - Create nested learning outcome groups - For each nested learning outcome group create learning outcomes > With FF improved_outcomes_management: OFF - On Rails console: calls to Outcomes::LearningOutcomeGroupChildren methods should return a default value - On web: when generating actions over ContentTag, LearningOutcome and LearningOutcomeGroup it should not lead to clear any cache > With FF improved_outcomes_management: ON - On Rails console: call Outcomes::LearningOutcomeGroupChildren methods for getting total subgroups and outcomes, queries to the DB should be made (it will need the root context) - Call again the same methods, it should return the values from cache - Create a new instance of the class and call the same methods, it should return the values from cache - Clear the cache with `Rails.cache.clear` - On web (or through GraphiQL) get the total subgroups and total outcomes multiple times; it should cache the data and should not run additional queries - On web: clear the cache by executing the following actions: - Add a Learning Outcome Group - Adopt a Learning Outcome Group - Copy a Learning Outcome Group from global - Remove a Learning Outcome Group - Add an Outcome - Remove an Outcome - Get the total subgroups and total outcomes, it should run new queries - Run the same tests for global context, it should generate and clear the cache in the same way Change-Id: I9b0bfc68b84b3e36869d69a926ef84d9989ea96d Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257257 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Reviewed-by: Augusto Callejas <acallejas@instructure.com> QA-Review: Chrystal Langston <chrystal.langston@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com>
2021-01-23 09:03:53 +08:00
return 0 unless improved_outcomes_management?
children_ids(learning_outcome_group_id).length
end
def total_outcomes(learning_outcome_group_id, args={})
Add cache to Outcomes::LearningOutcomeGroupChildren Updates Outcomes::LearningOutcomeGroupChildren by adding cache at the queries for getting the total subgroups and outcomes. closes OUT-4148 flag=improved_outcomes_management Test plan: - Create nested learning outcome groups - For each nested learning outcome group create learning outcomes > With FF improved_outcomes_management: OFF - On Rails console: calls to Outcomes::LearningOutcomeGroupChildren methods should return a default value - On web: when generating actions over ContentTag, LearningOutcome and LearningOutcomeGroup it should not lead to clear any cache > With FF improved_outcomes_management: ON - On Rails console: call Outcomes::LearningOutcomeGroupChildren methods for getting total subgroups and outcomes, queries to the DB should be made (it will need the root context) - Call again the same methods, it should return the values from cache - Create a new instance of the class and call the same methods, it should return the values from cache - Clear the cache with `Rails.cache.clear` - On web (or through GraphiQL) get the total subgroups and total outcomes multiple times; it should cache the data and should not run additional queries - On web: clear the cache by executing the following actions: - Add a Learning Outcome Group - Adopt a Learning Outcome Group - Copy a Learning Outcome Group from global - Remove a Learning Outcome Group - Add an Outcome - Remove an Outcome - Get the total subgroups and total outcomes, it should run new queries - Run the same tests for global context, it should generate and clear the cache in the same way Change-Id: I9b0bfc68b84b3e36869d69a926ef84d9989ea96d Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257257 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Reviewed-by: Augusto Callejas <acallejas@instructure.com> QA-Review: Chrystal Langston <chrystal.langston@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com>
2021-01-23 09:03:53 +08:00
return 0 unless improved_outcomes_management?
if args == {}
cache_key = total_outcomes_cache_key(learning_outcome_group_id)
Rails.cache.fetch(cache_key) do
total_outcomes_for(learning_outcome_group_id, args)
end
else
total_outcomes_for(learning_outcome_group_id, args)
Add cache to Outcomes::LearningOutcomeGroupChildren Updates Outcomes::LearningOutcomeGroupChildren by adding cache at the queries for getting the total subgroups and outcomes. closes OUT-4148 flag=improved_outcomes_management Test plan: - Create nested learning outcome groups - For each nested learning outcome group create learning outcomes > With FF improved_outcomes_management: OFF - On Rails console: calls to Outcomes::LearningOutcomeGroupChildren methods should return a default value - On web: when generating actions over ContentTag, LearningOutcome and LearningOutcomeGroup it should not lead to clear any cache > With FF improved_outcomes_management: ON - On Rails console: call Outcomes::LearningOutcomeGroupChildren methods for getting total subgroups and outcomes, queries to the DB should be made (it will need the root context) - Call again the same methods, it should return the values from cache - Create a new instance of the class and call the same methods, it should return the values from cache - Clear the cache with `Rails.cache.clear` - On web (or through GraphiQL) get the total subgroups and total outcomes multiple times; it should cache the data and should not run additional queries - On web: clear the cache by executing the following actions: - Add a Learning Outcome Group - Adopt a Learning Outcome Group - Copy a Learning Outcome Group from global - Remove a Learning Outcome Group - Add an Outcome - Remove an Outcome - Get the total subgroups and total outcomes, it should run new queries - Run the same tests for global context, it should generate and clear the cache in the same way Change-Id: I9b0bfc68b84b3e36869d69a926ef84d9989ea96d Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257257 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Reviewed-by: Augusto Callejas <acallejas@instructure.com> QA-Review: Chrystal Langston <chrystal.langston@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com>
2021-01-23 09:03:53 +08:00
end
end
def suboutcomes_by_group_id(learning_outcome_group_id, args={})
return ContentTag.none unless improved_outcomes_management?
Fetch suboutcomes within a learning outcome group via graphql Add ability to fetch all sub-outcomes of a learning outcome group Ordered by parent group title then outcome short description. The Results should be paginated to accommodate for infinite scroll. closes OUT-4130 flag=improved_outcomes_management test plan - PreReq: Account/Course with outcomes and groups - Log into Canvas and navigate to graphiql - Run the below query - outcomes should be populated with all outcomes and suboutcomes For the root outcome group order by group title, outcome short description - Run the below query query GroupDetailQuery($id: ID!, $outcomesCursor: String) { group: legacyNode(type: LearningOutcomeGroup, _id: $id) { ... on LearningOutcomeGroup { _id description title outcomesCount outcomes(first: 10, after: $outcomesCursor) { pageInfo { hasNextPage startCursor endCursor } nodes { ... on LearningOutcome { _id description title } } } } } } { "id": 1, "outcomesCursor": "" } - All outcomes should be returned ordered by parent group name, then outcome name - Change the name of an outcome and/or group to force it to change the order. - Run the query again, and you should see the updated outcome or Parent group’s outcomes first before the remain outcomes Change-Id: Icdad82c0b57ca625924d9b0af7d7b3304b88c251 Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257653 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com> Reviewed-by: Pat Renner <prenner@instructure.com> Reviewed-by: Michael Brewer-Davis <mbd@instructure.com> QA-Review: Pat Renner <prenner@instructure.com>
2021-01-29 00:31:25 +08:00
Add cache to Outcomes::LearningOutcomeGroupChildren Updates Outcomes::LearningOutcomeGroupChildren by adding cache at the queries for getting the total subgroups and outcomes. closes OUT-4148 flag=improved_outcomes_management Test plan: - Create nested learning outcome groups - For each nested learning outcome group create learning outcomes > With FF improved_outcomes_management: OFF - On Rails console: calls to Outcomes::LearningOutcomeGroupChildren methods should return a default value - On web: when generating actions over ContentTag, LearningOutcome and LearningOutcomeGroup it should not lead to clear any cache > With FF improved_outcomes_management: ON - On Rails console: call Outcomes::LearningOutcomeGroupChildren methods for getting total subgroups and outcomes, queries to the DB should be made (it will need the root context) - Call again the same methods, it should return the values from cache - Create a new instance of the class and call the same methods, it should return the values from cache - Clear the cache with `Rails.cache.clear` - On web (or through GraphiQL) get the total subgroups and total outcomes multiple times; it should cache the data and should not run additional queries - On web: clear the cache by executing the following actions: - Add a Learning Outcome Group - Adopt a Learning Outcome Group - Copy a Learning Outcome Group from global - Remove a Learning Outcome Group - Add an Outcome - Remove an Outcome - Get the total subgroups and total outcomes, it should run new queries - Run the same tests for global context, it should generate and clear the cache in the same way Change-Id: I9b0bfc68b84b3e36869d69a926ef84d9989ea96d Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257257 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Reviewed-by: Augusto Callejas <acallejas@instructure.com> QA-Review: Chrystal Langston <chrystal.langston@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com>
2021-01-23 09:03:53 +08:00
learning_outcome_groups_ids = children_ids(learning_outcome_group_id) << learning_outcome_group_id
relation = ContentTag.active.learning_outcome_links.
Add cache to Outcomes::LearningOutcomeGroupChildren Updates Outcomes::LearningOutcomeGroupChildren by adding cache at the queries for getting the total subgroups and outcomes. closes OUT-4148 flag=improved_outcomes_management Test plan: - Create nested learning outcome groups - For each nested learning outcome group create learning outcomes > With FF improved_outcomes_management: OFF - On Rails console: calls to Outcomes::LearningOutcomeGroupChildren methods should return a default value - On web: when generating actions over ContentTag, LearningOutcome and LearningOutcomeGroup it should not lead to clear any cache > With FF improved_outcomes_management: ON - On Rails console: call Outcomes::LearningOutcomeGroupChildren methods for getting total subgroups and outcomes, queries to the DB should be made (it will need the root context) - Call again the same methods, it should return the values from cache - Create a new instance of the class and call the same methods, it should return the values from cache - Clear the cache with `Rails.cache.clear` - On web (or through GraphiQL) get the total subgroups and total outcomes multiple times; it should cache the data and should not run additional queries - On web: clear the cache by executing the following actions: - Add a Learning Outcome Group - Adopt a Learning Outcome Group - Copy a Learning Outcome Group from global - Remove a Learning Outcome Group - Add an Outcome - Remove an Outcome - Get the total subgroups and total outcomes, it should run new queries - Run the same tests for global context, it should generate and clear the cache in the same way Change-Id: I9b0bfc68b84b3e36869d69a926ef84d9989ea96d Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257257 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Reviewed-by: Augusto Callejas <acallejas@instructure.com> QA-Review: Chrystal Langston <chrystal.langston@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com>
2021-01-23 09:03:53 +08:00
where(associated_asset_id: learning_outcome_groups_ids).
Fetch suboutcomes within a learning outcome group via graphql Add ability to fetch all sub-outcomes of a learning outcome group Ordered by parent group title then outcome short description. The Results should be paginated to accommodate for infinite scroll. closes OUT-4130 flag=improved_outcomes_management test plan - PreReq: Account/Course with outcomes and groups - Log into Canvas and navigate to graphiql - Run the below query - outcomes should be populated with all outcomes and suboutcomes For the root outcome group order by group title, outcome short description - Run the below query query GroupDetailQuery($id: ID!, $outcomesCursor: String) { group: legacyNode(type: LearningOutcomeGroup, _id: $id) { ... on LearningOutcomeGroup { _id description title outcomesCount outcomes(first: 10, after: $outcomesCursor) { pageInfo { hasNextPage startCursor endCursor } nodes { ... on LearningOutcome { _id description title } } } } } } { "id": 1, "outcomesCursor": "" } - All outcomes should be returned ordered by parent group name, then outcome name - Change the name of an outcome and/or group to force it to change the order. - Run the query again, and you should see the updated outcome or Parent group’s outcomes first before the remain outcomes Change-Id: Icdad82c0b57ca625924d9b0af7d7b3304b88c251 Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257653 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com> Reviewed-by: Pat Renner <prenner@instructure.com> Reviewed-by: Michael Brewer-Davis <mbd@instructure.com> QA-Review: Pat Renner <prenner@instructure.com>
2021-01-29 00:31:25 +08:00
joins(:learning_outcome_content).
Add cache to Outcomes::LearningOutcomeGroupChildren Updates Outcomes::LearningOutcomeGroupChildren by adding cache at the queries for getting the total subgroups and outcomes. closes OUT-4148 flag=improved_outcomes_management Test plan: - Create nested learning outcome groups - For each nested learning outcome group create learning outcomes > With FF improved_outcomes_management: OFF - On Rails console: calls to Outcomes::LearningOutcomeGroupChildren methods should return a default value - On web: when generating actions over ContentTag, LearningOutcome and LearningOutcomeGroup it should not lead to clear any cache > With FF improved_outcomes_management: ON - On Rails console: call Outcomes::LearningOutcomeGroupChildren methods for getting total subgroups and outcomes, queries to the DB should be made (it will need the root context) - Call again the same methods, it should return the values from cache - Create a new instance of the class and call the same methods, it should return the values from cache - Clear the cache with `Rails.cache.clear` - On web (or through GraphiQL) get the total subgroups and total outcomes multiple times; it should cache the data and should not run additional queries - On web: clear the cache by executing the following actions: - Add a Learning Outcome Group - Adopt a Learning Outcome Group - Copy a Learning Outcome Group from global - Remove a Learning Outcome Group - Add an Outcome - Remove an Outcome - Get the total subgroups and total outcomes, it should run new queries - Run the same tests for global context, it should generate and clear the cache in the same way Change-Id: I9b0bfc68b84b3e36869d69a926ef84d9989ea96d Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257257 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Reviewed-by: Augusto Callejas <acallejas@instructure.com> QA-Review: Chrystal Langston <chrystal.langston@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com>
2021-01-23 09:03:53 +08:00
joins("INNER JOIN #{LearningOutcomeGroup.quoted_table_name} AS logs
ON logs.id = content_tags.associated_asset_id")
if args[:search_query]
relation = add_search_query(relation, args[:search_query])
add_search_order(relation, args[:search_query])
else
relation.order(
LearningOutcomeGroup.best_unicode_collation_key('logs.title'),
LearningOutcome.best_unicode_collation_key('short_description')
)
end
Fetch suboutcomes within a learning outcome group via graphql Add ability to fetch all sub-outcomes of a learning outcome group Ordered by parent group title then outcome short description. The Results should be paginated to accommodate for infinite scroll. closes OUT-4130 flag=improved_outcomes_management test plan - PreReq: Account/Course with outcomes and groups - Log into Canvas and navigate to graphiql - Run the below query - outcomes should be populated with all outcomes and suboutcomes For the root outcome group order by group title, outcome short description - Run the below query query GroupDetailQuery($id: ID!, $outcomesCursor: String) { group: legacyNode(type: LearningOutcomeGroup, _id: $id) { ... on LearningOutcomeGroup { _id description title outcomesCount outcomes(first: 10, after: $outcomesCursor) { pageInfo { hasNextPage startCursor endCursor } nodes { ... on LearningOutcome { _id description title } } } } } } { "id": 1, "outcomesCursor": "" } - All outcomes should be returned ordered by parent group name, then outcome name - Change the name of an outcome and/or group to force it to change the order. - Run the query again, and you should see the updated outcome or Parent group’s outcomes first before the remain outcomes Change-Id: Icdad82c0b57ca625924d9b0af7d7b3304b88c251 Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257653 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com> Reviewed-by: Pat Renner <prenner@instructure.com> Reviewed-by: Michael Brewer-Davis <mbd@instructure.com> QA-Review: Pat Renner <prenner@instructure.com>
2021-01-29 00:31:25 +08:00
end
Add cache to Outcomes::LearningOutcomeGroupChildren Updates Outcomes::LearningOutcomeGroupChildren by adding cache at the queries for getting the total subgroups and outcomes. closes OUT-4148 flag=improved_outcomes_management Test plan: - Create nested learning outcome groups - For each nested learning outcome group create learning outcomes > With FF improved_outcomes_management: OFF - On Rails console: calls to Outcomes::LearningOutcomeGroupChildren methods should return a default value - On web: when generating actions over ContentTag, LearningOutcome and LearningOutcomeGroup it should not lead to clear any cache > With FF improved_outcomes_management: ON - On Rails console: call Outcomes::LearningOutcomeGroupChildren methods for getting total subgroups and outcomes, queries to the DB should be made (it will need the root context) - Call again the same methods, it should return the values from cache - Create a new instance of the class and call the same methods, it should return the values from cache - Clear the cache with `Rails.cache.clear` - On web (or through GraphiQL) get the total subgroups and total outcomes multiple times; it should cache the data and should not run additional queries - On web: clear the cache by executing the following actions: - Add a Learning Outcome Group - Adopt a Learning Outcome Group - Copy a Learning Outcome Group from global - Remove a Learning Outcome Group - Add an Outcome - Remove an Outcome - Get the total subgroups and total outcomes, it should run new queries - Run the same tests for global context, it should generate and clear the cache in the same way Change-Id: I9b0bfc68b84b3e36869d69a926ef84d9989ea96d Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257257 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Reviewed-by: Augusto Callejas <acallejas@instructure.com> QA-Review: Chrystal Langston <chrystal.langston@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com>
2021-01-23 09:03:53 +08:00
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
private
def total_outcomes_for(learning_outcome_group_id, args={})
learning_outcome_groups_ids = children_ids(learning_outcome_group_id) << learning_outcome_group_id
relation = ContentTag.active.learning_outcome_links.
where(associated_asset_id: learning_outcome_groups_ids).
joins(:learning_outcome_content)
if args[:search_query]
relation = add_search_query(relation, args[:search_query])
end
relation.select(:content_id).distinct.count
end
def add_search_query(relation, search_query)
search_query_tokens = search_query.split(' ')
short_description_query = ContentTag.sanitize_sql_array(["#{SHORT_DESCRIPTION} ~* ANY(array[?])", search_query_tokens])
description_query = ContentTag.sanitize_sql_array(["#{DESCRIPTION} ~* ANY(array[?])", search_query_tokens])
relation.where("#{short_description_query} OR #{description_query}")
end
def add_search_order(relation, search_query)
select_query = ContentTag.sanitize_sql_array([<<-SQL, search_query, search_query])
"content_tags".*,
GREATEST(public.word_similarity(#{SHORT_DESCRIPTION}, ?), public.word_similarity(#{DESCRIPTION}, ?)) as sim
SQL
relation.select(select_query).order(
"sim DESC",
LearningOutcomeGroup.best_unicode_collation_key('logs.title'),
LearningOutcome.best_unicode_collation_key('short_description')
)
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
Add cache to Outcomes::LearningOutcomeGroupChildren Updates Outcomes::LearningOutcomeGroupChildren by adding cache at the queries for getting the total subgroups and outcomes. closes OUT-4148 flag=improved_outcomes_management Test plan: - Create nested learning outcome groups - For each nested learning outcome group create learning outcomes > With FF improved_outcomes_management: OFF - On Rails console: calls to Outcomes::LearningOutcomeGroupChildren methods should return a default value - On web: when generating actions over ContentTag, LearningOutcome and LearningOutcomeGroup it should not lead to clear any cache > With FF improved_outcomes_management: ON - On Rails console: call Outcomes::LearningOutcomeGroupChildren methods for getting total subgroups and outcomes, queries to the DB should be made (it will need the root context) - Call again the same methods, it should return the values from cache - Create a new instance of the class and call the same methods, it should return the values from cache - Clear the cache with `Rails.cache.clear` - On web (or through GraphiQL) get the total subgroups and total outcomes multiple times; it should cache the data and should not run additional queries - On web: clear the cache by executing the following actions: - Add a Learning Outcome Group - Adopt a Learning Outcome Group - Copy a Learning Outcome Group from global - Remove a Learning Outcome Group - Add an Outcome - Remove an Outcome - Get the total subgroups and total outcomes, it should run new queries - Run the same tests for global context, it should generate and clear the cache in the same way Change-Id: I9b0bfc68b84b3e36869d69a926ef84d9989ea96d Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/257257 Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com> Reviewed-by: Augusto Callejas <acallejas@instructure.com> QA-Review: Chrystal Langston <chrystal.langston@instructure.com> Product-Review: Michael Brewer-Davis <mbd@instructure.com>
2021-01-23 09:03:53 +08:00
Rails.cache.fetch(descendants_cache_key) do
LearningOutcomeGroup.connection.execute(learning_outcome_group_descendants_query).as_json
end
end
def learning_outcome_group_descendants_query
<<-SQL
WITH RECURSIVE levels AS (
SELECT id, learning_outcome_group_id AS parent_id
FROM (#{LearningOutcomeGroup.active.where(context: context).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
SQL
end
def context_timestamp_cache
Rails.cache.fetch(context_timestamp_cache_key) do
(Time.zone.now.to_f * 1000).to_i
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,
context_timestamp_cache,
learning_outcome_group_id].cache_key
end
def context_timestamp_cache_key
['learning_outcome_group_context_timestamp', context_asset_string].cache_key
end
def context_asset_string
(context || LearningOutcomeGroup.global_root_outcome_group).global_asset_string
end
def improved_outcomes_management?
@improved_outcomes_management ||= begin
return context.root_account.feature_enabled?(:improved_outcomes_management) if context
LoadAccount.default_domain_root_account.feature_enabled?(:improved_outcomes_management)
end
end
end
end