mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Apply syntax_tree formatting to app/*
This commit is contained in:
@@ -14,19 +14,11 @@ class SearchLog < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def self.search_types
|
||||
@search_types ||= Enum.new(
|
||||
header: 1,
|
||||
full_page: 2
|
||||
)
|
||||
@search_types ||= Enum.new(header: 1, full_page: 2)
|
||||
end
|
||||
|
||||
def self.search_result_types
|
||||
@search_result_types ||= Enum.new(
|
||||
topic: 1,
|
||||
user: 2,
|
||||
category: 3,
|
||||
tag: 4
|
||||
)
|
||||
@search_result_types ||= Enum.new(topic: 1, user: 2, category: 3, tag: 4)
|
||||
end
|
||||
|
||||
def self.redis_key(ip_address:, user_id: nil)
|
||||
@@ -39,13 +31,10 @@ class SearchLog < ActiveRecord::Base
|
||||
|
||||
# for testing
|
||||
def self.clear_debounce_cache!
|
||||
Discourse.redis.keys("__SEARCH__LOG_*").each do |k|
|
||||
Discourse.redis.del(k)
|
||||
end
|
||||
Discourse.redis.keys("__SEARCH__LOG_*").each { |k| Discourse.redis.del(k) }
|
||||
end
|
||||
|
||||
def self.log(term:, search_type:, ip_address:, user_id: nil)
|
||||
|
||||
return [:error] if term.blank?
|
||||
|
||||
search_type = search_types[search_type]
|
||||
@@ -60,22 +49,15 @@ class SearchLog < ActiveRecord::Base
|
||||
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
|
||||
)
|
||||
where(id: id.to_i).update_all(created_at: Time.zone.now, term: term)
|
||||
|
||||
result = [:updated, id.to_i]
|
||||
end
|
||||
end
|
||||
|
||||
if !result
|
||||
log = self.create!(
|
||||
term: term,
|
||||
search_type: search_type,
|
||||
ip_address: ip_address,
|
||||
user_id: user_id
|
||||
)
|
||||
log =
|
||||
self.create!(term: term, search_type: search_type, ip_address: ip_address, user_id: user_id)
|
||||
|
||||
result = [:created, log.id]
|
||||
end
|
||||
@@ -88,21 +70,21 @@ class SearchLog < ActiveRecord::Base
|
||||
def self.term_details(term, period = :weekly, search_type = :all)
|
||||
details = []
|
||||
|
||||
result = SearchLog.select("COUNT(*) AS count, created_at::date AS date")
|
||||
.where(
|
||||
'lower(term) = ? AND created_at > ?',
|
||||
term.downcase, start_of(period)
|
||||
result =
|
||||
SearchLog.select("COUNT(*) AS count, created_at::date AS date").where(
|
||||
"lower(term) = ? AND created_at > ?",
|
||||
term.downcase,
|
||||
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 = 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
|
||||
.order("date")
|
||||
.group("date")
|
||||
.each do |record|
|
||||
details << { x: Date.parse(record['date'].to_s), y: record['count'] }
|
||||
end
|
||||
.each { |record| details << { x: Date.parse(record["date"].to_s), y: record["count"] } }
|
||||
|
||||
{
|
||||
type: "search_log_term",
|
||||
@@ -110,7 +92,7 @@ class SearchLog < ActiveRecord::Base
|
||||
start_date: start_of(period),
|
||||
end_date: Time.zone.now,
|
||||
data: details,
|
||||
period: period.to_s
|
||||
period: period.to_s,
|
||||
}
|
||||
end
|
||||
|
||||
@@ -132,39 +114,40 @@ class SearchLog < ActiveRecord::Base
|
||||
END) AS click_through
|
||||
SQL
|
||||
|
||||
result = SearchLog.select(select_sql)
|
||||
.where('created_at > ?', start_date)
|
||||
result = SearchLog.select(select_sql).where("created_at > ?", start_date)
|
||||
|
||||
if end_date
|
||||
result = result.where('created_at < ?', end_date)
|
||||
end
|
||||
result = result.where("created_at < ?", end_date) if end_date
|
||||
|
||||
unless search_type == :all
|
||||
result = result.where('search_type = ?', search_types[search_type])
|
||||
end
|
||||
result = result.where("search_type = ?", search_types[search_type]) unless search_type == :all
|
||||
|
||||
result.group('lower(term)')
|
||||
.order('searches DESC, click_through DESC, term ASC')
|
||||
.limit(limit)
|
||||
result.group("lower(term)").order("searches DESC, click_through DESC, term ASC").limit(limit)
|
||||
end
|
||||
|
||||
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
|
||||
SearchLog.where('created_at < TIMESTAMP ?', SiteSetting.search_query_log_max_retention_days.days.ago).delete_all
|
||||
search_id =
|
||||
SearchLog.order(:id).offset(SiteSetting.search_query_log_max_size).limit(1).pluck(:id)
|
||||
SearchLog.where("id < ?", search_id[0]).delete_all if search_id.present?
|
||||
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
|
||||
when :yearly
|
||||
1.year.ago
|
||||
when :monthly
|
||||
1.month.ago
|
||||
when :quarterly
|
||||
3.months.ago
|
||||
when :weekly
|
||||
1.week.ago
|
||||
when :daily
|
||||
Time.zone.now
|
||||
else
|
||||
1000.years.ago
|
||||
end
|
||||
|
||||
period&.to_date
|
||||
|
||||
Reference in New Issue
Block a user