appstats 0.6.1 → 0.7.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/20110210225606_create_appstats_results.rb +33 -0
- data/db/schema.rb +21 -1
- data/lib/appstats.rb +1 -0
- data/lib/appstats/date_range.rb +21 -11
- data/lib/appstats/query.rb +40 -28
- data/lib/appstats/result.rb +20 -0
- data/lib/appstats/version.rb +1 -1
- data/spec/date_range_spec.rb +47 -12
- data/spec/entry_spec.rb +4 -4
- data/spec/logger_spec.rb +18 -18
- data/spec/query_spec.rb +42 -20
- data/spec/result_spec.rb +58 -0
- metadata +9 -5
data/Gemfile.lock
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
class CreateAppstatsResults < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :appstats_results do |t|
|
4
|
+
t.string :name
|
5
|
+
t.string :result_type
|
6
|
+
t.text :query
|
7
|
+
t.text :query_as_sql
|
8
|
+
t.integer :count
|
9
|
+
t.string :action
|
10
|
+
t.string :host
|
11
|
+
t.integer :page
|
12
|
+
t.datetime :from_date
|
13
|
+
t.datetime :to_date
|
14
|
+
t.timestamps
|
15
|
+
end
|
16
|
+
|
17
|
+
add_index :appstats_results, :name
|
18
|
+
add_index :appstats_results, :action
|
19
|
+
add_index :appstats_results, :host
|
20
|
+
add_index :appstats_results, :page
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.down
|
25
|
+
|
26
|
+
remove_index :appstats_results, :name
|
27
|
+
remove_index :appstats_results, :action
|
28
|
+
remove_index :appstats_results, :host
|
29
|
+
remove_index :appstats_results, :page
|
30
|
+
|
31
|
+
drop_table :appstats_results
|
32
|
+
end
|
33
|
+
end
|
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 => 20110210225606) do
|
14
14
|
|
15
15
|
create_table "appstats_actions", :force => true do |t|
|
16
16
|
t.string "name"
|
@@ -66,6 +66,26 @@ ActiveRecord::Schema.define(:version => 20110210185911) do
|
|
66
66
|
|
67
67
|
add_index "appstats_log_collectors", ["host"], :name => "index_appstats_log_collectors_on_host"
|
68
68
|
|
69
|
+
create_table "appstats_results", :force => true do |t|
|
70
|
+
t.string "name"
|
71
|
+
t.string "result_type"
|
72
|
+
t.text "query"
|
73
|
+
t.text "query_as_sql"
|
74
|
+
t.integer "count"
|
75
|
+
t.string "action"
|
76
|
+
t.string "host"
|
77
|
+
t.integer "page"
|
78
|
+
t.datetime "from_date"
|
79
|
+
t.datetime "to_date"
|
80
|
+
t.datetime "created_at"
|
81
|
+
t.datetime "updated_at"
|
82
|
+
end
|
83
|
+
|
84
|
+
add_index "appstats_results", ["action"], :name => "index_appstats_results_on_action"
|
85
|
+
add_index "appstats_results", ["host"], :name => "index_appstats_results_on_host"
|
86
|
+
add_index "appstats_results", ["name"], :name => "index_appstats_results_on_name"
|
87
|
+
add_index "appstats_results", ["page"], :name => "index_appstats_results_on_page"
|
88
|
+
|
69
89
|
create_table "appstats_test_objects", :force => true do |t|
|
70
90
|
t.string "name"
|
71
91
|
t.datetime "created_at"
|
data/lib/appstats.rb
CHANGED
@@ -11,6 +11,7 @@ require "#{File.dirname(__FILE__)}/appstats/tasks"
|
|
11
11
|
require "#{File.dirname(__FILE__)}/appstats/logger"
|
12
12
|
require "#{File.dirname(__FILE__)}/appstats/log_collector"
|
13
13
|
require "#{File.dirname(__FILE__)}/appstats/query"
|
14
|
+
require "#{File.dirname(__FILE__)}/appstats/result"
|
14
15
|
require "#{File.dirname(__FILE__)}/appstats/test_object"
|
15
16
|
|
16
17
|
# required in the appstats.gemspec
|
data/lib/appstats/date_range.rb
CHANGED
@@ -10,16 +10,26 @@ module Appstats
|
|
10
10
|
@format = data[:format] || :inclusive
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
13
|
+
def from_date
|
14
14
|
return nil if @from.nil?
|
15
15
|
mode = @format == :inclusive ? :start : :end
|
16
|
-
@from.to_time(mode)
|
16
|
+
@from.to_time(mode)
|
17
17
|
end
|
18
|
-
|
19
|
-
def
|
18
|
+
|
19
|
+
def to_date
|
20
20
|
return nil if @to.nil?
|
21
21
|
mode = @format == :exclusive ? :start : :end
|
22
|
-
@to.to_time(mode)
|
22
|
+
@to.to_time(mode)
|
23
|
+
end
|
24
|
+
|
25
|
+
def from_date_to_s
|
26
|
+
return nil if from_date.nil?
|
27
|
+
from_date.strftime('%Y-%m-%d %H:%M:%S')
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_date_to_s
|
31
|
+
return nil if to_date.nil?
|
32
|
+
to_date.strftime('%Y-%m-%d %H:%M:%S')
|
23
33
|
end
|
24
34
|
|
25
35
|
def to_sql
|
@@ -28,9 +38,9 @@ module Appstats
|
|
28
38
|
if !@from.nil? && @to.nil?
|
29
39
|
return case @format
|
30
40
|
when :inclusive then
|
31
|
-
"occurred_at >= '#{
|
41
|
+
"occurred_at >= '#{from_date_to_s}'"
|
32
42
|
when :exclusive then
|
33
|
-
"occurred_at > '#{
|
43
|
+
"occurred_at > '#{from_date_to_s}'"
|
34
44
|
when :fixed_point then
|
35
45
|
answer = "("
|
36
46
|
[:year,:month,:day,:hour,:min,:sec].each do |t|
|
@@ -43,14 +53,14 @@ module Appstats
|
|
43
53
|
end
|
44
54
|
elsif @from.nil? && !@to.nil?
|
45
55
|
return case @format
|
46
|
-
when :inclusive then "occurred_at <= '#{
|
47
|
-
when :exclusive then "occurred_at < '#{
|
56
|
+
when :inclusive then "occurred_at <= '#{to_date_to_s}'"
|
57
|
+
when :exclusive then "occurred_at < '#{to_date_to_s}'"
|
48
58
|
else "1=1"
|
49
59
|
end
|
50
60
|
else
|
51
61
|
return case @format
|
52
|
-
when :inclusive then "(occurred_at >= '#{
|
53
|
-
when :exclusive then "(occurred_at > '#{
|
62
|
+
when :inclusive then "(occurred_at >= '#{from_date_to_s}' and occurred_at <= '#{to_date_to_s}')"
|
63
|
+
when :exclusive then "(occurred_at > '#{from_date_to_s}' and occurred_at < '#{to_date_to_s}')"
|
54
64
|
else "1=1"
|
55
65
|
end
|
56
66
|
end
|
data/lib/appstats/query.rb
CHANGED
@@ -3,40 +3,22 @@ module Appstats
|
|
3
3
|
class Query
|
4
4
|
|
5
5
|
@@default = "1=1"
|
6
|
-
attr_accessor :
|
6
|
+
attr_accessor :query, :action, :host, :date_range, :query_to_sql
|
7
7
|
|
8
8
|
def initialize(data = {})
|
9
|
-
|
9
|
+
self.query=(data[:query])
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
12
|
+
def query=(value)
|
13
|
+
@query = value
|
14
|
+
parse_query
|
14
15
|
end
|
15
16
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
m = current_input.match(/^\s*(\#)\s*([^\s]*)\s*(.*)/)
|
22
|
-
return sql if m.nil?
|
23
|
-
if m[1] == "#"
|
24
|
-
sql += " where action = '#{normalize_action_name(m[2])}'"
|
25
|
-
end
|
26
|
-
current_input = m[3]
|
27
|
-
|
28
|
-
m_on_server = current_input.match(/^(.*)?\s*on\s*server\s*(.*)$/)
|
29
|
-
date_range = m_on_server.nil? ? current_input : m_on_server[1]
|
30
|
-
if date_range.size > 0
|
31
|
-
range = DateRange.parse(date_range)
|
32
|
-
sql += " and #{range.to_sql}" unless range.to_sql == "1=1"
|
33
|
-
end
|
34
|
-
return sql if m_on_server.nil?
|
35
|
-
|
36
|
-
host_name = m_on_server[2]
|
37
|
-
sql += " and exists (select * from appstats_log_collectors where appstats_entries.appstats_log_collector_id = appstats_log_collectors.id and host = '#{host_name}')"
|
38
|
-
|
39
|
-
sql
|
17
|
+
def run
|
18
|
+
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)
|
19
|
+
result.count = ActiveRecord::Base.connection.select_one(@query_to_sql)["count(*)"].to_i
|
20
|
+
result.save
|
21
|
+
result
|
40
22
|
end
|
41
23
|
|
42
24
|
def self.host_filter_to_sql(raw_input)
|
@@ -65,6 +47,36 @@ module Appstats
|
|
65
47
|
action = Appstats::Action.where("plural_name = ?",action_name).first
|
66
48
|
action.nil? ? action_name : action.name
|
67
49
|
end
|
50
|
+
|
51
|
+
def parse_query
|
52
|
+
@query_to_sql = "select count(*) from appstats_entries"
|
53
|
+
@action = nil
|
54
|
+
@host = nil
|
55
|
+
@date_range = DateRange.new
|
56
|
+
return @query_to_sql if @query.nil?
|
57
|
+
current_query = @query
|
58
|
+
|
59
|
+
m = current_query.match(/^\s*(\#)\s*([^\s]*)\s*(.*)/)
|
60
|
+
return @query_to_sql if m.nil?
|
61
|
+
if m[1] == "#"
|
62
|
+
@action = normalize_action_name(m[2])
|
63
|
+
@query_to_sql += " where action = '#{@action}'"
|
64
|
+
end
|
65
|
+
current_query = m[3]
|
66
|
+
|
67
|
+
m_on_server = current_query.match(/^(.*)?\s*on\s*server\s*(.*)$/)
|
68
|
+
date_range_text = m_on_server.nil? ? current_query : m_on_server[1]
|
69
|
+
if date_range_text.size > 0
|
70
|
+
@date_range = DateRange.parse(date_range_text)
|
71
|
+
@query_to_sql += " and #{@date_range.to_sql}" unless @date_range.to_sql == "1=1"
|
72
|
+
end
|
73
|
+
return @query_to_sql if m_on_server.nil?
|
74
|
+
|
75
|
+
@host = m_on_server[2]
|
76
|
+
@query_to_sql += " and exists (select * from appstats_log_collectors where appstats_entries.appstats_log_collector_id = appstats_log_collectors.id and host = '#{@host}')"
|
77
|
+
|
78
|
+
@query_to_sql
|
79
|
+
end
|
68
80
|
|
69
81
|
|
70
82
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Appstats
|
2
|
+
class Result < ActiveRecord::Base
|
3
|
+
set_table_name "appstats_results"
|
4
|
+
|
5
|
+
attr_accessible :name, :result_type, :query, :query_as_sql, :count, :action, :host, :from_date, :to_date
|
6
|
+
|
7
|
+
def ==(o)
|
8
|
+
o.class == self.class && o.send(:state) == state
|
9
|
+
end
|
10
|
+
alias_method :eql?, :==
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def state
|
15
|
+
[name, result_type, query, query_as_sql, count, action, host, from_date, to_date]
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
data/lib/appstats/version.rb
CHANGED
data/spec/date_range_spec.rb
CHANGED
@@ -197,41 +197,76 @@ module Appstats
|
|
197
197
|
end
|
198
198
|
|
199
199
|
|
200
|
-
describe "#
|
200
|
+
describe "#from_date_to_s" do
|
201
201
|
it "should handle nil" do
|
202
|
-
DateRange.new.
|
202
|
+
DateRange.new.from_date_to_s.should == nil
|
203
203
|
end
|
204
204
|
|
205
205
|
it "should be based on time" do
|
206
|
-
DateRange.new(:from => EntryDate.new(:year => 2010)).
|
206
|
+
DateRange.new(:from => EntryDate.new(:year => 2010)).from_date_to_s.should == "2010-01-01 00:00:00"
|
207
207
|
end
|
208
208
|
|
209
209
|
it "should be based on end_of time as-is for exclusive" do
|
210
|
-
DateRange.new(:from => EntryDate.new(:year => 2009), :format => :exclusive).
|
211
|
-
DateRange.new(:from => EntryDate.new(:year => 2009, :month => 2), :format => :exclusive).
|
212
|
-
DateRange.new(:from => EntryDate.new(:year => 2009, :month => 2, :day => 15), :format => :exclusive).
|
210
|
+
DateRange.new(:from => EntryDate.new(:year => 2009), :format => :exclusive).from_date_to_s.should == "2009-12-31 23:59:59"
|
211
|
+
DateRange.new(:from => EntryDate.new(:year => 2009, :month => 2), :format => :exclusive).from_date_to_s.should == "2009-02-28 23:59:59"
|
212
|
+
DateRange.new(:from => EntryDate.new(:year => 2009, :month => 2, :day => 15), :format => :exclusive).from_date_to_s.should == "2009-02-15 23:59:59"
|
213
213
|
end
|
214
214
|
|
215
215
|
end
|
216
216
|
|
217
|
-
describe "#
|
217
|
+
describe "#to_date_to_s" do
|
218
218
|
it "should handle nil" do
|
219
|
-
DateRange.new.
|
219
|
+
DateRange.new.to_date_to_s.should == nil
|
220
220
|
end
|
221
221
|
|
222
222
|
it "should be based on time as-is for exclusive" do
|
223
|
-
DateRange.new(:to => EntryDate.new(:year => 2009), :format => :exclusive).
|
223
|
+
DateRange.new(:to => EntryDate.new(:year => 2009), :format => :exclusive).to_date_to_s.should == "2009-01-01 00:00:00"
|
224
224
|
end
|
225
225
|
|
226
226
|
it "should be based on end_of time as-is for inclusive" do
|
227
|
-
DateRange.new(:to => EntryDate.new(:year => 2009), :format => :inclusive).
|
228
|
-
DateRange.new(:to => EntryDate.new(:year => 2009, :month => 2), :format => :inclusive).
|
229
|
-
DateRange.new(:to => EntryDate.new(:year => 2009, :month => 2, :day => 15), :format => :inclusive).
|
227
|
+
DateRange.new(:to => EntryDate.new(:year => 2009), :format => :inclusive).to_date_to_s.should == "2009-12-31 23:59:59"
|
228
|
+
DateRange.new(:to => EntryDate.new(:year => 2009, :month => 2), :format => :inclusive).to_date_to_s.should == "2009-02-28 23:59:59"
|
229
|
+
DateRange.new(:to => EntryDate.new(:year => 2009, :month => 2, :day => 15), :format => :inclusive).to_date_to_s.should == "2009-02-15 23:59:59"
|
230
230
|
end
|
231
231
|
|
232
232
|
|
233
233
|
end
|
234
234
|
|
235
|
+
describe "#from_date" do
|
236
|
+
it "should handle nil" do
|
237
|
+
DateRange.new.from_date.should == nil
|
238
|
+
end
|
239
|
+
|
240
|
+
it "should be based on time" do
|
241
|
+
DateRange.new(:from => EntryDate.new(:year => 2010)).from_date.to_s.should == Time.parse("2010-01-01 00:00:00").to_s
|
242
|
+
end
|
243
|
+
|
244
|
+
it "should be based on end_of time as-is for exclusive" do
|
245
|
+
DateRange.new(:from => EntryDate.new(:year => 2009), :format => :exclusive).from_date.to_s.should == Time.parse("2009-12-31 23:59:59").to_s
|
246
|
+
DateRange.new(:from => EntryDate.new(:year => 2009, :month => 2), :format => :exclusive).from_date.to_s.should == Time.parse("2009-02-28 23:59:59").to_s
|
247
|
+
DateRange.new(:from => EntryDate.new(:year => 2009, :month => 2, :day => 15), :format => :exclusive).from_date.to_s.should == Time.parse("2009-02-15 23:59:59").to_s
|
248
|
+
end
|
249
|
+
|
250
|
+
end
|
251
|
+
|
252
|
+
describe "#to_date" do
|
253
|
+
it "should handle nil" do
|
254
|
+
DateRange.new.to_date.should == nil
|
255
|
+
end
|
256
|
+
|
257
|
+
it "should be based on time as-is for exclusive" do
|
258
|
+
DateRange.new(:to => EntryDate.new(:year => 2009), :format => :exclusive).to_date.to_s.should == Time.parse("2009-01-01 00:00:00").to_s
|
259
|
+
end
|
260
|
+
|
261
|
+
it "should be based on end_of time as-is for inclusive" do
|
262
|
+
DateRange.new(:to => EntryDate.new(:year => 2009), :format => :inclusive).to_date.to_s.should == Time.parse("2009-12-31 23:59:59").to_s
|
263
|
+
DateRange.new(:to => EntryDate.new(:year => 2009, :month => 2), :format => :inclusive).to_date.to_s.should == Time.parse("2009-02-28 23:59:59").to_s
|
264
|
+
DateRange.new(:to => EntryDate.new(:year => 2009, :month => 2, :day => 15), :format => :inclusive).to_date.to_s.should == Time.parse("2009-02-15 23:59:59").to_s
|
265
|
+
end
|
266
|
+
|
267
|
+
|
268
|
+
end
|
269
|
+
|
235
270
|
describe "#to_sql" do
|
236
271
|
|
237
272
|
before(:each) do
|
data/spec/entry_spec.rb
CHANGED
@@ -188,18 +188,18 @@ module Appstats
|
|
188
188
|
end
|
189
189
|
|
190
190
|
it "should understand an entry without contexts" do
|
191
|
-
entry = Entry.load_from_logger_entry("0.
|
191
|
+
entry = Entry.load_from_logger_entry("0.7.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.7.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
|
195
195
|
entry.occurred_at.should == Time.parse("2010-09-21 23:15:20")
|
196
196
|
end
|
197
197
|
|
198
198
|
it "should understand contexts" do
|
199
|
-
entry = Entry.load_from_logger_entry("0.
|
199
|
+
entry = Entry.load_from_logger_entry("0.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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.7.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/query_spec.rb
CHANGED
@@ -10,15 +10,35 @@ module Appstats
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should set input to nil" do
|
13
|
-
@query.
|
13
|
+
@query.query.should == nil
|
14
14
|
end
|
15
15
|
|
16
|
-
it "should allow
|
17
|
-
query = Appstats::Query.new(:
|
18
|
-
query.
|
16
|
+
it "should allow query on constructor" do
|
17
|
+
query = Appstats::Query.new(:query => "# logins")
|
18
|
+
query.query.should == "# logins"
|
19
19
|
end
|
20
20
|
|
21
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 server 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 server 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
|
+
end
|
22
42
|
|
23
43
|
describe "#run" do
|
24
44
|
|
@@ -27,22 +47,24 @@ module Appstats
|
|
27
47
|
end
|
28
48
|
|
29
49
|
it "should return 0 if no results" do
|
30
|
-
query = Appstats::Query.new(:
|
31
|
-
query.run
|
50
|
+
query = Appstats::Query.new(:query => "# blahs")
|
51
|
+
result = query.run
|
52
|
+
result.new_record?.should == false
|
53
|
+
result.should == Appstats::Result.new(:result_type => :on_demand, :query => "# blahs", :query_as_sql => query.query_to_sql, :count => 0, :action => "blahs")
|
32
54
|
end
|
33
55
|
|
34
56
|
it "should track the count if available" do
|
35
57
|
Appstats::Entry.create(:action => "myblahs")
|
36
|
-
query = Appstats::Query.new(:
|
37
|
-
query.run.should == 1
|
58
|
+
query = Appstats::Query.new(:query => "# myblahs")
|
59
|
+
query.run.count.should == 1
|
38
60
|
Appstats::Entry.create(:action => "myblahs")
|
39
|
-
query.run.should == 2
|
61
|
+
query.run.count.should == 2
|
40
62
|
end
|
41
|
-
|
63
|
+
|
42
64
|
end
|
43
65
|
|
44
66
|
|
45
|
-
describe "#
|
67
|
+
describe "#query_to_sql" do
|
46
68
|
|
47
69
|
before(:all) do
|
48
70
|
Appstats::Action.delete_all
|
@@ -51,22 +73,22 @@ module Appstats
|
|
51
73
|
|
52
74
|
it "should return understand nil" do
|
53
75
|
expected_sql = "select count(*) from appstats_entries"
|
54
|
-
Appstats::Query.new(:
|
55
|
-
Appstats::Query.new(:
|
56
|
-
Appstats::Query.new.
|
76
|
+
Appstats::Query.new(:query => nil).query_to_sql.should == expected_sql
|
77
|
+
Appstats::Query.new(:query => "").query_to_sql.should == expected_sql
|
78
|
+
Appstats::Query.new.query_to_sql.should == expected_sql
|
57
79
|
end
|
58
80
|
|
59
81
|
describe "actions" do
|
60
82
|
|
61
83
|
it "should understand both singular and plural names" do
|
62
84
|
expected_sql = "select count(*) from appstats_entries where action = 'login'"
|
63
|
-
Appstats::Query.new(:
|
64
|
-
Appstats::Query.new(:
|
85
|
+
Appstats::Query.new(:query => "# logins").query_to_sql.should == expected_sql
|
86
|
+
Appstats::Query.new(:query => "# login").query_to_sql.should == expected_sql
|
65
87
|
end
|
66
88
|
|
67
89
|
it "should use 'itself' if action not found" do
|
68
90
|
expected_sql = "select count(*) from appstats_entries where action = 'garblygook'"
|
69
|
-
Appstats::Query.new(:
|
91
|
+
Appstats::Query.new(:query => "# garblygook").query_to_sql.should == expected_sql
|
70
92
|
end
|
71
93
|
|
72
94
|
end
|
@@ -74,7 +96,7 @@ module Appstats
|
|
74
96
|
describe "date ranges" do
|
75
97
|
it "should understand since dates" do
|
76
98
|
expected_sql = "select count(*) from appstats_entries where action = 'login' and occurred_at >= '2010-01-15 00:00:00'"
|
77
|
-
Appstats::Query.new(:
|
99
|
+
Appstats::Query.new(:query => "# logins since 2010-01-15").query_to_sql.should == expected_sql
|
78
100
|
end
|
79
101
|
end
|
80
102
|
|
@@ -82,7 +104,7 @@ module Appstats
|
|
82
104
|
|
83
105
|
it "should on server_name" do
|
84
106
|
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')"
|
85
|
-
Appstats::Query.new(:
|
107
|
+
Appstats::Query.new(:query => "# logins on server my.localnet").query_to_sql.should == expected_sql
|
86
108
|
end
|
87
109
|
|
88
110
|
end
|
@@ -90,7 +112,7 @@ module Appstats
|
|
90
112
|
describe "date range and server_name" do
|
91
113
|
it "should understand dates and 'on server'" do
|
92
114
|
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')"
|
93
|
-
Appstats::Query.new(:
|
115
|
+
Appstats::Query.new(:query => "# logins between 2010-01-15 and 2010-01-31 on server your.localnet").query_to_sql.should == expected_sql
|
94
116
|
end
|
95
117
|
end
|
96
118
|
end
|
data/spec/result_spec.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Appstats
|
4
|
+
describe Result do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@result = Appstats::Result.new
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#initialize" do
|
11
|
+
|
12
|
+
it "should set attributes to nil" do
|
13
|
+
@result.name.should == nil
|
14
|
+
@result.result_type.should == nil
|
15
|
+
@result.query.should == nil
|
16
|
+
@result.query_as_sql.should == nil
|
17
|
+
@result.count.should == nil
|
18
|
+
@result.action.should == nil
|
19
|
+
@result.action.should == nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should set on constructor" do
|
23
|
+
result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
24
|
+
result.name.should == 'a'
|
25
|
+
result.result_type.should == 'b'
|
26
|
+
result.query.should == 'c'
|
27
|
+
result.query_as_sql.should == 'd'
|
28
|
+
result.count.should == 10
|
29
|
+
result.action.should == 'e'
|
30
|
+
result.host.should == 'f'
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#==" do
|
36
|
+
|
37
|
+
it "should be equal on all attributes" do
|
38
|
+
result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
39
|
+
same_result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
40
|
+
(result == same_result).should == true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should be not equal if diferent attributes" do
|
44
|
+
result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
45
|
+
different_result = Appstats::Result.new(:name => 'xxx', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
46
|
+
|
47
|
+
[:name,:result_type,:query,:query_as_sql,:count,:action,:host].each do |attr|
|
48
|
+
different_result = Appstats::Result.new(:name => 'a', :result_type => 'b', :query => 'c', :query_as_sql => 'd', :count => 10, :action => 'e', :host => 'f')
|
49
|
+
different_result.send("#{attr}=","XXX")
|
50
|
+
|
51
|
+
different_result.should_not == result
|
52
|
+
(different_result == result).should == false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
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: 3
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 7
|
9
|
+
- 0
|
10
|
+
version: 0.7.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andrew Forward
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-11 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -150,6 +150,7 @@ files:
|
|
150
150
|
- db/migrations/20110207213514_create_appstats_actions.rb
|
151
151
|
- db/migrations/20110208210921_align_entry_time_names.rb
|
152
152
|
- db/migrations/20110210185911_create_appstats_test_object.rb
|
153
|
+
- db/migrations/20110210225606_create_appstats_results.rb
|
153
154
|
- db/schema.rb
|
154
155
|
- lib/appstats.rb
|
155
156
|
- lib/appstats/action.rb
|
@@ -162,6 +163,7 @@ files:
|
|
162
163
|
- lib/appstats/log_collector.rb
|
163
164
|
- lib/appstats/logger.rb
|
164
165
|
- lib/appstats/query.rb
|
166
|
+
- lib/appstats/result.rb
|
165
167
|
- lib/appstats/tasks.rb
|
166
168
|
- lib/appstats/test_object.rb
|
167
169
|
- lib/appstats/version.rb
|
@@ -194,6 +196,7 @@ files:
|
|
194
196
|
- spec/log_collector_spec.rb
|
195
197
|
- spec/logger_spec.rb
|
196
198
|
- spec/query_spec.rb
|
199
|
+
- spec/result_spec.rb
|
197
200
|
- spec/spec_helper.rb
|
198
201
|
- spec/test_object_spec.rb
|
199
202
|
has_rdoc: true
|
@@ -241,5 +244,6 @@ test_files:
|
|
241
244
|
- spec/log_collector_spec.rb
|
242
245
|
- spec/logger_spec.rb
|
243
246
|
- spec/query_spec.rb
|
247
|
+
- spec/result_spec.rb
|
244
248
|
- spec/spec_helper.rb
|
245
249
|
- spec/test_object_spec.rb
|