REFACTOR: Calculate CTR in SearchLog model and hide unique column (#6791)

This commit is contained in:
Vinoth Kannan 2018-12-18 19:13:46 +05:30 committed by GitHub
parent 577af81e76
commit 341a6bd78a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 26 deletions

View File

@ -10,8 +10,7 @@
<thead> <thead>
<th class="col heading term">{{i18n 'admin.logs.search_logs.term'}}</th> <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.searches'}}</th>
<th class="col heading">{{i18n 'admin.logs.search_logs.click_through'}}</th> <th class="col heading">{{i18n 'admin.logs.search_logs.click_through_rate'}}</th>
<th class="col heading" title="{{i18n 'admin.logs.search_logs.unique_title'}}">{{i18n 'admin.logs.search_logs.unique'}}</th>
</thead> </thead>
<tbody> <tbody>
{{#each model as |item|}} {{#each model as |item|}}
@ -20,8 +19,7 @@
{{#link-to 'adminSearchLogs.term' item.term}}{{item.term}}{{/link-to}} {{#link-to 'adminSearchLogs.term' item.term}}{{item.term}}{{/link-to}}
</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_rate'}}</div>{{item.ctr}}%</td>
<td class="col"><div class="label">{{i18n 'admin.logs.search_logs.unique'}}</div>{{item.unique_searches}}</td>
</tr> </tr>
{{/each}} {{/each}}
</tbody> </tbody>

View File

@ -723,7 +723,7 @@ class Report
title: I18n.t("reports.trending_search.labels.term") title: I18n.t("reports.trending_search.labels.term")
}, },
{ {
property: :unique_searches, property: :searches,
type: :number, type: :number,
title: I18n.t("reports.trending_search.labels.searches") title: I18n.t("reports.trending_search.labels.searches")
}, },
@ -744,17 +744,10 @@ class Report
) )
trends.each do |trend| 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 << { report.data << {
term: trend.term, term: trend.term,
unique_searches: trend.unique_searches, searches: trend.searches,
ctr: (ctr * 100).ceil(1) ctr: trend.ctr
} }
end end
end end

View File

@ -3,6 +3,14 @@ require_dependency 'enum'
class SearchLog < ActiveRecord::Base class SearchLog < ActiveRecord::Base
validates_presence_of :term 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 def self.search_types
@search_types ||= Enum.new( @search_types ||= Enum.new(
header: 1, header: 1,
@ -132,9 +140,9 @@ class SearchLog < ActiveRecord::Base
result = result.where('search_type = ?', search_types[search_type]) result = result.where('search_type = ?', search_types[search_type])
end end
result = result.group('lower(term)') result.group('lower(term)')
.order('unique_searches DESC, click_through ASC, term ASC') .order('searches DESC, click_through DESC, unique_searches DESC, term ASC')
.limit(limit).to_a .limit(limit)
end end
def self.start_of(period) def self.start_of(period)

View File

@ -1,6 +1,5 @@
class SearchLogsSerializer < ApplicationSerializer class SearchLogsSerializer < ApplicationSerializer
attributes :term, attributes :term,
:searches, :searches,
:click_through, :ctr
:unique_searches
end end

View File

@ -3628,9 +3628,7 @@ en:
title: "Search Logs" title: "Search Logs"
term: "Term" term: "Term"
searches: "Searches" searches: "Searches"
click_through: "Click Through" click_through_rate: "CTR"
unique: "Unique"
unique_title: "unique users performing the search"
types: types:
all_search_types: "All search types" all_search_types: "All search types"
header: "Header" header: "Header"

View File

@ -396,11 +396,11 @@ describe Report do
it "returns a report with data" do it "returns a report with data" do
expect(report.data[0][:term]).to eq("ruby") 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[0][:ctr]).to eq(33.4)
expect(report.data[1][:term]).to eq("php") 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 end
end end

View File

@ -194,10 +194,10 @@ RSpec.describe SearchLog, type: :model do
end end
it "considers time period" do 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) 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 end
it "correctly returns trending data" do it "correctly returns trending data" do

View File

@ -32,6 +32,8 @@ RSpec.describe Admin::SearchLogsController do
json = ::JSON.parse(response.body) json = ::JSON.parse(response.body)
expect(json[0]['term']).to eq('ruby') expect(json[0]['term']).to eq('ruby')
expect(json[0]['searches']).to eq(1)
expect(json[0]['ctr']).to eq(0)
end end
end end