appstats 0.12.2 → 0.12.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- appstats (0.12.2)
4
+ appstats (0.12.4)
5
5
  daemons
6
6
  net-scp
7
7
  rails (>= 2.3.0)
@@ -51,13 +51,20 @@ module Appstats
51
51
  def self.create_from_logger_string(action_and_contexts)
52
52
  return false if action_and_contexts.nil? || action_and_contexts == ''
53
53
  hash = Logger.entry_to_hash(action_and_contexts)
54
- entry = Appstats::Entry.new(:action => hash[:action], :raw_entry => action_and_contexts)
54
+
55
+ action_name = hash[:action].kind_of?(Array) ? hash[:action][0] : hash[:action]
56
+ entry = Appstats::Entry.new(:action => action_name, :raw_entry => action_and_contexts)
55
57
  entry.occurred_at = Time.parse(hash[:timestamp]) unless hash[:timestamp].nil?
56
58
  hash.each do |key,value|
57
- next if key == :action
58
59
  next if key == :timestamp
59
- context = Appstats::Context.create(:context_key => key, :context_value => value)
60
- entry.contexts<< context
60
+ all_values = value.kind_of?(Array) ? value : [value]
61
+ if key == :action
62
+ all_values = all_values[1..-1]
63
+ end
64
+ all_values.each do |value|
65
+ context = Appstats::Context.create(:context_key => key, :context_value => value)
66
+ entry.contexts<< context
67
+ end
61
68
  end
62
69
  entry.save
63
70
  entry
@@ -6,6 +6,7 @@ module Appstats
6
6
  set_table_name "appstats_log_collectors"
7
7
 
8
8
  attr_accessible :host, :filename, :status
9
+ has_many :entries, :table_name => 'appstats_entries', :foreign_key => 'appstats_log_collector_id', :order => 'action'
9
10
 
10
11
  def local_filename
11
12
  File.expand_path("#{File.dirname(__FILE__)}/../../log/appstats_remote_log_#{id}.log")
@@ -19,6 +20,16 @@ module Appstats
19
20
  "#{m[1]}#{prefix}#{m[2]}"
20
21
  end
21
22
 
23
+ def unprocess_entries
24
+ return false unless ["processed","destroyed"].include?(status)
25
+ entries.each do |entry|
26
+ entry.destroy
27
+ end
28
+ status = "downloaded"
29
+ save
30
+ true
31
+ end
32
+
22
33
  def self.should_process(last_time)
23
34
  return true if last_time.nil?
24
35
  Time.now.day > last_time.day
@@ -50,14 +50,25 @@ module Appstats
50
50
  return hash if action_and_contexts.nil?
51
51
  setup = action_and_contexts.match(/(.*?) setup\[(.*?),(.*?),(.*?)\] (.*? .*?) (.*)/)
52
52
  return hash if setup.nil?
53
+ hash.delete(:action)
53
54
  hash.delete(:raw_input)
54
55
  full, version, section_delimiter, assign_delimiter, newline_delimiter, timestamp, input = setup.to_a
55
56
 
56
57
  hash[:timestamp] = timestamp
57
58
  input.split(section_delimiter).each do |pair|
58
59
  key,value = pair.strip.split(assign_delimiter)
59
- hash[key.to_sym] = value
60
+ key_symbol = key.to_sym
61
+ if hash[key_symbol].nil?
62
+ hash[key.to_sym] = value
63
+ elsif hash[key_symbol].kind_of?(String)
64
+ hash[key.to_sym] = [ hash[key_symbol], value ]
65
+ else
66
+ all_values = hash[key_symbol]
67
+ all_values<< value
68
+ hash[key.to_sym] = all_values
69
+ end
60
70
  end
71
+ hash[:action] = "UNKNOWN_ACTION" if hash[:action].nil?
61
72
  hash
62
73
  end
63
74
 
@@ -1,3 +1,3 @@
1
1
  module Appstats
