Skip to content
Snippets Groups Projects
Commit 6bd16661 authored by Taico Aerts's avatar Taico Aerts
Browse files

Fix the number of searches graph

parent 830a2ee0
Branches
No related tags found
1 merge request!81Fix the number of searches graph
......@@ -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
......
......
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
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment