Sparklines and Turnaround Time Data
Uses Sparklines ruby library and MySQL. An example of the output can be seen here: http://labmedit.net/sparklines/TATS/workload/index.html
Only 3 files and two are basically the same but use different graph out puts.
This file will generate a sparkline from a MySQL database of data
require 'rubygems'
require "mysql"
require 'bigdecimal'
require 'fileutils'
require 'sparklines.rb'
class Array; def sum; inject( nil ) { |sum,x| sum ? sum+x : x }; end; end
class Array; def mean; sum / size; end; end
@output_dir = "TATS/medians"
FileUtils.mkdir_p(@output_dir)
my = Mysql::new("host", "user", "pass", "db")
res2 = my.query("select * from SetNames")
res2.each do |row1|
@query = "select * from data where SetName = '" + row1[0] + "' order by DateRecieved desc limit 50"
@counter = 0
res = my.query(@query)
@data= Array.new
@th= Array.new
@targets=Array.new
res.each do |row|
@data << row[2].to_f
@th << row[5].to_f
@targets << row[6].to_f
@counter = @counter +1
end
@average_no = @data.mean
@no_records = @data.length.to_s
@target = @targets.mean
if @target == -999
then @target= @th.mean
end
if @target == 0
then @target= @average_no
end
@name=row1[0]
if @counter >10 then
Sparklines.plot_to_file("#{@output_dir}/#{@name}.png",@data,
:type => "smooth",
:has_min => true,
:has_max => true,
:has_std_dev => true,
:has_last => true,
:target => @target,
:target_color => "#ff3399",
:line_color => 'black',
:label => @name + ' : No of records: '+ @no_records + " Median TAT: " + @average_no.to_s,
:height => 50
)
end
end
A final file just generates an HTML for display
require 'rubygems'
require 'tidy_table'
# HACK Make reference HTML file for viewing output
END {
def image_tag(image_path)
%()
end
reference_files = Dir['TATS/workload/*']
output = TidyTable.new(reference_files).to_html(%w(Workload Median-TATS)) do |record|
[image_tag("../../" + record), image_tag("../../" + record.gsub('workload', 'medians'))]
end
FileUtils.mkdir_p("TATS/workload")
File.open("TATS/workload/index.html", "w") do |f|
f.write <<-EOL
EOL
f.write output
end
}
Database has the following structure:
DROP TABLE IF EXISTS `tats`.`SetNames`;
CREATE TABLE `tats`.`SetNames` (
`SetName` varchar(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `tats`.`data`;
CREATE TABLE `tats`.`data` (
`SetName` varchar(50) NOT NULL,
`DateRecieved` date NOT NULL,
`NumberOfRequests` bigint(20) NOT NULL,
`AVGTurnaround` double NOT NULL,
`MedianTurnaround` double NOT NULL,
`95thTurnaround` double NOT NULL,
`Set_Target` double NOT NULL,
`Site` varchar(50) NOT NULL,
`TimePeriod` varchar(50) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


