Commit f93b8da3 authored by rpereira2's avatar rpereira2

Move code to read dashboard files in repo

Move code to read dashboards in a project's repository into a new
lib class. This code was previously in CustomDashboardService and in
Metrics::Dashboard::Finder. The code is now in one class.
parent 2bd95053
......@@ -111,7 +111,7 @@ module EnvironmentsHelper
'empty-no-data-svg-path' => image_path('illustrations/monitoring/no_data.svg'),
'empty-no-data-small-svg-path' => image_path('illustrations/chart-empty-state-small.svg'),
'empty-unable-to-connect-svg-path' => image_path('illustrations/monitoring/unable_to_connect.svg'),
'custom-dashboard-base-path' => Metrics::Dashboard::CustomDashboardService::DASHBOARD_ROOT
'custom-dashboard-base-path' => Gitlab::Metrics::Dashboard::RepoDashboardFinder::DASHBOARD_ROOT
}
end
end
......
......@@ -577,7 +577,7 @@ class Repository
cache_method :merge_request_template_names, fallback: []
def user_defined_metrics_dashboard_paths
Gitlab::Metrics::Dashboard::Finder.find_all_paths_from_source(project)
Gitlab::Metrics::Dashboard::RepoDashboardFinder.list_dashboards(project)
end
cache_method :user_defined_metrics_dashboard_paths, fallback: []
......
......@@ -9,7 +9,7 @@ module Metrics
include Gitlab::Utils::StrongMemoize
ALLOWED_FILE_TYPE = '.yml'
USER_DASHBOARDS_DIR = ::Metrics::Dashboard::CustomDashboardService::DASHBOARD_ROOT
USER_DASHBOARDS_DIR = ::Gitlab::Metrics::Dashboard::RepoDashboardFinder::DASHBOARD_ROOT
SEQUENCES = {
::Metrics::Dashboard::SystemDashboardService::DASHBOARD_PATH => [
::Gitlab::Metrics::Dashboard::Stages::CommonMetricsInserter,
......
......@@ -6,9 +6,6 @@
module Metrics
module Dashboard
class CustomDashboardService < ::Metrics::Dashboard::BaseService
DASHBOARD_ROOT = ".gitlab/dashboards"
DASHBOARD_EXTENSION = '.yml'
class << self
def valid_params?(params)
params[:dashboard_path].present?
......@@ -27,13 +24,9 @@ module Metrics
end
end
def file_finder(project)
Gitlab::Template::Finders::RepoTemplateFinder.new(project, DASHBOARD_ROOT, DASHBOARD_EXTENSION)
end
# Grabs the filepath after the base directory.
def name_for_path(filepath)
filepath.delete_prefix("#{DASHBOARD_ROOT}/")
filepath.delete_prefix("#{Gitlab::Metrics::Dashboard::RepoDashboardFinder::DASHBOARD_ROOT}/")
end
end
......@@ -41,7 +34,7 @@ module Metrics
# Searches the project repo for a custom-defined dashboard.
def get_raw_dashboard
yml = self.class.file_finder(project).read(dashboard_path)
yml = Gitlab::Metrics::Dashboard::RepoDashboardFinder.read_dashboard(project, dashboard_path)
load_yaml(yml)
end
......
......@@ -7,7 +7,7 @@ module Metrics
include Stepable
ALLOWED_FILE_TYPE = '.yml'
USER_DASHBOARDS_DIR = ::Metrics::Dashboard::CustomDashboardService::DASHBOARD_ROOT
USER_DASHBOARDS_DIR = ::Gitlab::Metrics::Dashboard::RepoDashboardFinder::DASHBOARD_ROOT
steps :check_push_authorized,
:check_branch_name,
......
......@@ -77,17 +77,6 @@ module Gitlab
end
end
# Returns list of all user-defined dashboard paths. Used to populate repo cache.
# Also deletes all dashboard cache entries.
# Prefer #find_all_paths.
# @return [Array] ex) ['.gitlab/dashboards/dashboard1.yml']
def find_all_paths_from_source(project)
Gitlab::Metrics::Dashboard::Cache.delete_all!
file_finder(project)
.list_files_for(::Metrics::Dashboard::CustomDashboardService::DASHBOARD_ROOT)
end
private
def user_facing_dashboard_services(project)
......@@ -105,14 +94,6 @@ module Gitlab
predefined_dashboard_services
end
def file_finder(project)
Gitlab::Template::Finders::RepoTemplateFinder.new(
project,
::Metrics::Dashboard::CustomDashboardService::DASHBOARD_ROOT,
::Metrics::Dashboard::CustomDashboardService::DASHBOARD_EXTENSION
)
end
def predefined_dashboard_services
::Metrics::Dashboard::PredefinedDashboardService.descendants - PREDEFINED_DASHBOARD_EXCLUSION_LIST
end
......
# frozen_string_literal: true
# Provides methods to list and read dashboard yaml files from a project's repository.
module Gitlab
module Metrics
module Dashboard
class RepoDashboardFinder
DASHBOARD_ROOT = ".gitlab/dashboards"
DASHBOARD_EXTENSION = '.yml'
class << self
# Returns list of all user-defined dashboard paths. Used to populate
# Repository model cache (Repository#user_defined_metrics_dashboard_paths).
# Also deletes all dashboard cache entries.
# @return [Array] ex) ['.gitlab/dashboards/dashboard1.yml']
def list_dashboards(project)
Gitlab::Metrics::Dashboard::Cache.delete_all!
file_finder(project).list_files_for(DASHBOARD_ROOT)
end
# Reads the given dashboard from repository, and returns the content as a string.
# @return [String]
def read_dashboard(project, dashboard_path)
file_finder(project).read(dashboard_path)
end
private
def file_finder(project)
Gitlab::Template::Finders::RepoTemplateFinder.new(project, DASHBOARD_ROOT, DASHBOARD_EXTENSION)
end
end
end
end
end
end
......@@ -42,7 +42,7 @@ RSpec.describe EnvironmentsHelper do
'custom-metrics-available' => 'true',
'alerts-endpoint' => project_prometheus_alerts_path(project, environment_id: environment.id, format: :json),
'prometheus-alerts-available' => 'true',
'custom-dashboard-base-path' => Metrics::Dashboard::CustomDashboardService::DASHBOARD_ROOT,
'custom-dashboard-base-path' => Gitlab::Metrics::Dashboard::RepoDashboardFinder::DASHBOARD_ROOT,
'operations-settings-path' => project_settings_operations_path(project),
'can-access-operations-settings' => 'true'
)
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Metrics::Dashboard::RepoDashboardFinder do
include MetricsDashboardHelpers
let_it_be(:project) { create(:project) }
describe '.list_dashboards' do
it 'deletes dashboard cache entries' do
expect(Gitlab::Metrics::Dashboard::Cache).to receive(:delete_all!).and_call_original
described_class.list_dashboards(project)
end
it 'returns empty array when there are no dashboards' do
expect(described_class.list_dashboards(project)).to eq([])
end
context 'when there are project dashboards available' do
let_it_be(:dashboard_path) { '.gitlab/dashboards/test.yml' }
let_it_be(:project) { project_with_dashboard(dashboard_path) }
it 'returns the dashboard list' do
expect(described_class.list_dashboards(project)).to contain_exactly(dashboard_path)
end
end
end
describe '.read_dashboard' do
it 'raises error when dashboard does not exist' do
dashboard_path = '.gitlab/dashboards/test.yml'
expect { described_class.read_dashboard(project, dashboard_path) }.to raise_error(
Gitlab::Metrics::Dashboard::Errors::NOT_FOUND_ERROR
)
end
context 'when there are project dashboards available' do
let_it_be(:dashboard_path) { '.gitlab/dashboards/test.yml' }
let_it_be(:project) { project_with_dashboard(dashboard_path) }
it 'reads dashboard' do
expect(described_class.read_dashboard(project, dashboard_path)).to eq(
fixture_file('lib/gitlab/metrics/dashboard/sample_dashboard.yml')
)
end
end
end
end
......@@ -106,7 +106,8 @@ RSpec.describe Metrics::Dashboard::CustomDashboardService, :use_clean_rails_memo
end
it 'caches repo file list' do
expect(Gitlab::Template::Finders::RepoTemplateFinder).to receive(:new)
expect(Gitlab::Metrics::Dashboard::RepoDashboardFinder).to receive(:list_dashboards)
.with(project)
.once
.and_call_original
......
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