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



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

def self.disable
  @disabled = true
end

.enableObject



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

def self.enable
  @disabled = false
end

.increment!(req_type) ⇒ Object



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

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

.statsObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/application_request.rb', line 45

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}_7_days"] = query.where("date > ?", 7.days.ago).sum(:count)
  end

  s
end

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



34
35
36
37
38
39
40
41
42
43
# File 'app/models/application_request.rb', line 34

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