jzimmek-reportme 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.11
1
+ 0.0.12
@@ -6,6 +6,7 @@ module Reportme
6
6
  def self.create(since=Date.today, &block)
7
7
  rme = ReportFactory.new(since)
8
8
  rme.instance_eval(&block)
9
+ rme.connect
9
10
  rme.run
10
11
  rme
11
12
  end
@@ -16,10 +17,19 @@ module Reportme
16
17
  @reports = []
17
18
  @since = since.to_date
18
19
  end
20
+
21
+ def connect
22
+ puts "connection: #{@properties}"
23
+ ActiveRecord::Base.establish_connection(@properties)
24
+ end
25
+
26
+ def connection(properties)
27
+ @properties = properties
28
+ end
19
29
 
20
- def periods(today = Date.today)
30
+ def self.periods(today = Date.today)
21
31
 
22
- periods = []
32
+ r = []
23
33
 
24
34
  [:today, :day, :week, :calendar_week, :month, :calendar_month].each do |period|
25
35
 
@@ -29,24 +39,26 @@ module Reportme
29
39
  when :day
30
40
  [today - 1.day, today - 1.day]
31
41
  when :week
32
- # [today - 1.day - 1.week, today - 1.day]
33
42
  [today - 1.week, today - 1.day]
34
43
  when :calendar_week
35
- n = today - 1.day
36
- [n - n.cwday + 1, n - n.cwday + 7]
44
+ day_lastweek = today.to_date - 7.days
45
+ monday = day_lastweek - (day_lastweek.cwday - 1).days
46
+ [monday, monday + 6.days]
37
47
  when :month
38
- # [today - 1.day - 1.month, today - 1.day]
39
- [today - 1.month, today - 1.day]
48
+ [today - 1.day - 30.days, today - 1.day]
40
49
  when :calendar_month
41
50
  n = today - 1.month
42
51
  [n.beginning_of_month, n.end_of_month]
43
52
  end
44
53
 
45
- periods << {:name => period, :von => von, :bis => bis}
54
+ von = von.to_datetime
55
+ bis = bis.to_datetime + 23.hours + 59.minutes + 59.seconds
56
+
57
+ r << {:name => period, :von => von, :bis => bis}
46
58
 
47
59
  end
48
60
 
49
- periods
61
+ r
50
62
  end
51
63
 
52
64
  def report_information_table_name
@@ -61,10 +73,30 @@ module Reportme
61
73
  select_value("select 1 from #{report_information_table_name} where report = '#{name}' and von = '#{von}' and bis = '#{bis}'") != nil
62
74
  end
63
75
 
76
+ def schema_name
77
+ schema = @properties[:database]
78
+ raise "missing :database in connection properties" unless schema
79
+ schema
80
+ end
81
+
82
+ def columns(table_name)
83
+ sql = <<-SQL
84
+ select
85
+ column_name
86
+ from
87
+ information_schema.columns
88
+ where
89
+ table_schema = '#{schema_name}'
90
+ and table_name = '#{table_name}'
91
+ ;
92
+ SQL
93
+ select_values(sql)
94
+ end
95
+
64
96
  def reset
65
97
  exec("drop table if exists #{report_information_table_name};")
66
98
 
67
- periods.each do |period|
99
+ ReportFactory.periods.each do |period|
68
100
  @reports.each do |r|
69
101
  exec("drop table if exists #{r.table_name(period[:name])};")
70
102
  end
@@ -102,12 +134,11 @@ module Reportme
102
134
  end
103
135
  end
104
136
 
105
- def try_weekly_report_by_daily_reports(report, _von, _bis)
106
-
107
- table_name = report.table_name(:week)
137
+ def try_report_by_daily_reports(report, period_name, _von, num_days, num_days_required)
138
+ table_name = report.table_name(period_name)
108
139
 
109
140
  von = _von.strftime("%Y-%m-%d 00:00:00")
110
- bis = _bis.strftime("%Y-%m-%d 23:59:59")
141
+ bis = (_von + num_days.days).strftime("%Y-%m-%d 23:59:59")
111
142
 
112
143
  existing_daily_reports = select_value(<<-SQL
113
144
  select
@@ -116,27 +147,31 @@ module Reportme
116
147
  #{report_information_table_name}
117
148
  where
118
149
  report = '#{report.table_name(:day)}'
119
- and von between '#{von}' and '#{(_von + 6.days).strftime("%Y-%m-%d 23:59:59")}'
150
+ and von between '#{von}' and '#{(_von + num_days.days).strftime("%Y-%m-%d 00:00:00")}'
120
151
  SQL
121
152
  ).to_i
