Welcome to mirror list, hosted at ThFree Co, Russian Federation.

metrics_dashboard_yml.rb « blob_viewer « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cac6b2192d0fcf28e58c525fcd1923fed68db3f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

module BlobViewer
  class MetricsDashboardYml < Base
    include ServerSide
    include Gitlab::Utils::StrongMemoize
    include Auxiliary

    self.partial_name = 'metrics_dashboard_yml'
    self.loading_partial_name = 'metrics_dashboard_yml_loading'
    self.file_types = %i(metrics_dashboard)
    self.binary = false

    def valid?
      errors.blank?
    end

    def errors
      strong_memoize(:errors) do
        prepare!
        parse_blob_data
      end
    end

    private

    def parse_blob_data
      if Feature.enabled?(:metrics_dashboard_exhaustive_validations, project)
        exhaustive_metrics_dashboard_validation
      else
        old_metrics_dashboard_validation
      end
    end

    def old_metrics_dashboard_validation
      yaml = ::Gitlab::Config::Loader::Yaml.new(blob.data).load_raw!
      ::PerformanceMonitoring::PrometheusDashboard.from_json(yaml)
      []
    rescue Gitlab::Config::Loader::FormatError => e
      ["YAML syntax: #{e.message}"]
    rescue ActiveModel::ValidationError => e
      e.model.errors.messages.map { |messages| messages.join(': ') }
    end

    def exhaustive_metrics_dashboard_validation
      yaml = ::Gitlab::Config::Loader::Yaml.new(blob.data).load_raw!
      Gitlab::Metrics::Dashboard::Validator
        .errors(yaml, dashboard_path: blob.path, project: project)
        .map(&:message)
    rescue Gitlab::Config::Loader::FormatError => e
      [e.message]
    end
  end
end