Skip to content

Instantly share code, notes, and snippets.

@smt116
Last active June 1, 2017 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smt116/9383b77b779d6b789a4f1fa649ab3b9b to your computer and use it in GitHub Desktop.
Save smt116/9383b77b779d6b789a4f1fa649ab3b9b to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'time'
require 'gnuplot'
require 'launchy'
require 'tempfile'
require 'fileutils'
ARGV.sort!.reverse!
raise ArgumentError.new("USAGE: #{__FILE__} input.tsv [--without-items] [--without-capacity] [--save]") if ARGV.empty?
data = {}
File.foreach(ARGV[0]) do |line|
next unless line.include?('DynamoDB query')
columns = line.split("\t")
timestamp = Time.parse("#{columns[1]} +0000").to_i
captures = columns[9].match(/DynamoDB query \((?<items>[\d]+) items, .*, (?<capacity>[\d\.]+) capacity/)
data[timestamp] ||= {}
%i(capacity items).each do |key|
data[timestamp][key] = data[timestamp].fetch(key, 0) + captures[key].to_f
end
end
tempfile = Tempfile.new(['plot', '.png'])
Gnuplot.open do |gp|
Gnuplot::Plot.new(gp) do |plot|
plot.term('png size 1200,600')
plot.output(tempfile.path)
plot.xlabel('Time')
plot.ylabel('Value')
plot.xdata(:time)
plot.timefmt('"%s"')
x = data.keys
%i(capacity items).each do |property|
unless ARGV.include?("--without-#{property.to_s}")
y = data.values.map { |hash| hash.fetch(property) }
plot.data.push Gnuplot::DataSet.new([x, y]) { |dataset|
dataset.using = "1:2 title '#{property.to_s.capitalize}'"
dataset.with = 'impulses'
}
end
end
end
end
if File.exist?(tempfile.path)
if ARGV.include?('--save')
path = File.join(File.dirname(__FILE__), File.basename(tempfile.path))
FileUtils.cp(tempfile.path, path)
Launchy.open(path)
else
Launchy.open(tempfile.path)
sleep(1) # Make sure that Ruby won't remove temporary file before opening it
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment