Class: SearchLog
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SearchLog
- Defined in:
- app/models/search_log.rb
Instance Attribute Summary collapse
-
#ctr ⇒ Object
readonly
Returns the value of attribute ctr.
Class Method Summary collapse
- .clean_up ⇒ Object
-
.clear_debounce_cache! ⇒ Object
for testing.
- .log(term:, search_type:, ip_address:, user_id: nil) ⇒ Object
- .redis_key(ip_address:, user_id: nil) ⇒ Object
- .search_result_types ⇒ Object
- .search_types ⇒ Object
- .term_details(term, period = :weekly, search_type = :all) ⇒ Object
- .trending(period = :all, search_type = :all) ⇒ Object
- .trending_from(start_date, options = {}) ⇒ Object
Instance Attribute Details
#ctr ⇒ Object (readonly)
Returns the value of attribute ctr.
8 9 10 |
# File 'app/models/search_log.rb', line 8 def ctr @ctr end |
Class Method Details
.clean_up ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'app/models/search_log.rb', line 126 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
33 34 35 |
# File 'app/models/search_log.rb', line 33 def self.clear_debounce_cache! Discourse.redis.keys("__SEARCH__LOG_*").each { |k| Discourse.redis.del(k) } end |
.log(term:, search_type:, ip_address:, user_id: nil) ⇒ Object
37 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 |
# File 'app/models/search_log.rb', line 37 def self.log(term:, search_type:, ip_address:, user_id: nil) return [:error] if term.blank? search_type = search_types[search_type] return [:error] unless search_type.present? && ip_address.present? ip_address = nil if user_id key = redis_key(user_id: user_id, ip_address: ip_address) 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_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
24 25 26 27 28 29 30 |
# File 'app/models/search_log.rb', line 24 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_types ⇒ Object
20 21 22 |
# File 'app/models/search_log.rb', line 20 def self.search_result_types @search_result_types ||= Enum.new(topic: 1, user: 2, category: 3, tag: 4) end |
.search_types ⇒ Object
16 17 18 |
# File 'app/models/search_log.rb', line 16 def self.search_types @search_types ||= Enum.new(header: 1, full_page: 2) end |
.term_details(term, period = :weekly, search_type = :all) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/models/search_log.rb', line 70 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 |
.trending(period = :all, search_type = :all) ⇒ Object
99 100 101 |
# File 'app/models/search_log.rb', line 99 def self.trending(period = :all, search_type = :all) SearchLog.trending_from(start_of(period), search_type: search_type) end |
.trending_from(start_date, options = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'app/models/search_log.rb', line 103 def self.trending_from(start_date, = {}) end_date = [:end_date] search_type = [:search_type] || :all limit = [: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 |