Reduce SQL queries for tag_statistics

Fixes CODEOCEAN-YP
This commit is contained in:
Sebastian Serth
2023-12-09 00:36:02 +01:00
parent 1bb61f73fe
commit 768eb53f7a
2 changed files with 6 additions and 10 deletions

View File

@ -84,7 +84,7 @@ class ExternalUsersController < ApplicationController
authorize! authorize!
statistics = [] statistics = []
tags = ProxyExercise.new.get_user_knowledge_and_max_knowledge(@user, @user.participations.uniq.compact) tags = ProxyExercise.new.get_user_knowledge_and_max_knowledge(@user, @user.participations.includes(:files, :tags, exercise_tags: [:tag]).uniq.compact)
tags[:user_topic_knowledge].each_pair do |tag, value| tags[:user_topic_knowledge].each_pair do |tag, value|
statistics.append({key: tag.name.to_s, value: (100.0 / tags[:max_topic_knowledge][tag] * value).round, statistics.append({key: tag.name.to_s, value: (100.0 / tags[:max_topic_knowledge][tag] * value).round,
id: tag.id}) id: tag.id})

View File

@ -226,20 +226,16 @@ class ProxyExercise < ApplicationRecord
tags_counter = all_used_tags_with_count.keys.index_with {|_tag| 0 } tags_counter = all_used_tags_with_count.keys.index_with {|_tag| 0 }
topic_knowledge_loss_user = all_used_tags_with_count.keys.index_with {|_t| 0 } topic_knowledge_loss_user = all_used_tags_with_count.keys.index_with {|_t| 0 }
topic_knowledge_max = all_used_tags_with_count.keys.index_with {|_t| 0 } topic_knowledge_max = all_used_tags_with_count.keys.index_with {|_t| 0 }
exercises_sorted = exercises.sort_by {|ex| ex.time_maximum_score(user) } exercises.each do |ex|
exercises_sorted.each do |ex|
Rails.logger.debug { "exercise: #{ex.id}: #{ex}" } Rails.logger.debug { "exercise: #{ex.id}: #{ex}" }
user_score_factor = score(user, ex) user_score_factor = score(user, ex)
ex.tags.each do |t| ex.exercise_tags.each do |ex_t|
t = ex_t.tag
tags_counter[t] += 1 tags_counter[t] += 1
tag_diminishing_return_factor = tag_diminishing_return_function(tags_counter[t], all_used_tags_with_count[t]) tag_diminishing_return_factor = tag_diminishing_return_function(tags_counter[t], all_used_tags_with_count[t])
tag_ratio = ex.exercise_tags.find_by(tag: t).factor.to_f / ex.exercise_tags.inject(0) do |sum, et| tag_ratio = ex_t.factor.to_f / ex.exercise_tags.inject(0) {|sum, et| sum + et.factor }
sum + et.factor
end
Rails.logger.debug do Rails.logger.debug do
"tag: #{t}, factor: #{ex.exercise_tags.find_by(tag: t).factor}, sumall: #{ex.exercise_tags.inject(0) do |sum, et| "tag: #{t}, factor: #{ex_t.factor}, sumall: #{ex.exercise_tags.inject(0) {|sum, et| sum + et.factor }}"
sum + et.factor
end }"
end end
Rails.logger.debug { "tag #{t}, count #{tags_counter[t]}, max: #{all_used_tags_with_count[t]}, factor: #{tag_diminishing_return_factor}" } Rails.logger.debug { "tag #{t}, count #{tags_counter[t]}, max: #{all_used_tags_with_count[t]}, factor: #{tag_diminishing_return_factor}" }
Rails.logger.debug { "tag_ratio #{tag_ratio}" } Rails.logger.debug { "tag_ratio #{tag_ratio}" }