class Chimp::ChimpDaemon::DisplayServlet

DisplayServlet

Public Instance Methods

do_GET(req, resp) click to toggle source

do_GET

# File lib/right_chimp/daemon/chimp_daemon.rb, line 561
def do_GET(req, resp)
  #
  # First determine the path to the files to serve
  #
  if ENV['CHIMP_TEST'] != 'TRUE'
    template_path = File.expand_path('../templates', __dir__)
  else
    template_path = 'lib/right_chimp/templates'
  end

  #
  # Check if we are asked for stats
  #
  if req.request_uri.path =~ /stats$/
    queue = ChimpQueue.instance
    stats = ""
    stats << "running: #{queue.get_jobs_by_status(:running).size} / "
    stats << "waiting: #{queue.get_jobs_by_status(:none).size} / "
    stats << "failed: #{queue.get_jobs_by_status(:error).size} / "
    stats << "done: #{queue.get_jobs_by_status(:done).size} / "
    stats << "processing: #{ChimpDaemon.instance.proc_counter} / "
    stats << "\n"

    resp.body = stats

    raise WEBrick::HTTPStatus::OK
  end

  if req.request_uri.path =~ /stats\.json$/
    # instance the queue
    queue = ChimpQueue.instance

    stats_hash = {"running" => queue.get_jobs_by_status(:running).size,
                  "waiting" => queue.get_jobs_by_status(:none).size,
                  "failed" => queue.get_jobs_by_status(:error).size,
                  "done" => queue.get_jobs_by_status(:done).size,
                  "processing" => ChimpDaemon.instance.proc_counter,
                  "holding" => queue.get_jobs_by_status(:holding).size
                }

    resp.body = JSON.generate(stats_hash)

    raise WEBrick::HTTPStatus::OK
  end

  if req.request_uri.path =~ /jobs\.json$/
    #instance the queue
    queue = ChimpQueue.instance

    job_types = [ :running, :error, :done ]

    jobs = {}

    job_types.each do |type|
      jobs[type] = queue.get_jobs_by_status(type).map do |job|
        { :id => job.job_id,
          :uuid => job.job_uuid,
          :server => job.server.name,
          :script => job.info,
          :audit_entry_url => job.audit_entry_url
        }
      end
    end

    resp.body = jobs.to_json

    raise WEBrick::HTTPStatus::OK
  end

  if req.request_uri.path =~ /jobs\.json\/id\/\d+$/

    job_id = File.basename(req.request_uri.path)
    queue = ChimpQueue.instance

    res = queue.get_job(job_id)

    case res
    when ExecRightScript

      result = {}
      result[:id] = job_id
      result[:uuid] = res.job_uuid
      result[:status] = res.status
      result[:server] = res.server.name
      result[:script] = res.info
      result[:audit_entry_url] = res.audit_entry_url

      resp.body = result.to_json
    end

    raise WEBrick::HTTPStatus::OK

  end
  #
  # Attempt to return just 1 job_UUID data
  #
  if req.request_uri.path =~ /jobs\.json\/uuid\/*\w{6}$/

    uuid = File.basename(req.request_uri.path)
    # instance the queue
    queue = ChimpQueue.instance

    res = queue.get_jobs_by_uuid(uuid)

    jobs = {}

    res.each_with_index do |r, i|
      jobs[i] = { id: r.job_id,
                  uuid: r.job_uuid,
                  status: r.status,
                  server: r.server.name,
                  script: r.info,
                  audit_entry_url: r.audit_entry_url
                }
    end

    resp.body = jobs.to_json

    raise WEBrick::HTTPStatus::OK
  end

  #
  # Check for static CSS files and serve them
  #
  if req.request_uri.path =~ /\.(css|js)$/
    filename = req.request_uri.path.split('/').last
    resp.body = File.read(File.join(template_path, filename))
    raise WEBrick::HTTPStatus::OK
  else

    #
    # Otherwise process ERB template
    #
    job_filter = self.get_verb(req) || "running"

    if not @template
      @template = ERB.new(File.read(File.join(template_path, "all_jobs.erb")), nil, ">")
    end

    queue = ChimpQueue.instance
    jobs = queue.get_jobs
    group_name = nil

    if job_filter == "group"
      group_name = req.request_uri.path.split('/')[-1]
      g = ChimpQueue[group_name.to_sym]
      jobs = g.get_jobs if g
    end

    count_jobs_running = queue.get_jobs_by_status(:running).size
    count_jobs_queued  = queue.get_jobs_by_status(:none).size
    count_jobs_holding  = queue.get_jobs_by_status(:holding).size
    count_jobs_failed  = queue.get_jobs_by_status(:error).size
    count_jobs_done    = queue.get_jobs_by_status(:done).size
    count_jobs_processing = queue.get_jobs_by_status(:processing).size

    resp.body = @template.result(binding)
    raise WEBrick::HTTPStatus::OK
  end
end