anerian-rack-analytics 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Anerian LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ = rack-analytics
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Anerian LLC. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-analytics"
8
+ gem.summary = %Q{A gem that provides rack middleware to capture analytics}
9
+ gem.email = "dev@anerian.com"
10
+ gem.homepage = "http://github.com/anerian/rack-analytics"
11
+ gem.authors = ["Jack Dempsey"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "rack-analytics #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 0
4
+ :patch: 0
@@ -0,0 +1,134 @@
1
+ require 'ruby-prof'
2
+ require 'ruby-debug'
3
+
4
+ $reporter = nil
5
+ $mutex = Mutex.new
6
+ module Rack
7
+ class Reporter
8
+ attr_accessor :logfile
9
+ def initialize(logfile=nil)
10
+ @logfile = logfile
11
+ end
12
+ end
13
+ # Set the profile=process_time query parameter to download a
14
+ # calltree profile of the request.
15
+ #
16
+ # Pass the :printer option to pick a different result format.
17
+ class Analytics
18
+ LOG_DIR = '.'
19
+ LOGFILE_MAX_SIZE = 1024 * 500# in bytes, ie 1 mb
20
+ LOGFILE_MAX_AGE = 3 # in seconds, ie 1 hour
21
+
22
+ MODES = %w(
23
+ process_time
24
+ wall_time
25
+ cpu_time
26
+ )
27
+
28
+ def initialize(app, options = {})
29
+ $reporter ||= Reporter.new
30
+ @app = app
31
+ @profile_type = :time
32
+ @write_type = :file
33
+ end
34
+
35
+ def call(env)
36
+ case @profile_type
37
+ when :prof
38
+ profile(env, mode)
39
+ when :time
40
+ start_time = Time.now
41
+ app_response = @app.call(env)
42
+ end_time = Time.now
43
+
44
+ time_taken = end_time - start_time
45
+
46
+ # dup, otherwise we screw up the env hash for rack
47
+ # also merge with an empty new Hash to create a real Hash and not a Mongrel::HttpParams 'hash'
48
+ @rack_env = Hash.new.merge(env.dup)
49
+
50
+ # a mix of IO and Action::* classes that rails can't to_yaml
51
+ @rack_env.delete('rack.errors')
52
+ @rack_env.delete('rack.input')
53
+ @rack_env.delete('action_controller.rescue.request')
54
+ @rack_env.delete('action_controller.rescue.response')
55
+ @rack_env.delete('rack.session')
56
+
57
+ data = {:time_taken => time_taken, :created_at => Time.now.to_i, :rack_env => @rack_env}
58
+
59
+ if @write_type == :file
60
+ $mutex.synchronize do
61
+ set_new_logfile unless $reporter.logfile
62
+ filename = $reporter.logfile
63
+ if logfile_needs_rotating?(filename)
64
+ filename = rotate_logfile(filename)
65
+ end
66
+ file_write(filename, data.to_yaml)
67
+ end
68
+ elsif @write_type == :db
69
+ ActiveRecord::Base.connection.insert("insert into log_entries (time_taken, details, created_at) values (#{time_taken}, #{ActiveRecord::Base.connection.quote(@rack_env)}, #{Time.now.to_i})")
70
+ end
71
+ app_response
72
+ else
73
+ @app.call(env)
74
+ end
75
+ end
76
+
77
+ def rotate_logfile(filename)
78
+ debug "rotating logfile"
79
+ `mv #{filename} archived.#{filename}`
80
+ set_new_logfile
81
+ end
82
+
83
+ def logfile_needs_rotating?(filename)
84
+ # this assumes an analysis.port_number.timestamp.log layout
85
+ created_at = filename.split('.')[2].to_i
86
+ (Time.now.to_i - created_at) > LOGFILE_MAX_AGE or (::File.exists?(filename) and ::File.size(filename) > LOGFILE_MAX_SIZE)
87
+ end
88
+
89
+ def set_new_logfile
90
+ $reporter.logfile = get_new_logfile_name
91
+ end
92
+
93
+ def get_new_logfile_name
94
+ "analysis.#{@rack_env['SERVER_PORT']}.#{Time.now.to_i}.log"
95
+ end
96
+
97
+ def profile(env, mode)
98
+ RubyProf.measure_mode = RubyProf.const_get(mode.upcase)
99
+
100
+ rails_response = []
101
+ result = RubyProf.profile do
102
+ rails_response = @app.call(env)
103
+ end
104
+
105
+ store_in_filesystem(result, env)
106
+
107
+ [200, {'Content-Type' => 'text/html'}, rails_response[2]]
108
+ end
109
+
110
+ def file_write(filename, data)
111
+ debug "writing out file #{filename}"
112
+ ::File.open(filename, 'a') {|file| file.write data}
113
+ end
114
+
115
+ def store_in_filesystem(result, env)
116
+ filename = "public/analytics.profile.#{timestamp}.txt"
117
+ string = StringIO.new
118
+ RubyProf::FlatPrinter.new(result).print(string, :min_percent => 0.01)
119
+ string.rewind
120
+ file_write(filename, string)
121
+
122
+ filename = "public/analytics.rack_env.#{timestamp}.txt"
123
+ file_write(filename, env.to_hash.to_yaml)
124
+ end
125
+
126
+ def timestamp
127
+ "%10.6f" % Time.now.to_f
128
+ end
129
+
130
+ def debug(text)
131
+ puts text if $DEBUG
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class RackAnalyticsTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'rack_analytics'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: anerian-rack-analytics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jack Dempsey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: dev@anerian.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/rack-analytics.rb
31
+ - test/rack_analytics_test.rb
32
+ - test/test_helper.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/anerian/rack-analytics
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: A gem that provides rack middleware to capture analytics
59
+ test_files:
60
+ - test/rack_analytics_test.rb
61
+ - test/test_helper.rb