summaryrefslogtreecommitdiff
path: root/app/services/metrics/dashboard/default_embed_service.rb
diff options
context:
space:
mode:
authorSarah Yasonik <syasonik@gitlab.com>2019-07-29 23:03:59 +0000
committerThong Kuah <tkuah@gitlab.com>2019-07-29 23:03:59 +0000
commitdfe13131d705c739a3b8747e70c004aaf2e58856 (patch)
treecbaf47bcedf0d518af7c6bf5cafbb59dbade78d7 /app/services/metrics/dashboard/default_embed_service.rb
parent95838fe44dfc5aa48428b09b4288ac1e1d637f94 (diff)
downloadgitlab-ce-dfe13131d705c739a3b8747e70c004aaf2e58856.tar.gz
Move BaseService to Services directory
In preparation for embedding specific metrics in issues https://gitlab.com/gitlab-org/gitlab-ce/issues/62971, this commit moves the BaseService for metrics dashboards to a new services subdirectory. This is purely for the sake of organization and maintainability.
Diffstat (limited to 'app/services/metrics/dashboard/default_embed_service.rb')
-rw-r--r--app/services/metrics/dashboard/default_embed_service.rb63
1 files changed, 63 insertions, 0 deletions
diff --git a/app/services/metrics/dashboard/default_embed_service.rb b/app/services/metrics/dashboard/default_embed_service.rb
new file mode 100644
index 00000000000..0967c5bcfeb
--- /dev/null
+++ b/app/services/metrics/dashboard/default_embed_service.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+# Responsible for returning a filtered system dashboard
+# containing only the default embedded metrics. In future,
+# this class may be updated to support filtering to
+# alternate metrics/panels.
+#
+# Why isn't this filtering in a processing stage? By filtering
+# here, we ensure the dynamically-determined dashboard is cached.
+#
+# Use Gitlab::Metrics::Dashboard::Finder to retrive dashboards.
+module Metrics
+ module Dashboard
+ class DefaultEmbedService < ::Metrics::Dashboard::BaseService
+ # For the default filtering for embedded metrics,
+ # uses the 'id' key in dashboard-yml definition for
+ # identification.
+ DEFAULT_EMBEDDED_METRICS_IDENTIFIERS = %w(
+ system_metrics_kubernetes_container_memory_total
+ system_metrics_kubernetes_container_cores_total
+ ).freeze
+
+ # Returns a new dashboard with only the matching
+ # metrics from the system dashboard, stripped of groups.
+ # @return [Hash]
+ def raw_dashboard
+ panels = panel_groups.each_with_object([]) do |group, panels|
+ matched_panels = group['panels'].select { |panel| matching_panel?(panel) }
+
+ panels.concat(matched_panels)
+ end
+
+ { 'panel_groups' => [{ 'panels' => panels }] }
+ end
+
+ def cache_key
+ "dynamic_metrics_dashboard_#{metric_identifiers.join('_')}"
+ end
+
+ private
+
+ # Returns an array of the panels groups on the
+ # system dashboard
+ def panel_groups
+ ::Metrics::Dashboard::SystemDashboardService
+ .new(project, nil)
+ .raw_dashboard['panel_groups']
+ end
+
+ # Identifies a panel as "matching" if any metric ids in
+ # the panel is in the list of identifiers to collect.
+ def matching_panel?(panel)
+ panel['metrics'].any? do |metric|
+ metric_identifiers.include?(metric['id'])
+ end
+ end
+
+ def metric_identifiers
+ DEFAULT_EMBEDDED_METRICS_IDENTIFIERS
+ end
+ end
+ end
+end