Install sitealizer stats
[selectricity-live] / vendor / plugins / sitealizer / lib / sitealizer.rb
1 # Copyright (c) 2007 Thiago Jackiw
2
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9
10 # The above copyright notice and this permission notice shall be included in all
11 # copies or substantial portions of the Software.
12
13 # The "Created with Sitealizer" footer text should not be removed from the 
14 # locations where it's currently shown (under the '/sitealizer' controller)
15
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 # SOFTWARE.
23
24 require 'yaml'
25
26 require 'app/models/site_tracker'
27 require 'app/controllers/sitealizer_controller'
28 require 'sitealizer/parser'
29
30 SA_CONFIG = YAML::load_file(File.dirname(__FILE__)+'/config.yml') unless defined?(SA_CONFIG)
31
32 module Sitealizer
33   
34     def initialize #:nodoc:
35       $visits = [] unless $visits
36     end
37
38     # This is the before_filter method that you will call when
39     # using the Sitealizer Web Stats plugin
40     # 
41     #   class ApplicationController < ActionController::Base
42     #     include Sitealizer
43     #     before_filter :use_sitealizer
44     #   end
45     # 
46     def use_sitealizer
47       $visits << request.env
48       if $visits.size == SA_CONFIG['sitealizer']['queue_size']
49         thread = Thread.start{store_visits}
50         thread.join
51         $visits = []
52       end
53     end
54     
55     private
56     def store_visits
57       begin
58         mutex = Mutex.new
59         mutex.lock
60         $visits.each do |env|
61           SiteTracker.create(
62             :user_agent => env['HTTP_USER_AGENT'],
63             :language => env['HTTP_ACCEPT_LANGUAGE'],
64             :path => env['PATH_INFO'],
65             :ip => env['REMOTE_ADDR'],
66             :referer => env['HTTP_REFERER']
67           )
68         end
69         open(File.dirname(__FILE__)+'/last_update','w'){|f| f.puts Time.now.to_s}
70         mutex.unlock
71       rescue => e
72         RAILS_ENV == 'production' ? logger.info(e) : logger.debug(e)
73       ensure
74         mutex.unlock
75       end
76     end
77
78 end

Benjamin Mako Hill || Want to submit a patch?