2
- VERSION = "0.12.2"
2
+ VERSION = "0.12.4"
3
3
  end
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.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search")
196
+ entry = Entry.create_from_logger_string("0.12.4 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.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
199
+ entry.raw_entry.should == "0.12.4 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.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
204
+ entry = Entry.create_from_logger_string("0.12.4 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.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
207
+ entry.raw_entry.should == "0.12.4 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"
@@ -213,6 +213,34 @@ module Appstats
213
213
  entry.contexts[1].context_value = "Live"
214
214
  end
215
215
 
216
+ it "should handle 'action' as a context" do
217
+ entry = Entry.create_from_logger_string('0.12.4 setup[:,=,-n] 2011-02-24 12:59:57 action=page-view : action=save_ovcen : app_name=cdb')
218
+ Entry.count.should == @before_count + 1
219
+ entry.action.should == "page-view"
220
+ entry.raw_entry.should == "0.12.4 setup[:,=,-n] 2011-02-24 12:59:57 action=page-view : action=save_ovcen : app_name=cdb"
221
+ entry.occurred_at.should == Time.parse("2011-02-24 12:59:57")
222
+ entry.contexts.size.should == 2
223
+ entry.contexts[0].context_key = "action"
224
+ entry.contexts[0].context_value = "save_ovcen"
225
+ entry.contexts[1].context_key = "app_name"
226
+ entry.contexts[1].context_value = "cdb"
227
+
228
+ end
229
+
230
+ it "should handle multiple of the same 'context'" do
231
+ entry = Entry.create_from_logger_string('0.12.4 setup[:,=,-n] 2011-02-24 12:59:57 action=page-view : app_name=market : app_name=cdb')
232
+ Entry.count.should == @before_count + 1
233
+ entry.action.should == "page-view"
234
+ entry.raw_entry.should == "0.12.4 setup[:,=,-n] 2011-02-24 12:59:57 action=page-view : app_name=market : app_name=cdb"
235
+ entry.occurred_at.should == Time.parse("2011-02-24 12:59:57")
236
+ entry.contexts.size.should == 2
237
+ entry.contexts[0].context_key = "app_name"
238
+ entry.contexts[0].context_value = "market"
239
+ entry.contexts[1].context_key = "app_name"
240
+ entry.contexts[1].context_value = "cdb"
241
+
242
+ end
243
+
216
244
  end
217
245
 
218
246
  describe "#create_from_logger" do
@@ -380,6 +380,79 @@ module Appstats
380
380
  end
381
381
  end
382
382
 
383
+ describe "#entries" do
384
+
385
+ it "should return 0 for new log_collectors" do
386
+ @log_collector.entries.empty?.should == true
387
+ end
388
+
389
+ it "should return entries" do
390
+ @log_collector.status = "processed"
391
+ entry = Entry.new
392
+ entry.log_collector = @log_collector
393
+ entry.save
394
+
395
+ @log_collector.reload
396
+ @log_collector.entries.size.should == 1
397
+
398
+ entry = Entry.new
399
+ entry.log_collector = @log_collector
400
+ entry.save
401
+
402
+ @log_collector.reload
403
+ @log_collector.entries.size.should == 2
404
+ end
405
+
406
+ end
407
+
408
+ describe "#unprocess_entries" do
409
+
410
+ before(:each) do
411
+ @log_collector.save.should == true
412
+ end
413
+
414
+ it "should ignore if not processed or destroyed" do
415
+ @log_collector.status = "blah"
416
+ @log_collector.unprocess_entries.should == false
417
+ end
418
+
419
+ it "should reset status back to downloaded" do
420
+ @log_collector.status = "processed"
421
+ @log_collector.unprocess_entries.should == true
422
+ @log_collector.reload
423
+ @log_collector.status = "downloaded"
424
+ end
425
+
426
+ it "should delete all entries" do
427
+ @log_collector.status = "processed"
428
+ entry = Entry.new
429
+ entry.log_collector = @log_collector
430
+ entry.save
431
+ entry_id = entry.id
432
+
433
+ @log_collector.unprocess_entries.should == true
434
+ @log_collector.reload
435
+ Entry.exists?(entry_id).should == false
436
+ end
437
+
438
+ it "should delete all contexts" do
439
+ @log_collector.status = "processed"
440
+ entry = Entry.new
441
+ entry.log_collector = @log_collector
442
+ entry.save
443
+
444
+ context = Context.new
445
+ context.entry = entry
446
+ context.save
447
+ context_id = context.id
448
+
449
+ @log_collector.unprocess_entries.should == true
450
+ @log_collector.reload
451
+ Context.exists?(context_id).should == false
452
+ end
453
+
454
+ end
455
+
383
456
 
384
457
  end
385
458
  end
data/spec/logger_spec.rb CHANGED
@@ -115,7 +115,7 @@ module Appstats
115
115
 
116
116
  it "should accept numbers" do
117
117
  Appstats::Logger.entry(5, :blah => 6)
118
- Appstats::Logger.raw_read.should == ["0.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=5 : blah=6"]
118
+ Appstats::Logger.raw_read.should == ["0.12.4 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.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=appstats-exception : error=blah : on=login"]
127
+ Appstats::Logger.raw_read.should == ["0.12.4 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,47 @@ 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.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search")
144
+ actual = Appstats::Logger.entry_to_hash("0.12.4 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.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
150
+ actual = Appstats::Logger.entry_to_hash("0.12.4 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live")
151
+ actual.should == expected
152
+ end
153
+
154
+ it "should handle multiple actions" do
155
+ expected = { :action => ["address_filter", "blah"], :timestamp => "2010-09-21 23:15:20", :server => "Live", :app_name => 'Market' }
156
+ actual = Appstats::Logger.entry_to_hash("0.12.4 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : action=blah : app_name=Market : server=Live")
157
+ actual.should == expected
158
+ end
159
+
160
+ it "should handle multiple of same context" do
161
+ expected = { :action => "address_filter", :timestamp => "2010-09-21 23:15:20", :server => "Live", :app_name => ['Sin','Market'] }
162
+ actual = Appstats::Logger.entry_to_hash("0.12.4 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Sin : app_name=Market : server=Live")
163
+ actual.should == expected
164
+ end
165
+
166
+ it "should handle no actions" do
167
+ expected = { :action => "UNKNOWN_ACTION", :timestamp => "2010-09-21 23:15:20", :server => "Live", :app_name => 'Market' }
168
+ actual = Appstats::Logger.entry_to_hash("0.12.4 setup[:,=,-n] 2010-09-21 23:15:20 app_name=Market : server=Live")
151
169
  actual.should == expected
152
170
  end
153
171
 
154
172
  it "should handle actions with the delimiter (and change the delimiter)" do
155
173
  expected = { :action => "address:=search-n", :timestamp => "2010-09-21 23:15:20" }
156
- actual = Appstats::Logger.entry_to_hash("0.12.2 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n")
174
+ actual = Appstats::Logger.entry_to_hash("0.12.4 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n")
157
175
  actual.should == expected
158
176
 
159
177
  expected = { :action => "address::search==--n", :timestamp => "2010-09-21 23:15:20" }
160
- actual = Appstats::Logger.entry_to_hash("0.12.2 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n")
178
+ actual = Appstats::Logger.entry_to_hash("0.12.4 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n")
161
179
  actual.should == expected
162
180
  end
163
181
 
164
182
  it "should handle contexts with the delimiter (and change the delimiter)" do
165
183
  expected = { :action => "address", :timestamp => "2010-09-21 23:15:20", :server => "market:eval=-n" }
166
- actual = Appstats::Logger.entry_to_hash("0.12.2 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n")
184
+ actual = Appstats::Logger.entry_to_hash("0.12.4 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n")
167
185
  actual.should == expected
168
186
  end
169
187
 
@@ -172,66 +190,66 @@ module Appstats
172
190
  describe "#entry_to_s" do
173
191
 
174
192
  it "should handle a statistics entry" do
175
- expected = "0.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
193
+ expected = "0.12.4 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search"
176
194
  actual = Appstats::Logger.entry_to_s("address_search")
177
195
  actual.should == expected
178
196
  end
179
197
 
180
198
  it "should handle numbers" do
181
- expected = "0.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=1 : note=2.2"
199
+ expected = "0.12.4 setup[:,=,-n] 2010-09-21 23:15:20 action=1 : note=2.2"
182
200
  actual = Appstats::Logger.entry_to_s(1,:note => 2.2)
183
201
  actual.should == expected
184
202
  end
185
203
 
186
204
  it "should handle default contexts" do
187
205
  Appstats::Logger.default_contexts[:app_name] = "market"
188
- expected = "0.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : app_name=market"
206
+ expected = "0.12.4 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : app_name=market"
189
207
  actual = Appstats::Logger.entry_to_s("address_search")
190
208
  actual.should == expected
191
209
  end
192
210
 
193
211
  it "should handle contexts (and sort them by symbol)" do
194
- expected = "0.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
212
+ expected = "0.12.4 setup[:,=,-n] 2010-09-21 23:15:20 action=address_filter : app_name=Market : server=Live"
195
213
  actual = Appstats::Logger.entry_to_s("address_filter", { :server => "Live", :app_name => 'Market' })
196
214
  actual.should == expected
197
215
  end
198
216
 
199
217
  it "should handle actions with the delimiter (and change the delimiter)" do
200
- expected = "0.12.2 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n"
218
+ expected = "0.12.4 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=search-n"
201
219
  actual = Appstats::Logger.entry_to_s("address:=search-n")
202
220
  actual.should == expected
203
221
 
204
- expected = "0.12.2 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n"
222
+ expected = "0.12.4 setup[:::,===,---n] 2010-09-21 23:15:20 action===address::search==--n"
205
223
  actual = Appstats::Logger.entry_to_s("address::search==--n")
206
224
  actual.should == expected
207
225
  end
208
226
 
209
227
  it "should handle contexts with the delimiter (and change the delimiter)" do
210
- expected = "0.12.2 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n"
228
+ expected = "0.12.4 setup[::,==,--n] 2010-09-21 23:15:20 action==address :: server==market:eval=-n"
211
229
  actual = Appstats::Logger.entry_to_s("address", :server => 'market:eval=-n')
212
230
  actual.should == expected
213
231
  end
214
232
 
215
233
  it "should ignore spaces" do
216
- expected = "0.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address search"
234
+ expected = "0.12.4 setup[:,=,-n] 2010-09-21 23:15:20 action=address search"
217
235
  actual = Appstats::Logger.entry_to_s("address search")
218
236
  actual.should == expected
219
237
  end
220
238
 
221
239
  it "should convert newlines in action" do
222
- expected = "0.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address_-nsearch"
240
+ expected = "0.12.4 setup[:,=,-n] 2010-09-21 23:15:20 action=address_-nsearch"
223
241
  actual = Appstats::Logger.entry_to_s("address_\nsearch")
224
242
  actual.should == expected
225
243
  end
226
244
 
227
245
  it "should convert newlines in context" do
228
- expected = "0.12.2 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : blah=some-nlong-nstatement"
246
+ expected = "0.12.4 setup[:,=,-n] 2010-09-21 23:15:20 action=address_search : blah=some-nlong-nstatement"
229
247
  actual = Appstats::Logger.entry_to_s("address_search",:blah => "some\nlong\nstatement")
230
248
  actual.should == expected
231
249
  end
232
250
 
233
251
  it "should convert newlines based on the delimiter" do
234
- expected = "0.12.2 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=--nsearch-n"
252
+ expected = "0.12.4 setup[::,==,--n] 2010-09-21 23:15:20 action==address:=--nsearch-n"
235
253
  actual = Appstats::Logger.entry_to_s("address:=\nsearch-n")
236
254
  actual.should == expected
237
255
  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: 43
4
+ hash: 39
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 12
9
- - 2
10
- version: 0.12.2
9
+ - 4
10
+ version: 0.12.4
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-23 00:00:00 -05:00
18
+ date: 2011-02-25 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency