From 6bd16661fa0f2fa84ed91bd0cc8a1bef85812397 Mon Sep 17 00:00:00 2001 From: Taico Aerts <t.v.aerts@tudelft.nl> Date: Fri, 15 Dec 2023 12:29:27 +0100 Subject: [PATCH] Fix the number of searches graph --- .../analytics_dashboard_controller.rb | 1 + app/helpers/analytics_dashboard_helper.rb | 33 +++++++++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/app/controllers/analytics_dashboard_controller.rb b/app/controllers/analytics_dashboard_controller.rb index 5db4f2eea..cc7546f51 100644 --- a/app/controllers/analytics_dashboard_controller.rb +++ b/app/controllers/analytics_dashboard_controller.rb @@ -58,6 +58,7 @@ class AnalyticsDashboardController < ApplicationController def searches_per_tag_chart options = { + tags: params[:tags].presence, start_date: params[:start_date].presence, end_date: params[:end_date].presence, top_n: params[:top_n].presence&.to_i diff --git a/app/helpers/analytics_dashboard_helper.rb b/app/helpers/analytics_dashboard_helper.rb index a2f1ef490..2b27fc358 100644 --- a/app/helpers/analytics_dashboard_helper.rb +++ b/app/helpers/analytics_dashboard_helper.rb @@ -1,8 +1,9 @@ module AnalyticsDashboardHelper # rubocop:disable Metrics/ParameterLists - def number_of_searches_per_tag(start_date: nil, end_date: nil, top_n: 10) + def number_of_searches_per_tag(tags: nil, start_date: nil, end_date: nil, top_n: 10) top_n ||= 10 - hash = {} + searched_tag_counts = {} + searched_tag_counts_by_id = {} base = ActionLog.where(controller_name: 'search', controller_action_name: 'search') base = base.where(created_at: start_date..end_date) if start_date.present? || end_date.present? base.find_each do |result| @@ -19,25 +20,31 @@ module AnalyticsDashboardHelper value = splat[1] # Only take the 'tag' qualifiers from the string and add them to result if parameter == 'tag' - hash[value] = hash.fetch(value, 0) + 1 + searched_tag_counts[value] = searched_tag_counts.fetch(value, 0) + 1 end end end # Extract the parameters from 'include_tags' parameters - if params_hash['include_tags'].present? - valid_value = { - integer: /^\d+$/ - } - # Filter only the valid ids - check that they are integers - tags_ids = params[:include_tags]&.all? { |id| id.match? valid_value[:integer] } - Tag.where(tags_ids).select(:name).find_each do |tag| - hash[tag.name] = hash.fetch(tag.name, 0) + 1 + included_tags = params_hash['included_tags']&.grep(/\d+/, &:to_i) + if included_tags.present? + included_tags.each do |tag_id| + searched_tag_counts_by_id[tag_id] = searched_tag_counts_by_id.fetch(tag_id, 0) + 1 end end end - puts hash + + # Convert the tags by id to the other format + Tag.where(id: searched_tag_counts_by_id.keys).select(:id, :name).find_each do |tag| + searched_tag_counts[tag.name] = searched_tag_counts.fetch(tag.name, 0) + searched_tag_counts_by_id[tag.id] + end + + # Filter to only the selected tags + if tags.present? + searched_tag_counts.slice!(Tag.where(id: tags).pluck(:name)) + end + # Take top 10 keys with largest values - hash.sort_by { |_, v| -v }.first(top_n) + searched_tag_counts.sort_by { |_, v| -v }.first(top_n) end def currently_searched -- GitLab