Commit 2bd95053 authored by rpereira2's avatar rpereira2

Cache only the user defined metrics dashboard paths

- Currently, the output of the #all_dashboard_paths method of all
dashboard services is collected and cached by the Repository model.
Unfortunately, this cache cannot be deleted when an update is made to
the #all_dashboard_paths method.

- This MR changes the Repository model cache to only contain a list of
user-defined dashboard paths under .gitlab/dashboards. The
all_dashboard_paths methods do not do any other expensive operations
(other than fetching a list of dashboards under .gitlab/dashboards).

- This change should remove the need to delete the cache when a change
is made to the all_dashboard_paths method.
parent 83314b1f
......@@ -43,7 +43,7 @@ class Repository
gitlab_ci_yml branch_names tag_names branch_count
tag_count avatar exists? root_ref merged_branch_names
has_visible_content? issue_template_names merge_request_template_names
metrics_dashboard_paths xcode_project?).freeze
user_defined_metrics_dashboard_paths xcode_project?).freeze
# Methods that use cache_method but only memoize the value
MEMOIZED_CACHED_METHODS = %i(license).freeze
......@@ -61,7 +61,7 @@ class Repository
avatar: :avatar,
issue_template: :issue_template_names,
merge_request_template: :merge_request_template_names,
metrics_dashboard: :metrics_dashboard_paths,
metrics_dashboard: :user_defined_metrics_dashboard_paths,
xcode_config: :xcode_project?
}.freeze
......@@ -576,10 +576,10 @@ class Repository
end
cache_method :merge_request_template_names, fallback: []
def metrics_dashboard_paths
def user_defined_metrics_dashboard_paths
Gitlab::Metrics::Dashboard::Finder.find_all_paths_from_source(project)
end
cache_method :metrics_dashboard_paths
cache_method :user_defined_metrics_dashboard_paths, fallback: []
def readme
head_tree&.readme
......
......@@ -7,6 +7,7 @@ module Metrics
module Dashboard
class CustomDashboardService < ::Metrics::Dashboard::BaseService
DASHBOARD_ROOT = ".gitlab/dashboards"
DASHBOARD_EXTENSION = '.yml'
class << self
def valid_params?(params)
......@@ -14,8 +15,7 @@ module Metrics
end
def all_dashboard_paths(project)
file_finder(project)
.list_files_for(DASHBOARD_ROOT)
project.repository.user_defined_metrics_dashboard_paths
.map do |filepath|
{
path: filepath,
......@@ -28,7 +28,7 @@ module Metrics
end
def file_finder(project)
Gitlab::Template::Finders::RepoTemplateFinder.new(project, DASHBOARD_ROOT, '.yml')
Gitlab::Template::Finders::RepoTemplateFinder.new(project, DASHBOARD_ROOT, DASHBOARD_EXTENSION)
end
# Grabs the filepath after the base directory.
......
......@@ -72,17 +72,20 @@ module Gitlab
# display_name: String,
# default: Boolean }]
def find_all_paths(project)
project.repository.metrics_dashboard_paths
user_facing_dashboard_services(project).flat_map do |service|
service.all_dashboard_paths(project)
end
end
# Summary of all known dashboards. Used to populate repo cache.
# 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!
user_facing_dashboard_services(project).flat_map do |service|
service.all_dashboard_paths(project)
end
file_finder(project)
.list_files_for(::Metrics::Dashboard::CustomDashboardService::DASHBOARD_ROOT)
end
private
......@@ -102,6 +105,14 @@ 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
......
......@@ -1926,7 +1926,7 @@ RSpec.describe Repository do
:has_visible_content?,
:issue_template_names,
:merge_request_template_names,
:metrics_dashboard_paths,
:user_defined_metrics_dashboard_paths,
:xcode_project?
])
......
......@@ -104,6 +104,15 @@ RSpec.describe Metrics::Dashboard::CustomDashboardService, :use_clean_rails_memo
}]
)
end
it 'caches repo file list' do
expect(Gitlab::Template::Finders::RepoTemplateFinder).to receive(:new)
.once
.and_call_original
described_class.all_dashboard_paths(project)
described_class.all_dashboard_paths(project)
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