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

format_duration(dur) click to toggle source

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
new(type, filename = nil) click to toggle source

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

fields_for_each_job(jobs) { |fields| ... } click to toggle source

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
flush() click to toggle source

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
print_jobs(jobs) click to toggle source

Prints a table containing all details of jobs

jobs should be an ActiveRecord model or relation.

To consider: allow any Enumerable?

print_summary(jobs, summaries, systems, time_range = nil) click to toggle source

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