122
153
 
123
- puts "weekly report depends on 7 daily reports ... #{existing_daily_reports} daily found"
154
+ puts "#{period_name}ly report depends on #{num_days_required} daily reports ... #{existing_daily_reports} daily found"
124
155
 
125
- if existing_daily_reports == 7
156
+ if existing_daily_reports == num_days_required
157
+
158
+ column_names = ["'#{von}' as von"]
159
+ column_names += columns(table_name).find_all{|c|c != "von"}.collect{|c|"d.#{c} as #{c}"}
160
+
126
161
  ActiveRecord::Base.transaction do
127
162
  exec("insert into #{report_information_table_name} values ('#{table_name}', '#{von}', '#{bis}', now());")
128
- exec("insert into #{table_name} select * from #{report.table_name(:day)} where von between '#{von}' and '#{(_von + 6.days).strftime("%Y-%m-%d 00:00:00")}';")
163
+ exec("insert into #{table_name} select #{column_names.join(',')} from #{report.table_name(:day)} as d where d.von between '#{von}' and '#{(_von + num_days.days).strftime("%Y-%m-%d 00:00:00")}';")
129
164
  end
130
165
  end
131
166
  end
132
-
167
+
133
168
  def run
134
169
 
135
170
  ensure_report_informations_table_exist
136
171
 
137
172
  while !@since.future?
138
173
 
139
- periods(@since).each do |period|
174
+ ReportFactory.periods(@since).each do |period|
140
175
 
141
176
  @reports.each do |r|
142
177
 
@@ -155,13 +190,19 @@ module Reportme
155
190
  table_exist = r.table_exist?(period_name)
156
191
  sql = r.sql(von, bis, period_name)
157
192
 
158
- puts "report: #{r.table_name(period_name)} exist: #{table_exist}"
193
+ puts "report: #{r.table_name(period_name)} von: #{von}, bis: #{bis}"
159
194
 
160
195
  ensure_report_table_exist(r, period_name)
161
196
 
162
197
  report_exists = report_exists?(table_name, von, bis)
163
198
 
164
- try_weekly_report_by_daily_reports(r, _von, _bis) if period_name == :week && !report_exists
199
+ try_report_by_daily_reports(r, :week, _von, 6, 7) if period_name == :week && !report_exists
200
+ try_report_by_daily_reports(r, :calendar_week, _von, 6, 7) if period_name == :calendar_week && !report_exists
201
+
202
+ # TODO: implement monat by daily reports
203
+ # try_report_by_daily_reports(r, :month, _von, 29 + (_von.end_of_month.day == 31 ? 1 : 0), 30) if period_name == :month && !report_exists
204
+
205
+ try_report_by_daily_reports(r, :calendar_month, _von, _bis.day - _von.day, _bis.day) if period_name == :calendar_month && !report_exists
165
206
 
166
207
  report_exists = report_exists?(table_name, von, bis)
167
208
 
@@ -199,6 +240,13 @@ module Reportme
199
240
  puts "------------------------ //"
200
241
  ActiveRecord::Base.connection.select_value(sql)
201
242
  end
243
+
244
+ def select_values(sql)
245
+ puts "// ------------------------"
246
+ puts "select_values: #{sql}"
247
+ puts "------------------------ //"
248
+ ActiveRecord::Base.connection.select_values(sql)
249
+ end
202
250
 
203
251
  def report(name, &block)
204
252
 
@@ -27,6 +27,9 @@ class ReportmeTest < Test::Unit::TestCase
27
27
  opts = defaults.merge(opts)
28
28
 
29
29
  @rme = Reportme::ReportFactory.create opts[:since] do
30
+
31
+ connection :adapter => "mysql", :database => "report_me_test", :username => "root", :password => "root", :host => "localhost", :port => 3306
32
+
30
33
  report :visits do
31
34
  periods opts[:periods]
32
35
  source do |von, bis|
@@ -64,7 +67,7 @@ class ReportmeTest < Test::Unit::TestCase
64
67
  exec("truncate visits;");
65
68
  end
66
69
  end
67
-
70
+
68
71
  should "create one visitor in the today report for channel sem" do
69
72
  exec("insert into visits values (null, 'sem', now())");
70
73
  create_visit_report_factory.run
@@ -154,17 +157,201 @@ class ReportmeTest < Test::Unit::TestCase
154
157
  exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 8 day));");
