diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-25 15:07:47 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-25 15:07:47 +0000 |
commit | 6f2065c468b05658125b746169c56764a8ccddb1 (patch) | |
tree | 544a488ae2e65e2fcfe4acce4e56623b85bbce5e /app/services/metrics | |
parent | e6baeabaa9651d90b03bb64ffce75a2c3cb89aab (diff) | |
download | gitlab-ce-6f2065c468b05658125b746169c56764a8ccddb1.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/metrics')
-rw-r--r-- | app/services/metrics/dashboard/gitlab_alert_embed_service.rb | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/app/services/metrics/dashboard/gitlab_alert_embed_service.rb b/app/services/metrics/dashboard/gitlab_alert_embed_service.rb new file mode 100644 index 00000000000..5515b84f112 --- /dev/null +++ b/app/services/metrics/dashboard/gitlab_alert_embed_service.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +# Responsible for returning an embed containing the specified +# metrics chart for an alert. Creates panels based on the +# matching metric stored in the database. +# +# Use Gitlab::Metrics::Dashboard::Finder to retrieve dashboards. +module Metrics + module Dashboard + class GitlabAlertEmbedService < ::Metrics::Dashboard::BaseEmbedService + include Gitlab::Utils::StrongMemoize + + SEQUENCE = [STAGES::EndpointInserter].freeze + + class << self + # Determines whether the provided params are sufficient + # to uniquely identify a panel composed of user-defined + # custom metrics from the DB. + def valid_params?(params) + [ + embedded?(params[:embedded]), + params[:prometheus_alert_id].is_a?(Integer) + ].all? + end + end + + def raw_dashboard + panels_not_found!(alert_id: alert_id) unless alert && prometheus_metric + + { 'panel_groups' => [{ 'panels' => [panel] }] } + end + + private + + def allowed? + Ability.allowed?(current_user, :read_prometheus_alerts, project) + end + + def alert_id + params[:prometheus_alert_id] + end + + def alert + strong_memoize(:alert) do + Projects::Prometheus::AlertsFinder.new(id: alert_id).execute.first + end + end + + def process_params + params.merge(environment: alert.environment) + end + + def prometheus_metric + strong_memoize(:prometheus_metric) do + PrometheusMetricsFinder.new(id: alert.prometheus_metric_id).execute.first + end + end + + def panel + { + title: prometheus_metric.title, + y_label: prometheus_metric.y_label, + metrics: [prometheus_metric.to_metric_hash] + } + end + + def sequence + SEQUENCE + end + end + end +end |