request-log-analyzer 1.2.1 → 1.2.3

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 (35) hide show
  1. data/Rakefile +1 -1
  2. data/lib/request_log_analyzer/aggregator/database.rb +59 -35
  3. data/lib/request_log_analyzer/controller.rb +12 -7
  4. data/lib/request_log_analyzer/line_definition.rb +9 -0
  5. data/lib/request_log_analyzer/source/log_parser.rb +4 -0
  6. data/spec/fixtures/header_and_footer.log +6 -0
  7. data/spec/integration/command_line_usage_spec.rb +0 -2
  8. data/spec/lib/{helper.rb → helpers.rb} +1 -3
  9. data/spec/lib/macros.rb +2 -0
  10. data/spec/lib/matchers.rb +63 -0
  11. data/spec/lib/testing_format.rb +6 -0
  12. data/spec/spec.opts +3 -0
  13. data/spec/spec_helper.rb +13 -4
  14. data/spec/unit/aggregator/database_spec.rb +208 -0
  15. data/spec/unit/aggregator/summarizer_spec.rb +0 -2
  16. data/spec/unit/controller/controller_spec.rb +0 -2
  17. data/spec/unit/controller/log_processor_spec.rb +0 -2
  18. data/spec/unit/file_format/file_format_api_spec.rb +50 -71
  19. data/spec/unit/file_format/line_definition_spec.rb +49 -45
  20. data/spec/unit/file_format/merb_format_spec.rb +0 -1
  21. data/spec/unit/file_format/rails_format_spec.rb +0 -2
  22. data/spec/unit/filter/anonymize_filter_spec.rb +0 -1
  23. data/spec/unit/filter/field_filter_spec.rb +0 -3
  24. data/spec/unit/filter/filter_spec.rb +17 -0
  25. data/spec/unit/filter/timespan_filter_spec.rb +0 -3
  26. data/spec/unit/source/log_parser_spec.rb +5 -4
  27. data/spec/unit/source/request_spec.rb +17 -4
  28. data/spec/unit/tracker/duration_tracker_spec.rb +0 -6
  29. data/spec/unit/tracker/frequency_tracker_spec.rb +0 -6
  30. data/spec/unit/tracker/hourly_spread_spec.rb +0 -4
  31. data/spec/unit/tracker/timespan_tracker_spec.rb +0 -4
  32. data/spec/unit/tracker/tracker_api_spec.rb +0 -2
  33. data/tasks/rspec.rake +8 -1
  34. metadata +12 -6
  35. data/spec/unit/aggregator/database_inserter_spec.rb +0 -106
@@ -1,8 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe RequestLogAnalyzer::Aggregator::Summarizer do
4
-
5
- include RequestLogAnalyzer::Spec::Helper
6
4
 
7
5
  before(:each) do
8
6
  @summarizer = RequestLogAnalyzer::Aggregator::Summarizer.new(mock_source, :output => mock_output)
@@ -2,8 +2,6 @@ require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe RequestLogAnalyzer::Controller do
4
4
 
5
- include RequestLogAnalyzer::Spec::Helper
6
-
7
5
  it "should use a custom output generator correctly" do
8
6
 
9
7
  mock_output = mock('RequestLogAnalyzer::Output::Base')
@@ -4,8 +4,6 @@ require 'request_log_analyzer/log_processor'
4
4
 
5
5
  describe RequestLogAnalyzer::LogProcessor, 'stripping log files' do
6
6
 
7
- include RequestLogAnalyzer::Spec::Helper
8
-
9
7
  before(:each) do
10
8
  @log_stripper = RequestLogAnalyzer::LogProcessor.new(testing_format, :strip, {})
11
9
  end
@@ -1,90 +1,69 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
- describe RequestLogAnalyzer::FileFormat, :format_definition do
3
+ describe RequestLogAnalyzer::FileFormat do
4
+
5
+ describe ".format_definition" do
4
6
 
5
- before(:each) do
6
- @first_file_format = Class.new(RequestLogAnalyzer::FileFormat::Base)
7
- @second_file_format = Class.new(RequestLogAnalyzer::FileFormat::Base)
8
- end
9
-
10
- it "should specify lines with a hash" do
11
-
12
- @first_file_format.new.should have(0).line_definitions
13
-
14
- @first_file_format.format_definition do |line|
15
- line.hash_test :regexp => /test/, :captures => []
7
+ before(:each) do
8
+ @first_file_format = Class.new(RequestLogAnalyzer::FileFormat::Base)
9
+ @second_file_format = Class.new(RequestLogAnalyzer::FileFormat::Base)
16
10
  end
