Module: Resque::Metrics::Server

Defined in:
lib/resque/metrics/server.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/resque/metrics/server.rb', line 8

def self.included(base)
  base.class_eval do
    helpers do
      # reads a 'local' template file.
      def local_template(path)
        # Is there a better way to specify alternate template locations with sinatra?
        File.read(File.join(File.dirname(__FILE__), "server/views/#{path}"))
      end

      def metrics_formatted_ms(milliseconds)
        seconds = milliseconds / 1000
        hours = (seconds / 3600).floor
        minutes = (seconds % 3600) / 60
        seconds = seconds % 60
        millis = (milliseconds - (milliseconds / 1000) * 1000)

        str = []
        str << "#{hours} hours" if hours > 0
        str << "#{minutes} min" if minutes > 0
        str << "#{seconds} sec" if seconds > 0
        str << "#{millis} ms" if millis > 0

        str.join(" ")
      end
    end

    get "/metrics" do
      erb local_template("metrics.erb")
    end
  end
end