class MultiForecast::CLI

Public Class Methods

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

  # NOTE: Please note that @options receives only strings, no symbols anymore
  if options['config'] && File.exists?(options['config'])
    @options = YAML.load_file(options['config']).merge(@options)
  end
  @client = MultiForecast::Client.new(@options)
end

Public Instance Methods

color() click to toggle source
# File lib/multiforecast/cli.rb, line 74
def color
  base_path = lstrip(@options['base_path'], '/') if @options['base_path']
  graphs = @client.list_graph(base_path)
  setup_colors(@options['colors'], graphs)
end
create_complex() click to toggle source
# File lib/multiforecast/cli.rb, line 89
def create_complex
  base_path = lstrip(@options['base_path'], '/') if @options['base_path']
  graphs = @client.list_graph(base_path)
  setup_complex(options['from_graphs'], options['to_complex'], graphs)
end
delete(base_path) click to toggle source
# File lib/multiforecast/cli.rb, line 54
def delete(base_path)
  base_path = lstrip(base_path, '/')
  regexp = Regexp.new(@options['regexp']) if @options['regexp']

  graphs = @client.list_graph(base_path, regexp)
  delete_graphs(graphs, @options['graph_names'])

  complexes = @client.list_complex(base_path, regexp)
  delete_complexes(complexes, @options['graph_names'])
  $stderr.puts "Not found" if graphs.empty? and complexes.empty? unless @options['silent']
end
generate(target) click to toggle source
# File lib/multiforecast/cli.rb, line 21
def generate(target)
  config = {
    'mapping' => { '' => 'http://localhost:5125' },
  }
  File.open("multiforecast.yml", "w") do |file|
    YAML.dump(config, file)
    $stdout.puts "Generated #{file.path}"
  end
end
post(json, path) click to toggle source
# File lib/multiforecast/cli.rb, line 37
def post(json, path)
  path = lstrip(path, '/')
  exec do
    res = @client.post_graph(path, JSON.parse(json))
    $stdout.puts res unless @options['silent']
  end
end

Private Instance Methods

delete_complexes(complexes, graph_names = nil) click to toggle source
# File lib/multiforecast/cli.rb, line 106
def delete_complexes(complexes, graph_names = nil)
  complexes.each do |graph|
    path = graph['path']
    next if graph_names and !graph_names.include?(File.basename(path))
    puts "Delete #{path}" unless @options['silent']
    exec { @client.delete_complex(path) }
  end
end
delete_graphs(graphs, graph_names = nil) click to toggle source
# File lib/multiforecast/cli.rb, line 97
def delete_graphs(graphs, graph_names = nil)
  graphs.each do |graph|
    path = graph['path']
    next if graph_names and !graph_names.include?(File.basename(path))
    puts "Delete #{path}" unless @options['silent']
    exec { @client.delete_graph(path) }
  end
end
exec() { || ... } click to toggle source
# File lib/multiforecast/cli.rb, line 139
def exec(&blk)
  begin
    yield
  rescue => e
    $stderr.puts "\tclass:#{e.class}\t#{e.message}"
  end
end
setup_colors(colors, graphs) click to toggle source
# File lib/multiforecast/cli.rb, line 115
def setup_colors(colors, graphs)
  graphs.each do |graph|
    path = graph['path']
    next unless color = colors[File.basename(path)]
    data = { 'color' => color }
    puts "Setup #{path} with #{color}" unless @options['silent']
    exec { @client.edit_graph(path, data) }
  end
end
setup_complex(from_graphs, to_complex, graphs) click to toggle source
# File lib/multiforecast/cli.rb, line 125
def setup_complex(from_graphs, to_complex, graphs)
  from_graph_first = from_graphs.first
  graphs.each do |graph|
    next unless File.basename(graph['path']) == from_graph_first
    dirname = File.dirname(graph['path'])

    base = {'gmode' => 'gauge', 'stack' => true, 'type' => 'AREA'}
    from_graphs_params = from_graphs.map {|name| base.merge('path' => "#{dirname}/#{name}") }
    to_complex_params = { 'path' => "#{dirname}/#{to_complex}", 'sort' => 0 }
    puts "Setup #{dirname}/#{to_complex} with #{from_graphs}" unless @options['silent']
    exec { @client.create_complex(from_graphs_params, to_complex_params) }
  end
end