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 ...@@ -43,7 +43,7 @@ class Repository
gitlab_ci_yml branch_names tag_names branch_count gitlab_ci_yml branch_names tag_names branch_count
tag_count avatar exists? root_ref merged_branch_names tag_count avatar exists? root_ref merged_branch_names
has_visible_content? issue_template_names merge_request_template_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 # Methods that use cache_method but only memoize the value
MEMOIZED_CACHED_METHODS = %i(license).freeze MEMOIZED_CACHED_METHODS = %i(license).freeze
...@@ -61,7 +61,7 @@ class Repository ...@@ -61,7 +61,7 @@ class Repository
avatar: :avatar, avatar: :avatar,
issue_template: :issue_template_names, issue_template: :issue_template_names,
merge_request_template: :merge_request_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? xcode_config: :xcode_project?
}.freeze }.freeze
...@@ -576,10 +576,10 @@ class Repository ...@@ -576,10 +576,10 @@ class Repository
end end
cache_method :merge_request_template_names, fallback: [] 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) Gitlab::Metrics::Dashboard::Finder.find_all_paths_from_source(project)
end end
cache_method :metrics_dashboard_paths cache_method :user_defined_metrics_dashboard_paths, fallback: []
def readme def readme
head_tree&.readme head_tree&.readme
......
...@@ -7,6 +7,7 @@ module Metrics ...@@ -7,6 +7,7 @@ module Metrics
module Dashboard module Dashboard
class CustomDashboardService < ::Metrics::Dashboard::BaseService class CustomDashboardService < ::Metrics::Dashboard::BaseService
DASHBOARD_ROOT = ".gitlab/dashboards" DASHBOARD_ROOT = ".gitlab/dashboards"
DASHBOARD_EXTENSION = '.yml'
class << self class << self
def valid_params?(params) def valid_params?(params)
...@@ -14,8 +15,7 @@ module Metrics ...@@ -14,8 +15,7 @@ module Metrics
end end
def all_dashboard_paths(project) def all_dashboard_paths(project)
file_finder(project) project.repository.user_defined_metrics_dashboard_paths
.list_files_for(DASHBOARD_ROOT)
.map do |filepath| .map do |filepath|
{ {
path: filepath, path: filepath,
...@@ -28,7 +28,7 @@ module Metrics ...@@ -28,7 +28,7 @@ module Metrics
end end
def file_finder(project) def file_finder(project)
Gitlab::Template::Finders::RepoTemplateFinder.new(project, DASHBOARD_ROOT, '.yml') Gitlab::Template::Finders::RepoTemplateFinder.new(project, DASHBOARD_ROOT, DASHBOARD_EXTENSION)
end end
# Grabs the filepath after the base directory. # Grabs the filepath after the base directory.
......
...@@ -72,17 +72,20 @@ module Gitlab ...@@ -72,17 +72,20 @@ module Gitlab
# display_name: String, # display_name: String,
# default: Boolean }] # default: Boolean }]
def find_all_paths(project) 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 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. # Prefer #find_all_paths.
# @return [Array] ex) ['.gitlab/dashboards/dashboard1.yml']
def find_all_paths_from_source(project) def find_all_paths_from_source(project)
Gitlab::Metrics::Dashboard::Cache.delete_all! Gitlab::Metrics::Dashboard::Cache.delete_all!
user_facing_dashboard_services(project).flat_map do |service| file_finder(project)
service.all_dashboard_paths(project) .list_files_for(::Metrics::Dashboard::CustomDashboardService::DASHBOARD_ROOT)
end
end end
private private
...@@ -102,6 +105,14 @@ module Gitlab ...@@ -102,6 +105,14 @@ module Gitlab
predefined_dashboard_services predefined_dashboard_services
end 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 def predefined_dashboard_services
::Metrics::Dashboard::PredefinedDashboardService.descendants - PREDEFINED_DASHBOARD_EXCLUSION_LIST ::Metrics::Dashboard::PredefinedDashboardService.descendants - PREDEFINED_DASHBOARD_EXCLUSION_LIST
end end
......
...@@ -1926,7 +1926,7 @@ RSpec.describe Repository do ...@@ -1926,7 +1926,7 @@ RSpec.describe Repository do
:has_visible_content?, :has_visible_content?,
:issue_template_names, :issue_template_names,
:merge_request_template_names, :merge_request_template_names,
:metrics_dashboard_paths, :user_defined_metrics_dashboard_paths,
:xcode_project? :xcode_project?
]) ])
......
...@@ -104,6 +104,15 @@ RSpec.describe Metrics::Dashboard::CustomDashboardService, :use_clean_rails_memo ...@@ -104,6 +104,15 @@ RSpec.describe Metrics::Dashboard::CustomDashboardService, :use_clean_rails_memo
}] }]
) )
end 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
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