appstats 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/db/migrations/20110215155830_create_appstats_hosts.rb +13 -0
- data/db/schema.rb +8 -1
- data/lib/appstats.rb +2 -0
- data/lib/appstats/host.rb +18 -0
- data/lib/appstats/parser.rb +239 -0
- data/lib/appstats/query.rb +94 -35
- data/lib/appstats/version.rb +1 -1
- data/spec/entry_spec.rb +4 -4
- data/spec/host_spec.rb +51 -0
- data/spec/logger_spec.rb +18 -18
- data/spec/parser_spec.rb +329 -0
- data/spec/query_spec.rb +172 -23
- metadata +11 -4
data/lib/appstats/version.rb
CHANGED
data/spec/entry_spec.rb
CHANGED
@@ -188,18 +188,18 @@ module Appstats
|
|
188
188
|
end
|
189
189
|
|
190
190
|
it "should understand an entry without contexts" do
|
191
|
-
entry = Entry.load_from_logger_entry("0.
|
191
|
+
entry = Entry.load_from_logger_entry("0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search")
|
192
192
|
Entry.count.should == @before_count + 1
|
193
193
|
entry.action.should == "address_search"
|
194
|
-
entry.raw_entry.should == "0.
|
194
|
+
entry.raw_entry.should == "0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
|
195
195
|
entry.occurred_at.should == Time.parse("2010-09-21 23:15:20")
|
196
196
|
end
|
197
197
|
|
198
198
|
it "should understand contexts" do
|
199
|
-
entry = Entry.load_from_logger_entry("0.
|
199
|
+
entry = Entry.load_from_logger_entry("0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
|
200
200
|
Entry.count.should == @before_count + 1
|
201
201
|
entry.action.should == "address_filter"
|
202
|
-
entry.raw_entry.should == "0.
|
202
|
+
entry.raw_entry.should == "0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
|
203
203
|
entry.occurred_at.should == Time.parse("2010-09-21 23:15:20")
|
204
204
|
entry.contexts.size.should == 2
|
205
205
|
entry.contexts[0].context_key = "app_name"
|
data/spec/host_spec.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Appstats
|
4
|
+
describe Host do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
Appstats::LogCollector.delete_all
|
8
|
+
Appstats::Host.delete_all
|
9
|
+
@host = Appstats::Host.new
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#initialize" do
|
13
|
+
|
14
|
+
it "should set name to nil" do
|
15
|
+
@host.name.should == nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should set status to nil" do
|
19
|
+
@host.status.should == nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should set on constructor" do
|
23
|
+
host = Appstats::Host.new(:name => 'a', :status => 'c')
|
24
|
+
host.name.should == 'a'
|
25
|
+
host.status.should == 'c'
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "#update_hosts" do
|
31
|
+
|
32
|
+
it "should do nothing if no events" do
|
33
|
+
Appstats::Host.update_hosts.should == 0
|
34
|
+
Appstats::Host.count.should == 0
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should add entry host names" do
|
38
|
+
Appstats::LogCollector.create(:host => 'a')
|
39
|
+
Appstats::Host.update_hosts.should == 1
|
40
|
+
Appstats::Host.count.should == 1
|
41
|
+
|
42
|
+
host = Appstats::Host.last
|
43
|
+
host.name = 'a'
|
44
|
+
host.status = 'derived'
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
data/spec/logger_spec.rb
CHANGED
@@ -115,7 +115,7 @@ module Appstats
|
|
115
115
|
|
116
116
|
it "should accept numbers" do
|
117
117
|
Appstats::Logger.entry(5, :blah => 6)
|
118
|
-
Appstats::Logger.raw_read.should == ["0.
|
118
|
+
Appstats::Logger.raw_read.should == ["0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=5 : blah=6"]
|
119
119
|
end
|
120
120
|
|
121
121
|
end
|
@@ -124,7 +124,7 @@ module Appstats
|
|
124
124
|
|
125
125
|
it "should look similar to regular entry" do
|
126
126
|
Appstats::Logger.exception_entry(RuntimeError.new("blah"),:on => "login")
|
127
|
-
Appstats::Logger.raw_read.should == ["0.
|
127
|
+
Appstats::Logger.raw_read.should == ["0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=appstats-exception : error=blah : on=login"]
|
128
128
|
end
|
129
129
|
|
130
130
|
end
|
@@ -141,29 +141,29 @@ module Appstats
|
|
141
141
|
|
142
142
|
it "should handle a statistics entry" do
|
143
143
|
expected = { :action => "address_search", :timestamp => "2010-09-21 23:15:20" }
|
144
|
-
actual = Appstats::Logger.entry_to_hash("0.
|
144
|
+
actual = Appstats::Logger.entry_to_hash("0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search")
|
145
145
|
actual.should == expected
|
146
146
|
end
|
147
147
|
|
148
148
|
it "should handle contexts" do
|
149
149
|
expected = { :action => "address_filter", :timestamp => "2010-09-21 23:15:20", :server => "Live", :app_name => 'Market' }
|
150
|
-
actual = Appstats::Logger.entry_to_hash("0.
|
150
|
+
actual = Appstats::Logger.entry_to_hash("0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
|
151
151
|
actual.should == expected
|
152
152
|
end
|
153
153
|
|
154
154
|
it "should handle actions with the delimiter (and change the delimiter)" do
|
155
155
|
expected = { :action => "address:=search-n", :timestamp => "2010-09-21 23:15:20" }
|
156
|
-
actual = Appstats::Logger.entry_to_hash("0.
|
156
|
+
actual = Appstats::Logger.entry_to_hash("0.8.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n")
|
157
157
|
actual.should == expected
|
158
158
|
|
159
159
|
expected = { :action => "address::search==--n", :timestamp => "2010-09-21 23:15:20" }
|
160
|
-
actual = Appstats::Logger.entry_to_hash("0.
|
160
|
+
actual = Appstats::Logger.entry_to_hash("0.8.0 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n")
|
161
161
|
actual.should == expected
|
162
162
|
end
|
163
163
|
|
164
164
|
it "should handle contexts with the delimiter (and change the delimiter)" do
|
165
165
|
expected = { :action => "address", :timestamp => "2010-09-21 23:15:20", :server => "market:eval=-n" }
|
166
|
-
actual = Appstats::Logger.entry_to_hash("0.
|
166
|
+
actual = Appstats::Logger.entry_to_hash("0.8.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n")
|
167
167
|
actual.should == expected
|
168
168
|
end
|
169
169
|
|
@@ -172,66 +172,66 @@ module Appstats
|
|
172
172
|
describe "#entry_to_s" do
|
173
173
|
|
174
174
|
it "should handle a statistics entry" do
|
175
|
-
expected = "0.
|
175
|
+
expected = "0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
|
176
176
|
actual = Appstats::Logger.entry_to_s("address_search")
|
177
177
|
actual.should == expected
|
178
178
|
end
|
179
179
|
|
180
180
|
it "should handle numbers" do
|
181
|
-
expected = "0.
|
181
|
+
expected = "0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=1 : note=2.2"
|
182
182
|
actual = Appstats::Logger.entry_to_s(1,:note => 2.2)
|
183
183
|
actual.should == expected
|
184
184
|
end
|
185
185
|
|
186
186
|
it "should handle default contexts" do
|
187
187
|
Appstats::Logger.default_contexts[:app_name] = "market"
|
188
|
-
expected = "0.
|
188
|
+
expected = "0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : app_name=market"
|
189
189
|
actual = Appstats::Logger.entry_to_s("address_search")
|
190
190
|
actual.should == expected
|
191
191
|
end
|
192
192
|
|
193
193
|
it "should handle contexts (and sort them by symbol)" do
|
194
|
-
expected = "0.
|
194
|
+
expected = "0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
|
195
195
|
actual = Appstats::Logger.entry_to_s("address_filter", { :server => "Live", :app_name => 'Market' })
|
196
196
|
actual.should == expected
|
197
197
|
end
|
198
198
|
|
199
199
|
it "should handle actions with the delimiter (and change the delimiter)" do
|
200
|
-
expected = "0.
|
200
|
+
expected = "0.8.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n"
|
201
201
|
actual = Appstats::Logger.entry_to_s("address:=search-n")
|
202
202
|
actual.should == expected
|
203
203
|
|
204
|
-
expected = "0.
|
204
|
+
expected = "0.8.0 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n"
|
205
205
|
actual = Appstats::Logger.entry_to_s("address::search==--n")
|
206
206
|
actual.should == expected
|
207
207
|
end
|
208
208
|
|
209
209
|
it "should handle contexts with the delimiter (and change the delimiter)" do
|
210
|
-
expected = "0.
|
210
|
+
expected = "0.8.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n"
|
211
211
|
actual = Appstats::Logger.entry_to_s("address", :server => 'market:eval=-n')
|
212
212
|
actual.should == expected
|
213
213
|
end
|
214
214
|
|
215
215
|
it "should ignore spaces" do
|
216
|
-
expected = "0.
|
216
|
+
expected = "0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address search"
|
217
217
|
actual = Appstats::Logger.entry_to_s("address search")
|
218
218
|
actual.should == expected
|
219
219
|
end
|
220
220
|
|
221
221
|
it "should convert newlines in action" do
|
222
|
-
expected = "0.
|
222
|
+
expected = "0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_-nsearch"
|
223
223
|
actual = Appstats::Logger.entry_to_s("address_\nsearch")
|
224
224
|
actual.should == expected
|
225
225
|
end
|
226
226
|
|
227
227
|
it "should convert newlines in context" do
|
228
|
-
expected = "0.
|
228
|
+
expected = "0.8.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : blah=some-nlong-nstatement"
|
229
229
|
actual = Appstats::Logger.entry_to_s("address_search",:blah => "some\nlong\nstatement")
|
230
230
|
actual.should == expected
|
231
231
|
end
|
232
232
|
|
233
233
|
it "should convert newlines based on the delimiter" do
|
234
|
-
expected = "0.
|
234
|
+
expected = "0.8.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=--nsearch-n"
|
235
235
|
actual = Appstats::Logger.entry_to_s("address:=\nsearch-n")
|
236
236
|
actual.should == expected
|
237
237
|
end
|
data/spec/parser_spec.rb
ADDED
@@ -0,0 +1,329 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Appstats
|
4
|
+
describe Parser do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@parser = Parser.new
|
8
|
+
end
|
9
|
+
|
10
|
+
describe("#initialize") do
|
11
|
+
|
12
|
+
it "should set rules to nil" do
|
13
|
+
@parser.raw_rules.should == nil
|
14
|
+
@parser.raw_tokenize.should == nil
|
15
|
+
@parser.repeating.should == false
|
16
|
+
@parser.tokenize_regex == nil
|
17
|
+
@parser.rules.should == []
|
18
|
+
@parser.tokenize.should == []
|
19
|
+
@parser.constants.should == []
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should set rules from constructor" do
|
23
|
+
parser = Parser.new(:rules => ":name or :bust", :tokenize => "a bb c", :repeating => true)
|
24
|
+
parser.raw_rules.should == ":name or :bust"
|
25
|
+
parser.raw_tokenize.should == "a bb c"
|
26
|
+
parser.repeating.should == true
|
27
|
+
parser.rules.should == [ { :rule => :name, :stop => :constant }, "OR", { :rule => :bust, :stop => :end} ]
|
28
|
+
parser.tokenize.should == ["\\s+A","\\s+BB","\\s+C"]
|
29
|
+
parser.tokenize_no_spaces.should == ["A","BB","C"]
|
30
|
+
parser.constants.should == ["OR"]
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should espace tokens as required" do
|
34
|
+
parser = Parser.new(:tokenize => "( ) abc |")
|
35
|
+
parser.tokenize.should == ['\(','\)','\s+ABC','\|']
|
36
|
+
parser.tokenize_regex.should == '\(|\)|\s+ABC|\|'
|
37
|
+
parser.tokenize_no_spaces.should == ['\(','\)','ABC','\|']
|
38
|
+
parser.tokenize_regex_no_spaces.should == '\(|\)|ABC|\|'
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
describe "#rules" do
|
45
|
+
|
46
|
+
it "should end on constant if tokens present" do
|
47
|
+
Parser.new(:rules => ":name", :tokenize => ")").rules.should == [ { :rule => :name, :stop => :end } ]
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
it "should handle one variable" do
|
52
|
+
Parser.new(:rules => ":name").rules.should == [ { :rule => :name, :stop => :end } ]
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should handle many variables" do
|
56
|
+
Parser.new(:rules => ":name :date").rules.should == [ { :rule => :name, :stop => :space }, { :rule => :date, :stop => :end } ]
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should deal with colons" do
|
60
|
+
Parser.new(:rules => ":name : :date").rules.should == [ { :rule => :name, :stop => :constant }, ":", { :rule => :date, :stop => :end } ]
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should deal with constant" do
|
64
|
+
Parser.new(:rules => "blah").rules.should == ["BLAH"]
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should deal with constant and variables" do
|
68
|
+
Parser.new(:rules => ":name blah :date").rules.should == [ { :rule => :name, :stop => :constant }, "BLAH", { :rule => :date, :stop => :end } ]
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should deal with multiple constants and variables" do
|
72
|
+
Parser.new(:rules => ":name blah more blah :date").rules.should == [ { :rule => :name, :stop => :constant }, "BLAH", "MORE", "BLAH", { :rule => :date, :stop => :end } ]
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "#constants" do
|
78
|
+
|
79
|
+
it "should be empty if only variables" do
|
80
|
+
Parser.new(:rules => ":name :blah").constants.should == [ ]
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should track all constants" do
|
84
|
+
Parser.new(:rules => ":name : :date").constants.should == [ ":" ]
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should upper case constants" do
|
88
|
+
Parser.new(:rules => "blah").constants.should == ["BLAH"]
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should deal with multiple constants" do
|
92
|
+
Parser.new(:rules => ":name = :blah and :moreblah").constants.should == ["=","AND"]
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "#parse_constant" do
|
98
|
+
|
99
|
+
it "should handle nil" do
|
100
|
+
Parser.parse_constant(nil,nil).should == [nil,nil]
|
101
|
+
Parser.parse_constant("",nil).should == [nil,nil]
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should find the constant" do
|
105
|
+
Parser.parse_constant("= blah blah more blah ","=").should == ["=","blah blah more blah"]
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should find the constants with multiple characters" do
|
109
|
+
Parser.parse_constant("hey blah blah more blah ","hey").should == ["hey","blah blah more blah"]
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should return nil if not found" do
|
113
|
+
Parser.parse_constant("blah blah more blah ","=").should == [nil,"blah blah more blah"]
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should be case insensitive" do
|
117
|
+
Parser.parse_constant(" blah stuff on more blah stuff ","blah").should == ["blah","stuff on more blah stuff"]
|
118
|
+
Parser.parse_constant(" BLAH stuff on more blah stuff ","blah").should == ["BLAH","stuff on more blah stuff"]
|
119
|
+
Parser.parse_constant(" blah stuff on more blah stuff ","BLAH").should == ["blah","stuff on more blah stuff"]
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should only find the first instance" do
|
123
|
+
Parser.parse_constant("one == two","==").should == [nil,"one == two"]
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "#parse_word" do
|
129
|
+
|
130
|
+
it "should handle nil" do
|
131
|
+
@parser.parse_word(nil,nil).should == [nil,nil]
|
132
|
+
@parser.parse_word("",nil).should == [nil,nil]
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should look for global tokens" do
|
136
|
+
parser = Parser.new(:tokenize => "xx")
|
137
|
+
parser.parse_word("blah xx",:end).should == ["blah","xx"]
|
138
|
+
parser.parse_word("blah xx stop","stop").should == ["blah","xx stop"]
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should handle :end" do
|
142
|
+
@parser.parse_word("blah",:end).should == ["blah",nil]
|
143
|
+
@parser.parse_word("more blah blah blah",:end).should == ["more blah blah blah",nil]
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should handle :space" do
|
147
|
+
@parser.parse_word("more blah blah blah",:space).should == ["more","blah blah blah"]
|
148
|
+
@parser.parse_word("who dat",:space).should == ["who","dat"]
|
149
|
+
end
|
150
|
+
|
151
|
+
it "should handle :space when no more spaces" do
|
152
|
+
@parser.parse_word("blah",:space).should == ["blah",nil]
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should strip front space" do
|
156
|
+
@parser.parse_word(" blah ",:space).should == ["blah",nil]
|
157
|
+
@parser.parse_word(" more blah blah blah ",:space).should == ["more","blah blah blah"]
|
158
|
+
@parser.parse_word(" who dat ",:space).should == ["who","dat"]
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should on constant" do
|
162
|
+
@parser.parse_word(" blah stuff = more blah stuff ","=").should == ["blah stuff","= more blah stuff"]
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should be case insensitive" do
|
166
|
+
@parser.parse_word(" blah stuff on more blah stuff ","on").should == ["blah stuff","on more blah stuff"]
|
167
|
+
@parser.parse_word(" blah stuff ON more blah stuff ","on").should == ["blah stuff","ON more blah stuff"]
|
168
|
+
@parser.parse_word(" blah stuff on more blah stuff ","ON").should == ["blah stuff","on more blah stuff"]
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should grab whole string if not constant not present" do
|
172
|
+
@parser.parse_word(" blah ","x").should == ["blah", nil]
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should be able to explicitly set if you want an exact match or not" do
|
176
|
+
@parser.parse_word(" aa bbb ","x",false).should == ["aa bbb", nil]
|
177
|
+
@parser.parse_word(" aa bbb ","x",true).should == [nil, "aa bbb"]
|
178
|
+
|
179
|
+
@parser.parse_word(" aa bbb ","bbb",false).should == ["aa", "bbb"]
|
180
|
+
@parser.parse_word(" aa bbb ","bbb",true).should == ["aa", "bbb"]
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should remove tokens from the beginning" do
|
184
|
+
parser = Appstats::Parser.new(:tokenize => "xx yy zz")
|
185
|
+
parser.parse_word(" xx yy zz zzaa bbb ",:space).should == ["aa", "bbb"]
|
186
|
+
parser.raw_results.should == [ "xx", "yy", "zz", "zz" ]
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should remove tokens from the end (:space)" do
|
190
|
+
parser = Parser.new(:tokenize => "xx yy zz")
|
191
|
+
parser.parse_word(" aa xx y z bbb ",:space).should == ["aa", "xx y z bbb"]
|
192
|
+
parser.raw_results.should == [ ]
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should remove tokens from the end (:end)" do
|
196
|
+
parser = Parser.new(:tokenize => "xx yy zz")
|
197
|
+
parser.parse_word(" aa xx y z bbb ",:end).should == ["aa", "xx y z bbb"]
|
198
|
+
parser.raw_results.should == [ ]
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should remove tokens from the end (constant)" do
|
202
|
+
parser = Parser.new(:tokenize => "xx yy zz")
|
203
|
+
parser.parse_word(" aa xx y on z bbb ","on").should == ["aa", "xx y on z bbb"]
|
204
|
+
parser.raw_results.should == [ ]
|
205
|
+
end
|
206
|
+
|
207
|
+
it "should handle constants" do
|
208
|
+
parser = Parser.new(:rules => ":operation :action :date on :server where :contexts")
|
209
|
+
parser.parse_word("today on my.local where stuff","on",true).should == ["today","on my.local where stuff"]
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
|
214
|
+
describe "#merge_regex_filter" do
|
215
|
+
|
216
|
+
it "should handle nil" do
|
217
|
+
Parser.merge_regex_filter(nil,nil).should == ""
|
218
|
+
Parser.merge_regex_filter('','').should == ""
|
219
|
+
end
|
220
|
+
|
221
|
+
it "should handle nil on one side" do
|
222
|
+
Parser.merge_regex_filter('\s',nil).should == '(\s)'
|
223
|
+
Parser.merge_regex_filter(nil,'\s').should == '(\s)'
|
224
|
+
Parser.merge_regex_filter('\s','').should == '(\s)'
|
225
|
+
Parser.merge_regex_filter('','\s').should == '(\s)'
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should handle both sides" do
|
229
|
+
Parser.merge_regex_filter('\s','a|b').should == '(\s|a|b)'
|
230
|
+
end
|
231
|
+
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
describe "#parse" do
|
236
|
+
|
237
|
+
|
238
|
+
it "fails if no rules" do
|
239
|
+
@parser.parse("blah").should == false
|
240
|
+
end
|
241
|
+
|
242
|
+
it "fails on nil" do
|
243
|
+
parser = Parser.new(:rules => ":name")
|
244
|
+
parser.parse(nil).should == false
|
245
|
+
end
|
246
|
+
|
247
|
+
describe "non repeating" do
|
248
|
+
|
249
|
+
it "passes on a simple rule" do
|
250
|
+
parser = Parser.new(:rules => ":name")
|
251
|
+
parser.parse("blah").should == true
|
252
|
+
parser.raw_results.should == [ {:name => "blah"} ]
|
253
|
+
end
|
254
|
+
|
255
|
+
it "passes on several rules" do
|
256
|
+
parser = Parser.new(:rules => ":one :two :three")
|
257
|
+
parser.parse("a bbb cc").should == true
|
258
|
+
parser.results.should == {:one => "a", :two => "bbb", :three => "cc" }
|
259
|
+
end
|
260
|
+
|
261
|
+
it "passes on combination of constants and rules" do
|
262
|
+
parser = Parser.new(:rules => ":operation :action :date on :server where :contexts")
|
263
|
+
parser.parse("# logins today on my.local where stuff").should == true
|
264
|
+
parser.results.should == {:operation => "#", :action => "logins", :date => "today" , :server => "my.local", :contexts => "stuff" }
|
265
|
+
end
|
266
|
+
|
267
|
+
it "passes on combination of constants and rules" do
|
268
|
+
parser = Parser.new(:rules => ":operation :action :date on :server where :contexts")
|
269
|
+
parser.parse("# logins where stuff").should == true
|
270
|
+
parser.results.should == {:operation => "#", :action => "logins", :date => nil , :server => nil, :contexts => "stuff" }
|
271
|
+
end
|
272
|
+
|
273
|
+
it "should be able to skip constants" do
|
274
|
+
parser = Parser.new(:rules => ":one aa :two bbb :three")
|
275
|
+
parser.parse("1 bbb 3")
|
276
|
+
parser.results.should == { :one => "1", :two => nil, :three => "3" }
|
277
|
+
end
|
278
|
+
|
279
|
+
it "should handle missing constants" do
|
280
|
+
parser = Parser.new(:rules => ":one aa :two bbb :three")
|
281
|
+
parser.parse("1").should == true
|
282
|
+
parser.results.should == {:one => "1", :two => nil, :three => nil }
|
283
|
+
end
|
284
|
+
|
285
|
+
it "should handle missing constants" do
|
286
|
+
parser = Parser.new(:rules => ":one aa :two bbb :three")
|
287
|
+
parser.parse("1 one aa 222 ").should == true
|
288
|
+
parser.results.should == {:one => "1 one", :two => "222", :three => nil }
|
289
|
+
end
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
describe "repeating" do
|
294
|
+
|
295
|
+
it "should tokenize single repeating entities" do
|
296
|
+
parser = Appstats::Parser.new(:rules => ":context", :repeating => true, :tokenize => "and or || && = <= >= <> !=")
|
297
|
+
parser.parse("a = b").should == true
|
298
|
+
parser.raw_results.should == [ { :context => "a"}, "=", { :context => "b"} ]
|
299
|
+
end
|
300
|
+
|
301
|
+
end
|
302
|
+
|
303
|
+
it "should tokenize tokens" do
|
304
|
+
parser = Appstats::Parser.new(:rules => ":context_key = :context_value", :tokenize => "( )")
|
305
|
+
parser.parse("( ( a = b ) )").should == true
|
306
|
+
parser.raw_results.should == [ "(", "(", { :context_key => "a"}, "=", { :context_value => "b"}, ")", ")" ]
|
307
|
+
end
|
308
|
+
|
309
|
+
it "should tokenize letters only if spaces between them" do
|
310
|
+
parser = Appstats::Parser.new(:rules => ":one :two", :tokenize => "( aa a1 )")
|
311
|
+
parser.parse("(blaa aa a1 bla1)").should == true
|
312
|
+
parser.raw_results.should == [ "(", { :one => "blaa"}, "aa", "a1", { :two => "bla1"}, ")" ]
|
313
|
+
end
|
314
|
+
|
315
|
+
|
316
|
+
describe "real examples" do
|
317
|
+
|
318
|
+
it "should find host" do
|
319
|
+
parser = Appstats::Parser.new(:rules => ":operation :action :date on :host where :contexts")
|
320
|
+
parser.parse("# logins between 2010-01-15 and 2010-01-31 on your.localnet").should == true
|
321
|
+
parser.results.should == {:operation => "#", :action => "logins", :date => "between 2010-01-15 and 2010-01-31", :host => "your.localnet", :contexts => nil }
|
322
|
+
end
|
323
|
+
|
324
|
+
end
|
325
|
+
|
326
|
+
end
|
327
|
+
|
328
|
+
end
|
329
|
+
end
|