155
158
  # should be ignored in weekly
156
159
  exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 9 day));");
157
-
158
- create_visit_report_factory(:since => 10.days.ago,:periods => [:day]).run
160
+
161
+ create_visit_report_factory(:since => 10.days.ago, :periods => [:day]).run
159
162
 
160
163
  exec("truncate visits;")
161
-
164
+
162
165
  create_visit_report_factory(:periods => [:week]).run
163
-
164
- assert_equal 7, one("select count(1) as cnt from visits_week where von between date_sub(curdate(), interval 7 day) and date_sub(curdate(), interval 1 day)")["cnt"].to_i
166
+
167
+ assert_equal 7, one("select count(1) as cnt from visits_week where date(von) between date_sub(curdate(), interval 7 day) and date_sub(curdate(), interval 1 day)")["cnt"].to_i
165
168
  end
166
169
 
167
- # should "handle today and day periods but nothing else" do
168
- # end
170
+ should "generate the von/bis range for the periods" do
171
+
172
+ ##
173
+ # anfang monat - 30 tage
174
+ ##
175
+
176
+ periods = {}
177
+ Reportme::ReportFactory.periods('2009-06-01'.to_date).each{|p| periods[p[:name]] = p}
178
+
179
+ assert_equal '2009-06-01 00:00:00'.to_datetime, periods[:today][:von]
180
+ assert_equal '2009-06-01 23:59:59'.to_datetime, periods[:today][:bis]
181
+
182
+ assert_equal '2009-05-31 00:00:00'.to_datetime, periods[:day][:von]
183
+ assert_equal '2009-05-31 23:59:59'.to_datetime, periods[:day][:bis]
184
+
185
+ assert_equal '2009-05-25 00:00:00'.to_datetime, periods[:week][:von]
186
+ assert_equal '2009-05-31 23:59:59'.to_datetime, periods[:week][:bis]
187
+
188
+ assert_equal '2009-05-25 00:00:00'.to_datetime, periods[:calendar_week][:von]
189
+ assert_equal '2009-05-31 23:59:59'.to_datetime, periods[:calendar_week][:bis]
190
+
191
+ assert_equal '2009-05-01 00:00:00'.to_datetime, periods[:month][:von]
192
+ assert_equal '2009-05-31 23:59:59'.to_datetime, periods[:month][:bis]
193
+
194
+ assert_equal '2009-05-01 00:00:00'.to_datetime, periods[:calendar_month][:von]
195
+ assert_equal '2009-05-31 23:59:59'.to_datetime, periods[:calendar_month][:bis]
196
+
197
+
198
+ ##
199
+ # mitten monat - 30 tage
200
+ ##
201
+
202
+ periods.clear
203
+ Reportme::ReportFactory.periods('2009-06-24'.to_date).each{|p| periods[p[:name]] = p}
204
+
205
+ assert_equal '2009-06-24 00:00:00'.to_datetime, periods[:today][:von]
206
+ assert_equal '2009-06-24 23:59:59'.to_datetime, periods[:today][:bis]
207
+
208
+ assert_equal '2009-06-23 00:00:00'.to_datetime, periods[:day][:von]
209
+ assert_equal '2009-06-23 23:59:59'.to_datetime, periods[:day][:bis]
210
+
211
+ assert_equal '2009-06-17 00:00:00'.to_datetime, periods[:week][:von]
212
+ assert_equal '2009-06-23 23:59:59'.to_datetime, periods[:week][:bis]
213
+
214
+ assert_equal '2009-06-15 00:00:00'.to_datetime, periods[:calendar_week][:von]
215
+ assert_equal '2009-06-21 23:59:59'.to_datetime, periods[:calendar_week][:bis]
216
+
217
+ assert_equal '2009-05-24 00:00:00'.to_datetime, periods[:month][:von]
218
+ assert_equal '2009-06-23 23:59:59'.to_datetime, periods[:month][:bis]
169
219
 
