mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 04:03:57 -06:00
REFACTOR: Calculate CTR in SearchLog model and hide unique column (#6791)
This commit is contained in:
parent
577af81e76
commit
341a6bd78a
@ -10,8 +10,7 @@
|
||||
<thead>
|
||||
<th class="col heading term">{{i18n 'admin.logs.search_logs.term'}}</th>
|
||||
<th class="col heading">{{i18n 'admin.logs.search_logs.searches'}}</th>
|
||||
<th class="col heading">{{i18n 'admin.logs.search_logs.click_through'}}</th>
|
||||
<th class="col heading" title="{{i18n 'admin.logs.search_logs.unique_title'}}">{{i18n 'admin.logs.search_logs.unique'}}</th>
|
||||
<th class="col heading">{{i18n 'admin.logs.search_logs.click_through_rate'}}</th>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{#each model as |item|}}
|
||||
@ -20,8 +19,7 @@
|
||||
{{#link-to 'adminSearchLogs.term' item.term}}{{item.term}}{{/link-to}}
|
||||
</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_searches}}</td>
|
||||
<td class="col"><div class="label">{{i18n 'admin.logs.search_logs.click_through_rate'}}</div>{{item.ctr}}%</td>
|
||||
</tr>
|
||||
{{/each}}
|
||||
</tbody>
|
||||
|
@ -723,7 +723,7 @@ class Report
|
||||
title: I18n.t("reports.trending_search.labels.term")
|
||||
},
|
||||
{
|
||||
property: :unique_searches,
|
||||
property: :searches,
|
||||
type: :number,
|
||||
title: I18n.t("reports.trending_search.labels.searches")
|
||||
},
|
||||
@ -744,17 +744,10 @@ class Report
|
||||
)
|
||||
|
||||
trends.each do |trend|
|
||||
ctr =
|
||||
if trend.click_through == 0 || trend.searches == 0
|
||||
0
|
||||
else
|
||||
trend.click_through.to_f / trend.searches.to_f
|
||||
end
|
||||
|
||||
report.data << {
|
||||
term: trend.term,
|
||||
unique_searches: trend.unique_searches,
|
||||
ctr: (ctr * 100).ceil(1)
|
||||
searches: trend.searches,
|
||||
ctr: trend.ctr
|
||||
}
|
||||
end
|
||||
end
|
||||
|
@ -3,6 +3,14 @@ require_dependency 'enum'
|
||||
class SearchLog < ActiveRecord::Base
|
||||
validates_presence_of :term
|
||||
|
||||
attr_reader :ctr
|
||||
|
||||
def ctr
|
||||
return 0 if click_through == 0 || searches == 0
|
||||
|
||||
((click_through.to_f / searches.to_f) * 100).ceil(1)
|
||||
end
|
||||
|
||||
def self.search_types
|
||||
@search_types ||= Enum.new(
|
||||
header: 1,
|
||||
@ -132,9 +140,9 @@ class SearchLog < ActiveRecord::Base
|
||||
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
|
||||
result.group('lower(term)')
|
||||
.order('searches DESC, click_through DESC, unique_searches DESC, term ASC')
|
||||
.limit(limit)
|
||||
end
|
||||
|
||||
def self.start_of(period)
|
||||
|
@ -1,6 +1,5 @@
|
||||
class SearchLogsSerializer < ApplicationSerializer
|
||||
attributes :term,
|
||||
:searches,
|
||||
:click_through,
|
||||
:unique_searches
|
||||
:ctr
|
||||
end
|
||||
|
@ -3628,9 +3628,7 @@ en:
|
||||
title: "Search Logs"
|
||||
term: "Term"
|
||||
searches: "Searches"
|
||||
click_through: "Click Through"
|
||||
unique: "Unique"
|
||||
unique_title: "unique users performing the search"
|
||||
click_through_rate: "CTR"
|
||||
types:
|
||||
all_search_types: "All search types"
|
||||
header: "Header"
|
||||
|
@ -396,11 +396,11 @@ describe Report do
|
||||
|
||||
it "returns a report with data" do
|
||||
expect(report.data[0][:term]).to eq("ruby")
|
||||
expect(report.data[0][:unique_searches]).to eq(2)
|
||||
expect(report.data[0][:searches]).to eq(3)
|
||||
expect(report.data[0][:ctr]).to eq(33.4)
|
||||
|
||||
expect(report.data[1][:term]).to eq("php")
|
||||
expect(report.data[1][:unique_searches]).to eq(1)
|
||||
expect(report.data[1][:searches]).to eq(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -194,10 +194,10 @@ RSpec.describe SearchLog, type: :model do
|
||||
end
|
||||
|
||||
it "considers time period" do
|
||||
expect(SearchLog.trending.count).to eq(4)
|
||||
expect(SearchLog.trending.to_a.count).to eq(4)
|
||||
|
||||
SearchLog.where(term: 'swift').update_all(created_at: 1.year.ago)
|
||||
expect(SearchLog.trending(:monthly).count).to eq(3)
|
||||
expect(SearchLog.trending(:monthly).to_a.count).to eq(3)
|
||||
end
|
||||
|
||||
it "correctly returns trending data" do
|
||||
|
@ -32,6 +32,8 @@ RSpec.describe Admin::SearchLogsController do
|
||||
|
||||
json = ::JSON.parse(response.body)
|
||||
expect(json[0]['term']).to eq('ruby')
|
||||
expect(json[0]['searches']).to eq(1)
|
||||
expect(json[0]['ctr']).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user