17
11
 
18
- @format_instance = @first_file_format.new
19
- @format_instance.should have(1).line_definitions
20
- @format_instance.line_definitions[:hash_test].should_not be_nil
21
- end
22
-
23
- it "should specift lines directly" do
24
- @first_file_format.new.should have(0).line_definitions
25
-
26
- @first_file_format.format_definition.direct_test do |line|
27
- line.regexp = /test/
28
- end
29
-
30
- @first_file_format.new.line_definitions[:direct_test].should_not be_nil
31
- end
12
+ it "should specify line definitions directly within the file_format" do
13
+ @first_file_format.format_definition.direct_test :regexp => /test/
14
+ @first_file_format.should have_line_definition(:direct_test)
15
+ end
32
16
 
33
- it "specify lines with a block" do
34
-
35
- @first_file_format.new.should have(0).line_definitions
17
+ it "specify lines with a block for the format definition" do
18
+ @first_file_format.format_definition do |format|
19
+ format.block_test :regexp => /test (\w+)/, :captures => [:tester]
20
+ end
21
+
22
+ @first_file_format.should have_line_definition(:block_test).capturing(:tester)
23
+ end
36
24
 
37
- @first_file_format.format_definition do |format|
38
- format.block_test do |line|
25
+ it "should specify a line with a block" do
26
+ @first_file_format.format_definition.hash_test do |line|
39
27
  line.regexp = /test/
40
28
  line.captures = []
41
29
  end
42
- end
43
-
44
- @first_file_format.new.line_definitions[:block_test].should_not be_nil
45
- end
46
-
47
- it "should define lines only for itself" do
48
30
 
49
- @first_file_format.format_definition do |line|
50
- line.first_test :regexp => /test/, :captures => []
31
+ @first_file_format.should have_line_definition(:hash_test)
51
32
  end
52
-
53
-
54
- @second_file_format.format_definition do |line|
55
- line.second_test :regexp => /test/, :captures => []
56
- end
57
-
58
- @first_file_format.line_definer.should_not eql(@second_file_format.line_definer)
59
- @first_file_format.new.should have(1).line_definitions
60
- @second_file_format.new.line_definitions[:second_test].should_not be_nil
61
- end
62
- end
63
-
64
- describe RequestLogAnalyzer::FileFormat, :load do
65
-
66
- include RequestLogAnalyzer::Spec::Helper
67
-
68
- it "should return an instance of a FileFormat class" do
69
- @file_format = RequestLogAnalyzer::FileFormat.load(TestingFormat)
70
- @file_format.should be_kind_of(TestingFormat)
33
+
34
+ it "should define lines only for its own language" do
35
+ @first_file_format.format_definition.first :regexp => /test 123/
36
+ @second_file_format.format_definition.second :regexp => /test 456/
37
+
38
+ @first_file_format.should have_line_definition(:first)
39
+ @first_file_format.should_not have_line_definition(:second)
40
+ @second_file_format.should_not have_line_definition(:first)
41
+ @second_file_format.should have_line_definition(:second)
42
+ end
71
43
  end
72
44
 
45
+ describe ".load" do
73
46
 
74
- it "should return itself if it already is a FileFormat::Base instance" do
75
- @file_format = RequestLogAnalyzer::FileFormat.load(testing_format)
76
- @file_format.should be_kind_of(TestingFormat)
77
- end
47
+ it "should return an instance of a FileFormat class" do
48
+ @file_format = RequestLogAnalyzer::FileFormat.load(TestingFormat)
49
+ @file_format.should be_kind_of(TestingFormat)
50
+ end
78
51
 
79
- it "should load a predefined file format from the /file_format dir" do
80
- @file_format = RequestLogAnalyzer::FileFormat.load(:rails)
81
- @file_format.should be_kind_of(RequestLogAnalyzer::FileFormat::Rails)
82
- end
52
+ it "should return itself if it already is a FileFormat::Base instance" do
53
+ @file_format = RequestLogAnalyzer::FileFormat.load(testing_format)
54
+ @file_format.should be_kind_of(TestingFormat)
55
+ end
56
+
57
+ it "should load a predefined file format from the /file_format dir" do
58
+ @file_format = RequestLogAnalyzer::FileFormat.load(:rails)
59
+ @file_format.should be_kind_of(RequestLogAnalyzer::FileFormat::Rails)
60
+ end
83
61
 
