Skip to content
Snippets Groups Projects

WIP: Add finder for prometheus metrics

Closed Sarah Yasonik requested to merge add-metrics-finder into master
2 unresolved threads
4 files
+ 162
17
Compare changes
  • Side-by-side
  • Inline
Files
4
# frozen_string_literal: true
class PrometheusMetricsFinder
attr_reader :params
ACCEPTED_PARAMS = [
:project,
:group,
:title,
:y_label,
:identifier,
:common,
:order_by
].freeze
# @params [Hash<Symbol, any>] representing any prometheus metrics column
def initialize(params = {})
validate_params!(params)
@params = params
end
# rubocop: disable CodeReuse/ActiveRecord
def execute
PrometheusMetric.where(params)
# Scopes
metrics = by_project(::PrometheusMetric)
metrics = by_group(metrics)
metrics = by_title(metrics)
metrics = by_y_label(metrics)
metrics = common(metrics)
metrics = ordered(metrics)
# Finds
metrics = find_identifier(metrics)
metrics
end
private
attr_reader :params
def by_project(metrics)
return metrics unless params[:project]
metrics.for_project(params[:project])
end
def by_group(metrics)
return metrics unless params[:group]
metrics.for_group(params[:group])
end
def by_title(metrics)
return metrics unless params[:title]
metrics.for_title(params[:title])
end
def by_y_label(metrics)
return metrics unless params[:y_label]
metrics.for_y_label(params[:y_label])
end
def common(metrics)
return metrics unless params[:common]
metrics.common
end
def ordered(metrics)
return metrics unless params[:order_by]
metrics.order(params[:order_by])
end
def find_identifier(metrics)
return metrics unless params[:identifier]
metrics.for_identifier(params[:identifier]).first
end
def validate_params!(params)
raise ArgumentError, "Please provide one or more of: #{ACCEPTED_PARAMS}" if params.blank?
raise ArgumentError, ":identifier must be scoped to a :project or :common" if params[:identifier] && !(params[:project] || params[:common])
end
# rubocop: enable CodeReuse/ActiveRecord
end
Loading