To edit pages or tickets please login with username/password: aaf/aaf

root/trunk/demo/app/models/stats.rb

Revision 275, 0.9 kB (checked in by jk, 11 months ago)

--

Line 
1 class Stats < ActiveRecord::Base
2   def self.compute(kind)
3     start_date = minimum :created_at
4     sql = <<-END
5   select min(processing_time), max(processing_time), avg(processing_time), stddev(processing_time),
6          concat_ws(':', hour(timediff(created_at, ?)), lpad(minute(timediff(created_at, ?)), 2, '0')) as time,
7          group_concat(processing_time) as data
8       from stats
9       where kind=? group by time;
10   END
11     result = connection.execute sanitize_sql([sql, start_date, start_date, kind.to_s])
12     returning [] do |res|
13       while row = result.fetch_row
14         data = row.last.split(',').map{|t|t.to_i}
15         median = data.size.odd? ? data[data.size/2] : ((data[data.size/2-1]+data[data.size/2]) / 2.0)
16         res << { :min => row[0].to_f, :max => row[1].to_f, :avg => row[2].to_f, :stddev => row[3].to_f, :time => row[4], :median => median }
17       end
18     end
19   end
20 end
Note: See TracBrowser for help on using the browser.

To edit pages or tickets please login with username/password: aaf/aaf