Sha256: 91b64405e29cc941baffee7f83471892bb8123c6ab3f04e6267ff6a61b238061

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

require "grape"
require "haml"
require "sys/cpu"
require "facter"

module RestFtpDaemon
  module API

    # Offers an HTML dashboard through the Grape API (hum...)
    class Dashbaord < Grape::API

      ### HELPERS

      helpers do
        def logger
          Root.logger
        end

        def render name, values={}
          template = File.read("#{APP_LIBS}/views/#{name}.haml")

          haml_engine = Haml::Engine.new(template, encoding: Encoding::UTF_8)
              #:encoding => Encoding::ASCII_8BIT
          haml_engine.render(binding, values)
        end
      end


      ### Common request logging

      before do
        log_info "HTTP #{request.request_method} #{request.fullpath}", params
      end


      ### DASHBOARD

      get "/" do
        # Initialize Facter
        Facter.loadfacts

        # Detect QS filters
        @only = params["only"].to_s

        # Get jobs for this view, order jobs by their weights
        jobs_with_status = $queue.jobs_with_status(@only).reverse

        # Provide queue only if no filtering set
        if @only.empty?
          @jobs_queued = $queue.jobs_queued
        else
          @jobs_queued = []
        end

        # Get workers status
        @worker_variables = $pool.worker_variables

        # Build paginator
        @paginate = Paginate.new jobs_with_status
        @paginate.only = params["only"]
        @paginate.page = params["page"]
        @paginate.all = params.keys.include? "all"

        # Compile haml template
        output = render :dashboard

        # Send response
        env["api.format"] = :html
        format "html"
        status 200
        content_type "text/html"
        body output
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rest-ftp-daemon-0.245.1 lib/rest-ftp-daemon/api/dashboard.rb
rest-ftp-daemon-0.245 lib/rest-ftp-daemon/api/dashboard.rb