class GrowthForecast::CLI

Public Class Methods

new(args = [], opts = [], config = {}) click to toggle source
Calls superclass method
# File lib/growthforecast/cli.rb, line 6
def initialize(args = [], opts = [], config = {})
  super(args, opts, config)
end

Public Instance Methods

bench(url) click to toggle source
# File lib/growthforecast/cli.rb, line 100
def bench(url)
  requests, concurrency = options[:requests], options[:concurrency]
  require 'parallel'
  base_uri = split_url(url).first
  @client = client(base_uri)
  # generate unique paths of the same number with number of requests beforehand
  paths = []
  requests.times do |i|
    paths << [(i/100).to_s, (i/50).to_s, i.to_s]
  end
  # Take a benchmark in parallel
  start = Time.now
  Parallel.each_with_index(paths, :in_processes => concurrency) do |path, i|
    puts "Completed #{i} requests" if i % 1000 == 0 and i > 0
    begin
      @client.post_graph(path[0], path[1], path[2], { "number" => rand(1000) })
    rescue => e
      $stderr.puts "#{e.class} #{e.message}"
    end
  end
  puts "Completed #{requests} requests"
  duration = (Time.now - start).to_f
  puts "Requests per second: #{requests / duration} [#/sec] (mean)"
  puts "Time per request:    #{duration / requests * 1000} [ms] (mean)"
end
client(base_uri) click to toggle source
# File lib/growthforecast/cli.rb, line 183
def client(base_uri)
  GrowthForecast::Client.new(base_uri)
end
color(url) click to toggle source
# File lib/growthforecast/cli.rb, line 53
def color(url)
  colors = options[:colors]

  base_uri, service_name, section_name, graph_name = split_url(url)
  @client = client(base_uri)

  graphs = @client.list_graph(service_name, section_name, graph_name)
  setup_colors(colors, graphs)
end
create_complex(url) click to toggle source
# File lib/growthforecast/cli.rb, line 71
def create_complex(url)
  from_graphs, to_complex = options[:from_graphs], options[:to_complex]
  base_uri, service_name, section_name, graph_name = split_url(url)
  @client = client(base_uri)

  graphs = @client.list_graph(service_name, section_name, graph_name)
  setup_complex(from_graphs, to_complex, graphs)
end
delete(url) click to toggle source
# File lib/growthforecast/cli.rb, line 33
def delete(url)
  section_names, graph_names = options[:section_names], options[:graph_names]

  base_uri, service_name, section_name, graph_name = split_url(url)
  @client = client(base_uri)

  graphs = @client.list_graph(service_name, section_name, graph_name)
  delete_graphs(graphs, graph_names, section_names)

  complexes = @client.list_complex(service_name, section_name, graph_name)
  delete_complexes(complexes, graph_names, section_names)
end
delete_complexes(complexes, graph_names = nil, section_names = nil) click to toggle source
# File lib/growthforecast/cli.rb, line 138
def delete_complexes(complexes, graph_names = nil, section_names = nil)
  complexes.each do |graph|
    service_name, section_name, graph_name = graph['service_name'], graph['section_name'], graph['graph_name']
    next if section_names and !section_names.include?(section_name)
    next if graph_names   and !graph_names.include?(graph_name)

    puts "Delete #{service_name}/#{section_name}/#{graph_name}" unless @options[:silent]
    exec { @client.delete_complex(service_name, section_name, graph_name) }
  end
end
delete_graphs(graphs, graph_names = nil, section_names = nil) click to toggle source
# File lib/growthforecast/cli.rb, line 127
def delete_graphs(graphs, graph_names = nil, section_names = nil)
  graphs.each do |graph|
    service_name, section_name, graph_name = graph['service_name'], graph['section_name'], graph['graph_name']
    next if section_names and !section_names.include?(section_name)
    next if graph_names   and !graph_names.include?(graph_name)

    puts "Delete #{service_name}/#{section_name}/#{graph_name}" unless @options[:silent]
    exec { @client.delete_graph(service_name, section_name, graph_name) }
  end
end
exec() { || ... } click to toggle source
# File lib/growthforecast/cli.rb, line 175
def exec(&blk)
  begin
    yield
  rescue => e
    $stderr.puts "\tclass:#{e.class}\t#{e.message}"
  end
end
post(json, url) click to toggle source
# File lib/growthforecast/cli.rb, line 16
def post(json, url)
  base_uri, service_name, section_name, graph_name = split_url(url)
  @client = client(base_uri)
  puts @client.post_graph(service_name, section_name, graph_name, JSON.parse(json))
end
setup_colors(colors, graphs) click to toggle source
# File lib/growthforecast/cli.rb, line 149
def setup_colors(colors, graphs)
  graphs.each do |graph|
    service_name, section_name, graph_name = graph['service_name'], graph['section_name'], graph['graph_name']
    next unless color = colors[graph_name]

    params = { 'color'  => color }
    puts "Setup #{service_name}/#{section_name}/#{graph_name} with #{color}" unless @options[:silent]
    exec { @client.edit_graph(service_name, section_name, graph_name, params) }
  end
end
setup_complex(from_graphs, to_complex, graphs) click to toggle source
# File lib/growthforecast/cli.rb, line 160
def setup_complex(from_graphs, to_complex, graphs)
  from_graph_first = from_graphs.first
  graphs.each do |graph|
    service_name, section_name, graph_name = graph['service_name'], graph['section_name'], graph['graph_name']
    next unless graph_name == from_graph_first

    base = { "service_name" => service_name, "section_name" => section_name, "gmode" => 'gauge', "stack" => true, "type" => 'AREA' }
    from_graphs_params = from_graphs.map {|graph_name| base.merge('graph_name' => graph_name) }
    to_complex_params = { "service_name" => service_name, "section_name" => section_name, "graph_name" => to_complex, "sort" => 0 }

    puts "Setup #{service_name}/#{section_name}/#{to_complex} with #{from_graphs}" unless @options[:silent]
    exec { @client.create_complex(from_graphs_params, to_complex_params) }
  end
end
split_path(path) click to toggle source
# File lib/growthforecast/cli.rb, line 193
def split_path(path)
  path = path.gsub(/.*list\/?/, '').gsub(/.*view_graph\/?/, '').gsub(/.*api\/?/, '')
  path.split('/').map {|p| CGI.unescape(p.gsub('%20', '+')) }
end
split_url(url) click to toggle source
# File lib/growthforecast/cli.rb, line 187
def split_url(url)
  uri = URI.parse(url)
  base_uri = "#{uri.scheme}://#{uri.host}:#{uri.port}"
  [base_uri] + split_path(uri.path)
end
vrule(json, url) click to toggle source
# File lib/growthforecast/cli.rb, line 86
def vrule(json, url)
  base_uri, service_name, section_name, graph_name = split_url(url)
  @client = client(base_uri)
  puts @client.post_vrule(service_name, section_name, graph_name, JSON.parse(json))
end