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/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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search")
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
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"
@@ -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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=5 : blah=6"]
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=appstats-exception : error=blah : on=login"]
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search")
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
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.10.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n")
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.10.0 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n")
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.10.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n")
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=1 : note=2.2"
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : app_name=market"
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
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.10.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n"
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.10.0 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n"
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.10.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n"
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address search"
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_-nsearch"
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.10.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : blah=some-nlong-nstatement"
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.10.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=--nsearch-n"
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
- # 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 track contexts" do
71
- # query = Appstats::Query.new(:query => "# blahs where (a=b and c=4) or (aaa=5)")
72
- # result = query.run
73
- # result.new_record?.should == false
74
- # result.contexts.should == "(a=b and c=4) or (aaa=5)"
75
- # end
76
- #
77
- #
78
- # it "should track the count if available" do
79
- # Appstats::Entry.create(:action => "myblahs")
80
- # query = Appstats::Query.new(:query => "# myblahs")
81
- # query.run.count.should == 1
82
- # Appstats::Entry.create(:action => "myblahs")
83
- # query.run.count.should == 2
84
- # end
85
- #
86
- # it "should perform the action search" do
87
- # Appstats::Entry.create_from_logger("myblahs", :one => "11", :two => "222")
88
- # Appstats::Entry.create_from_logger("myblahs", :one => "111", :two => "22")
89
- #
90
- # query = Appstats::Query.new(:query => "# myblahs where one=11")
91
- # result = query.run
92
- # result.count.should == 1
93
- #
94
- # query = Appstats::Query.new(:query => "# myblahs where one=anything")
95
- # query.run.count.should == 0
96
- #
97
- # query = Appstats::Query.new(:query => "# myblahs where one=11 && two=22")
98
- # query.run.count.should == 0
99
- #
100
- # query = Appstats::Query.new(:query => "# myblahs where one=11 || two=22")
101
- # query.run.count.should == 2
102
- # end
103
- #
104
- # end
105
- #
106
- #
107
- # describe "#query_to_sql" do
108
- #
109
- # before(:all) do
110
- # Appstats::Action.delete_all
111
- # Appstats::Action.create(:name => "login", :plural_name => "logins")
112
- # end
113
- #
114
- # it "should return understand nil" do
115
- # expected_sql = "select 0 from appstats_entries LIMIT 1"
116
- # Appstats::Query.new(:query => nil).query_to_sql.should == expected_sql
117
- # Appstats::Query.new(:query => "").query_to_sql.should == expected_sql
118
- # Appstats::Query.new.query_to_sql.should == expected_sql
119
- # end
120
- #
121
- # describe "actions" do
122
- #
123
- # it "should understand both singular and plural names" do
124
- # expected_sql = "select count(*) from appstats_entries where action = 'login'"
125
- # Appstats::Query.new(:query => "# logins").query_to_sql.should == expected_sql
126
- # Appstats::Query.new(:query => "# login").query_to_sql.should == expected_sql
127
- # end
128
- #
129
- # it "should use 'itself' if action not found" do
130
- # expected_sql = "select count(*) from appstats_entries where action = 'garblygook'"
131
- # Appstats::Query.new(:query => "# garblygook").query_to_sql.should == expected_sql
132
- # end
133
- #
134
- # end
135
- #
136
- # describe "date ranges" do
137
- # it "should understand since dates" do
138
- # expected_sql = "select count(*) from appstats_entries where action = 'login' and occurred_at >= '2010-01-15 00:00:00'"
139
- # Appstats::Query.new(:query => "# logins since 2010-01-15").query_to_sql.should == expected_sql
140
- # end
141
- # end
142
- #
143
- # describe "server_name" do
144
- #
145
- # it "should on_name" do
146
- # 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' )"
147
- # Appstats::Query.new(:query => "# logins on my.localnet").query_to_sql.should == expected_sql
148
- # end
149
- #
150
- # end
151
- #
152
- # describe "date range and server_name" do
153
- # it "should understand dates and 'on'" do
154
- # 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' )"
155
- # Appstats::Query.new(:query => "# logins between 2010-01-15 and 2010-01-31 on your.localnet").query_to_sql.should == expected_sql
156
- # end
157
- # end
158
- #
159
- # describe "where clause" do
160
- #
161
- # it "should understand no quotes" do
162
- # 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')))"
163
- # Appstats::Query.new(:query => "# logins where user = aforward").query_to_sql.should == expected_sql
164
- # end
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
- # end
172
- #
173
- # end
174
- #
175
- # describe "#host_filter_to_sql" do
176
- #
177
- # it "should translate blah into EXISTS query" do
178
- # expected = "EXISTS (select * from appstats_log_collectors where appstats_entries.appstats_log_collector_id = appstats_log_collectors.id and host = 'a' )"
179
- # Appstats::Query.host_filter_to_sql("a").should == expected
180
- # Appstats::Query.host_filter_to_sql(" a ").should == expected
181
- # end
182
- #
183
- # it "should ignore single quotes and spaces" do
184
- # Appstats::Query.host_filter_to_sql("bl'ah").should == "1=1"
185
- # Appstats::Query.host_filter_to_sql("bl ah").should == "1=1"
186
- # end
187
- #
188
- # it "should do simple 1=1 if invalid" do
189
- # Appstats::Query.host_filter_to_sql("").should == "1=1"
190
- # Appstats::Query.host_filter_to_sql(nil).should == "1=1"
191
- # end
192
- #
193
- # end
194
- #
195
- # describe "#contexts_filter_to_sql" do
196
- #
197
- # before(:each) do
198
- # @template = "EXISTS (select * from appstats_contexts where appstats_entries.id = appstats_contexts.appstats_entry_id and ("
199
- # end
200
- #
201
- # it "should translate a = b into EXISTS query" do
202
- # Appstats::Query.contexts_filter_to_sql("a=b").should == "#{@template} (context_key = 'a' and context_value = 'b')))"
203
- # Appstats::Query.contexts_filter_to_sql(" a = b ").should == "#{@template} (context_key = 'a' and context_value = 'b')))"
204
- # end
205
- #
206
- # it "should ignore single quotes" do
207
- # Appstats::Query.contexts_filter_to_sql("'aaa'='bbbb'").should == "#{@template} (context_key = 'aaa' and context_value = 'bbbb')))"
208
- # Appstats::Query.contexts_filter_to_sql(" 'aaa' = 'bbbb' ").should == "#{@template} (context_key = 'aaa' and context_value = 'bbbb')))"
209
- # end
210
- #
211
- # it "should allow for searching for all entries of a certain context" do
212
- # Appstats::Query.contexts_filter_to_sql("aaa").should == "#{@template} (context_key = 'aaa')))"
213
- # end
214
- #
215
- # it "should allow for searching for several entries of a certain context" do
216
- # Appstats::Query.contexts_filter_to_sql("aaa || bbb").should == "#{@template} (context_key = 'aaa') or (context_key = 'bbb')))"
217
- # end
218
- #
219
- # it "should allow complex queries" do
220
- # 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')))"
221
- # end
222
- #
223
- # it "should support or" do
224
- # 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')))"
225
- # end
226
- #
227
- # it "should support like" do
228
- # Appstats::Query.contexts_filter_to_sql("user like '%andrew%'").should == "#{@template} (context_key = 'user' and context_value like '%andrew%')))"
229
- # end
230
- #
231
- # it "should support and" do
232
- # 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')))"
233
- # end
234
- #
235
- #
236
- # it "should do simple 1 = 1 if invalid" do
237
- # Appstats::Query.contexts_filter_to_sql("").should == "1=1"
238
- # Appstats::Query.contexts_filter_to_sql(nil).should == "1=1"
239
- # end
240
- #
241
- # end
242
- #
243
- # describe "#sqlize" do
244
- #
245
- # it "should handle nil" do
246
- # Appstats::Query.sqlize(nil).should == nil
247
- # Appstats::Query.sqlize('').should == ''
248
- # end
249
- #
250
- # it "should understand &&" do
251
- # Appstats::Query.sqlize("&&").should == "and"
252
- # end
253
- #
254
- # it "should understand ||" do
255
- # Appstats::Query.sqlize("||").should == "or"
256
- # end
257
- #
258
- # it "should understand !=" do
259
- # Appstats::Query.sqlize("!=").should == "<>"
260
- # end
261
- #
262
- # it "should set everything else as-is" do
263
- # Appstats::Query.sqlize("blah").should == "blah"
264
- # end
265
- #
266
- # end
267
- #
268
- # describe "#sqlclean" do
269
- #
270
- # it "should handle nil" do
271
- # Appstats::Query.sqlclean(nil).should == nil
272
- # Appstats::Query.sqlclean('').should == ''
273
- # end
274
- #
275
- # it "should remove exterior quotes" do
276
- # Appstats::Query.sqlclean("'a'").should == "a"
277
- # Appstats::Query.sqlclean("'bbb'").should == "bbb"
278
- # Appstats::Query.sqlclean('"a"').should == "a"
279
- # Appstats::Query.sqlclean('"bbb"').should == "bbb"
280
- # end
281
- #
282
- # it "should handle normal text" do
283
- # Appstats::Query.sqlclean('abc').should == 'abc'
284
- # end
285
- #
286
- # it "should handle slashes" do
287
- # Appstats::Query.sqlclean('a\b').should == 'a\\\\b'
288
- # end
289
- #
290
- # it "should handle single quotes" do
291
- # Appstats::Query.sqlclean("a'b").should == "a''b"
292
- # end
293
- #
294
- # end
295
- #
296
- # describe "#comparators" do
297
- #
298
- # it "should be a list " do
299
- # Appstats::Query.comparators.should == ["=","!=","<>",">","<",">=","<=","like"]
300
- # end
301
- #
302
- # end
303
- #
304
- # describe "#comparator?" do
305
- #
306
- # it "should not consider nil" do
307
- # Appstats::Query.comparator?(nil).should == false
308
- # Appstats::Query.comparator?("").should == false
309
- # end
310
- #
311
- #
312
- # it "should not consider &&" do
313
- # Appstats::Query.comparator?("&&").should == false
314
- # end
315
- #
316
- # it "should not consider ||" do
317
- # Appstats::Query.comparator?("||").should == false
318
- # end
319
- #
320
- # it "should not consider and" do
321
- # Appstats::Query.comparator?("and").should == false
322
- # end
323
- #
324
- # it "should not consider or" do
325
- # Appstats::Query.comparator?("or").should == false
326
- # end
327
- #
328
- # it "should consider =" do
329
- # Appstats::Query.comparator?("=").should == true
330
- # end
331
- #
332
- # it "should consider !=" do
333
- # Appstats::Query.comparator?("!=").should == true
334
- # end
335
- #
336
- # it "should consider <>" do
337
- # Appstats::Query.comparator?("<>").should == true
338
- # end
339
- #
340
- # it "should consider >" do
341
- # Appstats::Query.comparator?(">").should == true
342
- # end
343
- #
344
- # it "should consider <" do
345
- # Appstats::Query.comparator?("<").should == true
346
- # end
347
- #
348
- # it "should consider >=" do
349
- # Appstats::Query.comparator?(">=").should == true
350
- # end
351
- #
352
- # it "should consider <=" do
353
- # Appstats::Query.comparator?("<=").should == true
354
- # end
355
- #
356
- #
357
- # end
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