refactors trending search report to use SearchLog

This commit is contained in:
Joffrey JAFFEUX 2018-09-07 16:54:38 +02:00 committed by Sam
parent 173d0d53d5
commit e51c676135
4 changed files with 34 additions and 26 deletions

View File

@ -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>

View File

@ -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 =

View File

@ -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)

View File

@ -2,5 +2,5 @@ class SearchLogsSerializer < ApplicationSerializer
attributes :term,
:searches,
:click_through,
:unique
:unique_searches
end