Sha256: 57cb3309c50a066d9c8ce35f774f085444f94598e16994c435e5833e691db994

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 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)
          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
        result = $queue.filter_jobs(@only).reverse

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

        # Get workers status
        @worker_variables = $pool.worker_variables

        # Build paginator
        @paginate = Paginate.new result
        @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.242.5 lib/rest-ftp-daemon/api/dashboard.rb
rest-ftp-daemon-0.242.4 lib/rest-ftp-daemon/api/dashboard.rb