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