Class: MultiForecast::Client
- Inherits:
-
Object
- Object
- MultiForecast::Client
- Includes:
- ConversionRule
- Defined in:
- lib/multiforecast/client.rb
Instance Attribute Summary collapse
-
#clients(base_path = nil) ⇒ Object
Returns the value of attribute clients.
-
#debug_dev ⇒ Object
Returns the value of attribute debug_dev.
-
#short_metrics ⇒ Object
Returns the value of attribute short_metrics.
Instance Method Summary collapse
- #client(path) ⇒ Object
-
#create_complex(from_graphs, to_complex) ⇒ Object
Create a complex graph.
-
#delete_complex(path) ⇒ Hash
Delete a complex graph.
-
#delete_graph(path) ⇒ Object
Delete a graph, POST /delete/:path.
-
#edit_graph(path, params) ⇒ Hash
Update the property of a graph, /json/edit/graph/:id “error”=>0 #=> Success “error”=>1 #=> Error.
-
#get_complex(path) ⇒ Hash
Get a complex graph.
-
#get_complex_uri(path, params = nil) ⇒ Hash
Get complex graph image uri.
-
#get_graph(path) ⇒ Hash
Get the propety of a graph, GET /api/:path { “base_uri” => “xxxxxx”, “path” => “test/hostname/<4sec_count”, “service_name”=>“mbclient”, “section_name”=>“mbclient”, “graph_name”=>“test%2Fhostname%2F%3C4sec_count”, “number”=>1, “llimit”=>-1000000000, “mode”=>“gauge”, “stype”=>“AREA”, “adjustval”=>“1”, “meta”=>“”, “gmode”=>“gauge”, “color”=>“#cc6633”, “created_at”=>“2013/02/02 00:41:11”, “ulimit”=>1000000000, “id”=>21, “description”=>“”, “sulimit”=>100000, “unit”=>“”, “sort”=>0, “updated_at”=>“2013/02/02 02:32:10”, “adjust”=>“*”, “type”=>“AREA”, “sllimit”=>-100000, “md5”=>“3c59dc048e8850243be8079a5c74d079”}.
-
#get_graph_uri(path, params = nil) ⇒ Hash
Get graph image uri.
-
#get_json(path) ⇒ Hash
GET the JSON API.
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #last_client ⇒ Object
- #last_response ⇒ Object
-
#list_complex(base_path = nil, regexp = nil) ⇒ Hash
Get the list of complex graphs, /json/list/complex [ “path”=>“test/hostname/<2sec_count”, “service_name”=>“mbclient”, “section_name”=>“mbclient”, “graph_name”=>“test%2Fhostname%2F%3C2sec_count”, “id”=>4, “path”=>“test/hostname/<1sec_count”, “service_name”=>“mbclient”, “section_name”=>“mbclient”, “graph_name”=>“test%2Fhostname%2F%3C1sec_count”, “id”=>3, ].
-
#list_graph(base_path = nil, regexp = nil) ⇒ Hash
Get the list of graphs, /json/list/graph [ “service_name”=>“mbclient”, “section_name”=>“mbclient”, “graph_name”=>“test%2Fhostname%2F%3C2sec_count”, “path”=>“test/hostname/<2sec_count”, “id”=>4, “service_name”=>“mbclient”, “section_name”=>“mbclient”, “graph_name”=>“test%2Fhostname%2F%3C1sec_count”, “path”=>“test/hostname/<1sec_count”, “id”=>3, ].
-
#post_graph(path, params) ⇒ Object
Post parameters to a graph, POST /api/:path.
-
#post_json(path, data = {}) ⇒ Hash
POST the JSON API.
-
#post_query(path, data = {}) ⇒ String
POST the non-JSON API.
-
#preprocess_time_params(params) ⇒ Object
process the time params (from and to).
Methods included from ConversionRule
#graph_name, #id, #ids, #lstrip, #path, #section_name, #service_name, #uri_escape, #uri_unescape
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/multiforecast/client.rb', line 17 def initialize(opts = {}) @mapping = {} mapping = opts['mapping'] || { '' => 'http://localhost:5125' } # remove heading / of path mapping.each {|key, val| @mapping[lstrip(key, '/')] = val } @short_metrics = opts['short_metrics'].nil? || opts['short_metrics'] # default true @clients = {} @base_uris = {} @mapping.each do |path, base_uri| if base_uri.kind_of?(Hash) out_uri = base_uri['out_uri'] base_uri = base_uri['in_uri'] end @clients[path] = GrowthForecast::Client.new(base_uri) @base_uris[path] = out_uri || base_uri end self.debug_dev = opts['debug_dev'] if opts['debug_dev'] end |
Instance Attribute Details
#clients(base_path = nil) ⇒ Object
Returns the value of attribute clients.
11 12 13 |
# File 'lib/multiforecast/client.rb', line 11 def clients @clients end |
#debug_dev ⇒ Object
Returns the value of attribute debug_dev.
12 13 14 |
# File 'lib/multiforecast/client.rb', line 12 def debug_dev @debug_dev end |
#short_metrics ⇒ Object
Returns the value of attribute short_metrics.
13 14 15 |
# File 'lib/multiforecast/client.rb', line 13 def short_metrics @short_metrics end |
Instance Method Details
#client(path) ⇒ Object
48 49 50 |
# File 'lib/multiforecast/client.rb', line 48 def client(path) @last_client = @clients[id(path)] end |
#create_complex(from_graphs, to_complex) ⇒ Object
Create a complex graph
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/multiforecast/client.rb', line 217 def create_complex(from_graphs, to_complex) from_graphs = from_graphs.dup to_complex = to_complex.dup from_graphs.each do |from_graph| from_graph['service_name'] = service_name(from_graph['path']) from_graph['section_name'] = section_name(from_graph['path']) from_graph['graph_name'] = graph_name(from_graph['path']) from_graph.delete('path') from_graph.delete('base_uri') end to_complex['service_name'] = service_name(to_complex['path']) to_complex['section_name'] = section_name(to_complex['path']) to_complex['graph_name'] = graph_name(to_complex['path']) path = to_complex.delete('path') # NOTE: FROM_GRAPHS AND TO _COMPLEX MUST BE THE SAME GF SERVER client(path).create_complex(from_graphs, to_complex) end |
#delete_complex(path) ⇒ Hash
Delete a complex graph
“error”=>0 #=> Success “error”=>1 #=> Error
271 272 273 |
# File 'lib/multiforecast/client.rb', line 271 def delete_complex(path) client(path).delete_complex(service_name(path), section_name(path), graph_name(path)) end |
#delete_graph(path) ⇒ Object
Delete a graph, POST /delete/:path
162 163 164 |
# File 'lib/multiforecast/client.rb', line 162 def delete_graph(path) client(path).delete_graph(service_name(path), section_name(path), graph_name(path)) end |
#edit_graph(path, params) ⇒ Hash
Update the property of a graph, /json/edit/graph/:id “error”=>0 #=> Success “error”=>1 #=> Error
174 175 176 |
# File 'lib/multiforecast/client.rb', line 174 def edit_graph(path, params) client(path).edit_graph(service_name(path), section_name(path), graph_name(path), params) end |
#get_complex(path) ⇒ Hash
Get a complex graph
"complex"=>true,
"created_at"=>"2013/05/20 15:08:28",
"service_name"=>"app name",
"section_name"=>"host name",
"id"=>18,
"graph_name"=>"complex graph test",
"data"=>
[{"gmode"=>"gauge", "stack"=>false, "type"=>"AREA", "graph_id"=>218,
"stack"=>true, "type"=>"AREA", "graph_id"=>217],
"sumup"=>false,
"description"=>"complex graph test",
"sort"=>10,
"updated_at"=>"2013/05/20 15:08:28"}
257 258 259 260 261 262 |
# File 'lib/multiforecast/client.rb', line 257 def get_complex(path) client(path).get_complex(service_name(path), section_name(path), graph_name(path)).tap do |graph| graph['base_uri'] = client(path).base_uri graph['path'] = path end end |
#get_complex_uri(path, params = nil) ⇒ Hash
Get complex graph image uri
307 308 309 310 311 |
# File 'lib/multiforecast/client.rb', line 307 def get_complex_uri(path, params = nil) params ||= {} params = preprocess_time_params(params) unless params.empty? "#{@base_uris[id(path)]}/complex/graph/#{uri_escape(service_name(path))}/#{uri_escape(section_name(path))}/#{uri_escape(graph_name(path))}#{'?' unless params.empty?}#{query_string(params)}" end |
#get_graph(path) ⇒ Hash
Get the propety of a graph, GET /api/:path
"base_uri" => "xxxxxx",
"path" => "test/hostname/<4sec_count",
"service_name"=>"mbclient",
"section_name"=>"mbclient",
"graph_name"=>"test%2Fhostname%2F%3C4sec_count",
"number"=>1,
"llimit"=>-1000000000,
"mode"=>"gauge",
"stype"=>"AREA",
"adjustval"=>"1",
"meta"=>"",
"gmode"=>"gauge",
"color"=>"#cc6633",
"created_at"=>"2013/02/02 00:41:11",
"ulimit"=>1000000000,
"id"=>21,
"description"=>"",
"sulimit"=>100000,
"unit"=>"",
"sort"=>0,
"updated_at"=>"2013/02/02 02:32:10",
"adjust"=>"*",
"type"=>"AREA",
"sllimit"=>-100000,
"md5"=>"3c59dc048e8850243be8079a5c74d079"
146 147 148 149 150 151 |
# File 'lib/multiforecast/client.rb', line 146 def get_graph(path) client(path).get_graph(service_name(path), section_name(path), graph_name(path)).tap do |graph| graph['base_uri'] = client(path).base_uri graph['path'] = path end end |
#get_graph_uri(path, params = nil) ⇒ Hash
Get graph image uri
288 289 290 291 292 |
# File 'lib/multiforecast/client.rb', line 288 def get_graph_uri(path, params = nil) params ||= {} params = preprocess_time_params(params) unless params.empty? "#{@base_uris[id(path)]}/graph/#{uri_escape(service_name(path))}/#{uri_escape(section_name(path))}/#{uri_escape(graph_name(path))}#{'?' unless params.empty?}#{query_string(params)}" end |
#get_json(path) ⇒ Hash
GET the JSON API
63 64 65 |
# File 'lib/multiforecast/client.rb', line 63 def get_json(path) client(path).get_json(path) end |
#last_client ⇒ Object
52 53 54 |
# File 'lib/multiforecast/client.rb', line 52 def last_client @last_client end |
#last_response ⇒ Object
56 57 58 |
# File 'lib/multiforecast/client.rb', line 56 def last_response @last_client.last_response end |
#list_complex(base_path = nil, regexp = nil) ⇒ Hash
Get the list of complex graphs, /json/list/complex [
{"base_uri"=>"xxxxx",
"path"=>"test/hostname/<2sec_count",
"service_name"=>"mbclient",
"section_name"=>"mbclient",
"graph_name"=>"test%2Fhostname%2F%3C2sec_count",
"id"=>4},
{"base_uri"=>"xxxxx",
"path"=>"test/hostname/<1sec_count",
"service_name"=>"mbclient",
"section_name"=>"mbclient",
"graph_name"=>"test%2Fhostname%2F%3C1sec_count",
"id"=>3},
]
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/multiforecast/client.rb', line 197 def list_complex(base_path = nil, regexp = nil) clients(base_path).inject([]) do |ret, client| graphs = [] client.list_complex.each do |graph| graph['base_uri'] = client.base_uri graph['path'] = path(graph['service_name'], graph['section_name'], graph['graph_name']) if base_path.nil? or graph['path'].index(base_path) == 0 graphs << graph if !regexp or regexp.match(graph['path']) end end ret = ret + graphs end end |
#list_graph(base_path = nil, regexp = nil) ⇒ Hash
Get the list of graphs, /json/list/graph [
{"base_uri"=>"xxxxx",
"service_name"=>"mbclient",
"section_name"=>"mbclient",
"graph_name"=>"test%2Fhostname%2F%3C2sec_count",
"path"=>"test/hostname/<2sec_count",
"id"=>4},
{"base_uri"=>"xxxxx",
"service_name"=>"mbclient",
"section_name"=>"mbclient",
"graph_name"=>"test%2Fhostname%2F%3C1sec_count",
"path"=>"test/hostname/<1sec_count",
"id"=>3},
]
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/multiforecast/client.rb', line 102 def list_graph(base_path = nil, regexp = nil) clients(base_path).inject([]) do |ret, client| graphs = [] client.list_graph.each do |graph| graph['base_uri'] = client.base_uri graph['path'] = path(graph['service_name'], graph['section_name'], graph['graph_name']) if base_path.nil? or graph['path'].index(base_path) == 0 graphs << graph if !regexp or regexp.match(graph['path']) end end ret = ret + graphs end end |
#post_graph(path, params) ⇒ Object
Post parameters to a graph, POST /api/:path
156 157 158 |
# File 'lib/multiforecast/client.rb', line 156 def post_graph(path, params) client(path).post_graph(service_name(path), section_name(path), graph_name(path), params) end |
#post_json(path, data = {}) ⇒ Hash
POST the JSON API
71 72 73 |
# File 'lib/multiforecast/client.rb', line 71 def post_json(path, data = {}) client(path).post_json(path, data) end |
#post_query(path, data = {}) ⇒ String
POST the non-JSON API
79 80 81 |
# File 'lib/multiforecast/client.rb', line 79 def post_query(path, data = {}) client(path).post_query(path, data) end |
#preprocess_time_params(params) ⇒ Object
process the time params (from and to)
314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/multiforecast/client.rb', line 314 def preprocess_time_params(params) params = params.dup params['from'] = Time.parse(params['from']) if params['from'].kind_of?(String) params['to'] = Time.parse(params['to']) if params['to'].kind_of?(String) if params['from'] and params['to'] # if from is more future than 3 days ago, use 'sc' (short period time worker) params['t'] = (@short_metrics && params['from'] > Time.now - 60 * 60 * 24 * 3) ? 'sc' : 'c' params['from'] = params['from'].strftime("%F %T %z") # format is determined params['to'] = params['to'].strftime("%F %T %z") end params end |