2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-05 11:46:10 -04:00
|
|
|
class SiteStatistic < ActiveRecord::Base
|
|
|
|
# prevents the creation of multiple rows
|
|
|
|
default_value_for :id, 1
|
|
|
|
|
|
|
|
COUNTER_ATTRIBUTES = %w(repositories_count wikis_count).freeze
|
2018-07-17 20:30:55 -04:00
|
|
|
REQUIRED_SCHEMA_VERSION = 20180629153018
|
2018-07-05 11:46:10 -04:00
|
|
|
|
2018-07-24 12:43:40 -04:00
|
|
|
# Tracks specific attribute
|
|
|
|
#
|
|
|
|
# @param [String] raw_attribute must be one of the values listed in COUNTER_ATTRIBUTES
|
2018-07-05 11:46:10 -04:00
|
|
|
def self.track(raw_attribute)
|
|
|
|
with_statistics_available(raw_attribute) do |attribute|
|
|
|
|
SiteStatistic.update_all(["#{attribute} = #{attribute}+1"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-24 12:43:40 -04:00
|
|
|
# Untracks specific attribute
|
|
|
|
#
|
|
|
|
# @param [String] raw_attribute must be one of the values listed in COUNTER_ATTRIBUTES
|
2018-07-05 11:46:10 -04:00
|
|
|
def self.untrack(raw_attribute)
|
|
|
|
with_statistics_available(raw_attribute) do |attribute|
|
|
|
|
SiteStatistic.update_all(["#{attribute} = #{attribute}-1 WHERE #{attribute} > 0"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-24 12:43:40 -04:00
|
|
|
# Wrapper for track/untrack operations with basic validations and enforced requirements
|
|
|
|
#
|
|
|
|
# @param [String] raw_attribute must be one of the values listed in COUNTER_ATTRIBUTES
|
|
|
|
# @yield [String] attribute quoted to be used inside SQL / Arel query
|
2018-07-05 11:46:10 -04:00
|
|
|
def self.with_statistics_available(raw_attribute)
|
|
|
|
unless raw_attribute.in?(COUNTER_ATTRIBUTES)
|
|
|
|
raise ArgumentError, "Invalid attribute: '#{raw_attribute}' to '#{caller_locations(1, 1)[0].label}' method. " \
|
|
|
|
"Valid attributes are: #{COUNTER_ATTRIBUTES.join(', ')}"
|
|
|
|
end
|
|
|
|
|
2018-07-17 20:30:55 -04:00
|
|
|
return unless available?
|
|
|
|
|
2018-07-23 17:06:35 -04:00
|
|
|
self.fetch # make sure record exists
|
2018-07-05 11:46:10 -04:00
|
|
|
|
2018-07-23 17:06:35 -04:00
|
|
|
attribute = self.connection.quote_column_name(raw_attribute)
|
|
|
|
|
|
|
|
# will be running on its own transaction context
|
|
|
|
yield(attribute)
|
2018-07-05 11:46:10 -04:00
|
|
|
end
|
|
|
|
|
2018-07-24 12:43:40 -04:00
|
|
|
# Returns a site statistic record with tracked information
|
|
|
|
#
|
|
|
|
# @return [SiteStatistic] record with tracked information
|
2018-07-05 11:46:10 -04:00
|
|
|
def self.fetch
|
2018-08-03 19:02:38 -04:00
|
|
|
transaction(requires_new: true) do
|
2018-07-23 17:06:35 -04:00
|
|
|
SiteStatistic.first_or_create!
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
retry
|
2018-07-05 11:46:10 -04:00
|
|
|
end
|
2018-07-17 20:30:55 -04:00
|
|
|
|
2018-07-24 12:43:40 -04:00
|
|
|
# Return whether required schema change is available
|
|
|
|
#
|
|
|
|
# This is needed in order to degrade gracefully when testing schema migrations
|
|
|
|
#
|
|
|
|
# @return [Boolean] whether schema is available
|
2018-07-17 20:30:55 -04:00
|
|
|
def self.available?
|
|
|
|
@available_flag ||= ActiveRecord::Migrator.current_version >= REQUIRED_SCHEMA_VERSION
|
|
|
|
end
|
|
|
|
|
2018-07-24 12:43:40 -04:00
|
|
|
# Resets cached column information
|
|
|
|
#
|
|
|
|
# This is called during schema migration specs, in order to reset internal cache state
|
2018-07-17 20:30:55 -04:00
|
|
|
def self.reset_column_information
|
|
|
|
@available_flag = nil
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
2018-07-05 11:46:10 -04:00
|
|
|
end
|