#!/usr/bin/env ruby require 'cgi' require 'net/http' require 'optparse' require 'rubygems' gem 'json' require 'json' metrics = "*" token = ENV["INSTRUMENTAL_TOKEN"] allowed_durations = [1800, 3600, 10800, 21600, 43200, 86400, 259200, 604800, 1209600] duration = nil opts = OptionParser.new do |opts| opts.banner = "Usage: instrumental [options]" opts.on("-m", "--metrics METRICS", "Comma separated list of metrics you're interested in. Wildcard (*) allowed") do |m| metrics = m end opts.on("-t", "--token TOKEN", "Your API token. Also can be set as an environment variable, INSTRUMENTAL_TOKEN") do |t| token = t end opts.on("-d", "--duration DURATION", "Duration ago in seconds to view. You can specify #{allowed_durations.join(" ")}") do |d| duration = allowed_durations.index(d.to_i) && d.to_i end end.parse! search_line = CGI.escape(metrics) duration &&= "&duration=#{duration}" http = Net::HTTP.new("instrumentalapp.com", 80) request = Net::HTTP::Get.new("/metrics/#{search_line}?token=#{token}&format=json#{duration}") response = http.request(request) raise "Server error: #{response.message} #{response.code}" if response.code.to_i != 200 response = JSON.parse(response.body) bars = ["\342\226\201", "\342\226\202", "\342\226\203", "\342\226\204", "\342\226\205", "\342\226\206", "\342\226\207"] max_key_size = response.collect { |k, v| k.size }.max scale = bars.size - 1 response.each do |metric, data| data = data.collect { |h| h["value"].to_f } min = data.min range = data.max - min graph = data.collect do |v| if (idx = (((v - min) / range) * scale)).nan? idx = 0 elsif idx.infinite? idx = scale end bars[idx.round] end puts "%#{max_key_size}s | %s" % [metric, graph.join] end