diff --git a/app/assets/javascripts/admin/templates/search-logs-index.hbs b/app/assets/javascripts/admin/templates/search-logs-index.hbs
index a6bba95af51..f323f802386 100644
--- a/app/assets/javascripts/admin/templates/search-logs-index.hbs
+++ b/app/assets/javascripts/admin/templates/search-logs-index.hbs
@@ -10,8 +10,7 @@
{{i18n 'admin.logs.search_logs.term'}} |
{{i18n 'admin.logs.search_logs.searches'}} |
- {{i18n 'admin.logs.search_logs.click_through'}} |
- {{i18n 'admin.logs.search_logs.unique'}} |
+ {{i18n 'admin.logs.search_logs.click_through_rate'}} |
{{#each model as |item|}}
@@ -20,8 +19,7 @@
{{#link-to 'adminSearchLogs.term' item.term}}{{item.term}}{{/link-to}}
{{i18n 'admin.logs.search_logs.searches'}} {{item.searches}} |
- {{i18n 'admin.logs.search_logs.click_through'}} {{item.click_through}} |
- {{i18n 'admin.logs.search_logs.unique'}} {{item.unique_searches}} |
+ {{i18n 'admin.logs.search_logs.click_through_rate'}} {{item.ctr}}% |
{{/each}}
diff --git a/app/models/report.rb b/app/models/report.rb
index d4c048ccd3f..bf7da8ee8ac 100644
--- a/app/models/report.rb
+++ b/app/models/report.rb
@@ -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
diff --git a/app/models/search_log.rb b/app/models/search_log.rb
index 9fa4f92db64..738760fdb67 100644
--- a/app/models/search_log.rb
+++ b/app/models/search_log.rb
@@ -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)
diff --git a/app/serializers/search_logs_serializer.rb b/app/serializers/search_logs_serializer.rb
index 3eb6d6a2938..9191a2e434c 100644
--- a/app/serializers/search_logs_serializer.rb
+++ b/app/serializers/search_logs_serializer.rb
@@ -1,6 +1,5 @@
class SearchLogsSerializer < ApplicationSerializer
attributes :term,
:searches,
- :click_through,
- :unique_searches
+ :ctr
end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index d1cc60a4d2e..74c6ad113e3 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -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"
diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb
index aa6d86ae5f5..4cabd75a4e6 100644
--- a/spec/models/report_spec.rb
+++ b/spec/models/report_spec.rb
@@ -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
diff --git a/spec/models/search_log_spec.rb b/spec/models/search_log_spec.rb
index 0ae475e6cbe..d78a8800b81 100644
--- a/spec/models/search_log_spec.rb
+++ b/spec/models/search_log_spec.rb
@@ -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
diff --git a/spec/requests/admin/search_logs_spec.rb b/spec/requests/admin/search_logs_spec.rb
index b234ea2abff..ed393c57095 100644
--- a/spec/requests/admin/search_logs_spec.rb
+++ b/spec/requests/admin/search_logs_spec.rb
@@ -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