debian-mirror-gitlab/app/finders/alert_management/alerts_finder.rb

71 lines
1.9 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
module AlertManagement
class AlertsFinder
2021-01-03 14:25:43 +05:30
# @return [Hash<Symbol,Integer>] Mapping of status id to count
# ex) { triggered: 6, ...etc }
2020-05-24 23:13:21 +05:30
def self.counts_by_status(current_user, project, params = {})
new(current_user, project, params).execute.counts_by_status
end
def initialize(current_user, project, params)
@current_user = current_user
@project = project
@params = params
end
def execute
return AlertManagement::Alert.none unless authorized?
collection = project.alert_management_alerts
2021-02-22 17:27:13 +05:30
collection = by_domain(collection)
2020-05-24 23:13:21 +05:30
collection = by_status(collection)
collection = by_iid(collection)
2021-01-03 14:25:43 +05:30
collection = by_assignee(collection)
collection = by_search(collection)
2020-05-24 23:13:21 +05:30
sort(collection)
end
private
attr_reader :current_user, :project, :params
2021-02-22 17:27:13 +05:30
def by_domain(collection)
2021-03-08 18:12:59 +05:30
return collection if params[:iid].present?
2021-02-22 17:27:13 +05:30
collection.with_operations_alerts
end
2020-05-24 23:13:21 +05:30
def by_iid(collection)
return collection unless params[:iid]
collection.for_iid(params[:iid])
end
def by_status(collection)
2021-01-03 14:25:43 +05:30
values = AlertManagement::Alert.status_names & Array(params[:status])
2020-05-24 23:13:21 +05:30
values.present? ? collection.for_status(values) : collection
end
def by_search(collection)
params[:search].present? ? collection.search(params[:search]) : collection
end
def sort(collection)
params[:sort] ? collection.sort_by_attribute(params[:sort]) : collection
end
2021-01-03 14:25:43 +05:30
def by_assignee(collection)
params[:assignee_username].present? ? collection.for_assignee_username(params[:assignee_username]) : collection
end
2020-05-24 23:13:21 +05:30
def authorized?
Ability.allowed?(current_user, :read_alert_management_alert, project)
end
end
end
2021-02-22 17:27:13 +05:30
2021-06-08 01:23:25 +05:30
AlertManagement::AlertsFinder.prepend_mod_with('AlertManagement::AlertsFinder')