Sha256: d983277aeb3a782a6ecd03680671cb6afb7f47f4eb2d9e3c4c1df5552a4f48d9

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

module RocketJobMissionControl
  class ActiveProcessesDatatable
    delegate :params, :link_to, :job_path, :job_icon, to: :@view
    delegate :h, to: 'ERB::Util'

    def initialize(view, processes)
      @view = view
      @unfiltered_processes = processes
    end

    def as_json(options = {})
      {
        :draw => params[:draw].to_i,
        :recordsTotal =>  get_raw_records.count,
        :recordsFiltered => get_raw_records.count,
        :data => data
      }
    end

    private

    def data
      processes.map do |worker_name, job, started_at|
        {
          '0' => worker_name_with_icon(worker_name, job),
          '1' => job_name_with_link(job),
          '2' => h(job.description.try(:truncate, 50)),
          '3' => h(duration(started_at)),
          'DT_RowClass' => "card callout callout-running"
        }
      end
    end

    def get_raw_records
      @unfiltered_processes
    end

    def processes
      @processes ||= fetch_processes
    end

    def fetch_processes
      records = get_raw_records
      records = paginate_records(records) unless params[:length].present? && params[:length] == '-1'
      records
    end

    def page
      (params[:start].to_i / per_page) + 1
    end

    def per_page
      params.fetch(:length, 10).to_i
    end

    def paginate_records(records)
      Kaminari.paginate_array(records).page(page).per(per_page)
    end

    def worker_name_with_icon(worker_name, job)
      <<-EOS
        <i class="fa #{job_icon(job)}" style="font-size: 75%" title="#{job.state}"></i>
        #{worker_name}
      EOS
    end

    def job_name_with_link(job)
      <<-EOS
        <a href="#{job_path(job.id)}">
          #{job.class.name}
        </a>
      EOS
    end

    def duration(started_at)
      "#{RocketJob.seconds_as_duration(Time.now - started_at)} ago" if started_at
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rocketjob_mission_control-2.0.0 app/datatables/rocket_job_mission_control/active_processes_datatable.rb
rocketjob_mission_control-2.0.0.rc2 app/datatables/rocket_job_mission_control/active_processes_datatable.rb
rocketjob_mission_control-2.0.0.rc1 app/datatables/rocket_job_mission_control/active_processes_datatable.rb