Class: SearchLog

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/search_log.rb

Constant Summary collapse

MAXIMUM_USER_AGENT_LENGTH =
2000

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clean_upObject



141
142
143
144
145
146
147
148
149
# File 'app/models/search_log.rb', line 141

def self.clean_up
  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

.clear_debounce_cache!Object

for testing



34
35
36
# File 'app/models/search_log.rb', line 34

def self.clear_debounce_cache!
  Discourse.redis.keys("__SEARCH__LOG_*").each { |k| Discourse.redis.del(k) }
end

.log(term:, search_type:, ip_address:, user_agent: nil, user_id: nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/search_log.rb', line 38

def self.log(term:, search_type:, ip_address:, user_agent: nil, user_id: nil)
  return [:error] if term.blank?

  can_log_search =
    DiscoursePluginRegistry.apply_modifier(:search_log_can_log, term: term, user_id: user_id)
  return if !can_log_search

  search_type = search_types[search_type]
  return [:error] if search_type.blank? || ip_address.blank?

  ip_address = nil if user_id
  key = redis_key(user_id: user_id, ip_address: ip_address)

  if user_agent && user_agent.length > MAXIMUM_USER_AGENT_LENGTH
    user_agent = user_agent.truncate(MAXIMUM_USER_AGENT_LENGTH, omission: "")
  end

  result = nil

  if existing = Discourse.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

  if !result
    log =
      self.create!(
        term: term,
        search_type: search_type,
        ip_address: ip_address,
        user_agent: user_agent,
        user_id: user_id,
      )

    result = [:created, log.id]
  end

  Discourse.redis.setex(key, 5, "#{result[1]},#{term}")

  result
end

.redis_key(ip_address:, user_id: nil) ⇒ Object



25
26
27
28
29
30
31
# File 'app/models/search_log.rb', line 25

def self.redis_key(ip_address:, user_id: nil)
  if user_id
    "__SEARCH__LOG_#{user_id}"
  else
    "__SEARCH__LOG_#{ip_address}"
  end
end

.search_result_typesObject



21
22
23
# File 'app/models/search_log.rb', line 21

def self.search_result_types
  @search_result_types ||= Enum.new(topic: 1, user: 2, category: 3, tag: 4)
end

.search_typesObject



17
18
19
# File 'app/models/search_log.rb', line 17

def self.search_types
  @search_types ||= Enum.new(header: 1, full_page: 2)
end

.term_details(term, period = :weekly, search_type = :all) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/search_log.rb', line 85

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 = 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 { |record| details << { x: Date.parse(record["date"].to_s), y: record["count"] } }

  {
    type: "search_log_term",
    title: I18n.t("search_logs.graph_title"),
    start_date: start_of(period),
    end_date: Time.zone.now,
    data: details,
    period: period.to_s,
  }
end


114
115
116
# File 'app/models/search_log.rb', line 114

def self.trending(period = :all, search_type = :all)
  SearchLog.trending_from(start_of(period), search_type: search_type)
end


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'app/models/search_log.rb', line 118

def self.trending_from(start_date, options = {})
  end_date = options[:end_date]
  search_type = options[:search_type] || :all
  limit = options[:limit] || 100

  select_sql = <<~SQL
    lower(term) term,
    COUNT(*) AS searches,
    SUM(CASE
             WHEN search_result_id IS NOT NULL THEN 1
             ELSE 0
         END) AS click_through
  SQL

  result = SearchLog.select(select_sql).where("created_at > ?", start_date)

  result = result.where("created_at < ?", end_date) if end_date

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

Instance Method Details

#ctrObject



11
12
13
14
15
# File 'app/models/search_log.rb', line 11

def ctr
  return 0 if click_through == 0 || searches == 0

  ((click_through.to_f / searches.to_f) * 100).ceil(1)
end