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

REFACTOR: Calculate CTR in SearchLog model and hide unique column (#6791)
diff --git a/app/assets/javascripts/admin/templates/search-logs-index.hbs b/app/assets/javascripts/admin/templates/search-logs-index.hbs
index a6bba95..f323f80 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 @@
       <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>
diff --git a/app/models/report.rb b/app/models/report.rb
index d4c048c..bf7da8e 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 9fa4f92..738760f 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 3eb6d6a..9191a2e 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 d1cc60a..74c6ad1 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 aa6d86a..4cabd75 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 0ae475e..d78a880 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 b234ea2..ed393c5 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

GitHub
sha: 341a6bd7