jzimmek-reportme 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/reportme/report.rb +10 -1
- data/lib/reportme/report_factory.rb +62 -19
- data/test/reportme_test.rb +24 -2
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.1
|
data/lib/reportme/report.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Reportme
|
2
2
|
class Report
|
3
3
|
|
4
|
-
|
4
|
+
attr_accessor :name
|
5
5
|
|
6
6
|
def initialize(report_factory, name, temporary=false)
|
7
7
|
|
@@ -10,6 +10,7 @@ module Reportme
|
|
10
10
|
@report_factory = report_factory
|
11
11
|
@name = name
|
12
12
|
@depends_on = []
|
13
|
+
@histories = []
|
13
14
|
@temporary = temporary
|
14
15
|
end
|
15
16
|
|
@@ -21,6 +22,14 @@ module Reportme
|
|
21
22
|
@source = block
|
22
23
|
end
|
23
24
|
|
25
|
+
def histories(histories=[])
|
26
|
+
@histories = histories
|
27
|
+
end
|
28
|
+
|
29
|
+
def historice?(history)
|
30
|
+
@histories.include?(history)
|
31
|
+
end
|
32
|
+
|
24
33
|
def depends_on(dependencies=[])
|
25
34
|
dependencies = dependencies.collect{|d| d.to_sym}
|
26
35
|
@depends_on += dependencies
|
@@ -78,8 +78,8 @@ module Reportme
|
|
78
78
|
exec("alter table #{table_name} add index(von);")
|
79
79
|
|
80
80
|
if period_name != :day
|
81
|
-
exec("alter table #{table_name} add
|
82
|
-
exec("alter table #{table_name} add index(
|
81
|
+
exec("alter table #{table_name} add _day date after von;")
|
82
|
+
exec("alter table #{table_name} add index(_day);")
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
@@ -113,52 +113,68 @@ module Reportme
|
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
|
-
def
|
116
|
+
def __opts(opts={})
|
117
|
+
opts = {
|
118
|
+
:since => Date.today,
|
119
|
+
:init => false,
|
120
|
+
:notify_subscribers => true
|
121
|
+
}.merge(opts)
|
122
|
+
end
|
123
|
+
|
124
|
+
def historice(opts={})
|
125
|
+
|
126
|
+
opts = __opts(opts)
|
127
|
+
|
128
|
+
since = opts[:since]
|
129
|
+
|
117
130
|
raise "since cannot be in the future" if since.future?
|
118
131
|
|
119
|
-
__do_and_clean
|
132
|
+
__do_and_clean(:calendar_week, opts) do |period_name|
|
120
133
|
|
121
134
|
loop do
|
122
135
|
|
123
136
|
run_dependency_aware(@@reports) do |report|
|
137
|
+
|
124
138
|
next if report.temporary?
|
125
|
-
|
139
|
+
next unless report.historice?(period_name)
|
140
|
+
|
141
|
+
__report_period(report, Period.calc(since, [period_name]).first)
|
126
142
|
end
|
127
143
|
|
128
144
|
since += 1.day
|
129
145
|
break if since.future?
|
130
146
|
end
|
131
147
|
|
132
|
-
self.class.__notify_subscriber
|
133
148
|
end
|
134
149
|
end
|
135
150
|
|
136
|
-
def run(
|
151
|
+
def run(opts={})
|
152
|
+
|
153
|
+
opts = __opts(opts)
|
154
|
+
|
155
|
+
opts[:init] = true
|
156
|
+
|
157
|
+
since = opts[:since]
|
137
158
|
|
138
159
|
raise "since cannot be in the future" if since.future?
|
139
160
|
|
140
|
-
__do_and_clean
|
161
|
+
__do_and_clean(:day, opts) do |period_name|
|
141
162
|
|
142
163
|
loop do
|
143
164
|
|
144
165
|
run_dependency_aware(@@reports) do |report|
|
145
|
-
__report_period(report, Period.calc(since, [
|
166
|
+
__report_period(report, Period.calc(since, [period_name]).first)
|
146
167
|
end
|
147
168
|
|
148
169
|
since += 1.day
|
149
170
|
break if since.future?
|
150
171
|
end
|
151
172
|
|
152
|
-
self.class.__notify_subscriber
|
153
173
|
end
|
154
174
|
|
155
175
|
end
|
156
176
|
|
157
|
-
def __do_and_clean(period_name, opts
|
158
|
-
|
159
|
-
opts = {
|
160
|
-
:init => false
|
161
|
-
}.merge(opts)
|
177
|
+
def __do_and_clean(period_name, opts, &block)
|
162
178
|
|
163
179
|
begin
|
164
180
|
@@report_creations.clear
|
@@ -170,11 +186,18 @@ module Reportme
|
|
170
186
|
@@init.call if @@init && opts[:init]
|
171
187
|
|
172
188
|
run_dependency_aware(@@reports) do |report|
|
189
|
+
|
190
|
+
if period_name != :day
|
191
|
+
next unless report.historice?(period_name)
|
192
|
+
end
|
193
|
+
|
173
194
|
ensure_report_tables_exist(report, period_name)
|
174
195
|
end
|
175
196
|
|
176
|
-
|
177
|
-
|
197
|
+
block.call(period_name)
|
198
|
+
|
199
|
+
self.class.__notify_subscriber if opts[:notify_subscribers]
|
200
|
+
|
178
201
|
ensure
|
179
202
|
@@reports.each do |report|
|
180
203
|
|
@@ -243,6 +266,26 @@ module Reportme
|
|
243
266
|
end
|
244
267
|
|
245
268
|
end
|
269
|
+
|
270
|
+
def self.print_dependency_tree(level=0, reports=@@reports)
|
271
|
+
|
272
|
+
reports.each do |r|
|
273
|
+
|
274
|
+
if level > 0
|
275
|
+
(level + 1).times do
|
276
|
+
print " "
|
277
|
+
end
|
278
|
+
end
|
279
|
+
|
280
|
+
print "|-"
|
281
|
+
|
282
|
+
puts r.name
|
283
|
+
|
284
|
+
print_dependency_tree(level + 1, r.dependencies.collect{|d| report_by_name(d)})
|
285
|
+
|
286
|
+
end
|
287
|
+
|
288
|
+
end
|
246
289
|
|
247
290
|
def __dependency_hash
|
248
291
|
dependencies = {}
|
@@ -286,13 +329,13 @@ module Reportme
|
|
286
329
|
sql = <<-SQL
|
287
330
|
select
|
288
331
|
'#{von}' as von,
|
289
|
-
#{(
|
332
|
+
#{(['date(von) as _day'] + column_names).join("\n,")}
|
290
333
|
from
|
291
334
|
#{table_name_day}
|
292
335
|
where
|
293
336
|
von between '#{von}' and '#{bis}'
|
294
337
|
group by
|
295
|
-
#{(
|
338
|
+
#{(['date(von)'] + column_names).join("\n,")}
|
296
339
|
SQL
|
297
340
|
|
298
341
|
end
|
data/test/reportme_test.rb
CHANGED
@@ -159,7 +159,7 @@ class ReportmeTest < Test::Unit::TestCase
|
|
159
159
|
# should be ignored
|
160
160
|
exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 5 day));");
|
161
161
|
|
162
|
-
create_visit_report_factory(:periods => [:day]).run(3.days.ago)
|
162
|
+
create_visit_report_factory(:periods => [:day]).run(:since => 3.days.ago)
|
163
163
|
assert_equal 4, one("select count(1) as cnt from visits_day where von between date_sub(curdate(), interval 4 day) and date_sub(curdate(), interval 1 day)")["cnt"].to_i
|
164
164
|
end
|
165
165
|
|
@@ -457,6 +457,28 @@ class ReportmeTest < Test::Unit::TestCase
|
|
457
457
|
assert notifed
|
458
458
|
end
|
459
459
|
|
460
|
+
should "not notify subscribers" do
|
461
|
+
notifed = false
|
462
|
+
|
463
|
+
now = DateTime.now
|
464
|
+
_now = now.strftime("%Y-%m-%d 00:00:00")
|
465
|
+
_yesterday = (now - 1.day).strftime("%Y-%m-%d 00:00:00")
|
466
|
+
|
467
|
+
exec("insert into visits values (null, 'sem', '#{now}')");
|
468
|
+
rme = create_visit_report_factory(:periods => [:day])
|
469
|
+
rme.class.subscribe :visits do |period, von, report_name|
|
470
|
+
|
471
|
+
assert_equal :day, period
|
472
|
+
assert_equal :visits, report_name
|
473
|
+
assert_equal _yesterday, von.strftime("%Y-%m-%d 00:00:00")
|
474
|
+
|
475
|
+
notifed = true
|
476
|
+
end
|
477
|
+
rme.run(:notify_subscribers => false)
|
478
|
+
|
479
|
+
assert !notifed
|
480
|
+
end
|
481
|
+
|
460
482
|
should "call initializer before running reports" do
|
461
483
|
initialized = false
|
462
484
|
|
@@ -583,5 +605,5 @@ class ReportmeTest < Test::Unit::TestCase
|
|
583
605
|
|
584
606
|
assert "tmp_report1_day", ReportTemporaryTestReport.report_by_name(:report1).table_name(:day)
|
585
607
|
end
|
586
|
-
|
608
|
+
|
587
609
|
end
|