appstats 0.1.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/spec/entry_spec.rb CHANGED
@@ -70,24 +70,24 @@ module Appstats
70
70
  entry.month.should == 01
71
71
  entry.day.should == 15
72
72
  entry.hour.should == 10
73
- entry.minute.should == 11
74
- entry.second.should == 12
73
+ entry.min.should == 11
74
+ entry.sec.should == 12
75
75
 
76
76
  entry.occurred_at = Time.parse("2011-02-16 17:18:19")
77
77
  entry.year.should == 2011
78
78
  entry.month.should == 02
79
79
  entry.day.should == 16
80
80
  entry.hour.should == 17
81
- entry.minute.should == 18
82
- entry.second.should == 19
81
+ entry.min.should == 18
82
+ entry.sec.should == 19
83
83
 
84
84
  entry.occurred_at = nil
85
85
  entry.year.should == nil
86
86
  entry.month.should == nil
87
87
  entry.day.should == nil
88
88
  entry.hour.should == nil
89
- entry.minute.should == nil
90
- entry.second.should == nil
89
+ entry.min.should == nil
90
+ entry.sec.should == nil
91
91
 
92
92
  end
93
93
 
@@ -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.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search")
191
+ entry = Entry.load_from_logger_entry("0.3.1 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.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
194
+ entry.raw_entry.should == "0.3.1 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.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
199
+ entry = Entry.load_from_logger_entry("0.3.1 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.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
202
+ entry.raw_entry.should == "0.3.1 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"
@@ -210,25 +210,25 @@ module Appstats
210
210
 
211
211
  end
212
212
 
213
- # describe "#log_collector" do
214
- #
215
- # before(:each) do
216
- # @log_collector = Appstats::LogCollector.new(:host => "a")
217
- # @log_collector.save.should == true
218
- # end
219
- #
220
- # it "should have a log_collector" do
221
- # @entry.log_collector.should == nil
222
- # @entry.log_collector = @log_collector
223
- # @entry.save.should == true
224
- # @entry.reload
225
- # @entry.log_collector.should == @log_collector
226
- #
227
- # @entry = Entry.last
228
- # @entry.log_collector.should == @log_collector
229
- # end
230
- #
231
- # end
213
+ describe "#log_collector" do
214
+
215
+ before(:each) do
216
+ @log_collector = Appstats::LogCollector.new(:host => "a")
217
+ @log_collector.save.should == true
218
+ end
219
+
220
+ it "should have a log_collector" do
221
+ @entry.log_collector.should == nil
222
+ @entry.log_collector = @log_collector
223
+ @entry.save.should == true
224
+ @entry.reload
225
+ @entry.log_collector.should == @log_collector
226
+
227
+ @entry = Entry.last
228
+ @entry.log_collector.should == @log_collector
229
+ end
230
+
231
+ end
232
232
 
233
233
  end
234
234
  end
data/spec/logger_spec.rb CHANGED
@@ -127,29 +127,29 @@ module Appstats
127
127
 
128
128
  it "should handle a statistics entry" do
129
129
  expected = { :action => "address_search", :timestamp => "2010-09-21 23:15:20" }
130
- actual = Appstats::Logger.entry_to_hash("0.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search")
130
+ actual = Appstats::Logger.entry_to_hash("0.3.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search")
131
131
  actual.should == expected
132
132
  end
133
133
 
134
134
  it "should handle contexts" do
135
135
  expected = { :action => "address_filter", :timestamp => "2010-09-21 23:15:20", :server => "Live", :app_name => 'Market' }
136
- actual = Appstats::Logger.entry_to_hash("0.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
136
+ actual = Appstats::Logger.entry_to_hash("0.3.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
137
137
  actual.should == expected
138
138
  end
139
139
 
140
140
  it "should handle actions with the delimiter (and change the delimiter)" do
141
141
  expected = { :action => "address:=search-n", :timestamp => "2010-09-21 23:15:20" }
142
- actual = Appstats::Logger.entry_to_hash("0.1.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n")
142
+ actual = Appstats::Logger.entry_to_hash("0.3.1 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n")
143
143
  actual.should == expected
144
144
 
145
145
  expected = { :action => "address::search==--n", :timestamp => "2010-09-21 23:15:20" }
146
- actual = Appstats::Logger.entry_to_hash("0.1.0 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n")
146
+ actual = Appstats::Logger.entry_to_hash("0.3.1 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n")
147
147
  actual.should == expected
148
148
  end
149
149
 
150
150
  it "should handle contexts with the delimiter (and change the delimiter)" do
151
151
  expected = { :action => "address", :timestamp => "2010-09-21 23:15:20", :server => "market:eval=-n" }
152
- actual = Appstats::Logger.entry_to_hash("0.1.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n")
152
+ actual = Appstats::Logger.entry_to_hash("0.3.1 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n")
153
153
  actual.should == expected
154
154
  end
155
155
 
@@ -158,66 +158,66 @@ module Appstats
158
158
  describe "#entry_to_s" do
159
159
 
160
160
  it "should handle a statistics entry" do
161
- expected = "0.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
161
+ expected = "0.3.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
162
162
  actual = Appstats::Logger.entry_to_s("address_search")
163
163
  actual.should == expected
164
164
  end
165
165
 
166
166
  it "should handle numbers" do
167
- expected = "0.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=1 : note=2.2"
167
+ expected = "0.3.1 setup[:,=,-n] 2010-09-21 23:15:20 action=1 : note=2.2"
168
168
  actual = Appstats::Logger.entry_to_s(1,:note => 2.2)
169
169
  actual.should == expected
170
170
  end
171
171
 
172
172
  it "should handle default contexts" do
173
173
  Appstats::Logger.default_contexts[:app_name] = "market"
174
- expected = "0.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : app_name=market"
174
+ expected = "0.3.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : app_name=market"
175
175
  actual = Appstats::Logger.entry_to_s("address_search")
176
176
  actual.should == expected
177
177
  end
178
178
 
179
179
  it "should handle contexts (and sort them by symbol)" do
180
- expected = "0.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
180
+ expected = "0.3.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
181
181
  actual = Appstats::Logger.entry_to_s("address_filter", { :server => "Live", :app_name => 'Market' })
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
- expected = "0.1.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n"
186
+ expected = "0.3.1 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n"
187
187
  actual = Appstats::Logger.entry_to_s("address:=search-n")
188
188
  actual.should == expected
189
189
 
190
- expected = "0.1.0 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n"
190
+ expected = "0.3.1 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n"
191
191
  actual = Appstats::Logger.entry_to_s("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
- expected = "0.1.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n"
196
+ expected = "0.3.1 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n"
197
197
  actual = Appstats::Logger.entry_to_s("address", :server => 'market:eval=-n')
198
198
  actual.should == expected
199
199
  end
200
200
 
201
201
  it "should ignore spaces" do
202
- expected = "0.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address search"
202
+ expected = "0.3.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address search"
203
203
  actual = Appstats::Logger.entry_to_s("address search")
204
204
  actual.should == expected
205
205
  end
206
206
 
207
207
  it "should convert newlines in action" do
208
- expected = "0.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_-nsearch"
208
+ expected = "0.3.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_-nsearch"
209
209
  actual = Appstats::Logger.entry_to_s("address_\nsearch")
210
210
  actual.should == expected
211
211
  end
212
212
 
213
213
  it "should convert newlines in context" do
214
- expected = "0.1.0 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : blah=some-nlong-nstatement"
214
+ expected = "0.3.1 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : blah=some-nlong-nstatement"
215
215
  actual = Appstats::Logger.entry_to_s("address_search",:blah => "some\nlong\nstatement")
216
216
  actual.should == expected
217
217
  end
218
218
 
219
219
  it "should convert newlines based on the delimiter" do
220
- expected = "0.1.0 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=--nsearch-n"
220
+ expected = "0.3.1 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=--nsearch-n"
221
221
  actual = Appstats::Logger.entry_to_s("address:=\nsearch-n")
222
222
  actual.should == expected
223
223
  end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ module Appstats
4
+ describe Query do
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.input.should == nil
14
+ end
15
+
16
+ it "should allow input on constructor" do
17
+ query = Appstats::Query.new(:input => "# logins")
18
+ query.input.should == "# logins"
19
+ end
20
+
21
+ end
22
+
23
+ describe "#run" do
24
+
25
+ before(:each) do
26
+ Appstats::Entry.delete_all
27
+ end
28
+
29
+ it "should return 0 if no results" do
30
+ query = Appstats::Query.new(:input => "# blahs")
31
+ query.run.should == 0
32
+ end
33
+
34
+ it "should track the count if available" do
35
+ Appstats::Entry.create(:action => "myblahs")
36
+ query = Appstats::Query.new(:input => "# myblahs")
37
+ query.run.should == 1
38
+ Appstats::Entry.create(:action => "myblahs")
39
+ query.run.should == 2
40
+ end
41
+
42
+ end
43
+
44
+
45
+ describe "#to_sql" do
46
+
47
+ before(:all) do
48
+ Appstats::Action.delete_all
49
+ Appstats::Action.create(:name => "login", :plural_name => "logins")
50
+ end
51
+
52
+ it "should return understand nil" do
53
+ expected_sql = "select count(*) from appstats_entries"
54
+ Appstats::Query.new(:input => nil).to_sql.should == expected_sql
55
+ Appstats::Query.new(:input => "").to_sql.should == expected_sql
56
+ Appstats::Query.new.to_sql.should == expected_sql
57
+ end
58
+
59
+ describe "actions" do
60
+
61
+ it "should understand both singular and plural names" do
62
+ expected_sql = "select count(*) from appstats_entries where action = 'login'"
63
+ Appstats::Query.new(:input => "# logins").to_sql.should == expected_sql
64
+ Appstats::Query.new(:input => "# login").to_sql.should == expected_sql
65
+ end
66
+
67
+ it "should use 'itself' if action not found" do
68
+ expected_sql = "select count(*) from appstats_entries where action = 'garblygook'"
69
+ Appstats::Query.new(:input => "# garblygook").to_sql.should == expected_sql
70
+ end
71
+
72
+ end
73
+
74
+ describe "date ranges" do
75
+ it "should understand since dates" do
76
+ expected_sql = "select count(*) from appstats_entries where action = 'login' and occurred_at >= '2010-01-15 00:00:00'"
77
+ Appstats::Query.new(:input => "# logins since 2010-01-15").to_sql.should == expected_sql
78
+ end
79
+ end
80
+
81
+ describe "server_name" do
82
+
83
+ it "should on server_name" do
84
+ 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(:input => "# logins on server my.localnet").to_sql.should == expected_sql
86
+ end
87
+
88
+ end
89
+
90
+ describe "date range and server_name" do
91
+ it "should understand dates and 'on server'" do
92
+ 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(:input => "# logins between 2010-01-15 and 2010-01-31 on server your.localnet").to_sql.should == expected_sql
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "#host_filter_to_sql" do
99
+
100
+ it "should translate blah into EXISTS query" do
101
+ expected = "EXISTS (select * from appstats_log_collectors where appstats_entries.appstats_log_collector_id = id and host = 'a' )"
102
+ Appstats::Query.host_filter_to_sql("a").should == expected
103
+ Appstats::Query.host_filter_to_sql(" a ").should == expected
104
+ end
105
+
106
+ it "should ignore single quotes and spaces" do
107
+ Appstats::Query.host_filter_to_sql("bl'ah").should == "1=1"
108
+ Appstats::Query.host_filter_to_sql("bl ah").should == "1=1"
109
+ end
110
+
111
+ it "should do simple 1=1 if invalid" do
112
+ Appstats::Query.host_filter_to_sql("").should == "1=1"
113
+ Appstats::Query.host_filter_to_sql(nil).should == "1=1"
114
+ end
115
+
116
+ end
117
+
118
+ describe "#context_filter_to_sql" do
119
+
120
+ it "should translate a = b into EXISTS query" do
121
+ expected = "EXISTS (select * from appstats_contexts where appstats_entries.id = appstats_contexts.appstats_entry_id and context_key='a' and context_value='b' )"
122
+ Appstats::Query.context_filter_to_sql("a = b").should == expected
123
+ end
124
+
125
+ it "should ignore single quotes" do
126
+ Appstats::Query.context_filter_to_sql("'a' = b").should == "1=1"
127
+ end
128
+
129
+ it "should do simple 1 = 1 if invalid" do
130
+ Appstats::Query.context_filter_to_sql("blah").should == "1=1"
131
+ Appstats::Query.context_filter_to_sql("").should == "1=1"
132
+ Appstats::Query.context_filter_to_sql(nil).should == "1=1"
133
+ end
134
+
135
+ end
136
+ end
137
+ 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: 27
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
+ - 3
8
9
  - 1
9
- - 0
10
- version: 0.1.0
10
+ version: 0.3.1
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-07 00:00:00 -05:00
18
+ date: 2011-02-09 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -147,13 +147,19 @@ files:
147
147
  - db/migrations/20110204183259_create_log_collectors.rb
148
148
  - db/migrations/20110207200324_add_separated_entry_times.rb
149
149
  - db/migrations/20110207200431_add_indexes.rb
150
+ - db/migrations/20110207213514_create_appstats_actions.rb
151
+ - db/migrations/20110208210921_align_entry_time_names.rb
150
152
  - db/schema.rb
151
153
  - lib/appstats.rb
154
+ - lib/appstats/action.rb
152
155
  - lib/appstats/code_injections.rb
153
156
  - lib/appstats/context.rb
157
+ - lib/appstats/date_range.rb
154
158
  - lib/appstats/entry.rb
159
+ - lib/appstats/entry_date.rb
155
160
  - lib/appstats/log_collector.rb
156
161
  - lib/appstats/logger.rb
162
+ - lib/appstats/query.rb
157
163
  - lib/appstats/tasks.rb
158
164
  - lib/appstats/version.rb
159
165
  - lib/daemons/appstats_log_collector.rb
@@ -175,11 +181,15 @@ files:
175
181
  - script/console
176
182
  - script/destroy
177
183
  - script/generate
184
+ - spec/action_spec.rb
178
185
  - spec/appstats_spec.rb
179
186
  - spec/context_spec.rb
187
+ - spec/date_range_spec.rb
188
+ - spec/entry_date_spec.rb
180
189
  - spec/entry_spec.rb
181
190
  - spec/log_collector_spec.rb
182
191
  - spec/logger_spec.rb
192
+ - spec/query_spec.rb
183
193
  - spec/spec_helper.rb
184
194
  has_rdoc: true
185
195
  homepage: http://github.com/aforward/appstats
@@ -216,9 +226,13 @@ signing_key:
216
226
  specification_version: 3
217
227
  summary: Provide usage statistics about how your application is being used
218
228
  test_files:
229
+ - spec/action_spec.rb
219
230
  - spec/appstats_spec.rb
220
231
  - spec/context_spec.rb
232
+ - spec/date_range_spec.rb
233
+ - spec/entry_date_spec.rb
221
234
  - spec/entry_spec.rb
222
235
  - spec/log_collector_spec.rb
223
236
  - spec/logger_spec.rb
237
+ - spec/query_spec.rb
224
238
  - spec/spec_helper.rb