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.
This commit is contained in:
Guo Xiang Tan 2019-03-29 11:50:25 +08:00
parent d8faf5f79e
commit f458cba4cb
3 changed files with 41 additions and 19 deletions

View File

@ -3,6 +3,8 @@ require_dependency 'enum'
class SearchLog < ActiveRecord::Base class SearchLog < ActiveRecord::Base
validates_presence_of :term validates_presence_of :term
belongs_to :user
attr_reader :ctr attr_reader :ctr
def ctr def ctr
@ -146,16 +148,6 @@ class SearchLog < ActiveRecord::Base
.limit(limit) .limit(limit)
end 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 def self.clean_up
search_id = SearchLog.order(:id).offset(SiteSetting.search_query_log_max_size).limit(1).pluck(:id) 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 end
SearchLog.where('created_at < TIMESTAMP ?', SiteSetting.search_query_log_max_retention_days.days.ago).delete_all SearchLog.where('created_at < TIMESTAMP ?', SiteSetting.search_query_log_max_retention_days.days.ago).delete_all
end 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 end
# == Schema Information # == Schema Information

View File

@ -0,0 +1,5 @@
Fabricator(:search_log) do
term "ruby"
search_type SearchLog.search_types[:header]
ip_address "127.0.0.1"
end

View File

@ -162,19 +162,29 @@ RSpec.describe SearchLog, type: :model do
end end
describe ".term_details" do describe ".term_details" do
before do it "should only use the date for the period" do
SearchLog.log(term: "ruby", search_type: :header, ip_address: "127.0.0.1") time = Time.new(2019, 5, 23, 18, 15, 30)
SearchLog.log(term: 'rUby', search_type: :header, ip_address: '127.0.0.1', user_id: Fabricate(:user).id) freeze_time(time)
SearchLog.log(term: "ruBy", search_type: :full_page, ip_address: "127.0.0.2")
SearchLog.log( search_log = Fabricate(:search_log, created_at: time - 1.hour)
term: "ruby core", search_log2 = Fabricate(:search_log, created_at: time + 1.hour)
search_type: :header,
ip_address: "127.0.0.3" details = SearchLog.term_details(search_log.term, :daily)
)
expect(details[:data].first[:y]).to eq(2)
end end
it "correctly returns term details" do 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") term_details = SearchLog.term_details("ruby")
expect(term_details[:data][0][:y]).to eq(3) expect(term_details[:data][0][:y]).to eq(3)