mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 13:06:56 +08:00
refactors trending search report to use SearchLog
This commit is contained in:
@ -21,7 +21,7 @@
|
|||||||
</td>
|
</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.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.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>
|
</tr>
|
||||||
{{/each}}
|
{{/each}}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
@ -701,21 +701,10 @@ class Report
|
|||||||
|
|
||||||
report.modes = [:table]
|
report.modes = [:table]
|
||||||
|
|
||||||
select_sql = <<~SQL
|
trends = SearchLog.trending_from(report.start_date,
|
||||||
lower(term) term,
|
end_date: report.end_date,
|
||||||
COUNT(*) AS searches,
|
limit: report.limit
|
||||||
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.each do |trend|
|
trends.each do |trend|
|
||||||
ctr =
|
ctr =
|
||||||
|
@ -103,19 +103,38 @@ class SearchLog < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.trending(period = :all, search_type = :all)
|
def self.trending(period = :all, search_type = :all)
|
||||||
result = SearchLog.select("term,
|
SearchLog.trending_from(start_of(period), search_type: search_type)
|
||||||
COUNT(*) AS searches,
|
end
|
||||||
SUM(CASE
|
|
||||||
|
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
|
WHEN search_result_id IS NOT NULL THEN 1
|
||||||
ELSE 0
|
ELSE 0
|
||||||
END) AS click_through,
|
END) AS click_through,
|
||||||
COUNT(DISTINCT ip_address) AS unique")
|
COUNT(DISTINCT ip_address) AS unique_searches
|
||||||
.where('created_at > ?', start_of(period))
|
SQL
|
||||||
|
|
||||||
result = result.where('search_type = ?', search_types[search_type]) unless search_type == :all
|
result = SearchLog.select(select_sql)
|
||||||
result = result.group(:term)
|
.where('created_at > ?', start_date)
|
||||||
.order('COUNT(DISTINCT ip_address) DESC, COUNT(*) DESC')
|
|
||||||
.limit(100).to_a
|
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
|
end
|
||||||
|
|
||||||
def self.start_of(period)
|
def self.start_of(period)
|
||||||
|
@ -2,5 +2,5 @@ class SearchLogsSerializer < ApplicationSerializer
|
|||||||
attributes :term,
|
attributes :term,
|
||||||
:searches,
|
:searches,
|
||||||
:click_through,
|
:click_through,
|
||||||
:unique
|
:unique_searches
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user