FIX: Admin search logs should filter by date instead of timestamp.

FIX: Admin search logs should filter by date instead of timestamp.

The client side filters by date so it is confusion when the data changes as each second passes.

diff --git a/app/models/search_log.rb b/app/models/search_log.rb
index 152d6b5..0f67fcd 100644
--- a/app/models/search_log.rb
+++ b/app/models/search_log.rb
@@ -3,6 +3,8 @@ require_dependency 'enum'
 class SearchLog < ActiveRecord::Base
   validates_presence_of :term
 
+  belongs_to :user
+
   attr_reader :ctr
 
   def ctr
@@ -146,16 +148,6 @@ class SearchLog < ActiveRecord::Base
       .limit(limit)
   end
 
-  def self.start_of(period)
-    case period
-    when :yearly    then 1.year.ago
-    when :monthly   then 1.month.ago
-    when :quarterly then 3.months.ago
-    when :weekly    then 1.week.ago
-    when :daily     then 1.day.ago
-    else 1000.years.ago
-    end
-  end
 
   def self.clean_up
     search_id = SearchLog.order(:id).offset(SiteSetting.search_query_log_max_size).limit(1).pluck(:id)
@@ -164,6 +156,21 @@ class SearchLog < ActiveRecord::Base
     end
     SearchLog.where('created_at < TIMESTAMP ?', SiteSetting.search_query_log_max_retention_days.days.ago).delete_all
   end
+
+  def self.start_of(period)
+    period =
+      case period
+      when :yearly    then 1.year.ago
+      when :monthly   then 1.month.ago
+      when :quarterly then 3.months.ago
+      when :weekly    then 1.week.ago
+      when :daily     then Time.zone.now
+      else 1000.years.ago
+      end
+
+    period&.to_date
+  end
+  private_class_method :start_of
 end
 
 # == Schema Information
diff --git a/spec/fabricators/search_log_fabricator.rb b/spec/fabricators/search_log_fabricator.rb
new file mode 100644
index 0000000..19d934c
--- /dev/null
+++ b/spec/fabricators/search_log_fabricator.rb
@@ -0,0 +1,5 @@
+Fabricator(:search_log) do
+  term "ruby"
+  search_type SearchLog.search_types[:header]
+  ip_address "127.0.0.1"
+end
diff --git a/spec/models/search_log_spec.rb b/spec/models/search_log_spec.rb
index 952b890..995252e 100644
--- a/spec/models/search_log_spec.rb
+++ b/spec/models/search_log_spec.rb
@@ -162,19 +162,29 @@ RSpec.describe SearchLog, type: :model do
   end
 
   describe ".term_details" do
-    before do
-      SearchLog.log(term: "ruby", search_type: :header, ip_address: "127.0.0.1")
-      SearchLog.log(term: 'rUby', search_type: :header, ip_address: '127.0.0.1', user_id: Fabricate(:user).id)
-      SearchLog.log(term: "ruBy", search_type: :full_page, ip_address: "127.0.0.2")
-
-      SearchLog.log(
-        term: "ruby core",
-        search_type: :header,
-        ip_address: "127.0.0.3"
-      )
+    it "should only use the date for the period" do
+      time = Time.new(2019, 5, 23, 18, 15, 30)
+      freeze_time(time)
+
+      search_log = Fabricate(:search_log, created_at: time - 1.hour)
+      search_log2 = Fabricate(:search_log, created_at: time + 1.hour)
+
+      details = SearchLog.term_details(search_log.term, :daily)
+
+      expect(details[:data].first[:y]).to eq(2)
     end
 
     it "correctly returns term details" do
+      Fabricate(:search_log, term: "ruby")
+      Fabricate(:search_log, term: "ruBy", user: Fabricate(:user))
+      Fabricate(:search_log, term: "ruby core", ip_address: "127.0.0.3")
+
+      Fabricate(:search_log,
+        term: "ruBy",
+        search_type: SearchLog.search_types[:full_page],
+        ip_address: "127.0.0.2"
+      )
+
       term_details = SearchLog.term_details("ruby")
       expect(term_details[:data][0][:y]).to eq(3)

GitHub sha: f458cba4