From 05762e1f580213e710c0362724f7a29d4149c445 Mon Sep 17 00:00:00 2001
From: Rajendra Kadam <rkadam@gitlab.com>
Date: Mon, 7 Aug 2023 10:29:04 +0530
Subject: [PATCH] Delete importer and prometheus metrics importer

Delete related usages of class

Delete related specs
---
 lib/gitlab/metrics/dashboard/importer.rb      | 41 --------
 .../dashboard/importers/prometheus_metrics.rb | 76 ---------------
 .../gitlab/metrics/dashboard/importer_spec.rb | 55 -----------
 .../importers/prometheus_metrics_spec.rb      | 97 -------------------
 4 files changed, 269 deletions(-)
 delete mode 100644 lib/gitlab/metrics/dashboard/importer.rb
 delete mode 100644 lib/gitlab/metrics/dashboard/importers/prometheus_metrics.rb
 delete mode 100644 spec/lib/gitlab/metrics/dashboard/importer_spec.rb
 delete mode 100644 spec/lib/gitlab/metrics/dashboard/importers/prometheus_metrics_spec.rb

diff --git a/lib/gitlab/metrics/dashboard/importer.rb b/lib/gitlab/metrics/dashboard/importer.rb
deleted file mode 100644
index ca8356506489c6..00000000000000
--- a/lib/gitlab/metrics/dashboard/importer.rb
+++ /dev/null
@@ -1,41 +0,0 @@
-# frozen_string_literal: true
-
-module Gitlab
-  module Metrics
-    module Dashboard
-      class Importer
-        def initialize(dashboard_path, project)
-          @dashboard_path = dashboard_path.to_s
-          @project = project
-        end
-
-        def execute
-          return false unless Dashboard::Validator.validate(dashboard_hash, project: project, dashboard_path: dashboard_path)
-
-          Dashboard::Importers::PrometheusMetrics.new(dashboard_hash, project: project, dashboard_path: dashboard_path).execute
-        rescue Gitlab::Config::Loader::FormatError
-          false
-        end
-
-        def execute!
-          Dashboard::Validator.validate!(dashboard_hash, project: project, dashboard_path: dashboard_path)
-
-          Dashboard::Importers::PrometheusMetrics.new(dashboard_hash, project: project, dashboard_path: dashboard_path).execute!
-        end
-
-        private
-
-        attr_accessor :dashboard_path, :project
-
-        def dashboard_hash
-          @dashboard_hash ||= begin
-            raw_dashboard = Dashboard::RepoDashboardFinder.read_dashboard(project, dashboard_path)
-            return unless raw_dashboard.present?
-
-            ::Gitlab::Config::Loader::Yaml.new(raw_dashboard).load_raw!
-          end
-        end
-      end
-    end
-  end
-end
diff --git a/lib/gitlab/metrics/dashboard/importers/prometheus_metrics.rb b/lib/gitlab/metrics/dashboard/importers/prometheus_metrics.rb
deleted file mode 100644
index 531e4079632a49..00000000000000
--- a/lib/gitlab/metrics/dashboard/importers/prometheus_metrics.rb
+++ /dev/null
@@ -1,76 +0,0 @@
-# frozen_string_literal: true
-
-module Gitlab
-  module Metrics
-    module Dashboard
-      module Importers
-        class PrometheusMetrics
-          ALLOWED_ATTRIBUTES = %i(title query y_label unit legend group dashboard_path).freeze
-
-          # Takes a JSON schema validated dashboard hash and
-          # imports metrics to database
-          def initialize(dashboard_hash, project:, dashboard_path:)
-            @dashboard_hash = dashboard_hash
-            @project = project
-            @dashboard_path = dashboard_path
-            @affected_environment_ids = []
-          end
-
-          def execute
-            import
-          rescue ActiveRecord::RecordInvalid, Dashboard::Transformers::Errors::BaseError
-            false
-          end
-
-          def execute!
-            import
-          end
-
-          private
-
-          attr_reader :dashboard_hash, :project, :dashboard_path
-
-          def import
-            delete_stale_metrics
-            create_or_update_metrics
-          end
-
-          # rubocop: disable CodeReuse/ActiveRecord
-          def create_or_update_metrics
-            # TODO: use upsert and worker for callbacks?
-
-            affected_metric_ids = []
-            prometheus_metrics_attributes.each do |attributes|
-              prometheus_metric = PrometheusMetric.find_or_initialize_by(attributes.slice(:dashboard_path, :identifier, :project))
-              prometheus_metric.update!(attributes.slice(*ALLOWED_ATTRIBUTES))
-
-              affected_metric_ids << prometheus_metric.id
-            end
-          end
-          # rubocop: enable CodeReuse/ActiveRecord
-
-          def delete_stale_metrics
-            identifiers_from_yml = prometheus_metrics_attributes.map { |metric_attributes| metric_attributes[:identifier] }
-
-            stale_metrics = PrometheusMetric.for_project(project)
-              .for_dashboard_path(dashboard_path)
-              .for_group(Enums::PrometheusMetric.groups[:custom])
-              .not_identifier(identifiers_from_yml)
-
-            return unless stale_metrics.exists?
-
-            stale_metrics.each_batch { |batch| batch.delete_all }
-          end
-
-          def prometheus_metrics_attributes
-            @prometheus_metrics_attributes ||= Dashboard::Transformers::Yml::V1::PrometheusMetrics.new(
-              dashboard_hash,
-                project: project,
-                dashboard_path: dashboard_path
-            ).execute
-          end
-        end
-      end
-    end
-  end
-end
diff --git a/spec/lib/gitlab/metrics/dashboard/importer_spec.rb b/spec/lib/gitlab/metrics/dashboard/importer_spec.rb
deleted file mode 100644
index 8b705395a2c257..00000000000000
--- a/spec/lib/gitlab/metrics/dashboard/importer_spec.rb
+++ /dev/null
@@ -1,55 +0,0 @@
-# frozen_string_literal: true
-
-require 'spec_helper'
-
-RSpec.describe Gitlab::Metrics::Dashboard::Importer do
-  include MetricsDashboardHelpers
-
-  let_it_be(:dashboard_path) { '.gitlab/dashboards/sample_dashboard.yml' }
-  let_it_be(:project) { create(:project) }
-
-  before do
-    allow(subject).to receive(:dashboard_hash).and_return(dashboard_hash)
-  end
-
-  subject { described_class.new(dashboard_path, project) }
-
-  describe '.execute' do
-    context 'valid dashboard hash' do
-      let(:dashboard_hash) { load_sample_dashboard }
-
-      it 'imports metrics to database' do
-        expect { subject.execute }
-          .to change { PrometheusMetric.count }.from(0).to(3)
-      end
-    end
-
-    context 'invalid dashboard hash' do
-      let(:dashboard_hash) { {} }
-
-      it 'returns false' do
-        expect(subject.execute).to be(false)
-      end
-    end
-  end
-
-  describe '.execute!' do
-    context 'valid dashboard hash' do
-      let(:dashboard_hash) { load_sample_dashboard }
-
-      it 'imports metrics to database' do
-        expect { subject.execute }
-          .to change { PrometheusMetric.count }.from(0).to(3)
-      end
-    end
-
-    context 'invalid dashboard hash' do
-      let(:dashboard_hash) { {} }
-
-      it 'raises error' do
-        expect { subject.execute! }.to raise_error(Gitlab::Metrics::Dashboard::Validator::Errors::SchemaValidationError,
-          'root is missing required keys: dashboard, panel_groups')
-      end
-    end
-  end
-end
diff --git a/spec/lib/gitlab/metrics/dashboard/importers/prometheus_metrics_spec.rb b/spec/lib/gitlab/metrics/dashboard/importers/prometheus_metrics_spec.rb
deleted file mode 100644
index bc6cd3837584a0..00000000000000
--- a/spec/lib/gitlab/metrics/dashboard/importers/prometheus_metrics_spec.rb
+++ /dev/null
@@ -1,97 +0,0 @@
-# frozen_string_literal: true
-
-require 'spec_helper'
-
-RSpec.describe Gitlab::Metrics::Dashboard::Importers::PrometheusMetrics do
-  include MetricsDashboardHelpers
-
-  describe '#execute' do
-    let(:project) { create(:project) }
-    let(:dashboard_path) { 'path/to/dashboard.yml' }
-    let(:prometheus_adapter) { double('adapter', clear_prometheus_reactive_cache!: nil) }
-
-    subject { described_class.new(dashboard_hash, project: project, dashboard_path: dashboard_path) }
-
-    context 'valid dashboard' do
-      let(:dashboard_hash) { load_sample_dashboard }
-
-      context 'with all new metrics' do
-        it 'creates PrometheusMetrics' do
-          expect { subject.execute }.to change { PrometheusMetric.count }.by(3)
-        end
-      end
-
-      context 'with existing metrics' do
-        let(:existing_metric_attributes) do
-          {
-            project: project,
-            identifier: 'metric_b',
-            title: 'overwrite',
-            y_label: 'overwrite',
-            query: 'overwrite',
-            unit: 'overwrite',
-            legend: 'overwrite',
-            dashboard_path: dashboard_path
-          }
-        end
-
-        let!(:existing_metric) do
-          create(:prometheus_metric, existing_metric_attributes)
-        end
-
-        it 'updates existing PrometheusMetrics' do
-          subject.execute
-
-          expect(existing_metric.reload.attributes.with_indifferent_access).to include({
-            title: 'Super Chart B',
-            y_label: 'y_label',
-            query: 'query',
-            unit: 'unit',
-            legend: 'Legend Label'
-          })
-        end
-
-        it 'creates new PrometheusMetrics' do
-          expect { subject.execute }.to change { PrometheusMetric.count }.by(2)
-        end
-
-        context 'with stale metrics' do
-          let!(:stale_metric) do
-            create(:prometheus_metric,
-              project: project,
-              identifier: 'stale_metric',
-              dashboard_path: dashboard_path,
-              group: 3
-            )
-          end
-
-          it 'updates existing PrometheusMetrics' do
-            subject.execute
-
-            expect(existing_metric.reload.attributes.with_indifferent_access).to include({
-              title: 'Super Chart B',
-              y_label: 'y_label',
-              query: 'query',
-              unit: 'unit',
-              legend: 'Legend Label'
-            })
-          end
-
-          it 'deletes stale metrics' do
-            subject.execute
-
-            expect { stale_metric.reload }.to raise_error(ActiveRecord::RecordNotFound)
-          end
-        end
-      end
-    end
-
-    context 'invalid dashboard' do
-      let(:dashboard_hash) { {} }
-
-      it 'returns false' do
-        expect(subject.execute).to eq(false)
-      end
-    end
-  end
-end
-- 
GitLab