jzimmek-reportme 0.0.8 → 0.0.9

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.8
1
+ 0.0.9
@@ -3,18 +3,21 @@ require 'reportme/report'
3
3
  module Reportme
4
4
  class ReportFactory
5
5
 
6
- def self.create(&block)
7
- rme = ReportFactory.new
6
+ def self.create(since=Date.today, &block)
7
+ rme = ReportFactory.new(since)
8
8
  rme.instance_eval(&block)
9
9
  rme.run
10
10
  rme
11
11
  end
12
12
 
13
- def initialize
13
+ def initialize(since)
14
+ raise "since cannot be in the future" if since.future?
15
+
14
16
  @reports = []
17
+ @since = since.to_date
15
18
  end
16
19
 
17
- def periods(today = DateTime.now)
20
+ def periods(today = Date.today)
18
21
 
19
22
  periods = []
20
23
 
@@ -79,7 +82,7 @@ module Reportme
79
82
  von datetime not null,
80
83
  bis datetime not null,
81
84
  created_at datetime not null,
82
- primary key (report)
85
+ primary key (report, von, bis)
83
86
  )
84
87
  ENGINE=InnoDB default CHARSET=utf8;
85
88
  SQL
@@ -90,52 +93,57 @@ module Reportme
90
93
  # just for testing
91
94
  exec("truncate #{report_information_table_name};")
92
95
  end
93
-
94
- periods.each do |period|
96
+
97
+ while !@since.future?
98
+
99
+ periods(@since).each do |period|
95
100
 
96
- @reports.each do |r|
101
+ @reports.each do |r|
97
102
 
98
- period_name = period[:name]
103
+ period_name = period[:name]
99
104
 
100
- next unless r.wants_period?(period_name)
105
+ next unless r.wants_period?(period_name)
101
106
 
102
- von = period[:von].strftime("%Y-%m-%d 00:00:00")
103
- bis = period[:bis].strftime("%Y-%m-%d 23:59:59")
107
+ von = period[:von].strftime("%Y-%m-%d 00:00:00")
108
+ bis = period[:bis].strftime("%Y-%m-%d 23:59:59")
104
109
 
105
- table_name = r.table_name(period_name)
110
+ table_name = r.table_name(period_name)
106
111
 
107
- if debug
108
- # drop and create table while in testing mode
109
- exec("drop table if exists #{table_name};")
110
- end
112
+ # if debug
113
+ # # drop and create table while in testing mode
114
+ # exec("drop table if exists #{table_name};")
115
+ # end
111
116
 
112
- table_exist = r.table_exist?(period_name)
113
- sql = r.sql(von, bis, period_name)
114
- report_exist = report_exists?(table_name, von, bis)
115
-
116
- puts "report: #{r.name}_#{period_name}"
117
-
117
+ table_exist = r.table_exist?(period_name)
118
+ sql = r.sql(von, bis, period_name)
119
+ report_exist = report_exists?(table_name, von, bis)
118
120
 
119
- unless table_exist
120
- exec("create table #{table_name} ENGINE=InnoDB default CHARSET=utf8 as #{sql} limit 0;")
121
- end
121
+ puts "report: #{r.table_name(period_name)} exist: #{table_exist}"
122
122
 
123
123
 
124
- if !report_exist || period_name == :today
125
- ActiveRecord::Base.transaction do
126
- exec("insert into #{report_information_table_name} values ('#{table_name}', '#{von}', '#{bis}', now());") unless report_exist
127
-
128
- if period_name == :today
129
- exec("truncate #{table_name};")
130
- end
124
+ unless table_exist
125
+ exec("create table #{table_name} ENGINE=InnoDB default CHARSET=utf8 as #{sql} limit 0;")
126
+ end
127
+
128
+ if !report_exist || period_name == :today
129
+ ActiveRecord::Base.transaction do
130
+ exec("insert into #{report_information_table_name} values ('#{table_name}', '#{von}', '#{bis}', now());") unless report_exist
131
131
 
132
- exec("insert into #{table_name} #{sql};")
132
+ if period_name == :today
133
+ exec("truncate #{table_name};")
134
+ end
135
+
136
+ exec("insert into #{table_name} #{sql};")
137
+ end
133
138
  end
134
- end
135
139
 
136
140
 
141
+ end
137
142
  end
143
+
144
+ @since += 1.day
138
145
  end
146
+
139
147
  end
140
148
 
141
149
  def exec(sql)
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class ReportMeTest < Test::Unit::TestCase
3
+ class ReportmeTest < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
6
  exec("use report_me_test")
@@ -19,7 +19,7 @@ class ReportMeTest < Test::Unit::TestCase
19
19
 
20
20
  def create_visit_report_factory
21
21
 
22
- rme = ReportMe::ReportFactory.create do
22
+ @rme = Reportme::ReportFactory.create do
23
23
  report :visits do
24
24
  source do |von, bis|
25
25
  <<-SQL
@@ -39,8 +39,7 @@ class ReportMeTest < Test::Unit::TestCase
39
39
  end
40
40
  end
41
41
 
42
- rme.reset
43
- rme
42
+ @rme
44
43
  end
45
44
 
46
45
  def exec(sql)
@@ -52,6 +51,7 @@ class ReportMeTest < Test::Unit::TestCase
52
51
  end
53
52
 
54
53
  def teardown
54
+ @rme.reset
55
55
  exec("truncate visits;");
56
56
  end
57
57
 
@@ -78,7 +78,7 @@ class ReportMeTest < Test::Unit::TestCase
78
78
  assert_equal 2, one("select cnt from visits_today where channel = 'seo' and datum = curdate()")["cnt"].to_i
79
79
  assert_equal 3, one("select cnt from visits_today where channel = 'sem' and datum = curdate()")["cnt"].to_i
80
80
  end
81
-
81
+
82
82
  should "create visitors in the day report for channel sem and seo" do
83
83
  exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 1 day));");
84
84
  exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 1 day));");
@@ -90,7 +90,7 @@ class ReportMeTest < Test::Unit::TestCase
90
90
  assert_equal 3, one("select cnt from visits_day where channel = 'sem' and datum = date_sub(curdate(), interval 1 day)")["cnt"].to_i
91
91
  end
92
92
 
93
- should "handle today and day periods but nothing else" do
94
- end
93
+ # should "handle today and day periods but nothing else" do
94
+ # end
95
95
 
96
96
  end
data/test/test_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'shoulda'
4
- require 'report_me'
4
+ require 'reportme'
5
5
 
6
6
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
7
  $LOAD_PATH.unshift(File.dirname(__FILE__))
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.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Zimmek
@@ -30,7 +30,7 @@ files:
30
30
  - lib/reportme.rb
31
31
  - lib/reportme/report.rb
32
32
  - lib/reportme/report_factory.rb
33
- - test/report_me_test.rb
33
+ - test/reportme_test.rb
34
34
  - test/test_helper.rb
35
35
  has_rdoc: false
36
36
  homepage: http://github.com/jzimmek/reportme/tree/master
@@ -59,5 +59,5 @@ signing_key:
59
59
  specification_version: 3
60
60
  summary: Ruby wrapper to automate sql reports
61
61
  test_files:
62
- - test/report_me_test.rb
62
+ - test/reportme_test.rb
63
63
  - test/test_helper.rb