Sha256: 0f1ceb5e8e7868c5af641c4a5980d9fd4adbffa96b78561bff1e1d27dbf0054d

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 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 = @params[:time_horizon][/\AH(\d{1,2})$/i, 1]
      unless hours
        raise Foreman::Exception, 'Unexpected format of time: should be in form of "H24"'
      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

5 entries across 5 versions & 1 rubygems

Version Path
foreman-tasks-0.15.6 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.15.5 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.15.4 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.15.3 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.15.2 app/services/foreman_tasks/dashboard_table_filter.rb