1# frozen_string_literal: true
2
3module MetricsDashboardHelpers
4  # @param dashboards [Hash<string, string>] - Should contain a hash where
5  #     each key is the path to a dashboard in the repository and each value is
6  #     the dashboard content.
7  #     Ex: { '.gitlab/dashboards/dashboard1.yml' => fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml') }
8  def project_with_dashboards(dashboards, project_params = {})
9    create(:project, :custom_repo, **project_params, files: dashboards)
10  end
11
12  def project_with_dashboard(dashboard_path, dashboard_yml = nil, project_params = {})
13    dashboard_yml ||= fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml')
14
15    project_with_dashboards({ dashboard_path => dashboard_yml }, project_params)
16  end
17
18  def project_with_dashboard_namespace(dashboard_path, dashboard_yml = nil, project_params = {})
19    project_with_dashboard(dashboard_path, dashboard_yml, project_params.reverse_merge(path: 'monitor-project'))
20  end
21
22  def delete_project_dashboard(project, user, dashboard_path)
23    project.repository.delete_file(
24      user,
25      dashboard_path,
26      branch_name: 'master',
27      message: 'Delete dashboard'
28    )
29
30    project.repository.refresh_method_caches([:metrics_dashboard])
31  end
32
33  def load_sample_dashboard
34    load_dashboard_yaml(fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml'))
35  end
36
37  def load_dashboard_yaml(data)
38    ::Gitlab::Config::Loader::Yaml.new(data).load_raw!
39  end
40
41  def system_dashboard_path
42    Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH
43  end
44
45  def pod_dashboard_path
46    Metrics::Dashboard::PodDashboardService::DASHBOARD_PATH
47  end
48
49  def business_metric_title
50    Enums::PrometheusMetric.group_details[:business][:group_title]
51  end
52
53  def self_monitoring_dashboard_path
54    Metrics::Dashboard::SelfMonitoringDashboardService::DASHBOARD_PATH
55  end
56end
57