2017-07-14 01:34:31 +08:00
|
|
|
require_dependency 'enum'
|
|
|
|
|
|
|
|
class SearchLog < ActiveRecord::Base
|
|
|
|
validates_presence_of :term, :ip_address
|
|
|
|
|
|
|
|
def self.search_types
|
|
|
|
@search_types ||= Enum.new(
|
|
|
|
header: 1,
|
|
|
|
full_page: 2
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def self.log(term:, search_type:, ip_address:, user_id: nil)
|
2017-07-14 01:34:31 +08:00
|
|
|
|
2017-07-17 23:57:13 +08:00
|
|
|
search_type = search_types[search_type]
|
|
|
|
return [:error] unless search_type.present? && ip_address.present?
|
|
|
|
|
2017-07-14 01:34:31 +08:00
|
|
|
update_sql = <<~SQL
|
|
|
|
UPDATE search_logs
|
|
|
|
SET term = :term,
|
|
|
|
created_at = :created_at
|
|
|
|
WHERE created_at > :timeframe AND
|
|
|
|
position(term IN :term) = 1 AND
|
|
|
|
((:user_id IS NULL AND ip_address = :ip_address) OR
|
|
|
|
(user_id = :user_id))
|
|
|
|
RETURNING id
|
|
|
|
SQL
|
|
|
|
|
|
|
|
rows = exec_sql(
|
|
|
|
update_sql,
|
|
|
|
term: term,
|
|
|
|
created_at: Time.zone.now,
|
|
|
|
timeframe: 5.seconds.ago,
|
|
|
|
user_id: user_id,
|
|
|
|
ip_address: ip_address
|
|
|
|
)
|
|
|
|
|
|
|
|
if rows.cmd_tuples == 0
|
|
|
|
result = create(
|
|
|
|
term: term,
|
2017-07-17 23:57:13 +08:00
|
|
|
search_type: search_type,
|
2017-07-14 01:34:31 +08:00
|
|
|
ip_address: ip_address,
|
|
|
|
user_id: user_id
|
|
|
|
)
|
|
|
|
[:created, result.id]
|
|
|
|
else
|
|
|
|
[:updated, rows[0]['id'].to_i]
|
|
|
|
end
|
|
|
|
end
|
2017-07-15 02:29:31 +08:00
|
|
|
|
|
|
|
def self.clean_up
|
|
|
|
search_id = SearchLog.order(:id).offset(SiteSetting.search_query_log_max_size).limit(1).pluck(:id)
|
|
|
|
if search_id.present?
|
|
|
|
SearchLog.where('id < ?', search_id[0]).delete_all
|
|
|
|
end
|
|
|
|
end
|
2017-07-14 01:34:31 +08:00
|
|
|
end
|
2017-04-27 02:47:36 +08:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: search_logs
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# term :string not null
|
|
|
|
# user_id :integer
|
|
|
|
# ip_address :inet not null
|
|
|
|
# clicked_topic_id :integer
|
|
|
|
# search_type :integer not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
#
|