# Copyright (c) 2009, Yoan Blanc # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the nor the # names of its contributors may be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY ''AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. require 'memcache' class LiveStats def initialize(app, cache, options={}) @app = app @cache = cache @period = options[:period] || 3600 @precision = options[:precision] || 10 end def inc(key, value=1, timeout=nil) if @cache.incr(key, value).nil? @cache.add(key, value, timeout || @period) end end def call(env) env["rack.livestats.track"] = true now = Time::now response = @app.call(env) if env["rack.livestats.track"] ts = ((Time::now - now) * 1000).to_i # in ms key = Time::now.to_i / @precision # time spent inc("t"+key.to_s, ts) # visits inc("v"+key.to_s) end response end end class MyApplication def initialize(cache, args={}) @cache = cache @period = args[:period] || 3600 @precision = args[:precision] || 10 end def prepare_data(keys, prefix) keys = keys.map do |key| prefix + key end values = @cache.get(*keys).reverse!.map do |value| value || 0 end max = values.max div = [max, 1].max.to_f / @precision values.map! do |value| (value / @precision.to_f / div * 100).to_i end return values, max end def stats(env) now = Time::now.to_i / @precision keys = (0..(@period / @precision)).map do |i| (now - i).to_s end views, vmax = prepare_data(keys, "v") times, tmax = prepare_data(keys, "t") [200, {'Content-Type' => 'text/html; charset=utf-8'}, ['', '', '', 'live stats', '

Live stats

', '

Visits in the last hour

', '' % [views.join(','), times.join(','), @period / 60, # in minutes vmax, tmax ], '' ] ] end def default(env) [200, {"Content-Type", "text/plain; charset=utf-8"}, ["You've been counted.\n"] ] end def call(env) if env["PATH_INFO"] == "/stats" # do not track the views on this page env["rack.livestats.track"] = false stats(env) else default(env) end end end cache = MemCache.new("127.0.0.1:11211") args = {:period => 3600, :precision => 10} use Rack::CommonLogger use LiveStats, cache, args run MyApplication.new(cache, args)