diff --git a/app/assets/javascripts/admin/templates/search-logs-index.hbs b/app/assets/javascripts/admin/templates/search-logs-index.hbs index 450086964d1..507983747ca 100644 --- a/app/assets/javascripts/admin/templates/search-logs-index.hbs +++ b/app/assets/javascripts/admin/templates/search-logs-index.hbs @@ -21,7 +21,7 @@
{{i18n 'admin.logs.search_logs.searches'}}
{{item.searches}}
{{i18n 'admin.logs.search_logs.click_through'}}
{{item.click_through}} -
{{i18n 'admin.logs.search_logs.unique'}}
{{item.unique}} +
{{i18n 'admin.logs.search_logs.unique_searches'}}
{{item.unique_searches}} {{/each}} diff --git a/app/models/report.rb b/app/models/report.rb index 8ca5bf8fd8e..e1d8a65e4e0 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -701,21 +701,10 @@ class Report report.modes = [:table] - select_sql = <<~SQL - lower(term) term, - COUNT(*) AS searches, - SUM(CASE - WHEN search_result_id IS NOT NULL THEN 1 - ELSE 0 - END) AS click_through, - COUNT(DISTINCT ip_address) AS unique_searches - SQL - - trends = SearchLog.select(select_sql) - .where('created_at > ? AND created_at <= ?', report.start_date, report.end_date) - .group('lower(term)') - .order('unique_searches DESC, click_through ASC, term ASC') - .limit(report.limit || 20).to_a + trends = SearchLog.trending_from(report.start_date, + end_date: report.end_date, + limit: report.limit + ) trends.each do |trend| ctr = diff --git a/app/models/search_log.rb b/app/models/search_log.rb index c29a05e1c67..9fa4f92db64 100644 --- a/app/models/search_log.rb +++ b/app/models/search_log.rb @@ -103,19 +103,38 @@ class SearchLog < ActiveRecord::Base end def self.trending(period = :all, search_type = :all) - result = SearchLog.select("term, - COUNT(*) AS searches, - SUM(CASE + SearchLog.trending_from(start_of(period), search_type: search_type) + end + + def self.trending_from(start_date, options = {}) + end_date = options[:end_date] + search_type = options[:search_type] || :all + limit = options[:limit] || 100 + + select_sql = <<~SQL + lower(term) term, + COUNT(*) AS searches, + SUM(CASE WHEN search_result_id IS NOT NULL THEN 1 ELSE 0 END) AS click_through, - COUNT(DISTINCT ip_address) AS unique") - .where('created_at > ?', start_of(period)) + COUNT(DISTINCT ip_address) AS unique_searches + SQL - result = result.where('search_type = ?', search_types[search_type]) unless search_type == :all - result = result.group(:term) - .order('COUNT(DISTINCT ip_address) DESC, COUNT(*) DESC') - .limit(100).to_a + result = SearchLog.select(select_sql) + .where('created_at > ?', start_date) + + if end_date + result = result.where('created_at < ?', end_date) + end + + unless search_type == :all + result = result.where('search_type = ?', search_types[search_type]) + end + + result = result.group('lower(term)') + .order('unique_searches DESC, click_through ASC, term ASC') + .limit(limit).to_a end def self.start_of(period) diff --git a/app/serializers/search_logs_serializer.rb b/app/serializers/search_logs_serializer.rb index f059c33cb61..3eb6d6a2938 100644 --- a/app/serializers/search_logs_serializer.rb +++ b/app/serializers/search_logs_serializer.rb @@ -2,5 +2,5 @@ class SearchLogsSerializer < ApplicationSerializer attributes :term, :searches, :click_through, - :unique + :unique_searches end