220
+ assert_equal '2009-05-01 00:00:00'.to_datetime, periods[:calendar_month][:von]
221
+ assert_equal '2009-05-31 23:59:59'.to_datetime, periods[:calendar_month][:bis]
222
+
223
+ ##
224
+ # ende monat - 30 tage
225
+ ##
226
+
227
+ periods.clear
228
+ Reportme::ReportFactory.periods('2009-06-30'.to_date).each{|p| periods[p[:name]] = p}
229
+
230
+ assert_equal '2009-06-30 00:00:00'.to_datetime, periods[:today][:von]
231
+ assert_equal '2009-06-30 23:59:59'.to_datetime, periods[:today][:bis]
232
+
233
+ assert_equal '2009-06-29 00:00:00'.to_datetime, periods[:day][:von]
234
+ assert_equal '2009-06-29 23:59:59'.to_datetime, periods[:day][:bis]
235
+
236
+ assert_equal '2009-06-23 00:00:00'.to_datetime, periods[:week][:von]
237
+ assert_equal '2009-06-29 23:59:59'.to_datetime, periods[:week][:bis]
238
+
239
+ assert_equal '2009-06-22 00:00:00'.to_datetime, periods[:calendar_week][:von]
240
+ assert_equal '2009-06-28 23:59:59'.to_datetime, periods[:calendar_week][:bis]
241
+
242
+ assert_equal '2009-05-30 00:00:00'.to_datetime, periods[:month][:von]
243
+ assert_equal '2009-06-29 23:59:59'.to_datetime, periods[:month][:bis]
244
+
245
+ assert_equal '2009-05-01 00:00:00'.to_datetime, periods[:calendar_month][:von]
246
+ assert_equal '2009-05-31 23:59:59'.to_datetime, periods[:calendar_month][:bis]
247
+
248
+ ##
249
+ # anfang monat - 31 tage
250
+ ##
251
+
252
+ periods.clear
253
+ Reportme::ReportFactory.periods('2009-05-01'.to_date).each{|p| periods[p[:name]] = p}
254
+
255
+ assert_equal '2009-05-01 00:00:00'.to_datetime, periods[:today][:von]
256
+ assert_equal '2009-05-01 23:59:59'.to_datetime, periods[:today][:bis]
257
+
258
+ assert_equal '2009-04-30 00:00:00'.to_datetime, periods[:day][:von]
259
+ assert_equal '2009-04-30 23:59:59'.to_datetime, periods[:day][:bis]
260
+
261
+ assert_equal '2009-04-24 00:00:00'.to_datetime, periods[:week][:von]
262
+ assert_equal '2009-04-30 23:59:59'.to_datetime, periods[:week][:bis]
263
+
264
+ assert_equal '2009-04-20 00:00:00'.to_datetime, periods[:calendar_week][:von]
265
+ assert_equal '2009-04-26 23:59:59'.to_datetime, periods[:calendar_week][:bis]
266
+
267
+ assert_equal '2009-03-31 00:00:00'.to_datetime, periods[:month][:von]
268
+ assert_equal '2009-04-30 23:59:59'.to_datetime, periods[:month][:bis]
269
+
270
+ assert_equal '2009-04-01 00:00:00'.to_datetime, periods[:calendar_month][:von]
271
+ assert_equal '2009-04-30 23:59:59'.to_datetime, periods[:calendar_month][:bis]
272
+
273
+ ##
274
+ # mitte monat - 31 tage
275
+ ##
276
+
277
+ periods.clear
278
+ Reportme::ReportFactory.periods('2009-05-15'.to_date).each{|p| periods[p[:name]] = p}
279
+
280
+ assert_equal '2009-05-15 00:00:00'.to_datetime, periods[:today][:von]
281
+ assert_equal '2009-05-15 23:59:59'.to_datetime, periods[:today][:bis]
282
+
283
+ assert_equal '2009-05-14 00:00:00'.to_datetime, periods[:day][:von]
284
+ assert_equal '2009-05-14 23:59:59'.to_datetime, periods[:day][:bis]
285
+
286
+ assert_equal '2009-05-08 00:00:00'.to_datetime, periods[:week][:von]
287
+ assert_equal '2009-05-14 23:59:59'.to_datetime, periods[:week][:bis]
288
+
289
+ assert_equal '2009-05-04 00:00:00'.to_datetime, periods[:calendar_week][:von]
290
+ assert_equal '2009-05-10 23:59:59'.to_datetime, periods[:calendar_week][:bis]
291
+
292
+ assert_equal '2009-04-14 00:00:00'.to_datetime, periods[:month][:von]
293
+ assert_equal '2009-05-14 23:59:59'.to_datetime, periods[:month][:bis]
294
+
295
+ assert_equal '2009-04-01 00:00:00'.to_datetime, periods[:calendar_month][:von]
296
+ assert_equal '2009-04-30 23:59:59'.to_datetime, periods[:calendar_month][:bis]
297
+
298
+ ##
299
+ # ende monat - 31 tage
300
+ ##
301
+
302
+ periods.clear
303
+ Reportme::ReportFactory.periods('2009-05-31'.to_date).each{|p| periods[p[:name]] = p}
304
+
305
+ assert_equal '2009-05-31 00:00:00'.to_datetime, periods[:today][:von]
306
+ assert_equal '2009-05-31 23:59:59'.to_datetime, periods[:today][:bis]
307
+
308
+ assert_equal '2009-05-30 00:00:00'.to_datetime, periods[:day][:von]
309
+ assert_equal '2009-05-30 23:59:59'.to_datetime, periods[:day][:bis]
310
+
311
+ assert_equal '2009-05-24 00:00:00'.to_datetime, periods[:week][:von]
312
+ assert_equal '2009-05-30 23:59:59'.to_datetime, periods[:week][:bis]
313
+
314
+ assert_equal '2009-05-18 00:00:00'.to_datetime, periods[:calendar_week][:von]
315
+ assert_equal '2009-05-24 23:59:59'.to_datetime, periods[:calendar_week][:bis]
316
+
317
+ assert_equal '2009-04-30 00:00:00'.to_datetime, periods[:month][:von]
318
+ assert_equal '2009-05-30 23:59:59'.to_datetime, periods[:month][:bis]
319
+
320
+ assert_equal '2009-04-01 00:00:00'.to_datetime, periods[:calendar_month][:von]
321
+ assert_equal '2009-04-30 23:59:59'.to_datetime, periods[:calendar_month][:bis]
322
+
323
+
324
+ end
325
+
326
+ should "create the calendar_weekly report by using 7 daily reports" do
327
+
328
+ today = '2009-06-24'
329
+
330
+ # should be ignored in weekly
331
+ exec("insert into visits values (null, 'sem', '#{today}');");
332
+ # should be ignored in weekly
333
+ exec("insert into visits values (null, 'sem', date_sub('#{today}', interval 1 day));");
334
+ # should be ignored in weekly
335
+ exec("insert into visits values (null, 'sem', date_sub('#{today}', interval 2 day));");
336
+ exec("insert into visits values (null, 'sem', date_sub('#{today}', interval 3 day));");
337
+ exec("insert into visits values (null, 'sem', date_sub('#{today}', interval 4 day));");
338
+ exec("insert into visits values (null, 'sem', date_sub('#{today}', interval 5 day));");
339
+ exec("insert into visits values (null, 'sem', date_sub('#{today}', interval 6 day));");
340
+ exec("insert into visits values (null, 'sem', date_sub('#{today}', interval 7 day));");
341
+ exec("insert into visits values (null, 'sem', date_sub('#{today}', interval 8 day));");
342
+ exec("insert into visits values (null, 'sem', date_sub('#{today}', interval 9 day));");
343
+ # should be ignored in weekly
344
+ exec("insert into visits values (null, 'sem', date_sub('#{today}', interval 10 day));");
345
+
346
+ create_visit_report_factory(:since => 15.days.ago, :periods => [:day]).run
347
+
348
+ exec("truncate visits;")
349
+
350
+ create_visit_report_factory(:periods => [:calendar_week]).run
351
+
352
+ assert_equal 7, one("select count(1) as cnt from visits_calendar_week where von between '2009-06-15 00:00:00' and '2009-06-21 00:00:00'")["cnt"].to_i
353
+
354
+ end
355
+
356
+
170
357
  end
data/test/test_helper.rb CHANGED
@@ -10,8 +10,8 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
10
  class Test::Unit::TestCase
11
11
  end
12
12
 
13
- ActiveRecord::Base.establish_connection(:adapter => "mysql", :database => "mysql", :username => "root", :password => "root", :host => "localhost", :port => 3306)
13
+ ActiveRecord::Base.establish_connection(:adapter => "mysql", :database => "report_me_test", :username => "root", :password => "root", :host => "localhost", :port => 3306)
14
14
 
15
- ActiveRecord::Base.connection.execute "drop database if exists report_me_test;"
16
- ActiveRecord::Base.connection.execute "create database report_me_test;"
17
- ActiveRecord::Base.connection.execute "use report_me_test;"
15
+ # ActiveRecord::Base.connection.execute "drop database if exists report_me_test;"
16
+ # ActiveRecord::Base.connection.execute "create database report_me_test;"
17
+ # ActiveRecord::Base.connection.execute "use report_me_test;"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jzimmek-reportme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Zimmek