Class: ApplicationRequest

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
CachedCounting
Defined in:
app/models/application_request.rb

Constant Summary

Constants included from CachedCounting

CachedCounting::COUNTER_REDIS_HASH, CachedCounting::DB_COOLDOWN_KEY, CachedCounting::DB_FLUSH_COOLDOWN_SECONDS, CachedCounting::ENSURE_THREAD_COOLDOWN_SECONDS, CachedCounting::FLUSH_DB_ITERATIONS, CachedCounting::LUA_HGET_DEL, CachedCounting::MUTEX, CachedCounting::QUEUE, CachedCounting::SLEEP_SECONDS

Class Method Summary collapse

Methods included from CachedCounting

allowed_to_flush_to_db?, clear_flush_to_db_lock!, clear_queue!, enabled?, ensure_thread!, flush, flush_in_memory, flush_to_db, flush_to_db_lock_ttl, queue, reset, thread_loop

Class Method Details

.disableObject



26
27
28
# File 'app/models/application_request.rb', line 26

def self.disable
  @disabled = true
end

.enableObject



30
31
32
# File 'app/models/application_request.rb', line 30

def self.enable
  @disabled = false
end

.increment!(req_type) ⇒ Object



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

def self.increment!(req_type)
  return if @disabled
  perform_increment!(req_type)
end

.request_type_count_for_period(type, since) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'app/models/application_request.rb', line 64

def self.request_type_count_for_period(type, since)
  id = self.req_types[type]
  if !id
    raise ArgumentError.new(
            "unknown request type #{type.inspect} in ApplicationRequest.req_types",
          )
  end

  self.where(req_type: id).where("date >= ?", since).sum(:count)
end

.statsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/application_request.rb', line 50

def self.stats
  s = HashWithIndifferentAccess.new({})

  self.req_types.each do |key, i|
    query = self.where(req_type: i)
    s["#{key}_total"] = query.sum(:count)
    s["#{key}_30_days"] = query.where("date > ?", 30.days.ago).sum(:count)
    s["#{key}_28_days"] = query.where("date > ?", 28.days.ago).sum(:count)
    s["#{key}_7_days"] = query.where("date > ?", 7.days.ago).sum(:count)
  end

  s
end

.write_cache!(req_type, count, date) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'app/models/application_request.rb', line 39

def self.write_cache!(req_type, count, date)
  req_type_id = req_types[req_type]

  DB.exec(<<~SQL, date: date, req_type_id: req_type_id, count: count)
    INSERT INTO application_requests (date, req_type, count)
    VALUES (:date, :req_type_id, :count)
    ON CONFLICT (date, req_type)
    DO UPDATE SET count = application_requests.count + excluded.count
  SQL
end