class Decidim::MetricRegistry

This class acts as a registry for metrics. Each metric needs a name and a manager class, that will be used for calculations

Also, each metrics must have a collection of attributes:
  - highlighted: Determines if it showed in a highlighted chart
  - scopes: List of scopes where it will be used
  - weight: Priority of itself

In order to register a metric, you can follow this example:

Decidim.metrics_registry.register(:users) do
  metric_registry.manager_class = "Decidim::Metrics::UsersMetricManage"

  metric_registry.settings do |settings|
    settings.attribute :highlighted, type: :boolean, default: true
    settings.attribute :scopes, type: :array, default: %w(home)
    settings.attribute :weight, type: :integer, default: 1
  end
end

Metrics need to be registered in the `engine.rb` file of each module

Public Instance Methods

all() click to toggle source
# File lib/decidim/metric_registry.rb, line 57
def all
  metrics_manifests
end
filtered(highlight: nil, scope: nil, sort: nil, block: nil) click to toggle source
# File lib/decidim/metric_registry.rb, line 61
def filtered(highlight: nil, scope: nil, sort: nil, block: nil)
  result = all
  unless highlight.nil?
    result = if highlight
               highlighted(result)
             else
               not_highlighted(result)
             end
  end
  result = scoped(scope, result) if scope.present?
  result = sorted(result) if sort.present?
  result = stat_block(block, result) if block.present?
  result
end
for(metric_name, list = nil) click to toggle source
# File lib/decidim/metric_registry.rb, line 52
def for(metric_name, list = nil)
  list ||= all
  list.find { |manifest| manifest.metric_name == metric_name.to_s }
end
highlighted(list = nil) click to toggle source
# File lib/decidim/metric_registry.rb, line 76
def highlighted(list = nil)
  list ||= all
  list.find_all { |manifest| manifest.settings.attributes[:highlighted][:default] }
end
not_highlighted(list = nil) click to toggle source
# File lib/decidim/metric_registry.rb, line 81
def not_highlighted(list = nil)
  list ||= all
  list.find_all { |manifest| !manifest.settings.attributes[:highlighted][:default] }
end
register(metric_name) { |metric_manifest| ... } click to toggle source

Public: Registers a metric for calculations

metric_name - a symbol representing the name of the metric

Returns nothing. Raises an error if there's already a metric registered with that metric name.

# File lib/decidim/metric_registry.rb, line 32
def register(metric_name)
  metric_name = metric_name.to_s
  metric_exists = self.for(metric_name).present?

  if metric_exists
    raise(
      MetricAlreadyRegistered,
      "There's a metric already registered with the name `:#{metric_name}`, must be unique"
    )
  end

  metric_manifest = MetricManifest.new(metric_name: metric_name)

  yield(metric_manifest)

  metric_manifest.validate!

  metrics_manifests << metric_manifest
end
scoped(scope, list = nil) click to toggle source
# File lib/decidim/metric_registry.rb, line 86
def scoped(scope, list = nil)
  list ||= all
  list.find_all { |manifest| manifest.settings.attributes[:scopes][:default].include?(scope.to_s) }
end
sorted(list = nil) click to toggle source
# File lib/decidim/metric_registry.rb, line 96
def sorted(list = nil)
  list ||= all
  list.sort_by { |manifest| manifest.settings.attributes[:weight].default }
end
stat_block(block, list = nil) click to toggle source
# File lib/decidim/metric_registry.rb, line 91
def stat_block(block, list = nil)
  list ||= all
  list.find_all { |manifest| manifest.stat_block == block.to_s }
end

Private Instance Methods

metrics_manifests() click to toggle source
# File lib/decidim/metric_registry.rb, line 105
def metrics_manifests
  @metrics_manifests ||= []
end