Commit b511d1fa authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'mwaw/211330-add-annotations-finder' into 'master'

Add metrics dashboard annotations finder

See merge request gitlab-org/gitlab!29231
parents c7d5d85e 80183735
# frozen_string_literal: true
module Metrics
module Dashboards
class AnnotationsFinder
def initialize(dashboard:, params:)
@dashboard, @params = dashboard, params
end
def execute
if dashboard.environment
apply_filters_to(annotations_for_environment)
else
Metrics::Dashboard::Annotation.none
end
end
private
attr_reader :dashboard, :params
def apply_filters_to(annotations)
annotations = annotations.after(params[:from]) if params[:from].present?
annotations = annotations.before(params[:to]) if params[:to].present? && valid_timespan_boundaries?
by_dashboard(annotations)
end
def annotations_for_environment
dashboard.environment.metrics_dashboard_annotations
end
def by_dashboard(annotations)
annotations.for_dashboard(dashboard.path)
end
def valid_timespan_boundaries?
params[:from].blank? || params[:to] >= params[:from]
end
end
end
end
......@@ -15,6 +15,11 @@ module Metrics
validate :single_ownership
validate :orphaned_annotation
scope :after, ->(after) { where('starting_at >= ?', after) }
scope :before, ->(before) { where('starting_at <= ?', before) }
scope :for_dashboard, ->(dashboard_path) { where(dashboard_path: dashboard_path) }
private
def single_ownership
......
# frozen_string_literal: true
require 'spec_helper'
describe Metrics::Dashboards::AnnotationsFinder do
describe '#execute' do
subject(:annotations) { described_class.new(dashboard: dashboard, params: params).execute }
let_it_be(:current_user) { create(:user) }
let(:path) { 'config/prometheus/common_metrics.yml' }
let(:params) { {} }
let(:environment) { create(:environment) }
let(:dashboard) { PerformanceMonitoring::PrometheusDashboard.new(path: path, environment: environment) }
context 'there are no annotations records' do
it 'returns empty array' do
expect(annotations).to be_empty
end
end
context 'with annotation records' do
let!(:nine_minutes_old_annotation) { create(:metrics_dashboard_annotation, environment: environment, starting_at: 9.minutes.ago, dashboard_path: path) }
let!(:fifteen_minutes_old_annotation) { create(:metrics_dashboard_annotation, environment: environment, starting_at: 15.minutes.ago, dashboard_path: path) }
let!(:just_created_annotation) { create(:metrics_dashboard_annotation, environment: environment, dashboard_path: path) }
let!(:annotation_for_different_env) { create(:metrics_dashboard_annotation, dashboard_path: path) }
let!(:annotation_for_different_dashboard) { create(:metrics_dashboard_annotation, dashboard_path: '.gitlab/dashboards/test.yml') }
it 'loads annotations' do
expect(annotations).to match_array [fifteen_minutes_old_annotation, nine_minutes_old_annotation, just_created_annotation]
end
context 'when the from filter is present' do
let(:params) do
{
from: 14.minutes.ago
}
end
it 'loads only younger annotations' do
expect(annotations).to match_array [nine_minutes_old_annotation, just_created_annotation]
end
end
context 'when the to filter is present' do
let(:params) do
{
to: 5.minutes.ago
}
end
it 'loads only older annotations' do
expect(annotations).to match_array [fifteen_minutes_old_annotation, nine_minutes_old_annotation]
end
end
context 'when from and to filters are present' do
context 'and to is bigger than from' do
let(:params) do
{
from: 14.minutes.ago,
to: 5.minutes.ago
}
end
it 'loads only annotations assigned to this interval' do
expect(annotations).to match_array [nine_minutes_old_annotation]
end
end
context 'and from is bigger than to' do
let(:params) do
{
to: 14.minutes.ago,
from: 5.minutes.ago
}
end
it 'ignores to parameter and returns annotations starting at from filter' do
expect(annotations).to match_array [just_created_annotation]
end
end
context 'when from or to filters are empty strings' do
let(:params) do
{
from: '',
to: ''
}
end
it 'ignores this parameters' do
expect(annotations).to match_array [fifteen_minutes_old_annotation, nine_minutes_old_annotation, just_created_annotation]
end
end
end
context 'dashboard environment is missing' do
let(:dashboard) { PerformanceMonitoring::PrometheusDashboard.new(path: path, environment: nil) }
it 'returns empty relation', :aggregate_failures do
expect(annotations).to be_kind_of ::ActiveRecord::Relation
expect(annotations).to be_empty
end
end
end
end
end
......@@ -50,4 +50,30 @@ describe Metrics::Dashboard::Annotation do
end
end
end
describe 'scopes' do
let_it_be(:nine_minutes_old_annotation) { create(:metrics_dashboard_annotation, starting_at: 9.minutes.ago) }
let_it_be(:fifteen_minutes_old_annotation) { create(:metrics_dashboard_annotation, starting_at: 15.minutes.ago) }
let_it_be(:just_created_annotation) { create(:metrics_dashboard_annotation) }
describe '#after' do
it 'returns only younger annotations' do
expect(described_class.after(12.minutes.ago)).to match_array [nine_minutes_old_annotation, just_created_annotation]
end
end
describe '#before' do
it 'returns only older annotations' do
expect(described_class.before(5.minutes.ago)).to match_array [fifteen_minutes_old_annotation, nine_minutes_old_annotation]
end
end
describe '#for_dashboard' do
let!(:other_dashboard_annotation) { create(:metrics_dashboard_annotation, dashboard_path: 'other_dashboard.yml') }
it 'returns annotations only for appointed dashboard' do
expect(described_class.for_dashboard('other_dashboard.yml')).to match_array [other_dashboard_annotation]
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