class Bookie::Formatter
Takes jobs from the database and creates summaries and tables in various output formats.
Constants
- DETAILS_FIELD_LABELS
An array containing the labels for each field in a details table TODO: remove some fields?
- SUMMARY_FIELD_LABELS
An array containing the labels for each field in a summary
Public Class Methods
Formats a duration in a human-readable format
dur
should be an integer representing the number of seconds.
# File lib/bookie/formatter.rb, line 141 def self.format_duration(dur) dur = dur.to_i days = dur / (3600 * 24) dur -= days * (3600 * 24) hours = dur / 3600 dur -= hours * 3600 minutes = dur / 60 dur -= minutes * 60 seconds = dur weeks = days / 7 days = days % 7 "%i week%s, %i day%s, %02i:%02i:%02i" % [weeks, weeks == 1 ? '' : 's', days, days == 1 ? '' : 's', hours, minutes, seconds] end
Creates a new Formatter
object
type
should be a symbol that maps to one of the files in bookie/formatters
.
Examples¶ ↑
config = Bookie::Config.new('config.json') #Uses the spreadsheet formatter from 'bookie/formatters/spreadsheet' formatter = Bookie::Formatter::Formatter.new(config, :spreadsheet)
# File lib/bookie/formatter.rb, line 19 def initialize(type, filename = nil) #Needed for symbol arguments type = type.to_s require "bookie/formatters/#{type}" extend Bookie::Formatters.const_get(type.camelize) self.open(filename) end
Public Instance Methods
For each job, yields an array containing the field values to be used when printing a table of jobs
jobs
should be an ActiveRecord model or relation.
Examples¶ ↑
formatter.fields_for_each_job(jobs) do |fields| Bookie::Formatter::Formatter::DETAILS_FIELD_LABELS.zip(fields) do |label, field| puts "#{label}: #{field}" end end
# File lib/bookie/formatter.rb, line 112 def fields_for_each_job(jobs) jobs.each do |job| memory_stat_type = job.system.system_type.memory_stat_type if memory_stat_type == :unknown memory_stat_type = '' else memory_stat_type = " (#{memory_stat_type})" end yield [ job.user.name, job.user.group.name, job.system.name, #TODO: remove this field? job.system.system_type.name, job.start_time.getlocal.strftime('%Y-%m-%d %H:%M:%S'), job.end_time.getlocal.strftime('%Y-%m-%d %H:%M:%S'), Formatter.format_duration(job.end_time.to_i - job.start_time.to_i), Formatter.format_duration(job.cpu_time), "#{job.memory}kb#{memory_stat_type}", job.command_name, job.exit_code ] end end
Flushes all output
Should always be called after the desired information has been written
# File lib/bookie/formatter.rb, line 94 def flush() do_flush() if self.respond_to?(:do_flush) end
Prints a table containing all details of jobs
jobs
should be an ActiveRecord model or relation.
To consider: allow any Enumerable?
# File lib/bookie/formatter.rb, line 86 def print_jobs(jobs) do_print_jobs(jobs) end
Prints a summary of jobs
and systems
to io
, using cached data from summaries
Use start_time and end_time to filter the jobs by a time range.
It is probably not a good idea to apply any time-based filters to the arguments beforehand.
Both jobs
, summaries
, and systems
should be either models or ActiveRecord::Relation objects.
Returns the summaries for jobs
and systems
# File lib/bookie/formatter.rb, line 57 def print_summary(jobs, summaries, systems, time_range = nil) jobs = jobs.includes(:user, :group, :system, :system_type) jobs_summary = summaries.summary(:jobs => jobs, :range => time_range) num_jobs = jobs_summary[:num_jobs] systems_summary = systems.summary(time_range) cpu_time = jobs_summary[:cpu_time] avail_cpu_time = systems_summary[:avail_cpu_time] memory_time = jobs_summary[:memory_time] avail_memory_time = systems_summary[:avail_memory_time] successful = (num_jobs == 0) ? 0.0 : Float(jobs_summary[:successful]) / num_jobs field_values = [ num_jobs, Formatter.format_duration(cpu_time), '%.4f%%' % (successful * 100), Formatter.format_duration(systems_summary[:avail_cpu_time]), if avail_cpu_time == 0 then '0.0000%' else '%.4f%%' % (Float(cpu_time) / avail_cpu_time * 100) end, "#{Integer(systems_summary[:avail_memory_avg])} kb", if avail_memory_time == 0 then '0.0000%' else '%.4f%%' % (Float(memory_time) / avail_memory_time * 100) end ] do_print_summary(field_values) end