2020-04-28 11:09:29 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module AlertManagement
|
|
|
|
class AlertsFinder
|
2020-10-08 11:08:17 -04:00
|
|
|
# @return [Hash<Symbol,Integer>] Mapping of status id to count
|
|
|
|
# ex) { triggered: 6, ...etc }
|
2020-05-19 20:08:20 -04:00
|
|
|
def self.counts_by_status(current_user, project, params = {})
|
|
|
|
new(current_user, project, params).execute.counts_by_status
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:09:29 -04:00
|
|
|
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
|
2020-12-10 13:10:16 -05:00
|
|
|
collection = by_domain(collection)
|
2020-05-11 11:09:37 -04:00
|
|
|
collection = by_status(collection)
|
2020-05-07 02:09:38 -04:00
|
|
|
collection = by_iid(collection)
|
2020-10-12 14:08:31 -04:00
|
|
|
collection = by_assignee(collection)
|
|
|
|
collection = by_search(collection)
|
|
|
|
|
2020-05-07 02:09:38 -04:00
|
|
|
sort(collection)
|
2020-04-28 11:09:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :current_user, :project, :params
|
|
|
|
|
2020-12-10 13:10:16 -05:00
|
|
|
def by_domain(collection)
|
2020-12-21 10:10:05 -05:00
|
|
|
return collection if params[:iid].present?
|
|
|
|
|
2020-12-10 13:10:16 -05:00
|
|
|
collection.with_operations_alerts
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:09:29 -04:00
|
|
|
def by_iid(collection)
|
|
|
|
return collection unless params[:iid]
|
|
|
|
|
|
|
|
collection.for_iid(params[:iid])
|
|
|
|
end
|
|
|
|
|
2020-05-11 11:09:37 -04:00
|
|
|
def by_status(collection)
|
2020-10-08 11:08:17 -04:00
|
|
|
values = AlertManagement::Alert.status_names & Array(params[:status])
|
2020-05-11 11:09:37 -04:00
|
|
|
|
2021-08-18 02:11:01 -04:00
|
|
|
values.present? ? collection.with_status(values) : collection
|
2020-05-11 11:09:37 -04:00
|
|
|
end
|
|
|
|
|
2020-05-18 05:08:12 -04:00
|
|
|
def by_search(collection)
|
|
|
|
params[:search].present? ? collection.search(params[:search]) : collection
|
|
|
|
end
|
|
|
|
|
2020-05-07 02:09:38 -04:00
|
|
|
def sort(collection)
|
|
|
|
params[:sort] ? collection.sort_by_attribute(params[:sort]) : collection
|
|
|
|
end
|
|
|
|
|
2020-10-12 14:08:31 -04:00
|
|
|
def by_assignee(collection)
|
|
|
|
params[:assignee_username].present? ? collection.for_assignee_username(params[:assignee_username]) : collection
|
|
|
|
end
|
|
|
|
|
2020-04-28 11:09:29 -04:00
|
|
|
def authorized?
|
2020-05-07 17:09:26 -04:00
|
|
|
Ability.allowed?(current_user, :read_alert_management_alert, project)
|
2020-04-28 11:09:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|