appstats 0.9.2 → 0.10.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/20110217234136_add_appstats_results_contexts.rb +9 -0
- data/db/schema.rb +2 -1
- data/lib/appstats/parser.rb +4 -3
- data/lib/appstats/query.rb +5 -5
- data/lib/appstats/result.rb +2 -2
- data/lib/appstats/version.rb +1 -1
- data/spec/entry_spec.rb +4 -4
- data/spec/logger_spec.rb +18 -18
- data/spec/parser_spec.rb +6 -0
- data/spec/query_spec.rb +351 -336
- data/spec/result_spec.rb +18 -10
- metadata +5 -4
data/Gemfile.lock
CHANGED
data/db/schema.rb
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
#
|
11
11
|
# It's strongly recommended to check this file into your version control system.
|
12
12
|
|
13
|
-
ActiveRecord::Schema.define(:version =>
|
13
|
+
ActiveRecord::Schema.define(:version => 20110217234136) do
|
14
14
|
|
15
15
|
create_table "appstats_actions", :force => true do |t|
|
16
16
|
t.string "name"
|
@@ -102,6 +102,7 @@ ActiveRecord::Schema.define(:version => 20110217220357) do
|
|
102
102
|
t.datetime "to_date"
|
103
103
|
t.datetime "created_at"
|
104
104
|
t.datetime "updated_at"
|
105
|
+
t.text "contexts"
|
105
106
|
end
|
106
107
|
|
107
108
|
add_index "appstats_results", ["action"], :name => "index_appstats_results_on_action"
|
data/lib/appstats/parser.rb
CHANGED
@@ -61,7 +61,7 @@ module Appstats
|
|
61
61
|
end
|
62
62
|
else
|
63
63
|
p = parse_word(@text_so_far,rule[:stop],false)
|
64
|
-
add_results(rule[:rule],p[0])
|
64
|
+
add_results(rule[:rule],p[0]) unless p[0].nil?
|
65
65
|
@text_so_far = p[1]
|
66
66
|
end
|
67
67
|
end
|
@@ -95,7 +95,7 @@ module Appstats
|
|
95
95
|
current_text.strip!
|
96
96
|
|
97
97
|
current_text = remove_tokens_at_start(current_text)
|
98
|
-
|
98
|
+
|
99
99
|
if stop_on == :end
|
100
100
|
filter = Parser.merge_regex_filter([nil,@tokenize_regex])
|
101
101
|
m = current_text.match(/^(.*?)(#{filter}.*)$/im)
|
@@ -125,7 +125,8 @@ module Appstats
|
|
125
125
|
answer[1] = m[2] unless m.nil?
|
126
126
|
end
|
127
127
|
end
|
128
|
-
Parser.clean_parsed_word(answer)
|
128
|
+
answer = Parser.clean_parsed_word(answer)
|
129
|
+
answer
|
129
130
|
end
|
130
131
|
|
131
132
|
private
|
data/lib/appstats/query.rb
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
module Appstats
|
3
3
|
class Query
|
4
4
|
|
5
|
-
@@
|
6
|
-
@@
|
5
|
+
@@parser_template = Appstats::Parser.new(:rules => ":operation :action :date on :host where :contexts")
|
6
|
+
@@contexts_parser_template = Appstats::Parser.new(:rules => ":context", :repeating => true, :tokenize => "and or || && = <= >= <> != ( ) like")
|
7
7
|
|
8
8
|
@@nill_query = "select 0 from appstats_entries LIMIT 1"
|
9
9
|
@@default = "1=1"
|
@@ -19,7 +19,7 @@ module Appstats
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def run
|
22
|
-
result = Appstats::Result.new(:result_type => :on_demand, :query => @query, :query_as_sql => @query_to_sql, :action => @action, :host => @host, :from_date => @date_range.from_date, :to_date => @date_range.to_date)
|
22
|
+
result = Appstats::Result.new(:result_type => :on_demand, :query => @query, :query_as_sql => @query_to_sql, :action => @action, :host => @host, :from_date => @date_range.from_date, :to_date => @date_range.to_date, :contexts => @contexts)
|
23
23
|
result.count = ActiveRecord::Base.connection.select_one(@query_to_sql)["count(*)"].to_i
|
24
24
|
result.save
|
25
25
|
result
|
@@ -36,7 +36,7 @@ module Appstats
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def self.contexts_filter_to_sql(raw_input)
|
39
|
-
context_parser = @@
|
39
|
+
context_parser = @@contexts_parser_template.dup
|
40
40
|
return @@default if (raw_input.blank? || !context_parser.parse(raw_input))
|
41
41
|
sql = "EXISTS (select * from appstats_contexts where appstats_entries.id = appstats_contexts.appstats_entry_id and ("
|
42
42
|
|
@@ -104,7 +104,7 @@ module Appstats
|
|
104
104
|
return nil_query if @query.nil?
|
105
105
|
current_query = fix_legacy_structures(@query)
|
106
106
|
|
107
|
-
parser = @@
|
107
|
+
parser = @@parser_template.dup
|
108
108
|
return nil_query unless parser.parse(current_query)
|
109
109
|
|
110
110
|
@operation = parser.results[:operation]
|
data/lib/appstats/result.rb
CHANGED
@@ -2,7 +2,7 @@ module Appstats
|
|
2
2
|
class Result < ActiveRecord::Base
|
3
3
|
set_table_name "appstats_results"
|
4
4
|
|
5
|
-
attr_accessible :name, :result_type, :query, :query_as_sql, :count, :action, :host, :from_date, :to_date
|
5
|
+
attr_accessible :name, :result_type, :query, :query_as_sql, :count, :action, :host, :from_date, :to_date, :contexts
|
6
6
|
|
7
7
|
def date_to_s
|
8
8
|
return "" if from_date.nil? && to_date.nil?
|
@@ -30,7 +30,7 @@ module Appstats
|
|
30
30
|
private
|
31
31
|
|
32
32
|
def state
|
33
|
-
[name, result_type, query, query_as_sql, count, action, host, from_date, to_date]
|
33
|
+
[name, result_type, query, query_as_sql, count, action, host, from_date, to_date,contexts]
|
34
34
|
end
|
35
35
|
|
36
36
|
|
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.create_from_logger_string("0.
|
191
|
+
entry = Entry.create_from_logger_string("0.10.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.10.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.create_from_logger_string("0.
|
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")
|
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.10.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/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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.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.10.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
CHANGED
@@ -356,6 +356,12 @@ module Appstats
|
|
356
356
|
|
357
357
|
end
|
358
358
|
|
359
|
+
it "should handle lots of brackets" do
|
360
|
+
parser = Appstats::Parser.new(:rules => ":context", :repeating => true, :tokenize => "and or || && = <= >= <> != ( ) like")
|
361
|
+
parser.parse("(a=b and c=4) or (aaa=5)").should == true
|
362
|
+
parser.raw_results.should == ["(", {:context=>"a"}, "=", {:context=>"b"}, "and", {:context=>"c"}, "=", {:context=>"4"}, ")", "or", "(", {:context=>"aaa"}, "=", {:context=>"5"}, ")"]
|
363
|
+
end
|
364
|
+
|
359
365
|
end
|
360
366
|
|
361
367
|
end
|
data/spec/query_spec.rb
CHANGED
@@ -3,343 +3,358 @@ require 'spec_helper'
|
|
3
3
|
module Appstats
|
4
4
|
describe Query do
|
5
5
|
|
6
|
-
describe "#initialize" do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
describe "#input" do
|
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
|
-
end
|
56
|
-
|
57
|
-
describe "#run" do
|
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
|
-
|
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
|
+
#
|
166
|
+
it "should handle example" do
|
167
|
+
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
|
+
Appstats::Query.new(:query => "# blahs where (a=b and c=4) or (aaa=5)").query_to_sql.should == expected_sql
|
132
169
|
end
|
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
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
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
|
-
end
|
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
|
-
it "should consider <>" do
|
322
|
-
Appstats::Query.comparator?("<>").should == true
|
323
|
-
end
|
324
|
-
|
325
|
-
it "should consider >" do
|
326
|
-
Appstats::Query.comparator?(">").should == true
|
327
|
-
end
|
328
|
-
|
329
|
-
it "should consider <" do
|
330
|
-
Appstats::Query.comparator?("<").should == true
|
331
|
-
end
|
332
|
-
|
333
|
-
it "should consider >=" do
|
334
|
-
Appstats::Query.comparator?(">=").should == true
|
335
|
-
end
|
336
|
-
|
337
|
-
it "should consider <=" do
|
338
|
-
Appstats::Query.comparator?("<=").should == true
|
339
|
-
end
|
340
|
-
|
341
|
-
|
342
|
-
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
|
343
358
|
|
344
359
|
|
345
360
|
|
data/spec/result_spec.rb
CHANGED
@@ -17,11 +17,13 @@ module Appstats
|
|
17
17
|
@result.query_as_sql.should == nil
|
18
18
|
@result.count.should == nil
|
19
19
|
@result.action.should == nil
|
20
|
-
@result.
|
20
|
+
@result.contexts.should == nil
|
21
|
+
@result.from_date.should == nil
|
22
|
+
@result.to_date.should == nil
|
21
23
|
end
|
22
24
|
|
23
25
|
it "should set on constructor" do
|
24
|
-
result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
26
|
+
result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f', :contexts => 'g', :from_date => Time.parse("2010-01-02"), :to_date => Time.parse("2010-02-03"))
|
25
27
|
result.name.should == 'a'
|
26
28
|
result.result_type.should == 'b'
|
27
29
|
result.query.should == 'c'
|
@@ -29,6 +31,9 @@ module Appstats
|
|
29
31
|
result.count.should == 10
|
30
32
|
result.action.should == 'e'
|
31
33
|
result.host.should == 'f'
|
34
|
+
result.contexts.should == 'g'
|
35
|
+
result.from_date_to_s.should == '2010-01-02'
|
36
|
+
result.to_date_to_s.should == '2010-02-03'
|
32
37
|
end
|
33
38
|
|
34
39
|
end
|
@@ -36,21 +41,24 @@ module Appstats
|
|
36
41
|
describe "#==" do
|
37
42
|
|
38
43
|
it "should be equal on all attributes" do
|
39
|
-
result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
40
|
-
same_result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
44
|
+
result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f', :contexts => 'g', :from_date => Time.parse("2010-01-02"), :to_date => Time.parse("2010-02-03"))
|
45
|
+
same_result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f', :contexts => 'g', :from_date => Time.parse("2010-01-02"), :to_date => Time.parse("2010-02-03"))
|
41
46
|
(result == same_result).should == true
|
42
47
|
end
|
43
48
|
|
44
49
|
it "should be not equal if diferent attributes" do
|
45
|
-
result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
46
|
-
different_result = Appstats::Result.new(:name => 'xxx', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
50
|
+
result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f', :contexts => 'g', :from_date => Time.parse("2010-01-02"), :to_date => Time.parse("2010-02-03"))
|
47
51
|
|
48
|
-
[:name,:result_type,:query,:query_as_sql,:count,:action,:host].each do |attr|
|
49
|
-
different_result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
50
|
-
|
52
|
+
[:name,:result_type,:query,:query_as_sql,:count,:action,:host,:contexts,:from_date,:to_date].each do |attr|
|
53
|
+
different_result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f', :contexts => 'g', :from_date => Time.parse("2010-01-02"), :to_date => Time.parse("2010-02-03"))
|
54
|
+
|
55
|
+
if [:from_date,:to_date].include?(attr)
|
56
|
+
different_result.send("#{attr}=",Time.parse("2011-01-02"))
|
57
|
+
else
|
58
|
+
different_result.send("#{attr}=","XXX")
|
59
|
+
end
|
51
60
|
|
52
61
|
different_result.should_not == result
|
53
|
-
(different_result == result).should == false
|
54
62
|
end
|
55
63
|
end
|
56
64
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: appstats
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 55
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 10
|
9
|
+
- 0
|
10
|
+
version: 0.10.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrew Forward
|
@@ -155,6 +155,7 @@ files:
|
|
155
155
|
- db/migrations/20110217215309_create_appstats_context_keys.rb
|
156
156
|
- db/migrations/20110217220154_create_appstats_context_values.rb
|
157
157
|
- db/migrations/20110217220357_add_additional_contexts_indexes.rb
|
158
|
+
- db/migrations/20110217234136_add_appstats_results_contexts.rb
|
158
159
|
- db/schema.rb
|
159
160
|
- lib/appstats.rb
|
160
161
|
- lib/appstats/action.rb
|