84
- it "should load a provided format file" do
85
- format_filename = File.dirname(__FILE__) + '/../../lib/testing_format.rb'
86
- @file_format = RequestLogAnalyzer::FileFormat.load(format_filename)
87
- @file_format.should be_kind_of(TestingFormat)
88
- end
62
+ it "should load a provided format file" do
63
+ format_filename = File.dirname(__FILE__) + '/../../lib/testing_format.rb'
64
+ @file_format = RequestLogAnalyzer::FileFormat.load(format_filename)
65
+ @file_format.should be_kind_of(TestingFormat)
66
+ end
89
67
 
68
+ end
90
69
  end
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
- describe RequestLogAnalyzer::LineDefinition, :parsing do
4
-
3
+ describe RequestLogAnalyzer::LineDefinition do
4
+
5
5
  before(:each) do
6
6
  @line_definition = RequestLogAnalyzer::LineDefinition.new(:test, {
7
7
  :teaser => /Testing /,
@@ -10,55 +10,59 @@ describe RequestLogAnalyzer::LineDefinition, :parsing do
10
10
  })
11
11
  end
12
12
 
13
- it "should return false on an unmatching line" do
14
- (@line_definition =~ "nonmatching").should be_false
15
- end
16
-
17
- it "should return false when only the teaser matches" do
18
- (@line_definition =~ "Testing LineDefinition").should be_false
19
- end
13
+ describe '#matches' do
20
14
 
21
- it "should return a hash if the line matches" do
22
- (@line_definition =~ "Testing LineDefinition, tries: 123").should be_kind_of(Hash)
23
- end
15
+ it "should return false on an unmatching line" do
16
+ @line_definition.should_not parse("nonmatching")
17
+ end
24
18
 
25
- it "should return a hash with :captures set to an array" do
26
- hash = @line_definition.matches("Testing LineDefinition, tries: 123")
27
- hash[:captures][0].should == "LineDefinition"
28
- hash[:captures][1].should == "123"
29
- end
19
+ it "should return false when only the teaser matches" do
20
+ @line_definition.should_not parse("Testing LineDefinition")
21
+ end
30
22
 
31
- it "should return a hash with :line_definition set" do
32
- @line_definition.matches("Testing LineDefinition, tries: 123")[:line_definition].should == @line_definition
23
+ it "should parse a line and capture the expected values" do
24
+ @line_definition.should parse("Testing LineDefinition, tries: 123").capturing('LineDefinition', '123')
25
+ end
33
26
  end
34
- end
35
27
 
36
- describe RequestLogAnalyzer::LineDefinition, :converting do
28
+ describe '#convert_captured_values' do
37
29
 
38
- include RequestLogAnalyzer::Spec::Helper
39
-
40
- before(:each) do
41
- @file_format = testing_format
42
- @request = @file_format.request
43
- end
44
-
45
- it "should convert captures to a hash of converted values" do
46
- hash = @file_format.line_definitions[:first].convert_captured_values(["456"], @request)
47
- hash[:request_no].should == 456
48
- end
30
+ before(:each) do
31
+ @request = mock('request')
32
+ @request.stub!(:convert_value).and_return('foo')
33
+ end
34
+
35
+ it "should call convert_value for every captured value" do
36
+ @request.should_receive(:convert_value).twice
37
+ @line_definition.convert_captured_values(['test', '123'], @request)
38
+ end
39
+
40
+ it "should set the converted values" do
41
+ @line_definition.convert_captured_values(['test', '123'], @request).should == {:what => 'foo', :tries => 'foo'}
42
+ end
43
+
44
+ context 'when using :provides option' do
45
+ before(:each) do
46
+ @ld = RequestLogAnalyzer::LineDefinition.new(:test, :regexp => /Hash\: (\{.+\})/,
47
+ :captures => [{ :name => :hash, :type => :hash, :provides => {:bar => :string}}])
48
+
49
+ @request = mock('request')
49
50
 
