A platform for community discussion. Free, open, simple.
FIX: Admin search logs should filter by date instead of timestamp.
Guo Xiang Tanf458cba4cb39
diff --git a/app/models/search_log.rb b/app/models/search_log.rbindex 152d6b5a37c..0f67fcd8484 100644--- a/app/models/search_log.rb+++ b/app/models/search_log.rb@@ -3,6 +3,8 @@ require_dependency 'enum'class SearchLog < ActiveRecord::Basevalidates_presence_of :term+ belongs_to :user+attr_reader :ctrdef 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- enddef self.clean_upsearch_id = SearchLog.order(:id).offset(SiteSetting.search_query_log_max_size).limit(1).pluck(:id)@@ -164,6 +156,21 @@ class SearchLog < ActiveRecord::BaseendSearchLog.where('created_at < TIMESTAMP ?', SiteSetting.search_query_log_max_retention_days.days.ago).delete_allend++ 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_ofend# == Schema Informationdiff --git a/spec/fabricators/search_log_fabricator.rb b/spec/fabricators/search_log_fabricator.rbnew file mode 100644index 00000000000..19d934ce589--- /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"+enddiff --git a/spec/models/search_log_spec.rb b/spec/models/search_log_spec.rbindex 952b890783b..995252ee265 100644--- a/spec/models/search_log_spec.rb+++ b/spec/models/search_log_spec.rb@@ -162,19 +162,29 @@ RSpec.describe SearchLog, type: :model doenddescribe ".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)endit "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)