Class: GraphiteDashboardApi::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/graphite-dashboard-api/graph.rb

Constant Summary collapse

PROPS =
[:from, :until, :width, :height]

Instance Method Summary collapse

Constructor Details

#initialize(title = nil, &block) ⇒ Graph

Returns a new instance of Graph.



20
21
22
23
24
25
# File 'lib/graphite-dashboard-api/graph.rb', line 20

def initialize(title = nil, &block)
  @title = title
  @targets = []
  @compact_leading = false # this is tweaking stuff
  instance_eval(&block) if block
end

Instance Method Details

#default_titleObject

This is probably an over simplification TODO



32
33
34
# File 'lib/graphite-dashboard-api/graph.rb', line 32

def default_title
  @targets.first
end

#from_hash!(hash) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/graphite-dashboard-api/graph.rb', line 71

def from_hash!(hash)
  @title = hash['title']
  PROPS.each do |k|
    value = hash[k.to_s]
    instance_variable_set("@#{k}".to_sym, value) if value
  end
  if hash['target']
    hash['target'].each do |target|
      @targets << target
    end
  end
  self
end

#leading_entriesObject



46
47
48
49
50
51
52
# File 'lib/graphite-dashboard-api/graph.rb', line 46

def leading_entries
  if @compact_leading
    leading_entries = target_encode
  else
    leading_entries = @targets
  end
end

#render_options(default_options) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/graphite-dashboard-api/graph.rb', line 36

def render_options(default_options)
  opts = []
  PROPS.each do |k|
    v = instance_variable_get "@#{k}".to_sym
    v ||= default_options[k.to_s]
    opts << "#{k.to_s}=#{v}" if v
  end
  opts.join('&')
end

#target_encodeObject



54
55
56
57
58
# File 'lib/graphite-dashboard-api/graph.rb', line 54

def target_encode
  @targets.map do |target|
    'target=' + target
  end.join('&')
end

#to_hashObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/graphite-dashboard-api/graph.rb', line 60

def to_hash
  hash = {}
  hash['title'] = @title if @title
  PROPS.each do |k|
    v = instance_variable_get "@#{k}".to_sym
    hash[k.to_s] = v if v
  end
  hash['target'] = @targets
  hash
end

#url(default_options) ⇒ Object



27
28
29
# File 'lib/graphite-dashboard-api/graph.rb', line 27

def url(default_options)
  '/render?' + render_options(default_options) + '&' + target_encode + "&title=#{@title || default_title}"
end