50
- it "should convert captures to a hash" do
51
- hash = @file_format.line_definitions[:test].convert_captured_values(["great", nil], @request)
52
- hash[:test_capture].should == 'Testing is great'
53
- hash[:duration].should be_nil
51
+ @request.stub!(:convert_value).with("{:bar=>'baz'}", anything).and_return(:bar => 'baz')
52
+ @request.stub!(:convert_value).with('baz', anything).and_return('foo')
53
+ end
54
+
55
+ it "should call Request#convert_value for the initial hash and the value in the hash" do
56
+ @request.should_receive(:convert_value).with("{:bar=>'baz'}", anything).and_return(:bar => 'baz')
57
+ @request.should_receive(:convert_value).with("baz", anything)
58
+ @ld.convert_captured_values(["{:bar=>'baz'}"], @request)
59
+ end
60
+
61
+ it "should set the provides fields" do
62
+ # The captures field must be set and converted as well
63
+ @ld.convert_captured_values(["{:bar=>'baz'}"], @request)[:bar].should eql('foo')
64
+ end
65
+ end
66
+
54
67
  end
55
-
56
- it "should merge a hash capture into the line hash" do
57
- hash = @file_format.line_definitions[:eval].convert_captured_values(["{ 'greating' => 'hello', 'what' => 'world'}"], @request)
58
- hash[:evaluated].should == { :greating => "hello", :what => "world"}
59
- hash[:greating].should == 'hello'
60
- hash[:what].should == 'world'
61
- end
62
-
63
-
64
68
  end
@@ -1,7 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe RequestLogAnalyzer::Source::LogParser, :merb do
4
- include RequestLogAnalyzer::Spec::Helper
5
4
 
6
5
  before(:each) do
7
6
  @log_parser = RequestLogAnalyzer::Source::LogParser.new(RequestLogAnalyzer::FileFormat.load(:merb))
@@ -1,7 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe RequestLogAnalyzer::Source::LogParser, "Rails" do
4
- include RequestLogAnalyzer::Spec::Helper
5
4
 
6
5
  before(:each) do
