appstats 0.10.0 → 0.11.2
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/Gemfile.lock +1 -1
- data/db/migrations/20110222215437_create_appstats_jobs.rb +16 -0
- data/db/schema.rb +11 -1
- data/lib/appstats.rb +1 -0
- data/lib/appstats/date_range.rb +13 -6
- data/lib/appstats/entry_date.rb +15 -0
- data/lib/appstats/log_collector.rb +35 -1
- data/lib/appstats/query.rb +4 -2
- data/lib/appstats/result_job.rb +54 -0
- data/lib/appstats/version.rb +1 -1
- data/lib/daemons/appstats_log_collector.rb +10 -2
- data/spec/date_range_spec.rb +24 -4
- data/spec/entry_date_spec.rb +34 -0
- data/spec/entry_spec.rb +4 -4
- data/spec/log_collector_spec.rb +77 -0
- data/spec/logger_spec.rb +18 -18
- data/spec/query_spec.rb +355 -348
- data/spec/result_job_spec.rb +333 -0
- metadata +8 -4
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.create_from_logger_string("0.
|
191
|
+
entry = Entry.create_from_logger_string("0.11.2 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.11.2 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.create_from_logger_string("0.
|
199
|
+
entry = Entry.create_from_logger_string("0.11.2 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.11.2 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/log_collector_spec.rb
CHANGED
@@ -47,6 +47,30 @@ module Appstats
|
|
47
47
|
|
48
48
|
end
|
49
49
|
|
50
|
+
|
51
|
+
describe "#processed_filename" do
|
52
|
+
|
53
|
+
it "should handle nil filename" do
|
54
|
+
LogCollector.new.processed_filename.should == nil
|
55
|
+
LogCollector.new(:filename => "").processed_filename.should == ""
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should handle filenames without slashes" do
|
59
|
+
LogCollector.new(:filename => "blah_blah_blah").processed_filename.should == "__processed__blah_blah_blah"
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should handle filenames with one slash" do
|
63
|
+
LogCollector.new(:filename => "/blah_blah_blah").processed_filename.should == "/__processed__blah_blah_blah"
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should handle filenames with many slash" do
|
67
|
+
LogCollector.new(:filename => "one/two/three/blah_blah_blah").processed_filename.should == "one/two/three/__processed__blah_blah_blah"
|
68
|
+
LogCollector.new(:filename => "/one/two/blah_blah_blah").processed_filename.should == "/one/two/__processed__blah_blah_blah"
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
end
|
73
|
+
|
50
74
|
describe "#find_remote_files" do
|
51
75
|
|
52
76
|
before(:each) do
|
@@ -291,6 +315,59 @@ module Appstats
|
|
291
315
|
|
292
316
|
end
|
293
317
|
|
318
|
+
describe "#remove_remote_files" do
|
319
|
+
|
320
|
+
before(:each) do
|
321
|
+
@entry_count = Entry.count
|
322
|
+
end
|
323
|
+
|
324
|
+
it "should ignored non processed files" do
|
325
|
+
log = LogCollector.create(:status => "blah")
|
326
|
+
Appstats.should_receive(:log).with(:info,"No remote logs to remove.")
|
327
|
+
Appstats::LogCollector.remove_remote_files(@login).should == 0
|
328
|
+
end
|
329
|
+
|
330
|
+
it "should log all transactions" do
|
331
|
+
|
332
|
+
log1 = LogCollector.create(:status => "processed", :filename => "/my/path/log/mystats_2011-01-02.log", :host => "myhost.localnet")
|
333
|
+
log2 = LogCollector.create(:status => "processed", :filename => "/my/path/log/mystats_2011-01-03.log", :host => "myhost.localnet")
|
334
|
+
|
335
|
+
ssh = mock(Net::SSH)
|
336
|
+
Net::SSH.should_receive(:start).twice.with("myhost.localnet","deployer",{ :password => "pass"}).and_yield(ssh)
|
337
|
+
ssh.should_receive(:exec!).with("mv /my/path/log/mystats_2011-01-02.log /my/path/log/__processed__mystats_2011-01-02.log")
|
338
|
+
ssh.should_receive(:exec!).with("mv /my/path/log/mystats_2011-01-03.log /my/path/log/__processed__mystats_2011-01-03.log")
|
339
|
+
|
340
|
+
Appstats.should_receive(:log).with(:info, "About to remove 2 remote file(s) from the processing queue.")
|
341
|
+
Appstats.should_receive(:log).with(:info, " - deployer@myhost.localnet:/my/path/log/__processed__mystats_2011-01-02.log")
|
342
|
+
Appstats.should_receive(:log).with(:info, " - deployer@myhost.localnet:/my/path/log/__processed__mystats_2011-01-03.log")
|
343
|
+
Appstats.should_receive(:log).with(:info, "Removed 2 remote file(s).")
|
344
|
+
|
345
|
+
Appstats::LogCollector.remove_remote_files(@login).should == 2
|
346
|
+
log1.reload and log2.reload
|
347
|
+
log1.status.should == "destroyed"
|
348
|
+
log2.status.should == "destroyed"
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
describe "#should_process" do
|
353
|
+
|
354
|
+
it "should be true for nil" do
|
355
|
+
LogCollector.should_process(nil).should == true
|
356
|
+
end
|
357
|
+
|
358
|
+
it "should want an update if not in the same day" do
|
359
|
+
last_time = Time.parse("2010-01-02")
|
360
|
+
Time.stub!(:now).and_return(Time.parse('2010-01-03'))
|
361
|
+
LogCollector.should_process(last_time).should == true
|
362
|
+
end
|
363
|
+
|
364
|
+
it "should not want an update if same day" do
|
365
|
+
last_time = Time.parse("2010-01-02")
|
366
|
+
Time.stub!(:now).and_return(Time.parse('2010-01-02'))
|
367
|
+
LogCollector.should_process(last_time).should == false
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
294
371
|
|
295
372
|
end
|
296
373
|
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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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.11.2 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/query_spec.rb
CHANGED
@@ -3,358 +3,365 @@ require 'spec_helper'
|
|
3
3
|
module Appstats
|
4
4
|
describe Query do
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
6
|
+
describe "#initialize" do
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
@query = Appstats::Query.new
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should set input to nil" do
|
13
|
+
@query.query.should == nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should allow query on constructor" do
|
17
|
+
query = Appstats::Query.new(:query => "# logins")
|
18
|
+
query.query.should == "# logins"
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#input" do
|
24
|
+
|
25
|
+
it "should set the inputs to nil if input invalid" do
|
26
|
+
query = Appstats::Query.new(:query => "# myblahs today on xyz.localnet")
|
27
|
+
query.query = nil
|
28
|
+
query.action.should == nil
|
29
|
+
query.host.should == nil
|
30
|
+
query.date_range.should == DateRange.new
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should set the action and host" do
|
35
|
+
query = Appstats::Query.new(:query => "# myblahs today on xyz.localnet")
|
36
|
+
query.action.should == "myblahs"
|
37
|
+
query.host.should == "xyz.localnet"
|
38
|
+
query.date_range.should == DateRange.parse("today")
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should understand the short hand 'on' instead of 'on server'" do
|
42
|
+
query = Appstats::Query.new(:query => "# myblahs on xyz.localnet")
|
43
|
+
query.action.should == "myblahs"
|
44
|
+
query.host.should == "xyz.localnet"
|
45
|
+
query.date_range.should == DateRange.new
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should understand the old 'on server' instead of new 'on'" do
|
49
|
+
query = Appstats::Query.new(:query => "# myblahs on server xyz.localnet")
|
50
|
+
query.action.should == "myblahs"
|
51
|
+
query.host.should == "xyz.localnet"
|
52
|
+
query.date_range.should == DateRange.new
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#run" do
|
58
|
+
|
59
|
+
before(:each) do
|
60
|
+
Appstats::Entry.delete_all
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should return 0 if no results" do
|
64
|
+
query = Appstats::Query.new(:query => "# blahs")
|
65
|
+
result = query.run
|
66
|
+
result.new_record?.should == false
|
67
|
+
result.should == Appstats::Result.new(:result_type => "on_demand", :query => "# blahs", :query_as_sql => query.query_to_sql, :count => 0, :action => "blahs")
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should set name and result_type if provided" do
|
71
|
+
query = Appstats::Query.new(:name => "x", :result_type => "some_reason", :query => "# blahs")
|
72
|
+
result = query.run
|
73
|
+
result.new_record?.should == false
|
74
|
+
result.should == Appstats::Result.new(:name => "x", :result_type => "some_reason", :query => "# blahs", :query_as_sql => query.query_to_sql, :count => 0, :action => "blahs")
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should track contexts" do
|
78
|
+
query = Appstats::Query.new(:query => "# blahs where (a=b and c=4) or (aaa=5)")
|
79
|
+
result = query.run
|
80
|
+
result.new_record?.should == false
|
81
|
+
result.contexts.should == "(a=b and c=4) or (aaa=5)"
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
it "should track the count if available" do
|
86
|
+
Appstats::Entry.create(:action => "myblahs")
|
87
|
+
query = Appstats::Query.new(:query => "# myblahs")
|
88
|
+
query.run.count.should == 1
|
89
|
+
Appstats::Entry.create(:action => "myblahs")
|
90
|
+
query.run.count.should == 2
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should perform the action search" do
|
94
|
+
Appstats::Entry.create_from_logger("myblahs", :one => "11", :two => "222")
|
95
|
+
Appstats::Entry.create_from_logger("myblahs", :one => "111", :two => "22")
|
96
|
+
|
97
|
+
query = Appstats::Query.new(:query => "# myblahs where one=11")
|
98
|
+
result = query.run
|
99
|
+
result.count.should == 1
|
100
|
+
|
101
|
+
query = Appstats::Query.new(:query => "# myblahs where one=anything")
|
102
|
+
query.run.count.should == 0
|
103
|
+
|
104
|
+
query = Appstats::Query.new(:query => "# myblahs where one=11 && two=22")
|
105
|
+
query.run.count.should == 0
|
106
|
+
|
107
|
+
query = Appstats::Query.new(:query => "# myblahs where one=11 || two=22")
|
108
|
+
query.run.count.should == 2
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
describe "#query_to_sql" do
|
115
|
+
|
116
|
+
before(:all) do
|
117
|
+
Appstats::Action.delete_all
|
118
|
+
Appstats::Action.create(:name => "login", :plural_name => "logins")
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should return understand nil" do
|
122
|
+
expected_sql = "select 0 from appstats_entries LIMIT 1"
|
123
|
+
Appstats::Query.new(:query => nil).query_to_sql.should == expected_sql
|
124
|
+
Appstats::Query.new(:query => "").query_to_sql.should == expected_sql
|
125
|
+
Appstats::Query.new.query_to_sql.should == expected_sql
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "actions" do
|
129
|
+
|
130
|
+
it "should understand both singular and plural names" do
|
131
|
+
expected_sql = "select count(*) from appstats_entries where action = 'login'"
|
132
|
+
Appstats::Query.new(:query => "# logins").query_to_sql.should == expected_sql
|
133
|
+
Appstats::Query.new(:query => "# login").query_to_sql.should == expected_sql
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should use 'itself' if action not found" do
|
137
|
+
expected_sql = "select count(*) from appstats_entries where action = 'garblygook'"
|
138
|
+
Appstats::Query.new(:query => "# garblygook").query_to_sql.should == expected_sql
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
describe "date ranges" do
|
144
|
+
it "should understand since dates" do
|
145
|
+
expected_sql = "select count(*) from appstats_entries where action = 'login' and occurred_at >= '2010-01-15 00:00:00'"
|
146
|
+
Appstats::Query.new(:query => "# logins since 2010-01-15").query_to_sql.should == expected_sql
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "server_name" do
|
151
|
+
|
152
|
+
it "should on_name" do
|
153
|
+
expected_sql = "select count(*) from appstats_entries where action = 'login' and EXISTS (select * from appstats_log_collectors where appstats_entries.appstats_log_collector_id = appstats_log_collectors.id and host = 'my.localnet' )"
|
154
|
+
Appstats::Query.new(:query => "# logins on my.localnet").query_to_sql.should == expected_sql
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
describe "date range and server_name" do
|
160
|
+
it "should understand dates and 'on'" do
|
161
|
+
expected_sql = "select count(*) from appstats_entries where action = 'login' and (occurred_at >= '2010-01-15 00:00:00' and occurred_at <= '2010-01-31 23:59:59') and EXISTS (select * from appstats_log_collectors where appstats_entries.appstats_log_collector_id = appstats_log_collectors.id and host = 'your.localnet' )"
|
162
|
+
Appstats::Query.new(:query => "# logins between 2010-01-15 and 2010-01-31 on your.localnet").query_to_sql.should == expected_sql
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe "where clause" do
|
167
|
+
|
168
|
+
it "should understand no quotes" do
|
169
|
+
expected_sql = "select count(*) from appstats_entries where action = 'login' and EXISTS (select * from appstats_contexts where appstats_entries.id = appstats_contexts.appstats_entry_id and ( (context_key = 'user' and context_value = 'aforward')))"
|
170
|
+
Appstats::Query.new(:query => "# logins where user = aforward").query_to_sql.should == expected_sql
|
171
|
+
end
|
172
|
+
|
166
173
|
it "should handle example" do
|
167
174
|
expected_sql = "select count(*) from appstats_entries where action = 'blahs' and EXISTS (select * from appstats_contexts where appstats_entries.id = appstats_contexts.appstats_entry_id and ( ( (context_key = 'a' and context_value = 'b') and (context_key = 'c' and context_value = '4') ) or ( (context_key = 'aaa' and context_value = '5') )))"
|
168
175
|
Appstats::Query.new(:query => "# blahs where (a=b and c=4) or (aaa=5)").query_to_sql.should == expected_sql
|
169
176
|
end
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
177
|
+
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
181
|
+
|
182
|
+
describe "#host_filter_to_sql" do
|
183
|
+
|
184
|
+
it "should translate blah into EXISTS query" do
|
185
|
+
expected = "EXISTS (select * from appstats_log_collectors where appstats_entries.appstats_log_collector_id = appstats_log_collectors.id and host = 'a' )"
|
186
|
+
Appstats::Query.host_filter_to_sql("a").should == expected
|
187
|
+
Appstats::Query.host_filter_to_sql(" a ").should == expected
|
188
|
+
end
|
189
|
+
|
190
|
+
it "should ignore single quotes and spaces" do
|
191
|
+
Appstats::Query.host_filter_to_sql("bl'ah").should == "1=1"
|
192
|
+
Appstats::Query.host_filter_to_sql("bl ah").should == "1=1"
|
193
|
+
end
|
194
|
+
|
195
|
+
it "should do simple 1=1 if invalid" do
|
196
|
+
Appstats::Query.host_filter_to_sql("").should == "1=1"
|
197
|
+
Appstats::Query.host_filter_to_sql(nil).should == "1=1"
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
describe "#contexts_filter_to_sql" do
|
203
|
+
|
204
|
+
before(:each) do
|
205
|
+
@template = "EXISTS (select * from appstats_contexts where appstats_entries.id = appstats_contexts.appstats_entry_id and ("
|
206
|
+
end
|
207
|
+
|
208
|
+
it "should translate a = b into EXISTS query" do
|
209
|
+
Appstats::Query.contexts_filter_to_sql("a=b").should == "#{@template} (context_key = 'a' and context_value = 'b')))"
|
210
|
+
Appstats::Query.contexts_filter_to_sql(" a = b ").should == "#{@template} (context_key = 'a' and context_value = 'b')))"
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should ignore single quotes" do
|
214
|
+
Appstats::Query.contexts_filter_to_sql("'aaa'='bbbb'").should == "#{@template} (context_key = 'aaa' and context_value = 'bbbb')))"
|
215
|
+
Appstats::Query.contexts_filter_to_sql(" 'aaa' = 'bbbb' ").should == "#{@template} (context_key = 'aaa' and context_value = 'bbbb')))"
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should allow for searching for all entries of a certain context" do
|
219
|
+
Appstats::Query.contexts_filter_to_sql("aaa").should == "#{@template} (context_key = 'aaa')))"
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should allow for searching for several entries of a certain context" do
|
223
|
+
Appstats::Query.contexts_filter_to_sql("aaa || bbb").should == "#{@template} (context_key = 'aaa') or (context_key = 'bbb')))"
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should allow complex queries" do
|
227
|
+
Appstats::Query.contexts_filter_to_sql("user='andrew' || user='aforward'").should == "#{@template} (context_key = 'user' and context_value = 'andrew') or (context_key = 'user' and context_value = 'aforward')))"
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should support or" do
|
231
|
+
Appstats::Query.contexts_filter_to_sql("user='andrew' or user='aforward'").should == "#{@template} (context_key = 'user' and context_value = 'andrew') or (context_key = 'user' and context_value = 'aforward')))"
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should support like" do
|
235
|
+
Appstats::Query.contexts_filter_to_sql("user like '%andrew%'").should == "#{@template} (context_key = 'user' and context_value like '%andrew%')))"
|
236
|
+
end
|
237
|
+
|
238
|
+
it "should support and" do
|
239
|
+
Appstats::Query.contexts_filter_to_sql("user='andrew' and user='aforward'").should == "#{@template} (context_key = 'user' and context_value = 'andrew') and (context_key = 'user' and context_value = 'aforward')))"
|
240
|
+
end
|
241
|
+
|
242
|
+
|
243
|
+
it "should do simple 1 = 1 if invalid" do
|
244
|
+
Appstats::Query.contexts_filter_to_sql("").should == "1=1"
|
245
|
+
Appstats::Query.contexts_filter_to_sql(nil).should == "1=1"
|
246
|
+
end
|
247
|
+
|
248
|
+
end
|
249
|
+
|
250
|
+
describe "#sqlize" do
|
251
|
+
|
252
|
+
it "should handle nil" do
|
253
|
+
Appstats::Query.sqlize(nil).should == nil
|
254
|
+
Appstats::Query.sqlize('').should == ''
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should understand &&" do
|
258
|
+
Appstats::Query.sqlize("&&").should == "and"
|
259
|
+
end
|
260
|
+
|
261
|
+
it "should understand ||" do
|
262
|
+
Appstats::Query.sqlize("||").should == "or"
|
263
|
+
end
|
264
|
+
|
265
|
+
it "should understand !=" do
|
266
|
+
Appstats::Query.sqlize("!=").should == "<>"
|
267
|
+
end
|
268
|
+
|
269
|
+
it "should set everything else as-is" do
|
270
|
+
Appstats::Query.sqlize("blah").should == "blah"
|
271
|
+
end
|
272
|
+
|
273
|
+
end
|
274
|
+
|
275
|
+
describe "#sqlclean" do
|
276
|
+
|
277
|
+
it "should handle nil" do
|
278
|
+
Appstats::Query.sqlclean(nil).should == nil
|
279
|
+
Appstats::Query.sqlclean('').should == ''
|
280
|
+
end
|
281
|
+
|
282
|
+
it "should remove exterior quotes" do
|
283
|
+
Appstats::Query.sqlclean("'a'").should == "a"
|
284
|
+
Appstats::Query.sqlclean("'bbb'").should == "bbb"
|
285
|
+
Appstats::Query.sqlclean('"a"').should == "a"
|
286
|
+
Appstats::Query.sqlclean('"bbb"').should == "bbb"
|
287
|
+
end
|
288
|
+
|
289
|
+
it "should handle normal text" do
|
290
|
+
Appstats::Query.sqlclean('abc').should == 'abc'
|
291
|
+
end
|
292
|
+
|
293
|
+
it "should handle slashes" do
|
294
|
+
Appstats::Query.sqlclean('a\b').should == 'a\\\\b'
|
295
|
+
end
|
296
|
+
|
297
|
+
it "should handle single quotes" do
|
298
|
+
Appstats::Query.sqlclean("a'b").should == "a''b"
|
299
|
+
end
|
300
|
+
|
301
|
+
end
|
302
|
+
|
303
|
+
describe "#comparators" do
|
304
|
+
|
305
|
+
it "should be a list " do
|
306
|
+
Appstats::Query.comparators.should == ["=","!=","<>",">","<",">=","<=","like"]
|
307
|
+
end
|
308
|
+
|
309
|
+
end
|
310
|
+
|
311
|
+
describe "#comparator?" do
|
312
|
+
|
313
|
+
it "should not consider nil" do
|
314
|
+
Appstats::Query.comparator?(nil).should == false
|
315
|
+
Appstats::Query.comparator?("").should == false
|
316
|
+
end
|
317
|
+
|
318
|
+
|
319
|
+
it "should not consider &&" do
|
320
|
+
Appstats::Query.comparator?("&&").should == false
|
321
|
+
end
|
322
|
+
|
323
|
+
it "should not consider ||" do
|
324
|
+
Appstats::Query.comparator?("||").should == false
|
325
|
+
end
|
326
|
+
|
327
|
+
it "should not consider and" do
|
328
|
+
Appstats::Query.comparator?("and").should == false
|
329
|
+
end
|
330
|
+
|
331
|
+
it "should not consider or" do
|
332
|
+
Appstats::Query.comparator?("or").should == false
|
333
|
+
end
|
334
|
+
|
335
|
+
it "should consider =" do
|
336
|
+
Appstats::Query.comparator?("=").should == true
|
337
|
+
end
|
338
|
+
|
339
|
+
it "should consider !=" do
|
340
|
+
Appstats::Query.comparator?("!=").should == true
|
341
|
+
end
|
342
|
+
|
343
|
+
it "should consider <>" do
|
344
|
+
Appstats::Query.comparator?("<>").should == true
|
345
|
+
end
|
346
|
+
|
347
|
+
it "should consider >" do
|
348
|
+
Appstats::Query.comparator?(">").should == true
|
349
|
+
end
|
350
|
+
|
351
|
+
it "should consider <" do
|
352
|
+
Appstats::Query.comparator?("<").should == true
|
353
|
+
end
|
354
|
+
|
355
|
+
it "should consider >=" do
|
356
|
+
Appstats::Query.comparator?(">=").should == true
|
357
|
+
end
|
358
|
+
|
359
|
+
it "should consider <=" do
|
360
|
+
Appstats::Query.comparator?("<=").should == true
|
361
|
+
end
|
362
|
+
|
363
|
+
|
364
|
+
end
|
358
365
|
|
359
366
|
|
360
367
|
|