jzimmek-report_me 0.0.3 → 0.0.4
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/VERSION +1 -1
- data/lib/report_me/report_factory.rb +16 -7
- data/report_me.gemspec +1 -1
- data/test/report_me_test.rb +62 -16
- data/test/test_helper.rb +3 -2
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
@@ -56,11 +56,20 @@ module ReportMe
|
|
56
56
|
def report_exists?(name, von, bis)
|
57
57
|
ActiveRecord::Base.connection.select_value("select 1 from #{report_information_table_name} where report = '#{name}' and von = '#{von}' and bis = '#{bis}'") != nil
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
|
+
def reset
|
61
|
+
exec("drop table if exists #{report_information_table_name};")
|
62
|
+
|
63
|
+
periods.each do |period|
|
64
|
+
@reports.each do |r|
|
65
|
+
exec("drop table if exists #{r.table_name(period[:name])};")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
60
69
|
|
61
70
|
def run
|
62
71
|
|
63
|
-
debug =
|
72
|
+
debug = false
|
64
73
|
|
65
74
|
unless report_information_table_name_exist?
|
66
75
|
ddl = <<-SQL
|
@@ -75,12 +84,12 @@ module ReportMe
|
|
75
84
|
)
|
76
85
|
ENGINE=InnoDB default CHARSET=utf8;
|
77
86
|
SQL
|
78
|
-
|
87
|
+
exec(ddl)
|
79
88
|
end
|
80
89
|
|
81
90
|
if debug
|
82
91
|
# just for testing
|
83
|
-
|
92
|
+
exec("truncate #{report_information_table_name};")
|
84
93
|
end
|
85
94
|
|
86
95
|
periods.each do |period|
|
@@ -94,7 +103,7 @@ module ReportMe
|
|
94
103
|
|
95
104
|
if debug
|
96
105
|
# drop and create table while in testing mode
|
97
|
-
|
106
|
+
exec("drop table if exists #{table_name};")
|
98
107
|
end
|
99
108
|
|
100
109
|
table_exist = r.table_exist?(period[:name])
|
@@ -105,10 +114,10 @@ module ReportMe
|
|
105
114
|
|
106
115
|
|
107
116
|
unless table_exist
|
108
|
-
|
117
|
+
exec("create table #{table_name} ENGINE=InnoDB default CHARSET=utf8 as #{sql} limit 0;")
|
109
118
|
end
|
110
119
|
|
111
|
-
puts sql
|
120
|
+
# puts sql
|
112
121
|
|
113
122
|
if !report_exist || period[:name] == :today
|
114
123
|
ActiveRecord::Base.transaction do
|
data/report_me.gemspec
CHANGED
data/test/report_me_test.rb
CHANGED
@@ -3,45 +3,91 @@ require 'test_helper'
|
|
3
3
|
class ReportMeTest < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def setup
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
exec("use report_me_test")
|
7
|
+
exec "drop table if exists visits"
|
8
|
+
exec <<-SQL
|
9
9
|
create
|
10
|
-
table
|
10
|
+
table visits
|
11
11
|
(
|
12
12
|
id bigint auto_increment,
|
13
|
-
|
14
|
-
age bigint,
|
13
|
+
channel varchar(255),
|
15
14
|
created_at datetime,
|
16
15
|
primary key (id)
|
17
16
|
)
|
18
17
|
SQL
|
19
18
|
end
|
20
19
|
|
21
|
-
def
|
20
|
+
def create_visit_report_factory
|
22
21
|
|
23
|
-
ReportMe::ReportFactory.create do
|
24
|
-
report :
|
22
|
+
rme = ReportMe::ReportFactory.create do
|
23
|
+
report :visits do
|
25
24
|
source do |von, bis|
|
26
25
|
<<-SQL
|
27
26
|
select
|
28
|
-
date(
|
29
|
-
|
27
|
+
date('#{von}') as datum,
|
28
|
+
channel,
|
29
|
+
count(1) as cnt
|
30
30
|
from
|
31
|
-
|
31
|
+
visits
|
32
32
|
where
|
33
33
|
created_at between '#{von}' and '#{bis}'
|
34
34
|
group by
|
35
|
-
date(created_at)
|
35
|
+
date(created_at),
|
36
|
+
channel
|
36
37
|
SQL
|
37
38
|
end
|
38
39
|
end
|
39
40
|
end
|
40
|
-
|
41
|
-
|
41
|
+
|
42
|
+
rme.reset
|
43
|
+
rme
|
44
|
+
end
|
45
|
+
|
46
|
+
def exec(sql)
|
47
|
+
ActiveRecord::Base.connection.execute(sql)
|
48
|
+
end
|
49
|
+
|
50
|
+
def one(sql)
|
51
|
+
ActiveRecord::Base.connection.select_one(sql)
|
52
|
+
end
|
53
|
+
|
54
|
+
def teardown
|
55
|
+
exec("truncate visits;");
|
56
|
+
end
|
57
|
+
|
58
|
+
should "create one visitor in the today report for channel sem" do
|
59
|
+
exec("insert into visits values (null, 'sem', now())");
|
60
|
+
create_visit_report_factory.run
|
61
|
+
assert_equal 1, one("select count(1) as cnt from visits_today where channel = 'sem' and datum = curdate()")["cnt"].to_i
|
42
62
|
end
|
43
63
|
|
64
|
+
should "create two visitors in the today report for channel sem" do
|
65
|
+
exec("insert into visits values (null, 'sem', now())");
|
66
|
+
exec("insert into visits values (null, 'sem', now())");
|
67
|
+
create_visit_report_factory.run
|
68
|
+
assert_equal 2, one("select cnt from visits_today where channel = 'sem' and datum = curdate()")["cnt"].to_i
|
69
|
+
end
|
70
|
+
|
71
|
+
should "create visitors in the today report for channel sem and seo" do
|
72
|
+
exec("insert into visits values (null, 'sem', now())");
|
73
|
+
exec("insert into visits values (null, 'sem', now())");
|
74
|
+
exec("insert into visits values (null, 'seo', now())");
|
75
|
+
exec("insert into visits values (null, 'sem', now())");
|
76
|
+
exec("insert into visits values (null, 'seo', now())");
|
77
|
+
create_visit_report_factory.run
|
78
|
+
assert_equal 2, one("select cnt from visits_today where channel = 'seo' and datum = curdate()")["cnt"].to_i
|
79
|
+
assert_equal 3, one("select cnt from visits_today where channel = 'sem' and datum = curdate()")["cnt"].to_i
|
80
|
+
end
|
44
81
|
|
45
|
-
should "
|
82
|
+
should "create visitors in the day report for channel sem and seo" do
|
83
|
+
exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 1 day));");
|
84
|
+
exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 1 day));");
|
85
|
+
exec("insert into visits values (null, 'seo', date_sub(curdate(), interval 1 day));");
|
86
|
+
exec("insert into visits values (null, 'sem', date_sub(curdate(), interval 1 day));");
|
87
|
+
exec("insert into visits values (null, 'seo', date_sub(curdate(), interval 1 day));");
|
88
|
+
create_visit_report_factory.run
|
89
|
+
assert_equal 2, one("select cnt from visits_day where channel = 'seo' and datum = date_sub(curdate(), interval 1 day)")["cnt"].to_i
|
90
|
+
assert_equal 3, one("select cnt from visits_day where channel = 'sem' and datum = date_sub(curdate(), interval 1 day)")["cnt"].to_i
|
46
91
|
end
|
92
|
+
|
47
93
|
end
|
data/test/test_helper.rb
CHANGED
@@ -12,5 +12,6 @@ end
|
|
12
12
|
|
13
13
|
ActiveRecord::Base.establish_connection(:adapter => "mysql", :database => "mysql", :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"
|
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;"
|