2017-10-25 10:19:43 +08:00
|
|
|
# frozen_string_literal: true
|
2015-02-04 12:10:54 +08:00
|
|
|
class ApplicationRequest < ActiveRecord::Base
|
2024-05-27 18:27:13 +08:00
|
|
|
enum req_type: {
|
|
|
|
http_total: 0,
|
|
|
|
http_2xx: 1,
|
|
|
|
http_background: 2,
|
|
|
|
http_3xx: 3,
|
|
|
|
http_4xx: 4,
|
|
|
|
http_5xx: 5,
|
|
|
|
page_view_crawler: 6,
|
|
|
|
page_view_logged_in: 7,
|
|
|
|
page_view_anon: 8,
|
|
|
|
page_view_logged_in_mobile: 9,
|
|
|
|
page_view_anon_mobile: 10,
|
|
|
|
api: 11,
|
|
|
|
user_api: 12,
|
|
|
|
page_view_anon_browser: 13,
|
|
|
|
page_view_anon_browser_mobile: 14,
|
|
|
|
page_view_logged_in_browser: 15,
|
|
|
|
page_view_logged_in_browser_mobile: 16,
|
|
|
|
}
|
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
|
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
def self.increment!(req_type)
|
2020-05-23 12:56:13 +08:00
|
|
|
return if @disabled
|
2022-02-23 00:45:25 +08:00
|
|
|
perform_increment!(req_type)
|
2015-02-04 12:10:54 +08:00
|
|
|
end
|
|
|
|
|
2022-02-23 00:45:25 +08:00
|
|
|
def self.write_cache!(req_type, count, date)
|
2015-02-04 12:10:54 +08:00
|
|
|
req_type_id = req_types[req_type]
|
|
|
|
|
2023-03-17 17:35:29 +08:00
|
|
|
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
|
2015-02-04 12:10:54 +08:00
|
|
|
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)
|
2024-06-14 04:02:28 +08:00
|
|
|
s["#{key}_28_days"] = query.where("date > ?", 28.days.ago).sum(:count)
|
2015-02-12 06:41:29 +08:00
|
|
|
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
|
|
|
|
#
|