Skip to content

Commit

Permalink
Reduce SQL queries for tag_statistics
Browse files Browse the repository at this point in the history
Fixes CODEOCEAN-YP
  • Loading branch information
MrSerth committed Dec 8, 2023
1 parent 1bb61f7 commit 768eb53
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/controllers/external_users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def tag_statistics
authorize!

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|
statistics.append({key: tag.name.to_s, value: (100.0 / tags[:max_topic_knowledge][tag] * value).round,
id: tag.id})
Expand Down
14 changes: 5 additions & 9 deletions app/models/proxy_exercise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,16 @@ def get_user_knowledge_and_max_knowledge(user, exercises)
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_max = all_used_tags_with_count.keys.index_with {|_t| 0 }
exercises_sorted = exercises.sort_by {|ex| ex.time_maximum_score(user) }
exercises_sorted.each do |ex|
exercises.each do |ex|
Rails.logger.debug { "exercise: #{ex.id}: #{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
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|
sum + et.factor
end
tag_ratio = ex_t.factor.to_f / ex.exercise_tags.inject(0) {|sum, et| sum + et.factor }
Rails.logger.debug do
"tag: #{t}, factor: #{ex.exercise_tags.find_by(tag: t).factor}, sumall: #{ex.exercise_tags.inject(0) do |sum, et|
sum + et.factor
end }"
"tag: #{t}, factor: #{ex_t.factor}, sumall: #{ex.exercise_tags.inject(0) {|sum, et| sum + et.factor }}"
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_ratio #{tag_ratio}" }
Expand Down

0 comments on commit 768eb53

Please sign in to comment.