Class: MultiForecast::Client

Inherits:
Object
  • Object
show all
Includes:
ConversionRule
Defined in:
lib/multiforecast/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • opts (Hash) (defaults to: {})
    Hash

    mapping: Mapping rules from ‘path` to GrowthForecast’s ‘base_uri`.



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_devObject

Returns the value of attribute debug_dev.



12
13
14
# File 'lib/multiforecast/client.rb', line 12

def debug_dev
  @debug_dev
end

#short_metricsObject

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

Parameters:

  • from_graphs (Array)

    Array of graph properties whose keys are

    “path”, “gmode”, “stack”, “type”
  • to_complex (Hash)

    Property of Complex Graph, whose keys are like

    “path”, “description”, “sort”


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

Parameters:

  • path (String)

Returns:

  • (Hash)

    error response



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

Parameters:

  • path (String)


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

Parameters:

  • path (String)
  • params (Hash)

    All of parameters given by #get_graph are available except ‘number` and `mode`.

Returns:

  • (Hash)

    error response



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"}

Parameters:

  • path (String)

Returns:

  • (Hash)

    the graph property



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

Parameters:

  • path (String)
  • params (Hash) (defaults to: nil)

    for the query string t [String] the time unit such as ‘h’ (an hour), ‘4h’ (4 hours), ‘8h’, ‘n’ (half day), ‘d’ (a day), ‘3d’, ‘w’, (a week), ‘m’ (a month), ‘y’ (a year).

    Also, 'sh' 's4h' 's8h', 'sn', 'sd', 's3d' for graphs generated by short period GF worker.
    Also, this parameter is overrided with 'c' or 'sc' when `from` parameter is set.
    

    from [String|Time] the time period to show ‘from’. String describing a time, or a Time object to [String|Time] the time period to show ‘to’. String describing a time, or a Time Object width [String] the widh of image to show height [String] the height of image to show

Returns:

  • (Hash)

    error response



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"

Parameters:

  • path (String)

Returns:

  • (Hash)

    the graph property



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

Parameters:

  • path (String)
  • params (Hash) (defaults to: nil)

    for the query string t [String] the time unit such as ‘h’ (an hour), ‘4h’ (4 hours), ‘8h’, ‘n’ (half day), ‘d’ (a day), ‘3d’, ‘w’, (a week), ‘m’ (a month), ‘y’ (a year).

    Also, 'sh' 's4h' 's8h', 'sn', 'sd', 's3d' for graphs generated by short period GF worker.
    Also, this parameter is overrided with 'c' or 'sc' when `from` parameter is set.
    

    from [String|Time] the time period to show ‘from’. String describing a time, or a Time object to [String|Time] the time period to show ‘to’. String describing a time, or a Time Object width [String] the widh of image to show height [String] the height of image to show

Returns:

  • (Hash)

    error response



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

Parameters:

  • path (String)

Returns:

  • (Hash)

    response body



63
64
65
# File 'lib/multiforecast/client.rb', line 63

def get_json(path)
  client(path).get_json(path)
end

#last_clientObject



52
53
54
# File 'lib/multiforecast/client.rb', line 52

def last_client
  @last_client
end

#last_responseObject



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},

]

Parameters:

  • base_path (String) (defaults to: nil)
  • regexp (Regexp) (defaults to: nil)

    list only matched graphs

Returns:

  • (Hash)

    list of complex graphs



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},

]

Parameters:

  • base_path (String) (defaults to: nil)
  • regexp (Regexp) (defaults to: nil)

    list only matched graphs

Returns:

  • (Hash)

    list of graphs



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

Parameters:

  • path (String)
  • params (Hash)

    The POST parameters. See #get_graph



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

Parameters:

  • path (String)
  • data (Hash) (defaults to: {})

Returns:

  • (Hash)

    response body



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

Parameters:

  • path (String)
  • data (Hash) (defaults to: {})

Returns:

  • (String)

    response body



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