module Refinery #:nodoc:
# The StatsServer class provides a build in web server that provides
# a view into the refinery statistics.
class StatsServer
include Refinery::Loggable
# Run the stats server.
def run
begin
Ramaze::Log.loggers.clear # supress all Ramaze logging
Ramaze.start # start the Ramaze server on port 7000
rescue NameError
self.logger.warn "Install Ramaze to enable the stats server"
end
end
if const_defined?(:Ramaze)
class MainController < ::Ramaze::Controller #:nodoc:
map '/'
def index
%(
Refinery Stats
Refinery Stats
Runtime Averages
#{avg_run_time}
Last 5 Errors
#{errors_table}
Last 5 Completed Jobs
#{completed_jobs_table}
)
end
private
def db
Sequel.connect("sqlite://stats.db")
end
def avg_run_time
rows = db[:completed_jobs].group(:host, :pid).select(:host, :pid, :AVG.sql_function(:run_time)).map do |record|
%(
#{record[:host]} |
#{record[:pid]} |
#{sprintf("%.6f", record[:"AVG(`run_time`)"])} |
)
end.join
%(
Host |
PID |
Avg Run Time |
#{rows}
)
end
def completed_jobs_table
jobs_list = db[:completed_jobs].limit(5).map do |record|
%Q(
#{record[:host]} |
#{record[:pid]} |
#{record[:run_time]} |
)
end
%Q(
Host |
PID |
Run Time |
#{jobs_list.join}
)
end
def errors_table
errors = db[:errors].limit(5).map do |record|
%(
#{record[:host]} |
#{record[:pid]} |
#{record[:error_class]} |
#{record[:error_message]} |
)
end
%(
Host |
PID |
Error Class |
Error Message |
#{errors.join}
)
end
end
end
end
end