Class: GrowthForecast

Inherits:
Object
  • Object
show all
Defined in:
lib/growthforecast.rb,
lib/growthforecast.rb,
lib/growthforecast/version.rb

Defined Under Namespace

Modules: Client Classes: Cache, Complex, Graph, Path, Spec

Constant Summary collapse

TIME_FORMAT =
'%Y/%m/%d %H:%M:%S'
ADDITIONAL_PARAMS =
['description' 'sort' 'gmode' 'ulimit' 'llimit' 'sulimit' 'sllimit' 'type' 'stype' 'adjust' 'adjustval' 'unit']
VERSION =
"0.0.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 5125, prefix = nil, timeout = 30, debug = false, username = nil, password = nil) ⇒ GrowthForecast

Returns a new instance of GrowthForecast.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/growthforecast.rb', line 21

def initialize(host='localhost', port=5125, prefix=nil, timeout=30, debug=false, username=nil, password=nil)
  @host = host
  @port = port.to_i
  @prefix = if prefix && (prefix =~ /^\//) then prefix
            elsif prefix then '/' + prefix
            else '/'
            end
  @prefix.chop! if @prefix =~ /\/$/
  @timeout = timeout.to_i
  @debug = debug ? true : false

  @username = username
  @password = password

  @resolver = Resolve::Hostname.new(:system_resolver => true)
end

Instance Attribute Details

#debug(mode = nil) ⇒ Object

Returns the value of attribute debug.



18
19
20
# File 'lib/growthforecast.rb', line 18

def debug
  @debug
end

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/growthforecast.rb', line 18

def host
  @host
end

#passwordObject

Returns the value of attribute password.



19
20
21
# File 'lib/growthforecast.rb', line 19

def password
  @password
end

#portObject

Returns the value of attribute port.



18
19
20
# File 'lib/growthforecast.rb', line 18

def port
  @port
end

#prefixObject

Returns the value of attribute prefix.



18
19
20
# File 'lib/growthforecast.rb', line 18

def prefix
  @prefix
end

#timeoutObject

Returns the value of attribute timeout.



18
19
20
# File 'lib/growthforecast.rb', line 18

def timeout
  @timeout
end

#usernameObject

Returns the value of attribute username.



19
20
21
# File 'lib/growthforecast.rb', line 19

def username
  @username
end

Instance Method Details

#add(spec) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/growthforecast.rb', line 128

def add(spec)
  unless spec.respond_to?(:complex?)
    raise ArgumentError, "parameter of add() must be instance of GrowthForecast::Graph or GrowthForecast::Complex (or use add_graph/add_complex)"
  end
  if spec.complex?
    add_complex_spec(spec)
  else
    add_graph(spec.service_name, spec.section_name, spec.graph_name, spec.number, spec.color, spec.mode)
  end
end

#add_complex(service, section, graph_name, description, sumup, sort, type, gmode, stack, data_graph_ids) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/growthforecast.rb', line 151

def add_complex(service, section, graph_name, description, sumup, sort, type, gmode, stack, data_graph_ids)
  unless data_graph_ids.is_a?(Array) && data_graph_ids.size > 0
    raise ArgumentError, "To create complex graph, specify 1 or more sub graph ids"
  end
  unless sort >= 0 and sort <= 19
    raise ArgumentError, "sort must be 0..19"
  end
  unless type == 'AREA' || type == 'LINE1' || type == 'LINE2'
    raise ArgumentError, "type must be one of AREA/LINE1/LINE2"
  end
  unless gmode == 'gauge' || gmode == 'subtract'
    raise ArgumentError, "gmode must be one of gauge/subtract"
  end
  spec = GrowthForecast::Complex.new({
      complex: true,
      service_name: service, section_name: section, graph_name: graph_name,
      description: description, sumup: sumup, sort: sort,
      data: data_graph_ids.map{|id| {'graph_id' => id, 'type' => type, 'gmode' => gmode, 'stack' => stack} }
    })
  add_complex_spec(spec)
end

#add_graph(service, section, graph_name, initial_value = 0, color = nil, mode = nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/growthforecast.rb', line 139

def add_graph(service, section, graph_name, initial_value=0, color=nil, mode=nil)
  if service.empty? || section.empty? || graph_name.empty?
    raise ArgumentError, "service, section and graph_name must be specified"
  end
  if (not color.nil?)
    unless color =~ /^#[0-9a-fA-F]{6}/
      raise ArgumentError, "color must be specified like #FFFFFF"
    end
  end
  post(service, section, graph_name, initial_value, mode, color) and true # 'add' and 'add_graph' returns boolean not graph object
end

#allObject



86
87
88
# File 'lib/growthforecast.rb', line 86

def all
  request('GET', "/json/list/all", {}, '', true)
end

#by_name(service, section, name) ⇒ Object



55
56
57
# File 'lib/growthforecast.rb', line 55

def by_name(service, section, name)
  ((self.tree()[service] || {})[section] || {})[name]
end

#complex(id) ⇒ Object



66
67
68
69
70
71
# File 'lib/growthforecast.rb', line 66

def complex(id)
  if id.respond_to?(:complex?) && id.complex? # accept complex object itself to reload
    id = id.id
  end
  request('GET', "/json/complex/#{id}")
end

#complexesObject



77
78
79
80
81
82
83
84
# File 'lib/growthforecast.rb', line 77

def complexes
  list = request('GET', "/json/list/complex", {}, '', true)
  return nil if list.nil?
  list.each do |path|
    path.complex = true
  end
  list
end

#delete(spec) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/growthforecast.rb', line 115

def delete(spec)
  if spec.id.nil?
    raise ArgumentError, "cannot delete graph without id (get graph data from GrowthForecast at first)"
  end
  path = if spec.complex?
           "/delete_complex/#{spec.id}"
         else
           "/delete/#{spec.service_name}/#{spec.section_name}/#{spec.graph_name}"
         end
  request('POST', path)
end

#edit(spec) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/growthforecast.rb', line 103

def edit(spec)
  if spec.id.nil?
    raise ArgumentError, "cannot edit graph without id (get graph data from GrowthForecast at first)"
  end
  path = if spec.complex?
           "/json/edit/complex/#{spec.id}"
         else
           "/json/edit/graph/#{spec.id}"
         end
  request('POST', path, {}, spec.to_json)
end

#graph(id) ⇒ Object



59
60
61
62
63
64
# File 'lib/growthforecast.rb', line 59

def graph(id)
  if id.respond_to?(:complex?) && (not id.complex?) # accept graph object itself to reload
    id = id.id
  end
  request('GET', "/json/graph/#{id}")
end

#graphsObject



73
74
75
# File 'lib/growthforecast.rb', line 73

def graphs
  request('GET', "/json/list/graph", {}, '', true)
end

#post(service, section, name, value, mode = nil, color = nil) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/growthforecast.rb', line 46

def post(service, section, name, value, mode=nil, color=nil)
  form = {'number' => value}
  form.update({'mode' => mode}) if mode
  form.update({'color' => color}) if color
  content = URI.encode_www_form(form)

  request('POST', "/api/#{service}/#{section}/#{name}", {}, content)
end

#treeObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/growthforecast.rb', line 90

def tree
  list = self.all()
  return {} if list.nil?

  root = {}
  list.each do |i|
    root[i.service_name] ||= {}
    root[i.service_name][i.section_name] ||= {}
    root[i.service_name][i.section_name][i.graph_name] = i
  end
  root
end