Skip to content

Instantly share code, notes, and snippets.

@mash
Created January 24, 2014 10:57
Show Gist options
  • Save mash/8595393 to your computer and use it in GitHub Desktop.
Save mash/8595393 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# make complex graphs from graphs with same graph_name prefix
# provide separator, which defaults to '-'
#
# --memo--
# APIs
# * GET /json/list/graph
# * GET /json/list/complex
# * GET /json/graph/:id or /json/complex/:id
# * GET /json/list/all
# * POST /json/edit/graph/:id or /json/edit/complex/:id
# * POST /json/create/complex
require 'httpclient'
require 'json'
require 'pp'
GROWTHFORECAST = '' # http://growthforecast.example.com'
USER = ''
PASSWORD = ''
SEPARATOR = '_' # hi!
$client = HTTPClient.new
$client.set_auth("#{GROWTHFORECAST}/", USER, PASSWORD)
def get_graphs()
body = $client.get("#{GROWTHFORECAST}/json/list/graph").body
JSON.parse(body)
end
def make_complex_graphs(service_name, section_name, graph_name, first_graph, rest_graphs)
stack = true
print "stack ? [Y/n]"
line = $stdin.readline.chomp
if line == 'n' then
print "ok, stack disabled\n"
stack = false
else
print "ok, stacking\n"
end
mode = :gauge
print "Gauge mode or Subtract mode ? [G/s]"
line = $stdin.readline.chomp
if line == 's' then
print "ok, Subtract mode\n"
mode = :subtract
else
print "ok, Gauge mode\n"
end
type = :AREA
print "AREA or LINE1 or LINE2 ? [A/1/2]"
line = $stdin.readline.chomp
if line == '1' then
print "ok, LINE1 type\n"
type = :LINE1
elsif line == '2'
print "ok, LINE2 type\n"
type = :LINE2
else
print "ok, AREA type\n"
end
display_sumup = false
print "Display sum up value ? [y/N]"
line = $stdin.readline.chomp
if line == 'y' then
print "ok, displaying\n"
display_sumup = true
else
print "ok, not displaying\n"
end
display_order = 19
print "Display order ? [19]"
line = $stdin.readline.chomp
if line =~ /^\d+$/ then
print "ok, order is #{line}"
display_order = line
else
print "ok, order is 19"
end
data = [
{
:graph_id => first_graph["id"],
:type => type,
:gmode => mode,
:stack => stack,
}
]
rest_graphs.each { |graph|
data.push({
:graph_id => graph["id"],
:type => type,
:gmode => mode,
:stack => stack,
})
}
spec = {
:complex => true,
:service_name => service_name,
:section_name => section_name,
:graph_name => graph_name,
:description => '',
:sumup => display_sumup,
:sort => display_order,
:data => data
}
content_json = JSON.generate spec
res = $client.post_content("#{GROWTHFORECAST}/json/create/complex", content_json, 'Content-Type' => 'application/json')
pp res
end
def ask_and_make_complex_graphs(service_name, section_name, graph_name, first_graph, rest_graphs)
print "making complex graph at #{service_name}/#{section_name}/#{graph_name}\nwith following graphs:\n#{first_graph}\n#{rest_graphs}\n[Y/n]"
line = $stdin.readline.chomp
if line == 'n' then
print "ok, skipping\n"
return
end
make_complex_graphs(service_name, section_name, graph_name, first_graph, rest_graphs)
end
def run
service_name = ''
section_name = ''
first_prefix = ''
first_graph = nil
rest_graphs = []
graphs = get_graphs()
graphs.each do |graph|
prefix = graph["graph_name"].split("#{SEPARATOR}").first
if graph["service_name"] == service_name and
graph["section_name"] == section_name and
prefix == first_prefix and
prefix != nil
then
rest_graphs.push( graph )
else
if rest_graphs.count > 0 then
ask_and_make_complex_graphs( service_name, section_name, first_prefix, first_graph, rest_graphs )
end
service_name = graph["service_name"]
section_name = graph["section_name"]
first_prefix = prefix
first_graph = graph
rest_graphs = []
end
end
end
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment