request-log-analyzer 1.2.1 → 1.2.6

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.
Files changed (44) hide show
  1. data/.gitignore +10 -0
  2. data/Rakefile +3 -1
  3. data/lib/request_log_analyzer/aggregator/database.rb +79 -46
  4. data/lib/request_log_analyzer/controller.rb +12 -7
  5. data/lib/request_log_analyzer/file_format/merb.rb +5 -7
  6. data/lib/request_log_analyzer/line_definition.rb +9 -0
  7. data/lib/request_log_analyzer/request.rb +2 -2
  8. data/lib/request_log_analyzer/source/log_parser.rb +4 -0
  9. data/lib/request_log_analyzer.rb +1 -1
  10. data/request-log-analyzer.gemspec +36 -0
  11. data/spec/fixtures/header_and_footer.log +6 -0
  12. data/spec/fixtures/merb_prefixed.log +9 -0
  13. data/spec/integration/command_line_usage_spec.rb +0 -2
  14. data/spec/lib/{helper.rb → helpers.rb} +1 -3
  15. data/spec/lib/macros.rb +2 -0
  16. data/spec/lib/matchers.rb +63 -0
  17. data/spec/lib/mocks.rb +12 -1
  18. data/spec/lib/testing_format.rb +6 -0
  19. data/spec/spec.opts +3 -0
  20. data/spec/spec_helper.rb +13 -4
  21. data/spec/unit/aggregator/database_spec.rb +234 -0
  22. data/spec/unit/aggregator/summarizer_spec.rb +0 -2
  23. data/spec/unit/controller/controller_spec.rb +0 -2
  24. data/spec/unit/controller/log_processor_spec.rb +0 -2
  25. data/spec/unit/file_format/file_format_api_spec.rb +50 -71
  26. data/spec/unit/file_format/line_definition_spec.rb +49 -45
  27. data/spec/unit/file_format/merb_format_spec.rb +20 -2
  28. data/spec/unit/file_format/rails_format_spec.rb +0 -2
  29. data/spec/unit/filter/anonymize_filter_spec.rb +0 -1
  30. data/spec/unit/filter/field_filter_spec.rb +0 -3
  31. data/spec/unit/filter/filter_spec.rb +17 -0
  32. data/spec/unit/filter/timespan_filter_spec.rb +0 -3
  33. data/spec/unit/source/log_parser_spec.rb +5 -4
  34. data/spec/unit/source/request_spec.rb +91 -52
  35. data/spec/unit/tracker/duration_tracker_spec.rb +0 -6
  36. data/spec/unit/tracker/frequency_tracker_spec.rb +0 -6
  37. data/spec/unit/tracker/hourly_spread_spec.rb +0 -4
  38. data/spec/unit/tracker/timespan_tracker_spec.rb +0 -4
  39. data/spec/unit/tracker/tracker_api_spec.rb +0 -2
  40. data/tasks/github-gem.rake +199 -192
  41. metadata +120 -92
  42. data/RELEASE_NOTES.rdoc +0 -10
  43. data/spec/unit/aggregator/database_inserter_spec.rb +0 -106
  44. data/tasks/rspec.rake +0 -12
@@ -1,249 +1,256 @@
1
1
  require 'rubygems'
2
- require 'rubyforge'
3
2
  require 'rake'
4
3
  require 'rake/tasklib'
5
4
  require 'date'
5
+ require 'git'
6
6
 
7
- module Rake
7
+ module GithubGem
8
8
 
9
- class GithubGem < TaskLib
10
-
11
- attr_accessor :name
12
- attr_accessor :specification
13
-
14
- def self.define_tasks!
15
- gem_task_builder = Rake::GithubGem.new
16
- gem_task_builder.register_all_tasks!
17
- end
18
-
19
- def initialize
20
- reload_gemspec!
9
+ def self.detect_gemspec_file
10
+ FileList['*.gemspec'].first
11
+ end
12
+
13
+ def self.detect_main_include
14
+ if detect_gemspec_file =~ /^(\.*)\.gemspec$/ && File.exist?("lib/#{$1}.rb")
15
+ "lib/#{$1}.rb"
16
+ elsif FileList['lib/*.rb'].length == 1
17
+ FileList['lib/*.rb'].first
18
+ else
19
+ raise "Could not detect main include file!"
21
20
  end
21
+ end
22
+
23
+ class RakeTasks
24
+
25
+ attr_reader :gemspec, :modified_files, :git
26
+ attr_accessor :gemspec_file, :task_namespace, :main_include, :root_dir, :spec_pattern, :test_pattern, :remote, :remote_branch, :local_branch
27
+
28
+ def initialize(task_namespace = :gem)
29
+ @gemspec_file = GithubGem.detect_gemspec_file
30
+ @task_namespace = task_namespace
31
+ @main_include = GithubGem.detect_main_include
32
+ @modified_files = []
33
+ @root_dir = Dir.pwd
34
+ @test_pattern = 'test/**/*_test.rb'
35
+ @spec_pattern = 'spec/**/*_spec.rb'
36
+ @local_branch = 'master'
37
+ @remote = 'origin'
38
+ @remote_branch = 'master'
39
+
40
+
41
+ yield(self) if block_given?
22
42
 
23
- def register_all_tasks!
24
- namespace(:gem) do
25
- desc "Updates the file lists for this gem"
26
- task(:manifest) { manifest_task }
43
+ @git = Git.open(@root_dir)
44
+ load_gemspec!
45
+ define_tasks!
46
+ end
47
+
48
+ protected
49
+
50
+ def define_test_tasks!
51
+ require 'rake/testtask'
27
52
 
28
- desc "Releases a new version of #{@name}"
29
- task(:build => [:manifest]) { build_task }
30
-
31
-
32
- release_dependencies = [:check_clean_master_branch, :version, :build, :create_tag]
33
- release_dependencies.push 'doc:publish' if has_rdoc?
34
- release_dependencies.unshift 'test' if has_tests?
35
- release_dependencies.unshift 'spec' if has_specs?
36
-
37
- desc "Releases a new version of #{@name}"
38
- task(:release => release_dependencies) { release_task }
39
-
40
- # helper task for releasing
41
- task(:check_clean_master_branch) { verify_clean_status('master') }
42
- task(:check_version) { verify_version(ENV['VERSION'] || @specification.version) }
43
- task(:version => [:check_version]) { set_gem_version! }
44
- task(:create_tag) { create_version_tag! }
45
- end
46
-
47
- # Register RDoc tasks
48
- if has_rdoc?
49
- require 'rake/rdoctask'
50
-
51
- namespace(:doc) do
52
- desc 'Generate documentation for request-log-analyzer'
53
- Rake::RDocTask.new(:compile) do |rdoc|
54
- rdoc.rdoc_dir = 'doc'
55
- rdoc.title = @name
56
- rdoc.options += @specification.rdoc_options
57
- rdoc.rdoc_files.include(@specification.extra_rdoc_files)
58
- rdoc.rdoc_files.include('lib/**/*.rb')
59
- end
60
-
61
- desc "Publish RDoc files for #{@name} to Github"
62
- task(:publish => :compile) do
63
- sh 'git checkout gh-pages'
64
- sh 'git pull origin gh-pages'
65
- sh 'cp -rf doc/* .'
66
- sh "git commit -am \"Publishing newest RDoc documentation for #{@name}\""
67
- sh "git push origin gh-pages"
68
- sh "git checkout master"
69
- end
53
+ namespace(:test) do
54
+ Rake::TestTask.new(:basic) do |t|
55
+ t.pattern = test_pattern
56
+ t.verbose = true
57
+ t.libs << 'test'
70
58
  end
71
59
  end
60
+
61
+ desc "Run all unit tests for #{gemspec.name}"
62
+ task(:test => ['test:basic'])
63
+ end
72
64
 
73
- # Setup :spec task if RSpec files exist
74
- if has_specs?
75
- require 'spec/rake/spectask'
65
+ def define_rspec_tasks!
66
+ require 'spec/rake/spectask'
76
67
 
77
- desc "Run all specs for #{@name}"
78
- Spec::Rake::SpecTask.new(:spec) do |t|
79
- t.spec_files = FileList['spec/**/*_spec.rb']
68
+ namespace(:spec) do
69
+ desc "Verify all RSpec examples for #{gemspec.name}"
70
+ Spec::Rake::SpecTask.new(:basic) do |t|
71
+ t.spec_files = FileList[spec_pattern]
72
+ end
73
+
74
+ desc "Verify all RSpec examples for #{gemspec.name} and output specdoc"
75
+ Spec::Rake::SpecTask.new(:specdoc) do |t|
76
+ t.spec_files = FileList[spec_pattern]
77
+ t.spec_opts << '--format' << 'specdoc' << '--color'
80
78
  end
79
+
80
+ desc "Run RCov on specs for #{gemspec.name}"
81
+ Spec::Rake::SpecTask.new(:rcov) do |t|
82
+ t.spec_files = FileList[spec_pattern]
83
+ t.rcov = true
84
+ t.rcov_opts = ['--exclude', '"spec/*,gems/*"', '--rails']
85
+ end
81
86
  end
82
87
 
83
- # Setup :test task if unit test files exist
84
- if has_tests?
85
- require 'rake/testtask'
86
-
87
- desc "Run all unit tests for #{@name}"
88
- Rake::TestTask.new(:test) do |t|
89
- t.pattern = 'test/**/*_test.rb'
90
- t.verbose = true
91
- t.libs << 'test'
92
- end
93
- end
88
+ desc "Verify all RSpec examples for #{gemspec.name} and output specdoc"
89
+ task(:spec => ['spec:specdoc'])
94
90
  end
95
91
 
96
- protected
92
+ # Defines the rake tasks
93
+ def define_tasks!
94
+
95
+ define_test_tasks! if has_tests?
96
+ define_rspec_tasks! if has_specs?
97
+
98
+ namespace(@task_namespace) do
99
+ desc "Updates the filelist in the gemspec file"
100
+ task(:manifest) { manifest_task }
101
+
102
+ desc "Builds the .gem package"
103
+ task(:build => :manifest) { build_task }
97
104
 
98
- def has_rdoc?
99
- @specification.has_rdoc
100
- end
105
+ desc "Sets the version of the gem in the gemspec"
106
+ task(:set_version => [:check_version, :check_current_branch]) { version_task }
107
+ task(:check_version => :fetch_origin) { check_version_task }
108
+
109
+ task(:fetch_origin) { fetch_origin_task }
110
+ task(:check_current_branch) { check_current_branch_task }
111
+ task(:check_clean_status) { check_clean_status_task }
112
+ task(:check_not_diverged => :fetch_origin) { check_not_diverged_task }
113
+
114
+ checks = [:check_current_branch, :check_clean_status, :check_not_diverged, :check_version]
115
+ checks.unshift('spec:basic') if has_specs?
116
+ checks.unshift('test:basic') if has_tests?
117
+
118
+ desc "Perform all checks that would occur before a release"
119
+ task(:release_checks => checks)
101
120
 
102
- def has_specs?
103
- Dir['spec/**/*_spec.rb'].any?
121
+ desc "Release a new verison of the gem"
122
+ task(:release => [:release_checks, :set_version, :build, :push_changes]) { release_task }
123
+
124
+ task(:push_changes => [:commit_modified_files, :tag_version]) { push_changes_task }
125
+ task(:tag_version) { tag_version_task }
126
+ task(:commit_modified_files) { commit_modified_files_task }
127
+ end
104
128
  end
105
129
 
106
- def has_tests?
107
- Dir['test/**/*_test.rb'].any?
108
- end
109
-
110
- def reload_gemspec!
111
- raise "No gemspec file found!" if gemspec_file.nil?
112
- spec = File.read(gemspec_file)
113
- @specification = eval(spec)
114
- @name = specification.name
115
- end
116
-
117
- def run_command(command)
118
- lines = []
119
- IO.popen(command) { |f| lines = f.readlines }
120
- return lines
130
+ def manifest_task
131
+ # Load all the gem's files using "git ls-files"
132
+ repository_files = git.ls_files.keys
133
+ test_files = Dir[test_pattern] + Dir[spec_pattern]
134
+
135
+ update_gemspec(:files, repository_files)
136
+ update_gemspec(:test_files, repository_files & test_files)
121
137
  end
122
138
 
123
- def git_modified?(file)
124
- return !run_command('git status').detect { |line| Regexp.new(Regexp.quote(file)) =~ line }.nil?
139
+ def build_task
140
+ sh "gem build -q #{gemspec_file}"
141
+ Dir.mkdir('pkg') unless File.exist?('pkg')
142
+ sh "mv #{gemspec.name}-#{gemspec.version}.gem pkg/#{gemspec.name}-#{gemspec.version}.gem"
125
143
  end
126
144
 
127
- def git_commit_file(file, message, branch = nil)
128
- verify_current_branch(branch) unless branch.nil?
129
- if git_modified?(file)
130
- sh "git add #{file}"
131
- sh "git commit -m \"#{message}\""
132
- else
133
- raise "#{file} is not modified and cannot be committed!"
134
- end
145
+ def version_task
146
+ update_gemspec(:version, ENV['VERSION']) if ENV['VERSION']
147
+ update_gemspec(:date, Date.today)
148
+
149
+ update_version_file(gemspec.version)
150
+ update_version_constant(gemspec.version)
135
151
  end
136
152
 
