A platform for community discussion. Free, open, simple.
refactors trending search report to use SearchLog
Joffrey JAFFEUXe51c67613578
diff --git a/app/assets/javascripts/admin/templates/search-logs-index.hbs b/app/assets/javascripts/admin/templates/search-logs-index.hbsindex 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 @@</td><td class="col"><div class="label">{{i18n 'admin.logs.search_logs.searches'}}</div>{{item.searches}}</td><td class="col"><div class="label">{{i18n 'admin.logs.search_logs.click_through'}}</div>{{item.click_through}}</td>- <td class="col"><div class="label">{{i18n 'admin.logs.search_logs.unique'}}</div>{{item.unique}}</td>+ <td class="col"><div class="label">{{i18n 'admin.logs.search_logs.unique_searches'}}</div>{{item.unique_searches}}</td></tr>{{/each}}</tbody>diff --git a/app/models/report.rb b/app/models/report.rbindex 8ca5bf8fd8e..e1d8a65e4e0 100644--- a/app/models/report.rb+++ b/app/models/report.rb@@ -701,21 +701,10 @@ class Reportreport.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.rbindex c29a05e1c67..9fa4f92db64 100644--- a/app/models/search_log.rb+++ b/app/models/search_log.rb@@ -103,19 +103,38 @@ class SearchLog < ActiveRecord::Baseenddef 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(CASEWHEN search_result_id IS NOT NULL THEN 1ELSE 0END) AS click_through,- COUNT(DISTINCT ip_address) AS unique")- .where('created_at > ?', start_of(period))+ COUNT(DISTINCT ip_address) AS unique_searches+ SQL++ 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.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 = result.group('lower(term)')+ .order('unique_searches DESC, click_through ASC, term ASC')+ .limit(limit).to_aenddef self.start_of(period)diff --git a/app/serializers/search_logs_serializer.rb b/app/serializers/search_logs_serializer.rbindex f059c33cb61..3eb6d6a2938 100644--- a/app/serializers/search_logs_serializer.rb+++ b/app/serializers/search_logs_serializer.rb@@ -2,5 +2,5 @@ class SearchLogsSerializer < ApplicationSerializerattributes :term,:searches,:click_through,- :unique+ :unique_searchesend