Class: Impulse::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/impulse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug = false) ⇒ Graph

Returns a new instance of Graph.



5
6
7
# File 'lib/impulse.rb', line 5

def initialize(debug = false)
  @debug = debug
end

Instance Attribute Details

#debugObject (readonly)

Returns the value of attribute debug.



3
4
5
# File 'lib/impulse.rb', line 3

def debug
  @debug
end

Instance Method Details

#draw(name, time_scale = :day, params = {}, extra_params = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/impulse.rb', line 17

def draw(name, time_scale = :day, params = {}, extra_params = nil)
  params = { 
    :filename => 'impulse.png',
    :rrd_path => '.',
    :output_path => '.', 
    :height => 200, 
    :width => 600, 
    :title => 'Impulse Graph', 
    :vertical => '',
    :legend => '',
    :thickness => 1,
    :sort => false
  }.merge(params)
  
  defaults = "-E -W 'Generated at #{Time.at(Time.now)}' -e now -h #{params[:height]} -w #{params[:width]}"
  cmd = ["rrdtool graph #{params[:output_path]}/#{params[:filename]} -s -#{seconds_for(time_scale)} #{defaults} -t '#{params[:title]}' -v '#{params[:vertical]}'"]
   
  if name.is_a?(Hash)
    max_legend_length = name.keys.size > 1 ? name.keys.max {|a,b| a.length <=> b.length }.length : name.keys.first.length
          
    c = 0
    
    if params[:sort]
      name.keys.sort.each do |k|
        v = name[k]
        legend = "%-#{max_legend_length}s" % k
        v = { :color => '#FF0000', :data => :data }.merge(v)
        cmd << "DEF:d#{c}=#{params[:rrd_path]}/#{v[:name]}.rrd:#{v[:data].to_s}:MAX"
        cmd << "LINE#{params[:thickness]}:d#{c}#{v[:color]}:'#{legend}' GPRINT:d#{c}:LAST:\"Last\\:%8.0lf\" GPRINT:d#{c}:MIN:\"	Min\\:%8.0lf\" GPRINT:d#{c}:AVERAGE:\"	Avg\\:%8.0lf\" GPRINT:d#{c}:MAX:\"	Max\\:%8.0lf\\n\""
        c += 1
      end
    else
      name.each do |k,v|
        legend = "%-#{max_legend_length}s" % k
        v = { :color => '#FF0000', :data => :data }.merge(v)
        cmd << "DEF:d#{c}=#{params[:rrd_path]}/#{v[:name]}.rrd:#{v[:data].to_s}:MAX"
        cmd << "LINE#{params[:thickness]}:d#{c}#{v[:color]}:'#{legend}' GPRINT:d#{c}:LAST:\"Last\\:%8.0lf\" GPRINT:d#{c}:MIN:\"	Min\\:%8.0lf\" GPRINT:d#{c}:AVERAGE:\"	Avg\\:%8.0lf\" GPRINT:d#{c}:MAX:\"	Max\\:%8.0lf\\n\""
        c += 1
      end
    end

  else
    cmd << "DEF:average=#{params[:rrd_path]}/#{name}.rrd:data:MAX"
    cmd << "LINE#{params[:thickness]}:average#FF0000:'#{params[:legend]}' GPRINT:average:LAST:\"Last\\:%8.0lf\" GPRINT:average:MIN:\"	Min\\:%8.0lf\" GPRINT:average:AVERAGE:\"	Avg\\:%8.0lf\" GPRINT:average:MAX:\"	Max\\:%8.0lf\\n\""
  end

  cmd << extra_params unless extra_params.nil?
  system cmd.join(" ")
end

#push(name, datas, params = {}) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/impulse.rb', line 9

def push(name, datas, params = {})
  params = { 
    :rrd_path => '.',
  }.merge(params)
  create_or_find_rrd(name, datas, params)
  push_to_rrd(name, datas, params)
end