Sha256: 7cc4da650394643abb2bbb8f7c3a56395ea3a9867d9474c87e6b7091c8f1494c
Contents?: true
Size: 1.46 KB
Versions: 28
Compression:
Stored size: 1.46 KB
Contents
module ForemanTasks # narrows the scope for the tasks table based on params coming from tasks dashboard # # Supported filters: # # * :result # * :state # * :time_horizon - expected format of Hxy, where the xy is the time horizon in hours we're interested in # :time_mode can be set to 'recent' to filter the recent tasks, or 'older' (default) to filter earlier ones class DashboardTableFilter def initialize(scope, params) @scope = scope @params = params end def scope @new_scope = @scope scope_by(:result) scope_by(:state) scope_by_time @new_scope end private def scope_by(field) @new_scope = @new_scope.where(field => @params[field]) if @params[field].present? end def scope_by_time return if @params[:time_horizon].blank? hours = if @params[:time_horizon].casecmp('week') == 0 24 * 7 else @params[:time_horizon][/\AH(\d{1,2})$/i, 1] end unless hours raise Foreman::Exception, 'Unexpected format of time: should be in form of "H24" or equal to "week"' end timestamp = Time.now.utc - hours.to_i.hours case @params[:time_mode] when 'recent' operator = '>' else operator = '<' search_suffix = 'OR state_updated_at IS NULL' end @new_scope = @new_scope.where("state_updated_at #{operator} ? #{search_suffix}", timestamp) end end end
Version data entries
28 entries across 28 versions & 1 rubygems