Commit 7eb796e6 authored by syasonik's avatar syasonik

Inject dashboard response with db info

parent 185ec807
# frozen_string_literal: true
class MetricsDashboardProcessingService
DEFAULT_PANEL_TYPE = 'area-chart'
def initialize(dashboard, project)
@dashboard = dashboard.deep_transform_keys(&:to_sym)
@project = project
end
def process
insert_persisted_metrics!
insert_metric_ids!
@dashboard.to_json
end
private
# Inserts project-specific metrics into the dashboard config.
# If there are no project-specific metrics, this will have no effect.
def insert_persisted_metrics!
@project.prometheus_metrics.each do |persisted_metric|
group = find_or_create_group(@dashboard[:panel_groups], persisted_metric)
panel = find_or_create_panel(group[:panels], persisted_metric)
find_or_create_metric(panel[:metrics], persisted_metric)
end
end
# For each metric in the dashboard config, attempts to find a corresponding
# persisted record. If found, includes the record id in the config.
def insert_metric_ids!
@dashboard[:panel_groups].each do |group|
group[:panels].each do |panel|
panel[:metrics].each do |metric|
metric_record = common_metrics.find {|m| m.identifier == metric[:id] }
metric[:metric_id] = metric_record.id if metric_record
end
end
end
end
def common_metrics
@common_metrics ||= ::PrometheusMetric.common
end
def find_or_create_group(panel_groups, metric)
target_group = panel_groups.find { |group| group[:group] == metric.group_title }
unless target_group
target_group = {
group: metric.group_title,
priority: metric.priority,
panels: []
}
panel_groups << target_group
end
target_group
end
def find_or_create_panel(panels, metric)
panel_identifiers = [DEFAULT_PANEL_TYPE, metric.title, metric.y_label]
target_panel = panels.find { |panel| panel.values_at(:type, :title, :y_label) == panel_identifiers }
unless target_panel
target_panel = {
type: DEFAULT_PANEL_TYPE,
title: metric.title,
y_label: metric.y_label,
metrics: []
}
panels << target_panel
end
target_panel
end
def find_or_create_metric(metrics, metric)
target_metric = metrics.find { |m| m[:id] == metric.identifier }
unless target_metric
target_metric = metric.queries.first.merge(metric_id: metric.id)
metrics << target_metric
end
target_metric
end
end
...@@ -3,7 +3,11 @@ ...@@ -3,7 +3,11 @@
# Fetches the metrics dashboard layout and supplemented the output with DB info. # Fetches the metrics dashboard layout and supplemented the output with DB info.
class MetricsDashboardService class MetricsDashboardService
SYSTEM_DASHBOARD_NAME = 'system_dashboard' SYSTEM_DASHBOARD_NAME = 'system_dashboard'
SYSTEM_DASHBOARD_PATH = Rails.root.join("config/prometheus", "#{SYSTEM_DASHBOARD_NAME}.yml") SYSTEM_DASHBOARD_PATH = Rails.root.join('config', 'prometheus', "#{SYSTEM_DASHBOARD_NAME}.yml")
def initialize(project)
@project = project
end
# Returns a DB-supplemented json representation of a dashboard config file. # Returns a DB-supplemented json representation of a dashboard config file.
def get_dashboard def get_dashboard
...@@ -26,6 +30,6 @@ class MetricsDashboardService ...@@ -26,6 +30,6 @@ class MetricsDashboardService
# TODO: "Processing" the dashboard needs to include several steps such as # TODO: "Processing" the dashboard needs to include several steps such as
# inserting metric ids and alert information. # inserting metric ids and alert information.
def process_dashboard(dashboard) def process_dashboard(dashboard)
dashboard.to_json MetricsDashboardProcessingService.new(dashboard, @project).process
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment