appstats 0.20.0 → 0.20.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/appstats/appstats_query.rb +74 -71
- data/lib/appstats/version.rb +1 -1
- data/spec/entry_spec.rb +8 -8
- data/spec/logger_spec.rb +22 -22
- metadata +3 -3
data/Gemfile.lock
CHANGED
@@ -1,92 +1,95 @@
|
|
1
|
-
class AppstatsQuery
|
2
1
|
|
3
|
-
|
2
|
+
module Appstats
|
3
|
+
class AppstatsQuery
|
4
|
+
|
5
|
+
attr_accessor :query
|
4
6
|
|
5
|
-
|
7
|
+
@@default_query = "select 0 as num"
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
@@action_to_available_contexts =
|
10
|
+
{
|
11
|
+
"appstats_queries" => [ "action", "contexts", "group_by" ],
|
12
|
+
"bomb" => []
|
13
|
+
}
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
def query_to_sql
|
16
|
+
return @@default_query if query.nil?
|
17
|
+
query.query_to_sql
|
18
|
+
end
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def group_query_to_sql
|
21
|
+
return nil if query.nil?
|
22
|
+
query.group_query_to_sql
|
23
|
+
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
def process_query
|
26
|
+
return if query.nil?
|
27
|
+
query.query_to_sql = @@default_query
|
28
|
+
query.group_query_to_sql = nil
|
29
|
+
return if query.action.blank?
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
action = query.action.pluralize.downcase
|
32
|
+
case action
|
33
|
+
when "appstats_queries"
|
34
|
+
count_filter = "COUNT(*)"
|
35
|
+
query.query_to_sql = "select #{count_filter} as num from appstats_results#{build_where_clause}"
|
36
|
+
when "bombs"
|
37
|
+
query.query_to_sql = "invalid sql"
|
38
|
+
end
|
39
|
+
query.group_query_to_sql = query.query_to_sql.sub("#{count_filter} as num","#{context_key_filter_name(action)} as context_key_filter, #{context_value_filter_name(action)} as context_value_filter, COUNT(*) as num") + " group by context_value_filter" unless query.group_by.empty?
|
36
40
|
end
|
37
|
-
query.group_query_to_sql = query.query_to_sql.sub("#{count_filter} as num","#{context_key_filter_name(action)} as context_key_filter, #{context_value_filter_name(action)} as context_value_filter, COUNT(*) as num") + " group by context_value_filter" unless query.group_by.empty?
|
38
|
-
end
|
39
41
|
|
40
|
-
|
41
|
-
|
42
|
-
|
42
|
+
def run
|
43
|
+
query.run
|
44
|
+
end
|
43
45
|
|
44
|
-
|
45
|
-
|
46
|
-
|
46
|
+
def db_connection
|
47
|
+
ActiveRecord::Base.connection
|
48
|
+
end
|
47
49
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
50
|
+
def self.available_action?(action)
|
51
|
+
return false if action.blank?
|
52
|
+
return @@action_to_available_contexts.keys.include?(action.downcase.pluralize)
|
53
|
+
end
|
52
54
|
|
53
|
-
|
55
|
+
private
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
57
|
+
def build_where_clause
|
58
|
+
where_clause = ""
|
59
|
+
action = query.action.pluralize.downcase
|
60
|
+
available_contexts = @@action_to_available_contexts[action]
|
61
|
+
status = :context
|
62
|
+
query.parsed_contexts.each do |lookup|
|
63
|
+
next if status == :context && lookup.kind_of?(String)
|
64
|
+
next if status == :join && !lookup.kind_of?(String)
|
65
|
+
next if status == :context && (lookup[:context_value].nil? || !available_contexts.include?(lookup[:context_key]))
|
64
66
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
67
|
+
where_clause = " where" if where_clause.blank?
|
68
|
+
if lookup.kind_of?(String)
|
69
|
+
where_clause += " #{lookup}"
|
70
|
+
status = :context
|
71
|
+
elsif !lookup[:context_value].nil? && available_contexts.include?(lookup[:context_key])
|
72
|
+
where_clause += " #{database_column(action,lookup[:context_key])} #{lookup[:comparator]} '#{Appstats::Query.sqlclean(lookup[:context_value])}'"
|
73
|
+
status = :join
|
74
|
+
end
|
72
75
|
end
|
76
|
+
where_clause
|
73
77
|
end
|
74
|
-
where_clause
|
75
|
-
end
|
76
78
|
|
77
|
-
|
78
|
-
|
79
|
-
|
79
|
+
def context_key_filter_name(action)
|
80
|
+
"'" + query.group_by.join(",") + "'"
|
81
|
+
end
|
80
82
|
|
81
|
-
|
82
|
-
|
83
|
-
|
83
|
+
def context_value_filter_name(action)
|
84
|
+
database_names = query.group_by.collect do |name|
|
85
|
+
database_column(action,name)
|
86
|
+
end
|
87
|
+
"concat(ifnull("+ database_names.join(",'--'),',',ifnull(") +",'--'))"
|
84
88
|
end
|
85
|
-
"concat(ifnull("+ database_names.join(",'--'),',',ifnull(") +",'--'))"
|
86
|
-
end
|
87
89
|
|
88
|
-
|
89
|
-
|
90
|
-
|
90
|
+
def database_column(action,name)
|
91
|
+
name
|
92
|
+
end
|
91
93
|
|
92
|
-
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/appstats/version.rb
CHANGED
data/spec/entry_spec.rb
CHANGED
@@ -193,18 +193,18 @@ module Appstats
|
|
193
193
|
end
|
194
194
|
|
195
195
|
it "should understand an entry without contexts" do
|
196
|
-
entry = Entry.create_from_logger_string("0.20.
|
196
|
+
entry = Entry.create_from_logger_string("0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search")
|
197
197
|
Entry.count.should == @before_count + 1
|
198
198
|
entry.action.should == "address_search"
|
199
|
-
entry.raw_entry.should == "0.20.
|
199
|
+
entry.raw_entry.should == "0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
|
200
200
|
entry.occurred_at.should == Time.parse("2010-09-21 23:15:20")
|
201
201
|
end
|
202
202
|
|
203
203
|
it "should understand contexts" do
|
204
|
-
entry = Entry.create_from_logger_string("0.20.
|
204
|
+
entry = Entry.create_from_logger_string("0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
|
205
205
|
Entry.count.should == @before_count + 1
|
206
206
|
entry.action.should == "address_filter"
|
207
|
-
entry.raw_entry.should == "0.20.
|
207
|
+
entry.raw_entry.should == "0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
|
208
208
|
entry.occurred_at.should == Time.parse("2010-09-21 23:15:20")
|
209
209
|
entry.contexts.size.should == 2
|
210
210
|
entry.contexts[0].context_key = "app_name"
|
@@ -214,10 +214,10 @@ module Appstats
|
|
214
214
|
end
|
215
215
|
|
216
216
|
it "should handle 'action' as a context" do
|
217
|
-
entry = Entry.create_from_logger_string('0.20.
|
217
|
+
entry = Entry.create_from_logger_string('0.20.1 setup[:,=,-n] 2011-02-24 12:59:57 action=page-view : action=save_ovcen : app_name=cdb')
|
218
218
|
Entry.count.should == @before_count + 1
|
219
219
|
entry.action.should == "page-view"
|
220
|
-
entry.raw_entry.should == "0.20.
|
220
|
+
entry.raw_entry.should == "0.20.1 setup[:,=,-n] 2011-02-24 12:59:57 action=page-view : action=save_ovcen : app_name=cdb"
|
221
221
|
entry.occurred_at.should == Time.parse("2011-02-24 12:59:57")
|
222
222
|
entry.contexts.size.should == 2
|
223
223
|
entry.contexts[0].context_key = "action"
|
@@ -228,10 +228,10 @@ module Appstats
|
|
228
228
|
end
|
229
229
|
|
230
230
|
it "should handle multiple of the same 'context'" do
|
231
|
-
entry = Entry.create_from_logger_string('0.20.
|
231
|
+
entry = Entry.create_from_logger_string('0.20.1 setup[:,=,-n] 2011-02-24 12:59:57 action=page-view : app_name=market : app_name=cdb')
|
232
232
|
Entry.count.should == @before_count + 1
|
233
233
|
entry.action.should == "page-view"
|
234
|
-
entry.raw_entry.should == "0.20.
|
234
|
+
entry.raw_entry.should == "0.20.1 setup[:,=,-n] 2011-02-24 12:59:57 action=page-view : app_name=market : app_name=cdb"
|
235
235
|
entry.occurred_at.should == Time.parse("2011-02-24 12:59:57")
|
236
236
|
entry.contexts.size.should == 2
|
237
237
|
entry.contexts[0].context_key = "app_name"
|
data/spec/logger_spec.rb
CHANGED
@@ -122,12 +122,12 @@ module Appstats
|
|
122
122
|
|
123
123
|
it "should accept numbers" do
|
124
124
|
Appstats::Logger.entry(5, :blah => 6)
|
125
|
-
Appstats::Logger.raw_read.should == ["0.20.
|
125
|
+
Appstats::Logger.raw_read.should == ["0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=5 : blah=6"]
|
126
126
|
end
|
127
127
|
|
128
128
|
it "should accept arrays" do
|
129
129
|
Appstats::Logger.entry('search', :provider => [ 'one', 'two' ])
|
130
|
-
Appstats::Logger.raw_read.should == ["0.20.
|
130
|
+
Appstats::Logger.raw_read.should == ["0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=search : provider=one : provider=two"]
|
131
131
|
end
|
132
132
|
|
133
133
|
|
@@ -137,7 +137,7 @@ module Appstats
|
|
137
137
|
|
138
138
|
it "should look similar to regular entry" do
|
139
139
|
Appstats::Logger.exception_entry(RuntimeError.new("blah"),:on => "login")
|
140
|
-
Appstats::Logger.raw_read.should == ["0.20.
|
140
|
+
Appstats::Logger.raw_read.should == ["0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=appstats-exception : error=blah : on=login"]
|
141
141
|
end
|
142
142
|
|
143
143
|
end
|
@@ -154,47 +154,47 @@ module Appstats
|
|
154
154
|
|
155
155
|
it "should handle a statistics entry" do
|
156
156
|
expected = { :action => "address_search", :timestamp => "2010-09-21 23:15:20" }
|
157
|
-
actual = Appstats::Logger.entry_to_hash("0.20.
|
157
|
+
actual = Appstats::Logger.entry_to_hash("0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search")
|
158
158
|
actual.should == expected
|
159
159
|
end
|
160
160
|
|
161
161
|
it "should handle contexts" do
|
162
162
|
expected = { :action => "address_filter", :timestamp => "2010-09-21 23:15:20", :server => "Live", :app_name => 'Market' }
|
163
|
-
actual = Appstats::Logger.entry_to_hash("0.20.
|
163
|
+
actual = Appstats::Logger.entry_to_hash("0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
|
164
164
|
actual.should == expected
|
165
165
|
end
|
166
166
|
|
167
167
|
it "should handle multiple actions" do
|
168
168
|
expected = { :action => ["address_filter", "blah"], :timestamp => "2010-09-21 23:15:20", :server => "Live", :app_name => 'Market' }
|
169
|
-
actual = Appstats::Logger.entry_to_hash("0.20.
|
169
|
+
actual = Appstats::Logger.entry_to_hash("0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : action=blah : app_name=Market : server=Live")
|
170
170
|
actual.should == expected
|
171
171
|
end
|
172
172
|
|
173
173
|
it "should handle multiple of same context" do
|
174
174
|
expected = { :action => "address_filter", :timestamp => "2010-09-21 23:15:20", :server => "Live", :app_name => ['Sin','Market'] }
|
175
|
-
actual = Appstats::Logger.entry_to_hash("0.20.
|
175
|
+
actual = Appstats::Logger.entry_to_hash("0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Sin : app_name=Market : server=Live")
|
176
176
|
actual.should == expected
|
177
177
|
end
|
178
178
|
|
179
179
|
it "should handle no actions" do
|
180
180
|
expected = { :action => "UNKNOWN_ACTION", :timestamp => "2010-09-21 23:15:20", :server => "Live", :app_name => 'Market' }
|
181
|
-
actual = Appstats::Logger.entry_to_hash("0.20.
|
181
|
+
actual = Appstats::Logger.entry_to_hash("0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 app_name=Market : server=Live")
|
182
182
|
actual.should == expected
|
183
183
|
end
|
184
184
|
|
185
185
|
it "should handle actions with the delimiter (and change the delimiter)" do
|
186
186
|
expected = { :action => "address:=search-n", :timestamp => "2010-09-21 23:15:20" }
|
187
|
-
actual = Appstats::Logger.entry_to_hash("0.20.
|
187
|
+
actual = Appstats::Logger.entry_to_hash("0.20.1 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n")
|
188
188
|
actual.should == expected
|
189
189
|
|
190
190
|
expected = { :action => "address::search==--n", :timestamp => "2010-09-21 23:15:20" }
|
191
|
-
actual = Appstats::Logger.entry_to_hash("0.20.
|
191
|
+
actual = Appstats::Logger.entry_to_hash("0.20.1 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n")
|
192
192
|
actual.should == expected
|
193
193
|
end
|
194
194
|
|
195
195
|
it "should handle contexts with the delimiter (and change the delimiter)" do
|
196
196
|
expected = { :action => "address", :timestamp => "2010-09-21 23:15:20", :server => "market:eval=-n" }
|
197
|
-
actual = Appstats::Logger.entry_to_hash("0.20.
|
197
|
+
actual = Appstats::Logger.entry_to_hash("0.20.1 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n")
|
198
198
|
actual.should == expected
|
199
199
|
end
|
200
200
|
|
@@ -203,66 +203,66 @@ module Appstats
|
|
203
203
|
describe "#entry_to_s" do
|
204
204
|
|
205
205
|
it "should handle a statistics entry" do
|
206
|
-
expected = "0.20.
|
206
|
+
expected = "0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
|
207
207
|
actual = Appstats::Logger.entry_to_s("address_search")
|
208
208
|
actual.should == expected
|
209
209
|
end
|
210
210
|
|
211
211
|
it "should handle numbers" do
|
212
|
-
expected = "0.20.
|
212
|
+
expected = "0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=1 : note=2.2"
|
213
213
|
actual = Appstats::Logger.entry_to_s(1,:note => 2.2)
|
214
214
|
actual.should == expected
|
215
215
|
end
|
216
216
|
|
217
217
|
it "should handle default contexts" do
|
218
218
|
Appstats::Logger.default_contexts[:app_name] = "market"
|
219
|
-
expected = "0.20.
|
219
|
+
expected = "0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : app_name=market"
|
220
220
|
actual = Appstats::Logger.entry_to_s("address_search")
|
221
221
|
actual.should == expected
|
222
222
|
end
|
223
223
|
|
224
224
|
it "should handle contexts (and sort them by symbol)" do
|
225
|
-
expected = "0.20.
|
225
|
+
expected = "0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
|
226
226
|
actual = Appstats::Logger.entry_to_s("address_filter", { :server => "Live", :app_name => 'Market' })
|
227
227
|
actual.should == expected
|
228
228
|
end
|
229
229
|
|
230
230
|
it "should handle actions with the delimiter (and change the delimiter)" do
|
231
|
-
expected = "0.20.
|
231
|
+
expected = "0.20.1 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n"
|
232
232
|
actual = Appstats::Logger.entry_to_s("address:=search-n")
|
233
233
|
actual.should == expected
|
234
234
|
|
235
|
-
expected = "0.20.
|
235
|
+
expected = "0.20.1 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n"
|
236
236
|
actual = Appstats::Logger.entry_to_s("address::search==--n")
|
237
237
|
actual.should == expected
|
238
238
|
end
|
239
239
|
|
240
240
|
it "should handle contexts with the delimiter (and change the delimiter)" do
|
241
|
-
expected = "0.20.
|
241
|
+
expected = "0.20.1 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n"
|
242
242
|
actual = Appstats::Logger.entry_to_s("address", :server => 'market:eval=-n')
|
243
243
|
actual.should == expected
|
244
244
|
end
|
245
245
|
|
246
246
|
it "should ignore spaces" do
|
247
|
-
expected = "0.20.
|
247
|
+
expected = "0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address search"
|
248
248
|
actual = Appstats::Logger.entry_to_s("address search")
|
249
249
|
actual.should == expected
|
250
250
|
end
|
251
251
|
|
252
252
|
it "should convert newlines in action" do
|
253
|
-
expected = "0.20.
|
253
|
+
expected = "0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_-nsearch"
|
254
254
|
actual = Appstats::Logger.entry_to_s("address_\nsearch")
|
255
255
|
actual.should == expected
|
256
256
|
end
|
257
257
|
|
258
258
|
it "should convert newlines in context" do
|
259
|
-
expected = "0.20.
|
259
|
+
expected = "0.20.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : blah=some-nlong-nstatement"
|
260
260
|
actual = Appstats::Logger.entry_to_s("address_search",:blah => "some\nlong\nstatement")
|
261
261
|
actual.should == expected
|
262
262
|
end
|
263
263
|
|
264
264
|
it "should convert newlines based on the delimiter" do
|
265
|
-
expected = "0.20.
|
265
|
+
expected = "0.20.1 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=--nsearch-n"
|
266
266
|
actual = Appstats::Logger.entry_to_s("address:=\nsearch-n")
|
267
267
|
actual.should == expected
|
268
268
|
end
|
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: 77
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 20
|
9
|
-
-
|
10
|
-
version: 0.20.
|
9
|
+
- 1
|
10
|
+
version: 0.20.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrew Forward
|