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

Version Path
foreman-tasks-2.0.3 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-3.0.0 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-2.0.2 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-1.1.3 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-1.1.2 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-2.0.1 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-2.0.0 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-1.2.0 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-1.1.1 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-1.1.0 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.17.6 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-1.0.1 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-1.0.0 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.17.5 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.17.4 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.17.3 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.17.2 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.16.3 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.17.1 app/services/foreman_tasks/dashboard_table_filter.rb
foreman-tasks-0.15.11 app/services/foreman_tasks/dashboard_table_filter.rb