class RRD::Graph

Constants

DEF_OPTIONS
GRAPH_FLAGS
GRAPH_OPTIONS

Attributes

definitions[RW]
output[RW]
parameters[RW]
printables[RW]

Public Class Methods

new(output, parameters = {}) click to toggle source
# File lib/rrd/graph.rb, line 11
def initialize(output, parameters = {})
  @output = output
  
  @parameters = {:start => Time.now - 1.day, :end => Time.now, :title => ""}.merge parameters
  @parameters[:start] = @parameters[:start].to_i
  @parameters[:end] = @parameters[:end].to_i
  
  @definitions = []
  @printables = []
end

Public Instance Methods

area(rrd_file, options) click to toggle source
# File lib/rrd/graph.rb, line 87
def area(rrd_file, options)
  dataset = options.reject {|name, value| GRAPH_OPTIONS.include?(name.to_sym)}
  name = "#{dataset.keys.first}_#{dataset.values.first.to_s}"
  options = {:data => name}.merge(options)
  
  definition = for_rrd_data name, {:from => rrd_file}.merge(dataset)
  printable = draw_area options
  [definition, printable]
end
draw_area(options) click to toggle source
# File lib/rrd/graph.rb, line 73
def draw_area(options)
  draw("AREA", options)
end
draw_line(options) click to toggle source
# File lib/rrd/graph.rb, line 67
def draw_line(options)
  options = {:width => 1}.merge options
  type = "LINE#{options[:width]}"
  draw(type, options)
end
for_rrd_data(data_name, options) click to toggle source
# File lib/rrd/graph.rb, line 22
def for_rrd_data(data_name, options)
  dataset = options.reject {|name, value| DEF_OPTIONS.include?(name.to_sym)}
  start_at = dataset[:start] && dataset.delete(:start)
  end_at   = dataset[:end] && dataset.delete(:end)
  step     = dataset[:step] && dataset.delete(:step)

  definition = "DEF:#{data_name}=#{options[:from]}:#{dataset.keys.first}:#{dataset.values.first.to_s.upcase}"
  definition += ":step=#{step.to_i}" unless step.nil?
  definition += ":start=#{start_at.to_i}" unless start_at.nil?
  definition += ":end=#{end_at.to_i}" unless end_at.nil?

  definitions << definition
  definition
end
line(rrd_file, options) click to toggle source
# File lib/rrd/graph.rb, line 77
def line(rrd_file, options)
  dataset = options.reject {|name, value| GRAPH_OPTIONS.include?(name.to_sym)}
  name = "#{dataset.keys.first}_#{dataset.values.first.to_s}"
  options = {:data => name}.merge(options)
  
  definition = for_rrd_data name, {:from => rrd_file}.merge(dataset)
  printable = draw_line options
  [definition, printable]
end
print_comment(comment) click to toggle source
print_value(value_name, options) click to toggle source
save() click to toggle source
# File lib/rrd/graph.rb, line 97
def save    
  Wrapper.graph(*generate_args)
end
shift(options) click to toggle source
# File lib/rrd/graph.rb, line 55
def shift(options)
  definition = "SHIFT:#{options.keys.first}:#{options.values.first.to_i}"
  definitions << definition
  definition
end
using_calculated_data(data_name, options) click to toggle source
# File lib/rrd/graph.rb, line 37
def using_calculated_data(data_name, options)
  definition = "CDEF:#{data_name}=#{options[:calc]}"
  definitions << definition
  definition
end
using_value(value_name, options) click to toggle source
# File lib/rrd/graph.rb, line 43
def using_value(value_name, options)
  definition = "VDEF:#{value_name}=#{options[:calc]}"
  definitions << definition
  definition
end

Private Instance Methods

draw(type, options) click to toggle source
# File lib/rrd/graph.rb, line 109
def draw(type, options)
  printable = "#{type}:#{options[:data]}#{options[:color]}"

  if options[:label]
    options[:label] = options[:label].gsub(/^:/, "\\:").gsub(/([^\]):/, "\\1\\:") # Escape all non-escaped ':'
    printable += ":#{options[:label]}"
  end

  if options[:extra]
    printable += ":#{options[:extra]}"
  end

  printables << printable
  printable
end
generate_args() click to toggle source
# File lib/rrd/graph.rb, line 102
def generate_args
  args = [output]
  args += RRD.to_line_parameters(parameters, GRAPH_FLAGS)
  args += definitions
  args += printables
end