appstats 0.10.0 → 0.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile.lock +1 -1
- data/db/migrations/20110222215437_create_appstats_jobs.rb +16 -0
- data/db/schema.rb +11 -1
- data/lib/appstats.rb +1 -0
- data/lib/appstats/date_range.rb +13 -6
- data/lib/appstats/entry_date.rb +15 -0
- data/lib/appstats/log_collector.rb +35 -1
- data/lib/appstats/query.rb +4 -2
- data/lib/appstats/result_job.rb +54 -0
- data/lib/appstats/version.rb +1 -1
- data/lib/daemons/appstats_log_collector.rb +10 -2
- data/spec/date_range_spec.rb +24 -4
- data/spec/entry_date_spec.rb +34 -0
- data/spec/entry_spec.rb +4 -4
- data/spec/log_collector_spec.rb +77 -0
- data/spec/logger_spec.rb +18 -18
- data/spec/query_spec.rb +355 -348
- data/spec/result_job_spec.rb +333 -0
- metadata +8 -4
@@ -0,0 +1,333 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Appstats
|
4
|
+
describe ResultJob do
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@result_job = Appstats::ResultJob.new
|
8
|
+
@lasts_monday = Time.parse('2010-09-13 23:15:20')
|
9
|
+
@monday = Time.parse('2010-09-20 23:15:20')
|
10
|
+
@tuesday = Time.parse('2010-09-21 23:15:20')
|
11
|
+
@wednesday = Time.parse('2010-09-22 23:15:20')
|
12
|
+
Time.stub!(:now).and_return(@tuesday)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#initialize" do
|
16
|
+
|
17
|
+
it "should set attributes to nil" do
|
18
|
+
@result_job.name.should == nil
|
19
|
+
@result_job.frequency.should == nil
|
20
|
+
@result_job.status.should == nil
|
21
|
+
@result_job.query.should == nil
|
22
|
+
@result_job.last_run_at.should == nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should set on constructor" do
|
26
|
+
result = Appstats::ResultJob.new(:name => 'a', :frequency => 'b', :status => 'c', :query => 'd', :last_run_at => Time.parse("2010-02-03"))
|
27
|
+
result.name.should == 'a'
|
28
|
+
result.frequency.should == 'b'
|
29
|
+
result.status.should == 'c'
|
30
|
+
result.query.should == 'd'
|
31
|
+
result.last_run_at.to_s.should == Time.parse("2010-02-03").to_s
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#should_run" do
|
37
|
+
|
38
|
+
it "should be true if never run before" do
|
39
|
+
@result_job.frequency = "yearly"
|
40
|
+
@result_job.should_run.should == true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should be false if unknown frequency" do
|
44
|
+
@result_job.should_run.should == false
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "once" do
|
48
|
+
|
49
|
+
before(:each) do
|
50
|
+
@result_job.frequency = "once"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should run if not run yet" do
|
54
|
+
@result_job.should_run.should == true
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should not run if already run" do
|
58
|
+
@result_job.last_run_at = Time.parse('2010-09-22')
|
59
|
+
@result_job.should_run.should == false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "daily" do
|
64
|
+
|
65
|
+
before(:each) do
|
66
|
+
@result_job.frequency = "daily"
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should not run if today" do
|
70
|
+
@result_job.last_run_at = Time.parse('2010-09-21')
|
71
|
+
@result_job.should_run.should == false
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should not run if tomorrow" do
|
75
|
+
@result_job.last_run_at = Time.parse('2010-09-22')
|
76
|
+
@result_job.should_run.should == false
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should run if two days ago" do
|
80
|
+
@result_job.last_run_at = Time.parse('2010-09-20')
|
81
|
+
@result_job.should_run.should == true
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should run if more than 2 days ago" do
|
85
|
+
@result_job.last_run_at = Time.parse('2010-09-19')
|
86
|
+
@result_job.should_run.should == true
|
87
|
+
|
88
|
+
@result_job.last_run_at = Time.parse('2010-09-18')
|
89
|
+
@result_job.should_run.should == true
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "weekly" do
|
94
|
+
|
95
|
+
before(:each) do
|
96
|
+
@result_job.frequency = "weekly"
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should not run if today" do
|
100
|
+
@result_job.last_run_at = Time.parse('2010-09-21')
|
101
|
+
@result_job.should_run.should == false
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should not run if tomorrow" do
|
105
|
+
@result_job.last_run_at = Time.parse('2010-09-22')
|
106
|
+
@result_job.should_run.should == false
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should not run if within same week" do
|
110
|
+
@result_job.last_run_at = Time.parse('2010-09-20')
|
111
|
+
@result_job.should_run.should == false
|
112
|
+
|
113
|
+
Time.stub!(:now).and_return(@wednesday)
|
114
|
+
@result_job.should_run.should == false
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should run if exactly a week ago" do
|
118
|
+
@result_job.last_run_at = @last_monday
|
119
|
+
Time.stub!(:now).and_return(@monday)
|
120
|
+
@result_job.should_run.should == true
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should run if more than 1 week ago (based on monday start time)" do
|
124
|
+
@result_job.last_run_at = Time.parse('2010-09-19')
|
125
|
+
@result_job.should_run.should == true
|
126
|
+
|
127
|
+
@result_job.last_run_at = Time.parse('2010-09-18')
|
128
|
+
@result_job.should_run.should == true
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe "monthly" do
|
133
|
+
|
134
|
+
before(:each) do
|
135
|
+
@result_job.frequency = "monthly"
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should not run if today" do
|
139
|
+
@result_job.last_run_at = Time.parse('2010-09-21')
|
140
|
+
@result_job.should_run.should == false
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should not run if tomorrow" do
|
144
|
+
@result_job.last_run_at = Time.parse('2010-09-22')
|
145
|
+
@result_job.should_run.should == false
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should not run if within same month" do
|
149
|
+
@result_job.last_run_at = Time.parse('2010-09-01')
|
150
|
+
@result_job.should_run.should == false
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should run if exactly a month ago" do
|
154
|
+
@result_job.last_run_at = Time.parse('2010-08-01')
|
155
|
+
Time.stub!(:now).and_return(Time.parse('2010-09-01'))
|
156
|
+
@result_job.should_run.should == true
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should run if more than 1 month ago" do
|
160
|
+
@result_job.last_run_at = Time.parse('2010-08-01')
|
161
|
+
@result_job.should_run.should == true
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe "quarterly" do
|
166
|
+
|
167
|
+
before(:each) do
|
168
|
+
@result_job.frequency = "quarterly"
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should not run if today" do
|
172
|
+
@result_job.last_run_at = Time.parse('2010-09-21')
|
173
|
+
@result_job.should_run.should == false
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should not run if tomorrow" do
|
177
|
+
@result_job.last_run_at = Time.parse('2010-09-22')
|
178
|
+
@result_job.should_run.should == false
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should not run if within same quarter" do
|
182
|
+
@result_job.last_run_at = Time.parse('2010-07-03')
|
183
|
+
@result_job.should_run.should == false
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should run if exactly a quarter ago" do
|
187
|
+
@result_job.last_run_at = Time.parse('2010-07-01')
|
188
|
+
Time.stub!(:now).and_return(Time.parse('2010-10-01'))
|
189
|
+
@result_job.should_run.should == true
|
190
|
+
end
|
191
|
+
|
192
|
+
it "should run if more than a quarter ago" do
|
193
|
+
@result_job.last_run_at = Time.parse('2010-06-15')
|
194
|
+
@result_job.should_run.should == true
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
describe "yearly" do
|
199
|
+
|
200
|
+
before(:each) do
|
201
|
+
@result_job.frequency = "yearly"
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should not run if today" do
|
205
|
+
@result_job.last_run_at = Time.parse('2010-09-21')
|
206
|
+
@result_job.should_run.should == false
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should not run if tomorrow" do
|
210
|
+
@result_job.last_run_at = Time.parse('2010-09-22')
|
211
|
+
@result_job.should_run.should == false
|
212
|
+
end
|
213
|
+
|
214
|
+
it "should not run if within same year" do
|
215
|
+
@result_job.last_run_at = Time.parse('2010-02-03')
|
216
|
+
@result_job.should_run.should == false
|
217
|
+
end
|
218
|
+
|
219
|
+
it "should run if exactly a year ago" do
|
220
|
+
@result_job.last_run_at = Time.parse('2010-01-01')
|
221
|
+
Time.stub!(:now).and_return(Time.parse('2011-01-01'))
|
222
|
+
@result_job.should_run.should == true
|
223
|
+
end
|
224
|
+
|
225
|
+
it "should run if more than a year ago" do
|
226
|
+
@result_job.last_run_at = Time.parse('2009-06-15')
|
227
|
+
@result_job.should_run.should == true
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
|
233
|
+
describe "#==" do
|
234
|
+
|
235
|
+
it "should be equal on all attributes" do
|
236
|
+
result = Appstats::ResultJob.new(:name => 'a', :frequency => 'b', :status => 'c', :query => 'd', :last_run_at => Time.parse("2010-02-03"))
|
237
|
+
same_result = Appstats::ResultJob.new(:name => 'a', :frequency => 'b', :status => 'c', :query => 'd', :last_run_at => Time.parse("2010-02-03"))
|
238
|
+
(result == same_result).should == true
|
239
|
+
end
|
240
|
+
|
241
|
+
it "should be not equal if diferent attributes" do
|
242
|
+
result = Appstats::ResultJob.new(:name => 'a', :frequency => 'b', :status => 'c', :query => 'd', :last_run_at => Time.parse("2010-02-03"))
|
243
|
+
|
244
|
+
[:name,:frequency,:status,:query,:last_run_at].each do |attr|
|
245
|
+
different_result = Appstats::ResultJob.new(:name => 'a', :frequency => 'b', :status => 'c', :query => 'd', :last_run_at => Time.parse("2010-02-03"))
|
246
|
+
|
247
|
+
if [:last_run_at].include?(attr)
|
248
|
+
different_result.send("#{attr}=",Time.parse("2011-01-02"))
|
249
|
+
else
|
250
|
+
different_result.send("#{attr}=","XXX")
|
251
|
+
end
|
252
|
+
|
253
|
+
different_result.should_not == result
|
254
|
+
end
|
255
|
+
end
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
describe "#run" do
|
260
|
+
|
261
|
+
before(:each) do
|
262
|
+
Appstats::ResultJob.delete_all
|
263
|
+
Appstats::Result.delete_all
|
264
|
+
Appstats::Entry.delete_all
|
265
|
+
end
|
266
|
+
|
267
|
+
it "nothing to do" do
|
268
|
+
Appstats::ResultJob.run.should == 0
|
269
|
+
end
|
270
|
+
|
271
|
+
it "should update job" do
|
272
|
+
job1 = Appstats::ResultJob.create(:query => "# blahs", :frequency => "once")
|
273
|
+
Appstats::ResultJob.run.should == 1
|
274
|
+
|
275
|
+
job1.reload
|
276
|
+
job1.last_run_at.to_s.should == @tuesday.to_s
|
277
|
+
end
|
278
|
+
|
279
|
+
it "should log when no queries" do
|
280
|
+
Appstats.should_receive(:log).with(:info, "No result jobs to run.")
|
281
|
+
Appstats::ResultJob.run.should == 0
|
282
|
+
end
|
283
|
+
|
284
|
+
it "should ignore 'once' with a last_run_at date" do
|
285
|
+
job1 = Appstats::ResultJob.create(:name => "x", :query => "# blahs", :frequency => "once", :last_run_at => Time.now)
|
286
|
+
Appstats.should_receive(:log).with(:info, "No result jobs to run.")
|
287
|
+
Appstats::ResultJob.run.should == 0
|
288
|
+
end
|
289
|
+
|
290
|
+
it "should log which queries are run" do
|
291
|
+
job1 = Appstats::ResultJob.create(:name => "x", :query => "# blahs", :frequency => "weekly", :last_run_at => Time.now)
|
292
|
+
job2 = Appstats::ResultJob.create(:name => "y", :query => "# blahs where type=1", :frequency => "daily")
|
293
|
+
|
294
|
+
Appstats.should_receive(:log).with(:info, "About to analyze 2 result job(s).")
|
295
|
+
Appstats.should_receive(:log).with(:info, " - Job x NOT run [ID #{job1.id}, FREQUENCY weekly, QUERY # blahs]")
|
296
|
+
Appstats.should_receive(:log).with(:info, " - Job y run [ID #{job2.id}, FREQUENCY daily, QUERY # blahs where type=1]")
|
297
|
+
Appstats.should_receive(:log).with(:info, "Ran 1 query(ies).")
|
298
|
+
|
299
|
+
Appstats::ResultJob.run.should == 1
|
300
|
+
end
|
301
|
+
|
302
|
+
it "should filter out 'once' with a date" do
|
303
|
+
|
304
|
+
end
|
305
|
+
|
306
|
+
it "should create results" do
|
307
|
+
job1 = Appstats::ResultJob.create(:name => "x", :query => "# blahs", :frequency => "once")
|
308
|
+
job2 = Appstats::ResultJob.create(:name => "y", :query => "# blahs where type=1", :frequency => "once")
|
309
|
+
Appstats::Entry.create_from_logger("blahs", :type => "2")
|
310
|
+
Appstats::Entry.create_from_logger("blahs", :type => "1")
|
311
|
+
|
312
|
+
Appstats::ResultJob.run.should == 2
|
313
|
+
|
314
|
+
all = Appstats::Result.all
|
315
|
+
all.count.should == 2
|
316
|
+
|
317
|
+
all[0].name.should == "x"
|
318
|
+
all[1].name.should == "y"
|
319
|
+
|
320
|
+
all[0].query.should == "# blahs"
|
321
|
+
all[1].query.should == "# blahs where type=1"
|
322
|
+
|
323
|
+
all[0].result_type.should == "result_job"
|
324
|
+
all[1].result_type.should == "result_job"
|
325
|
+
|
326
|
+
all[0].count.should == 2
|
327
|
+
all[1].count.should == 1
|
328
|
+
end
|
329
|
+
|
330
|
+
end
|
331
|
+
|
332
|
+
end
|
333
|
+
end
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 11
|
9
|
+
- 2
|
10
|
+
version: 0.11.2
|
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-22 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -156,6 +156,7 @@ files:
|
|
156
156
|
- db/migrations/20110217220154_create_appstats_context_values.rb
|
157
157
|
- db/migrations/20110217220357_add_additional_contexts_indexes.rb
|
158
158
|
- db/migrations/20110217234136_add_appstats_results_contexts.rb
|
159
|
+
- db/migrations/20110222215437_create_appstats_jobs.rb
|
159
160
|
- db/schema.rb
|
160
161
|
- lib/appstats.rb
|
161
162
|
- lib/appstats/action.rb
|
@@ -173,6 +174,7 @@ files:
|
|
173
174
|
- lib/appstats/parser.rb
|
174
175
|
- lib/appstats/query.rb
|
175
176
|
- lib/appstats/result.rb
|
177
|
+
- lib/appstats/result_job.rb
|
176
178
|
- lib/appstats/tasks.rb
|
177
179
|
- lib/appstats/test_object.rb
|
178
180
|
- lib/appstats/version.rb
|
@@ -209,6 +211,7 @@ files:
|
|
209
211
|
- spec/logger_spec.rb
|
210
212
|
- spec/parser_spec.rb
|
211
213
|
- spec/query_spec.rb
|
214
|
+
- spec/result_job_spec.rb
|
212
215
|
- spec/result_spec.rb
|
213
216
|
- spec/spec_helper.rb
|
214
217
|
- spec/test_object_spec.rb
|
@@ -261,6 +264,7 @@ test_files:
|
|
261
264
|
- spec/logger_spec.rb
|
262
265
|
- spec/parser_spec.rb
|
263
266
|
- spec/query_spec.rb
|
267
|
+
- spec/result_job_spec.rb
|
264
268
|
- spec/result_spec.rb
|
265
269
|
- spec/spec_helper.rb
|
266
270
|
- spec/test_object_spec.rb
|