debian-mirror-gitlab/lib/gitlab/usage/metric.rb

44 lines
847 B
Ruby
Raw Normal View History

2021-03-08 18:12:59 +05:30
# frozen_string_literal: true
module Gitlab
module Usage
class Metric
include ActiveModel::Model
InvalidMetricError = Class.new(RuntimeError)
2021-03-11 19:13:27 +05:30
attr_accessor :key_path, :value
2021-03-08 18:12:59 +05:30
2021-03-11 19:13:27 +05:30
validates :key_path, presence: true
2021-03-08 18:12:59 +05:30
def definition
2021-03-11 19:13:27 +05:30
self.class.definitions[key_path]
2021-03-08 18:12:59 +05:30
end
2021-03-11 19:13:27 +05:30
def unflatten_key_path
unflatten(key_path.split('.'), value)
2021-03-08 18:12:59 +05:30
end
class << self
def definitions
@definitions ||= Gitlab::Usage::MetricDefinition.definitions
end
def dictionary
definitions.map { |key, definition| definition.to_dictionary }
end
end
private
def unflatten(keys, value)
loop do
value = { keys.pop.to_sym => value }
break if keys.blank?
end
value
end
end
end
end