2020-09-01 14:10:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Analytics
|
2021-02-18 10:09:43 -05:00
|
|
|
module UsageTrends
|
2020-09-01 14:10:48 -04:00
|
|
|
class Measurement < ApplicationRecord
|
2021-02-18 10:09:43 -05:00
|
|
|
self.table_name = 'analytics_instance_statistics_measurements'
|
|
|
|
|
2020-09-24 11:09:51 -04:00
|
|
|
EXPERIMENTAL_IDENTIFIERS = %i[pipelines_succeeded pipelines_failed pipelines_canceled pipelines_skipped].freeze
|
|
|
|
|
2020-09-10 08:08:54 -04:00
|
|
|
enum identifier: {
|
|
|
|
projects: 1,
|
|
|
|
users: 2,
|
|
|
|
issues: 3,
|
|
|
|
merge_requests: 4,
|
|
|
|
groups: 5,
|
2020-09-24 11:09:51 -04:00
|
|
|
pipelines: 6,
|
|
|
|
pipelines_succeeded: 7,
|
|
|
|
pipelines_failed: 8,
|
|
|
|
pipelines_canceled: 9,
|
2020-11-11 19:09:44 -05:00
|
|
|
pipelines_skipped: 10,
|
|
|
|
billable_users: 11
|
2020-09-10 08:08:54 -04:00
|
|
|
}
|
2020-09-01 14:10:48 -04:00
|
|
|
|
|
|
|
validates :recorded_at, :identifier, :count, presence: true
|
|
|
|
validates :recorded_at, uniqueness: { scope: :identifier }
|
2020-09-10 08:08:54 -04:00
|
|
|
|
|
|
|
scope :order_by_latest, -> { order(recorded_at: :desc) }
|
|
|
|
scope :with_identifier, -> (identifier) { where(identifier: identifier) }
|
2020-11-03 10:09:05 -05:00
|
|
|
scope :recorded_after, -> (date) { where(self.model.arel_table[:recorded_at].gteq(date)) if date.present? }
|
|
|
|
scope :recorded_before, -> (date) { where(self.model.arel_table[:recorded_at].lteq(date)) if date.present? }
|
2020-09-24 11:09:51 -04:00
|
|
|
|
2020-11-11 19:09:44 -05:00
|
|
|
def self.identifier_query_mapping
|
|
|
|
{
|
|
|
|
identifiers[:projects] => -> { Project },
|
|
|
|
identifiers[:users] => -> { User },
|
|
|
|
identifiers[:issues] => -> { Issue },
|
|
|
|
identifiers[:merge_requests] => -> { MergeRequest },
|
|
|
|
identifiers[:groups] => -> { Group },
|
|
|
|
identifiers[:pipelines] => -> { Ci::Pipeline },
|
|
|
|
identifiers[:pipelines_succeeded] => -> { Ci::Pipeline.success },
|
|
|
|
identifiers[:pipelines_failed] => -> { Ci::Pipeline.failed },
|
|
|
|
identifiers[:pipelines_canceled] => -> { Ci::Pipeline.canceled },
|
|
|
|
identifiers[:pipelines_skipped] => -> { Ci::Pipeline.skipped }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Customized min and max calculation, in some cases using the original scope is too slow.
|
|
|
|
def self.identifier_min_max_queries
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2020-09-24 11:09:51 -04:00
|
|
|
def self.measurement_identifier_values
|
2020-10-28 14:08:52 -04:00
|
|
|
identifiers.values
|
2020-09-24 11:09:51 -04:00
|
|
|
end
|
2020-11-11 19:09:44 -05:00
|
|
|
|
|
|
|
def self.find_latest_or_fallback(identifier)
|
|
|
|
with_identifier(identifier).order_by_latest.first || identifier_query_mapping[identifiers[identifier]].call
|
|
|
|
end
|
2020-09-01 14:10:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-11-11 19:09:44 -05:00
|
|
|
|
2021-02-18 10:09:43 -05:00
|
|
|
Analytics::UsageTrends::Measurement.prepend_if_ee('EE::Analytics::UsageTrends::Measurement')
|