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
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

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
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")
it "should only use the date for the period" do
time = Time.new(2019, 5, 23, 18, 15, 30)
freeze_time(time)
SearchLog.log(
term: "ruby core",
search_type: :header,
ip_address: "127.0.0.3"
)
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)