7
6
  @log_parser = RequestLogAnalyzer::Source::LogParser.new(
@@ -79,7 +78,6 @@ describe RequestLogAnalyzer::Source::LogParser, "Rails" do
79
78
  end
80
79
 
81
80
  describe RequestLogAnalyzer::FileFormat::RailsDevelopment, "Rails with development details" do
82
- include RequestLogAnalyzer::Spec::Helper
83
81
 
84
82
  before(:each) do
85
83
  @file_format = RequestLogAnalyzer::FileFormat.load(:rails_development)
@@ -1,7 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe RequestLogAnalyzer::Filter::Anonymize, 'anonymize request' do
4
- include RequestLogAnalyzer::Spec::Helper
5
4
 
6
5
  before(:each) do
7
6
  @filter = RequestLogAnalyzer::Filter::Anonymize.new(testing_format)
@@ -1,7 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe RequestLogAnalyzer::Filter::Field, 'string in accept mode' do
4
- include RequestLogAnalyzer::Spec::Helper
5
4
 
6
5
  before(:each) do
7
6
  @filter = RequestLogAnalyzer::Filter::Field.new(testing_format, :field => :test, :value => 'test', :mode => :select)
@@ -25,7 +24,6 @@ describe RequestLogAnalyzer::Filter::Field, 'string in accept mode' do
25
24
  end
26
25
 
27
26
  describe RequestLogAnalyzer::Filter::Field, 'string in reject mode' do
28
- include RequestLogAnalyzer::Spec::Helper
29
27
 
30
28
  before(:each) do
31
29
  @filter = RequestLogAnalyzer::Filter::Field.new(testing_format, :field => :test, :value => 'test', :mode => :reject)
@@ -49,7 +47,6 @@ describe RequestLogAnalyzer::Filter::Field, 'string in reject mode' do
49
47
  end
50
48
 
51
49
  describe RequestLogAnalyzer::Filter::Field, 'regexp in accept mode' do
52
- include RequestLogAnalyzer::Spec::Helper
53
50
 
54
51
  before(:each) do
55
52
  @filter = RequestLogAnalyzer::Filter::Field.new(testing_format, :field => :test, :value => '/test/', :mode => :select)
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe RequestLogAnalyzer::Filter::Base, 'base filter' do
4
+
5
+ before(:each) do
6
+ @filter = RequestLogAnalyzer::Filter::Base.new(testing_format)
7
+ end
8
+
9
+ it "should return everything" do
10
+ @filter.filter(request(:ip => '123.123.123.123'))[:ip].should eql('123.123.123.123')
11
+ end
12
+
13
+ it "should return nil on nil request" do
14
+ @filter.filter(nil).should be_nil
15
+ end
16
+
17
+ end
@@ -1,7 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe RequestLogAnalyzer::Filter::Timespan, 'both before and after' do
4
- include RequestLogAnalyzer::Spec::Helper
5
4
 
6
5
  before(:each) do
7
6
  @filter = RequestLogAnalyzer::Filter::Timespan.new(testing_format, :after => DateTime.parse('2009-01-01'), :before => DateTime.parse('2009-02-02'))
@@ -21,7 +20,6 @@ describe RequestLogAnalyzer::Filter::Timespan, 'both before and after' do
21
20
  end
22
21
 
23
22
  describe RequestLogAnalyzer::Filter::Timespan, 'only before' do
24
- include RequestLogAnalyzer::Spec::Helper
25
23
 
26
24
  before(:each) do
27
25
  @filter = RequestLogAnalyzer::Filter::Timespan.new(testing_format, :before => DateTime.parse('2009-02-02'))
@@ -41,7 +39,6 @@ describe RequestLogAnalyzer::Filter::Timespan, 'only before' do
41
39
  end
42
40
 
43
41
  describe RequestLogAnalyzer::Filter::Timespan, 'only after' do
44
- include RequestLogAnalyzer::Spec::Helper
45
42
 
46
43
  before(:each) do
47
44
  @filter = RequestLogAnalyzer::Filter::Timespan.new(testing_format, :after => DateTime.parse('2009-01-01'))
@@ -1,7 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe RequestLogAnalyzer::Source::LogParser, :requests do
4
- include RequestLogAnalyzer::Spec::Helper
5
4
 
6
5
  before(:each) do
7
6
  @log_parser = RequestLogAnalyzer::Source::LogParser.new(testing_format)
@@ -42,12 +41,15 @@ describe RequestLogAnalyzer::Source::LogParser, :requests do
42
41
  request[:test_capture].should_not be_nil
43
42
  end
44
43
  io.close
45
- end
44
+ end
46
45
 
46
+ it "should parse a request that only consists of one line" do
47
+ @log_parser.parse_file(log_fixture(:header_and_footer))
48
+ @log_parser.parsed_requests.should == 2
49
+ end
47
50
  end
48
51
 
49
52
  describe RequestLogAnalyzer::Source::LogParser, :warnings do
50
- include RequestLogAnalyzer::Spec::Helper
51
53
 
52
54
  before(:each) do
53
55
  @log_parser = RequestLogAnalyzer::Source::LogParser.new(testing_format, :parse_strategy => 'cautious')
@@ -67,7 +69,6 @@ describe RequestLogAnalyzer::Source::LogParser, :warnings do
67
69
  end
68
70
 
69
71
  describe RequestLogAnalyzer::Source::LogParser, :decompression do
70
- include RequestLogAnalyzer::Spec::Helper
71
72
 
72
73
  before(:each) do
73
74
  @log_parser = RequestLogAnalyzer::Source::LogParser.new(RequestLogAnalyzer::FileFormat::Rails.new)
@@ -2,8 +2,6 @@ require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe RequestLogAnalyzer::Request, :incomplete_request do
4
4
 
5
- include RequestLogAnalyzer::Spec::Helper
6
-
7
5
  before(:each) do
8
6
  @incomplete_request = testing_format.request
9
7
  @incomplete_request << { :line_type => :test, :lineno => 1, :test_capture => 'awesome!' }
@@ -34,8 +32,6 @@ end
34
32
 
35
33
  describe RequestLogAnalyzer::Request, :completed_request do
36
34
 
37
- include RequestLogAnalyzer::Spec::Helper
38
-
39
35
  before(:each) do
40
36
  @completed_request = testing_format.request
41
37
  @completed_request << { :line_type => :first, :lineno => 1, :name => 'first line!' }
@@ -69,4 +65,21 @@ describe RequestLogAnalyzer::Request, :completed_request do
69
65
  @completed_request.every(:test_capture).should == ['testing', "testing some more"]
70
66
  end
71
67
 
68
+ it "should set the first_lineno for a request to the lowest lineno encountered" do
69
+ @completed_request.first_lineno.should eql(1)
70
+ end
71
+
72
+ it "should set the last_lineno for a request to the highest encountered lineno" do
73
+ @completed_request.last_lineno.should eql(10)
74
+ end
75
+
76
+ it "should not have a timestamp if no such field is captured" do
77
+ @completed_request.timestamp.should be_nil
78
+ end
79
+
80
+ it "should set return a timestamp field if such a field is captured" do
81
+ @completed_request << { :line_type => :first, :lineno => 1, :name => 'first line!', :timestamp => Time.now}
82
+ @completed_request.timestamp.should_not be_nil
83
+ end
84
+
72
85
  end