Class: GraphiteDashboardApi::Dashboard

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

Constant Summary collapse

DEFAULT_GRAPH_PARAMS =
[:width, :from, :until, :height]
REFRESH_CONFIG =
[:interval, :enabled]
TIME_CONFIG =
[:startDate, :relativeStartUnits, :endDate, :relativeStartQuantity, :relativeUntilQuantity, :startTime, :endTime, :type, :relativeUntilUnits]
GRAPH_SIZE =
[:width, :height]
OPTIONS =
{
  'defaultGraphParams' => DEFAULT_GRAPH_PARAMS,
  'refreshConfig' => REFRESH_CONFIG,
  'timeConfig' => TIME_CONFIG,
  'graphSize' => GRAPH_SIZE,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api

#exists?, #load!, #save!, #search

Constructor Details

#initialize(name = nil, &block) ⇒ Dashboard

Returns a new instance of Dashboard.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/graphite-dashboard-api/dashboard.rb', line 10

def initialize(name = nil, &block)
  @name = name
  @graphs = []
  @defaultGraphParams_width  = '800'
  @defaultGraphParams_from   = '-5minutes'
  @defaultGraphParams_until  = 'now'
  @defaultGraphParams_height = '400'
  @refreshConfig_interval =  600000
  @refreshConfig_enabled  = false
  @graphSize_width        = 400
  @graphSize_height       = 250
  @stringify_graphSize    = false # tweaking option

  # default timeConfig
  @timeConfig_type = 'relative'
  @timeConfig_relativeStartUnits = 'minutes'
  @timeConfig_relativeStartQuantity = '5'
  @timeConfig_relativeUntilUnits = 'now'
  @timeConfig_relativeUntilQuantity = ''

  @timeConfig_startDate = '1970-01-01T00:00:00'
  @timeConfig_endDate = '1970-01-01T00:05:00'
  @timeConfig_startTime = '9:00 AM'
  @timeConfig_endTime  = '5:00 PM'

  instance_eval(&block) if block
end

Instance Attribute Details

#graphs(arg = nil) ⇒ Object

Returns the value of attribute graphs.



7
8
9
# File 'lib/graphite-dashboard-api/dashboard.rb', line 7

def graphs
  @graphs
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/graphite-dashboard-api/dashboard.rb', line 6

def name
  @name
end

#stringify_graphSize(arg = nil) ⇒ Object

Returns the value of attribute stringify_graphSize.



8
9
10
# File 'lib/graphite-dashboard-api/dashboard.rb', line 8

def stringify_graphSize
  @stringify_graphSize
end

Instance Method Details

#decode(encoded_string) ⇒ Object



129
130
131
132
# File 'lib/graphite-dashboard-api/dashboard.rb', line 129

def decode(encoded_string)
  json = URI.decode(encoded_string[/=(.*)/, 1], my_unsafe)
  JSON.parse(json)
end

#encodeObject



124
125
126
127
# File 'lib/graphite-dashboard-api/dashboard.rb', line 124

def encode
  json = JSON.generate(to_hash['state'])
  'state=' + URI.encode(json, my_unsafe)
end

#from_hash!(hash) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/graphite-dashboard-api/dashboard.rb', line 101

def from_hash!(hash)
  state = hash['state']
  @name = state['name']
  OPTIONS.each do |k, options|
    if state[k]
      options.each do |kk|
        value = state[k][kk.to_s]
        instance_variable_set("@#{k}_#{kk}".to_sym, value) if value
      end
    end
  end
  if state['graphs']
    state['graphs'].each do |graph_entry|
      fail 'Graph entry is supposed to have 3 elements' unless graph_entry.size.eql? 3
      graph = graph_entry[1]
      new_graph = Graph.new
      new_graph.from_hash!(graph)
      @graphs << new_graph
    end
  end
  self
end

#my_unsafeObject



134
135
136
# File 'lib/graphite-dashboard-api/dashboard.rb', line 134

def my_unsafe
  Regexp.union(URI::UNSAFE, /[,&+]/)
end

#to_hashObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/graphite-dashboard-api/dashboard.rb', line 81

def to_hash
  state = {}
  state['name'] = @name
  OPTIONS.each do |k, options|
    state[k] = Hash.new
    options.each do |kk|
      state[k][kk.to_s] = instance_variable_get "@#{k}_#{kk}".to_sym
      state[k][kk.to_s] = state[k][kk.to_s].to_s if (k.eql? 'graphSize' and @stringify_graphSize)
    end
  end

  state['graphs'] = @graphs.map do |graph|

    [graph.leading_entries, graph.to_hash, graph.url(state['graphSize'])]
  end

  hash = { 'state' => state }
  hash
end