2017-07-13 13:34:31 -04: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-11-28 12:54:27 -05:00
|
|
|
def self.search_result_types
|
|
|
|
@search_result_types ||= Enum.new(
|
|
|
|
topic: 1,
|
|
|
|
user: 2,
|
|
|
|
category: 3,
|
|
|
|
tag: 4
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-01-14 22:48:28 -05:00
|
|
|
def self.redis_key(ip_address:, user_id: nil)
|
|
|
|
if user_id
|
|
|
|
"__SEARCH__LOG_#{user_id}"
|
|
|
|
else
|
|
|
|
"__SEARCH__LOG_#{ip_address}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# for testing
|
|
|
|
def self.clear_debounce_cache!
|
|
|
|
$redis.keys("__SEARCH__LOG_*").each do |k|
|
|
|
|
$redis.del(k)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.log(term:, search_type:, ip_address:, user_id: nil)
|
2017-07-13 13:34:31 -04:00
|
|
|
|
2018-01-16 19:02:31 -05:00
|
|
|
return [:error] if term.blank?
|
|
|
|
|
2017-07-17 11:57:13 -04:00
|
|
|
search_type = search_types[search_type]
|
|
|
|
return [:error] unless search_type.present? && ip_address.present?
|
|
|
|
|
2018-01-14 22:48:28 -05:00
|
|
|
key = redis_key(user_id: user_id, ip_address: ip_address)
|
2017-07-13 13:34:31 -04:00
|
|
|
|
2018-01-14 22:48:28 -05:00
|
|
|
result = nil
|
|
|
|
|
|
|
|
if existing = $redis.get(key)
|
|
|
|
id, old_term = existing.split(",", 2)
|
|
|
|
if term.start_with?(old_term)
|
|
|
|
where(id: id.to_i).update_all(
|
|
|
|
created_at: Time.zone.now,
|
|
|
|
term: term
|
|
|
|
)
|
|
|
|
result = [:updated, id.to_i]
|
|
|
|
end
|
|
|
|
end
|
2017-07-13 13:34:31 -04:00
|
|
|
|
2018-01-14 22:48:28 -05:00
|
|
|
if !result
|
|
|
|
log = create(
|
2017-07-13 13:34:31 -04:00
|
|
|
term: term,
|
2017-07-17 11:57:13 -04:00
|
|
|
search_type: search_type,
|
2017-07-13 13:34:31 -04:00
|
|
|
ip_address: ip_address,
|
|
|
|
user_id: user_id
|
|
|
|
)
|
2018-01-14 22:48:28 -05:00
|
|
|
result = [:created, log.id]
|
2017-07-13 13:34:31 -04:00
|
|
|
end
|
2018-01-14 22:48:28 -05:00
|
|
|
|
|
|
|
$redis.setex(key, 5, "#{result[1]},#{term}")
|
|
|
|
|
|
|
|
result
|
2017-07-13 13:34:31 -04:00
|
|
|
end
|
2017-07-14 14:29:31 -04:00
|
|
|
|
2017-12-19 21:41:31 -05:00
|
|
|
def self.term_details(term, period = :weekly, search_type = :all)
|
|
|
|
details = []
|
|
|
|
|
|
|
|
result = SearchLog.select("COUNT(*) AS count, created_at::date AS date")
|
|
|
|
.where('term LIKE ?', term)
|
|
|
|
.where('created_at > ?', start_of(period))
|
|
|
|
|
|
|
|
result = result.where('search_type = ?', search_types[search_type]) if search_type == :header || search_type == :full_page
|
|
|
|
result = result.where('search_result_id IS NOT NULL') if search_type == :click_through_only
|
|
|
|
|
|
|
|
result.group(:term)
|
|
|
|
.order("date")
|
|
|
|
.group("date")
|
|
|
|
.each do |record|
|
|
|
|
details << { x: Date.parse(record['date'].to_s), y: record['count'] }
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: "search_log_term",
|
|
|
|
title: I18n.t("search_logs.graph_title"),
|
2018-01-12 21:43:49 -05:00
|
|
|
start_date: start_of(period),
|
|
|
|
end_date: Time.zone.now,
|
|
|
|
data: details,
|
|
|
|
period: period.to_s
|
2017-12-19 21:41:31 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-11-28 12:54:27 -05:00
|
|
|
def self.trending(period = :all, search_type = :all)
|
|
|
|
result = SearchLog.select("term,
|
|
|
|
COUNT(*) AS searches,
|
|
|
|
SUM(CASE
|
|
|
|
WHEN search_result_id IS NOT NULL THEN 1
|
|
|
|
ELSE 0
|
|
|
|
END) AS click_through,
|
|
|
|
COUNT(DISTINCT ip_address) AS unique")
|
2017-11-14 19:13:50 -05:00
|
|
|
.where('created_at > ?', start_of(period))
|
2017-11-28 12:54:27 -05:00
|
|
|
|
|
|
|
result = result.where('search_type = ?', search_types[search_type]) unless search_type == :all
|
|
|
|
result = result.group(:term)
|
|
|
|
.order('COUNT(DISTINCT ip_address) DESC, COUNT(*) DESC')
|
2017-11-14 19:13:50 -05:00
|
|
|
.limit(100).to_a
|
|
|
|
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
|
|
|
|
|
2017-07-14 14:29:31 -04: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-13 13:34:31 -04:00
|
|
|
end
|
2017-04-26 14:47:36 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: search_logs
|
|
|
|
#
|
2017-12-05 10:29:14 -05:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# term :string not null
|
|
|
|
# user_id :integer
|
|
|
|
# ip_address :inet not null
|
|
|
|
# search_result_id :integer
|
|
|
|
# search_type :integer not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# search_result_type :integer
|
2017-04-26 14:47:36 -04:00
|
|
|
#
|