2017-10-25 10:19:43 +08:00
|
|
|
# frozen_string_literal: true
|
2015-02-04 12:10:54 +08:00
|
|
|
class ApplicationRequest < ActiveRecord::Base
|
2018-03-16 05:10:45 +08:00
|
|
|
|
2015-02-06 11:39:04 +08:00
|
|
|
enum req_type: %i(http_total
|
|
|
|
http_2xx
|
|
|
|
http_background
|
|
|
|
http_3xx
|
|
|
|
http_4xx
|
|
|
|
http_5xx
|
|
|
|
page_view_crawler
|
|
|
|
page_view_logged_in
|
2015-07-04 05:02:57 +08:00
|
|
|
page_view_anon
|
|
|
|
page_view_logged_in_mobile
|
|
|
|
page_view_anon_mobile)
|
2015-02-04 12:10:54 +08:00
|
|
|
|
2018-03-16 05:10:45 +08:00
|
|
|
include CachedCounting
|
2015-02-04 12:10:54 +08:00
|
|
|
|
2020-05-18 17:22:39 +08:00
|
|
|
def self.disable
|
2020-05-23 12:56:13 +08:00
|
|
|
@disabled = true
|
2020-05-18 17:22:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.enable
|
2020-05-23 12:56:13 +08:00
|
|
|
@disabled = false
|
2020-05-18 17:22:39 +08:00
|
|
|
end
|
|
|
|
|
2015-02-04 12:10:54 +08:00
|
|
|
def self.increment!(type, opts = nil)
|
2020-05-23 12:56:13 +08:00
|
|
|
return if @disabled
|
|
|
|
perform_increment!(redis_key(type), opts)
|
2015-02-04 12:10:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.write_cache!(date = nil)
|
|
|
|
if date.nil?
|
|
|
|
write_cache!(Time.now.utc)
|
|
|
|
write_cache!(Time.now.utc.yesterday)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-02-07 06:20:11 +08:00
|
|
|
self.last_flush = Time.now.utc
|
2015-02-06 11:39:04 +08:00
|
|
|
|
2015-02-04 12:10:54 +08:00
|
|
|
date = date.to_date
|
|
|
|
|
|
|
|
req_types.each do |req_type, _|
|
2018-03-16 05:10:45 +08:00
|
|
|
val = get_and_reset(redis_key(req_type, date))
|
2015-02-04 12:10:54 +08:00
|
|
|
|
|
|
|
next if val == 0
|
|
|
|
|
|
|
|
id = req_id(date, req_type)
|
|
|
|
where(id: id).update_all(["count = count + ?", val])
|
|
|
|
end
|
2017-10-25 10:31:44 +08:00
|
|
|
rescue Redis::CommandError => e
|
|
|
|
raise unless e.message =~ /READONLY/
|
|
|
|
nil
|
2015-02-04 12:10:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_cache!(date = nil)
|
|
|
|
if date.nil?
|
|
|
|
clear_cache!(Time.now.utc)
|
|
|
|
clear_cache!(Time.now.utc.yesterday)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
req_types.each do |req_type, _|
|
|
|
|
key = redis_key(req_type, date)
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.del key
|
2015-02-04 12:10:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def self.req_id(date, req_type, retries = 0)
|
|
|
|
|
|
|
|
req_type_id = req_types[req_type]
|
|
|
|
|
|
|
|
# a poor man's upsert
|
2019-10-21 18:32:27 +08:00
|
|
|
id = where(date: date, req_type: req_type_id).pluck_first(:id)
|
2015-02-04 12:10:54 +08:00
|
|
|
id ||= create!(date: date, req_type: req_type_id, count: 0).id
|
|
|
|
|
|
|
|
rescue # primary key violation
|
|
|
|
if retries == 0
|
|
|
|
req_id(date, req_type, 1)
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.redis_key(req_type, time = Time.now.utc)
|
|
|
|
"app_req_#{req_type}#{time.strftime('%Y%m%d')}"
|
|
|
|
end
|
|
|
|
|
2015-02-05 04:05:16 +08:00
|
|
|
def self.stats
|
2015-02-12 06:41:29 +08:00
|
|
|
s = HashWithIndifferentAccess.new({})
|
2015-02-05 04:05:16 +08:00
|
|
|
|
2015-02-12 06:41:29 +08:00
|
|
|
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)
|
2015-02-05 04:05:16 +08:00
|
|
|
end
|
2015-02-12 06:41:29 +08:00
|
|
|
|
|
|
|
s
|
2015-02-05 04:05:16 +08:00
|
|
|
end
|
2015-02-04 12:10:54 +08:00
|
|
|
end
|
|
|
|
|
2015-02-04 13:34:25 +08:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: application_requests
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# date :date not null
|
|
|
|
# req_type :integer not null
|
|
|
|
# count :integer default(0), not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_application_requests_on_date_and_req_type (date,req_type) UNIQUE
|
|
|
|
#
|