137
- def git_create_tag(tag_name, message)
138
- sh "git tag -a \"#{tag_name}\" -m \"#{message}\""
153
+ def check_version_task
154
+ raise "#{ENV['VERSION']} is not a valid version number!" if ENV['VERSION'] && !Gem::Version.correct?(ENV['VERSION'])
155
+ proposed_version = Gem::Version.new(ENV['VERSION'] || gemspec.version)
156
+ # Loads the latest version number using the created tags
157
+ newest_version = git.tags.map { |tag| tag.name.split('-').last }.compact.map { |v| Gem::Version.new(v) }.max
158
+ raise "This version (#{proposed_version}) is not higher than the highest tagged version (#{newest_version})" if newest_version && newest_version >= proposed_version
139
159
  end
140
160
 
141
- def git_push(remote = 'origin', branch = 'master', options = [])
142
- verify_clean_status(branch)
143
- options_str = options.map { |o| "--#{o}"}.join(' ')
144
- sh "git push #{options_str} #{remote} #{branch}"
161
+ def check_not_diverged_task
162
+ raise "The current branch is diverged from the remote branch!" if git.log.between('HEAD', git.branches["#{remote}/#{remote_branch}"].gcommit).any?
145
163
  end
146
164
 
147
- def gemspec_version=(new_version)
148
- spec = File.read(gemspec_file)
149
- spec.gsub!(/^(\s*s\.version\s*=\s*)('|")(.+)('|")(\s*)$/) { "#{$1}'#{new_version}'#{$5}" }
150
- spec.gsub!(/^(\s*s\.date\s*=\s*)('|")(.+)('|")(\s*)$/) { "#{$1}'#{Date.today.strftime('%Y-%m-%d')}'#{$5}" }
151
- File.open(gemspec_file, 'w') { |f| f << spec }
152
- reload_gemspec!
165
+ def check_clean_status_task
166
+ raise "The current working copy contains modifications" if git.status.changed.any?
153
167
  end
154
168
 
155
- def gemspec_date=(new_date)
156
- spec = File.read(gemspec_file)
157
- spec.gsub!(/^(\s*s\.date\s*=\s*)('|")(.+)('|")(\s*)$/) { "#{$1}'#{new_date.strftime('%Y-%m-%d')}'#{$5}" }
158
- File.open(gemspec_file, 'w') { |f| f << spec }
159
- reload_gemspec!
169
+ def check_current_branch_task
170
+ raise "Currently not on #{local_branch} branch!" unless git.branch.name == local_branch.to_s
160
171
  end
161
172
 
162
- def gemspec_file
163
- @gemspec_file ||= Dir['*.gemspec'].first
173
+ def fetch_origin_task
174
+ git.fetch('origin')
164
175
  end
165
176
 
166
- def verify_current_branch(branch)
167
- run_command('git branch').detect { |line| /^\* (.+)/ =~ line }
168
- raise "You are currently not working in the master branch!" unless branch == $1
177
+ def commit_modified_files_task
178
+ if modified_files.any?
179
+ modified_files.each { |file| git.add(file) }
180
+ git.commit("Released #{gemspec.name} gem version #{gemspec.version}")
181
+ end
169
182
  end
170
183
 
171
- def verify_clean_status(on_branch = nil)
172
- sh "git fetch"
173
- lines = run_command('git status')
174
- raise "You don't have the most recent version available. Run git pull first." if /^\# Your branch is behind/ =~ lines[1]
175
- raise "You are currently not working in the #{on_branch} branch!" unless on_branch.nil? || (/^\# On branch (.+)/ =~ lines.first && $1 == on_branch)
176
- raise "Your master branch contains modifications!" unless /^nothing to commit \(working directory clean\)/ =~ lines.last
184
+ def tag_version_task
185
+ git.add_tag("#{gemspec.name}-#{gemspec.version}")
177
186
  end
178
187
 
179
- def verify_version(new_version)
180
- newest_version = run_command('git tag').map { |tag| tag.split(name + '-').last }.compact.map { |v| Gem::Version.new(v) }.max
181
- raise "This version number (#{new_version}) is not higher than the highest tagged version (#{newest_version})" if !newest_version.nil? && newest_version >= Gem::Version.new(new_version.to_s)
188
+ def push_changes_task
189
+ git.push(remote, remote_branch, true)
182
190
  end
183
191
 
184
- def set_gem_version!
185
- # update gemspec file
186
- self.gemspec_version = ENV['VERSION'] if Gem::Version.correct?(ENV['VERSION'])
187
- self.gemspec_date = Date.today
192
+ def release_task
193
+ puts
194
+ puts '------------------------------------------------------------'
195
+ puts "Released #{gemspec.name} version #{gemspec.version}"
188
196
  end
189
-
190
- def manifest_task
191
- verify_current_branch('master')
192
-
193
- list = Dir['**/*'].sort
194
- list -= [gemspec_file]
195
-
196
- if File.exist?('.gitignore')
197
- File.read('.gitignore').each_line do |glob|
198
- glob = glob.chomp.sub(/^\//, '')
199
- list -= Dir[glob]
200
- list -= Dir["#{glob}/**/*"] if File.directory?(glob) and !File.symlink?(glob)
201
- end
202
- end
203
-
204
- # update the spec file
205
- spec = File.read(gemspec_file)
206
- spec.gsub! /^(\s* s.(test_)?files \s* = \s* )( \[ [^\]]* \] | %w\( [^)]* \) )/mx do
207
- assignment = $1
208
- bunch = $2 ? list.grep(/^(test.*_test\.rb|spec.*_spec.rb)$/) : list
209
- '%s%%w(%s)' % [assignment, bunch.join(' ')]
210
- end
211
-
212
- File.open(gemspec_file, 'w') { |f| f << spec }
213
- reload_gemspec!
197
+
198
+ private
199
+
200
+ def has_specs?
201
+ FileList[spec_pattern].any?
214
202
  end
215
203
 
216
- def build_task
217
- sh "gem build #{gemspec_file}"
218
- Dir.mkdir('pkg') unless File.exist?('pkg')
219
- sh "mv #{name}-#{specification.version}.gem pkg/#{name}-#{specification.version}.gem"
204
+ def has_tests?
205
+ FileList[test_pattern].any?
220
206
  end
221
207
 
222
- def install_task
223
- raise "#{name} .gem file not found" unless File.exist?("pkg/#{name}-#{specification.version}.gem")
224
- sh "gem install pkg/#{name}-#{specification.version}.gem"
208
+
209
+ # Loads the gemspec file
210
+ def load_gemspec!
211
+ @gemspec = eval(File.read(@gemspec_file))
225
212
  end
226
213
 
227
- def uninstall_task
228
- raise "#{name} .gem file not found" unless File.exist?("pkg/#{name}-#{specification.version}.gem")
229
- sh "gem uninstall #{name}"
230
- end
214
+ # Updates the VERSION file with the new version
215
+ def update_version_file(version)
216
+ if File.exists?('VERSION')
217
+ File.open('VERSION', 'w') { |f| f << version.to_s }
218
+ modified_files << 'VERSION'
219
+ end
220
+ end
231
221
 
232
- def create_version_tag!
233
- # commit the gemspec file
234
- git_commit_file(gemspec_file, "Updated #{gemspec_file} for release of version #{@specification.version}") if git_modified?(gemspec_file)
235
-
236
- # create tag and push changes
237
- git_create_tag("#{@name}-#{@specification.version}", "Tagged version #{@specification.version}")
238
- git_push('origin', 'master', [:tags])
222
+ # Updates the VERSION constant in the main include file if it exists
223
+ def update_version_constant(version)
224
+ file_contents = File.read(main_include)
225
+ if file_contents.sub!(/^(\s+VERSION\s*=\s*)[^\s].*$/) { $1 + version.to_s.inspect }
226
+ File.open(main_include, 'w') { |f| f << file_contents }
227
+ modified_files << main_include
228
+ end
239
229
  end
240
230
 
241
- def release_task
242
- puts
243
- puts '------------------------------------------------------------'
244
- puts "Released #{@name} - version #{@specification.version}"
231
+ # Updates an attribute of the gemspec file.
232
+ # This function will open the file, and search/replace the attribute using a regular expression.
233
+ def update_gemspec(attribute, new_value, literal = false)
234
+
235
+ unless literal
236
+ new_value = case new_value
237
+ when Array then "%w(#{new_value.join(' ')})"
238
+ when Hash, String then new_value.inspect
239
+ when Date then new_value.strftime('%Y-%m-%d').inspect
240
+ else raise "Cannot write value #{new_value.inspect} to gemspec file!"
241
+ end
242
+ end
243
+
244
+ spec = File.read(gemspec_file)
245
+ regexp = Regexp.new('^(\s+\w+\.' + Regexp.quote(attribute.to_s) + '\s*=\s*)[^\s].*$')
246
+ if spec.sub!(regexp) { $1 + new_value }
247
+ File.open(gemspec_file, 'w') { |f| f << spec }
248
+ modified_files << gemspec_file
249
+
250
+ # Reload the gemspec so the changes are incorporated
251
+ load_gemspec!
252
+ end
245
253
  end
254
+
246
255
  end
247
256
  end
248
-
249
- Rake::GithubGem.define_tasks!