--- /dev/null
+#!/usr/bin/ruby
+
+require 'rubygems'
+require 'date'
+require 'hpricot'
+require 'open-uri'
+
+def uri_and_times_from_status(server)
+ file = open("http://#{server}/server-status").read
+ doc = Hpricot.parse(file)
+ tables = doc / 'table'
+ rows = (tables[0] / 'tr')[1..-1]
+ data = rows.collect {|r| (r / 'td').collect {|x| x.inner_html} }
+ # filter where the PID is numeric, status is 'W' and host matches the server
+ matching_data = data.select {|r| (r[1].to_i > 0) && r[3].match(/W/) && r[11].match(server)}
+ # return URI and number of seconds processing for each request
+ matching_data.collect {|r| [r[12], r[5].to_i]}
+end
+
+CALL_TYPES = {
+ :map => "Map API calls",
+ :upload => "Changeset diff uploads",
+ :amf => "AMF API calls",
+ :history => "Element history fetches",
+ :full => "Full element fetches",
+ :trkpts => "GPX trackpoints calls",
+ :web => "Web site traffic",
+ :other => "Other API calls"
+}
+
+def categorise_uri(line)
+ uri = line.split(" ")[1]
+
+ case uri
+ when /api\/0\.6\/map/ then :map
+ when /api\/0\.6\/changeset\/[0-9]*\/upload/ then :upload
+ when /api\/0\.6\/amf/ then :amf
+ when /api\/0\.6\/(node|way|relation)\/[0-9]*\/history/ then :history
+ when /api\/0\.6\/(node|way|relation)\/[0-9]*\/full/ then :full
+ when /api\/0\.6\/trackpoints/ then :trkpts
+ when /api\/0\.6\// then :other
+ else :web
+ end
+end
+
+server = $0.match("api_waits_(.*)")[1]
+
+if ARGV[0] == 'config'
+ puts "graph_title Wait times for active requests"
+ puts "graph_vlabel Average time of requests"
+ puts "graph_category api"
+ CALL_TYPES.each { |k, v| puts "#{k}.label #{v}" }
+
+else
+ counts = uri_and_times_from_status(server).
+ collect {|x,y| [categorise_uri(x), y]}.
+ inject(Hash.new) do |h, e|
+ category, time = e
+ if h.has_key? category
+ h[category] += [time]
+ else
+ h[category] = [time]
+ end
+ h
+ end
+
+ CALL_TYPES.keys.each do |type|
+ count = counts[type] || [0]
+ avg = count.inject(0){|x,y|x+y} / (1.0 * count.length)
+ puts "#{type}.value #{avg}"
+ end
+end