Commit 1254cd91 authored by Ryan Cobb's avatar Ryan Cobb

Add scopes and specs

Adds scopes to avoid CodeReuse and adds more specs
parent 204f4191
...@@ -86,6 +86,7 @@ class Environment < ApplicationRecord ...@@ -86,6 +86,7 @@ class Environment < ApplicationRecord
scope :with_rank, -> do scope :with_rank, -> do
select('environments.*, rank() OVER (PARTITION BY project_id ORDER BY id DESC)') select('environments.*, rank() OVER (PARTITION BY project_id ORDER BY id DESC)')
end end
scope :for_id, -> (id) { where(id: id) }
state_machine :state, initial: :available do state_machine :state, initial: :available do
event :start do event :start do
......
...@@ -36,6 +36,7 @@ class PrometheusAlert < ApplicationRecord ...@@ -36,6 +36,7 @@ class PrometheusAlert < ApplicationRecord
scope :for_metric, -> (metric) { where(prometheus_metric: metric) } scope :for_metric, -> (metric) { where(prometheus_metric: metric) }
scope :for_project, -> (project) { where(project_id: project) } scope :for_project, -> (project) { where(project_id: project) }
scope :for_environment, -> (environment) { where(environment_id: environment) } scope :for_environment, -> (environment) { where(environment_id: environment) }
scope :get_environment_id, -> { select(:environment_id).pluck(:environment_id) }
def self.distinct_projects def self.distinct_projects
sub_query = self.group(:project_id).select(1) sub_query = self.group(:project_id).select(1)
......
...@@ -76,7 +76,7 @@ module Git ...@@ -76,7 +76,7 @@ module Git
def branch_change_hooks def branch_change_hooks
enqueue_process_commit_messages enqueue_process_commit_messages
enqueue_jira_connect_sync_messages enqueue_jira_connect_sync_messages
enqueue_metrics_dashboard_sync if Feature.enabled?(:sync_metrics_dashboards, project) enqueue_metrics_dashboard_sync
end end
def branch_remove_hooks def branch_remove_hooks
...@@ -84,6 +84,9 @@ module Git ...@@ -84,6 +84,9 @@ module Git
end end
def enqueue_metrics_dashboard_sync def enqueue_metrics_dashboard_sync
return unless Feature.enabled?(:sync_metrics_dashboards, project)
return unless default_branch?
::Metrics::Dashboard::SyncDashboardsWorker.perform_async(project.id) ::Metrics::Dashboard::SyncDashboardsWorker.perform_async(project.id)
end end
......
...@@ -13,8 +13,6 @@ module Metrics ...@@ -13,8 +13,6 @@ module Metrics
project = Project.find(project_id) project = Project.find(project_id)
dashboard_paths = ::Gitlab::Metrics::Dashboard::RepoDashboardFinder.list_dashboards(project) dashboard_paths = ::Gitlab::Metrics::Dashboard::RepoDashboardFinder.list_dashboards(project)
return unless dashboard_paths.present?
dashboard_paths.each do |dashboard_path| dashboard_paths.each do |dashboard_path|
::Gitlab::Metrics::Dashboard::Importer.new(dashboard_path, project).execute! ::Gitlab::Metrics::Dashboard::Importer.new(dashboard_path, project).execute!
end end
......
...@@ -48,7 +48,7 @@ module Gitlab ...@@ -48,7 +48,7 @@ module Gitlab
affected_metric_ids << prometheus_metric.id affected_metric_ids << prometheus_metric.id
end end
@affected_environment_ids += find_alerts(affected_metric_ids).pluck(:environment_id) @affected_environment_ids += find_alerts(affected_metric_ids).get_environment_id
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
...@@ -60,22 +60,21 @@ module Gitlab ...@@ -60,22 +60,21 @@ module Gitlab
.for_group(Enums::PrometheusMetric.groups[:custom]) .for_group(Enums::PrometheusMetric.groups[:custom])
.not_identifier(identifiers_from_yml) .not_identifier(identifiers_from_yml)
return unless stale_metrics.present? return unless stale_metrics.exists?
delete_stale_alerts(stale_metrics) delete_stale_alerts(stale_metrics)
stale_metrics.each_batch { |batch| batch.delete_all } stale_metrics.each_batch { |batch| batch.delete_all }
end end
# rubocop: disable CodeReuse/ActiveRecord
def delete_stale_alerts(stale_metrics) def delete_stale_alerts(stale_metrics)
stale_alerts = find_alerts(stale_metrics) stale_alerts = find_alerts(stale_metrics)
return unless stale_alerts.present? affected_environment_ids = stale_alerts.get_environment_id
return unless affected_environment_ids.present?
@affected_environment_ids += stale_alerts.pluck(:environment_id) @affected_environment_ids += affected_environment_ids
stale_alerts.each_batch { |batch| batch.delete_all } stale_alerts.each_batch { |batch| batch.delete_all }
end end
# rubocop: enable CodeReuse/ActiveRecord
def find_alerts(metrics) def find_alerts(metrics)
Projects::Prometheus::AlertsFinder.new(project: project, metric: metrics).execute Projects::Prometheus::AlertsFinder.new(project: project, metric: metrics).execute
...@@ -91,11 +90,10 @@ module Gitlab ...@@ -91,11 +90,10 @@ module Gitlab
end end
end end
# rubocop: disable CodeReuse/ActiveRecord
def update_prometheus_environments def update_prometheus_environments
affected_environments = ::Environment.where(id: @affected_environment_ids.flatten.uniq, project: project) affected_environments = ::Environment.for_id(@affected_environment_ids.flatten.uniq).for_project(project)
return unless affected_environments.present? return unless affected_environments.exists?
affected_environments.each do |affected_environment| affected_environments.each do |affected_environment|
::Clusters::Applications::ScheduleUpdateService.new( ::Clusters::Applications::ScheduleUpdateService.new(
...@@ -104,7 +102,6 @@ module Gitlab ...@@ -104,7 +102,6 @@ module Gitlab
).execute ).execute
end end
end end
# rubocop: enable CodeReuse/ActiveRecord
end end
end end
end end
......
...@@ -429,4 +429,26 @@ RSpec.describe Git::BranchHooksService do ...@@ -429,4 +429,26 @@ RSpec.describe Git::BranchHooksService do
end end
end end
end end
describe 'Metrics dashboard sync' do
context 'with feature flag enabled' do
before do
Feature.enable(:metrics_dashboards_sync)
end
it 'imports metrics to database' do
expect(Metrics::Dashboard::SyncDashboardsWorker).to receive(:perform_async)
service.execute
end
end
context 'with feature flag disabled' do
it 'imports metrics to database' do
expect(Metrics::Dashboard::SyncDashboardsWorker).to receive(:perform_async)
service.execute
end
end
end
end end
...@@ -3,24 +3,23 @@ ...@@ -3,24 +3,23 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Metrics::Dashboard::SyncDashboardsWorker do RSpec.describe Metrics::Dashboard::SyncDashboardsWorker do
include MetricsDashboardHelpers
subject(:worker) { described_class.new } subject(:worker) { described_class.new }
let(:project) { create(:project) } let(:project) { project_with_dashboard(dashboard_path) }
let(:dashboard_paths) { [".gitlab/dashboards/dashboard1.yml", ".gitlab/dashboards/dashboard2.yml"] } let(:dashboard_path) { '.gitlab/dashboards/test.yml' }
describe ".perform" do describe ".perform" do
before do it 'imports metrics' do
expect(::Gitlab::Metrics::Dashboard::RepoDashboardFinder).to receive(:list_dashboards).with(project) expect { worker.perform(project.id) }.to change(PrometheusMetric, :count).by(3)
.and_return(dashboard_paths)
end end
it 'calls importer for each dashboard path' do it 'is idempotent' do
dashboard_paths.each do |dashboard_path| 2.times do
expect(::Gitlab::Metrics::Dashboard::Importer).to receive(:new) worker.perform(project.id)
.with(dashboard_path, project).and_return(double('importer', execute!: true))
end end
worker.perform(project.id) expect(PrometheusMetric.count).to eq(3)
end 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