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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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