jzimmek-report_me 0.0.2 → 0.0.3
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/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +29 -0
- data/Rakefile +50 -12
- data/VERSION +1 -0
- data/lib/report_me/report.rb +29 -0
- data/lib/report_me/report_factory.rb +142 -0
- data/lib/report_me.rb +1 -1
- data/report_me.gemspec +49 -0
- data/test/report_me_test.rb +47 -0
- data/test/test_helper.rb +10 -8
- metadata +23 -32
- data/History.txt +0 -8
- data/Manifest.txt +0 -10
- data/README +0 -1
- data/README.txt +0 -42
- data/lib/report_me/factory.rb +0 -54
- data/test/test_report_me.rb +0 -131
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Jan Zimmek
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
= report_me
|
2
|
+
|
3
|
+
ReportMe is a thin ruby wrapper around your reporting sql queries which empowers you to automate, historicize, graph and mail them in an easy manner.
|
4
|
+
|
5
|
+
== Usage
|
6
|
+
|
7
|
+
ReportFactory.create do
|
8
|
+
report :visits do
|
9
|
+
source do |von, bis|
|
10
|
+
<<-SQL
|
11
|
+
select
|
12
|
+
date(v.created_at) as datum,
|
13
|
+
channel,
|
14
|
+
count(1) as cnt
|
15
|
+
from
|
16
|
+
visits v
|
17
|
+
where
|
18
|
+
v.created_at between '#{von}' and '#{bis}'
|
19
|
+
group by
|
20
|
+
date(v.created_at),
|
21
|
+
channel
|
22
|
+
SQL
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
== Copyright
|
28
|
+
|
29
|
+
Copyright (c) 2009 Jan Zimmek. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,19 +1,57 @@
|
|
1
|
-
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
2
3
|
|
3
|
-
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "report_me"
|
8
|
+
gem.summary = "Ruby wrapper to automate sql reports"
|
9
|
+
gem.description = "ReportMe is a thin ruby wrapper around your reporting sql queries which empowers you to automate, historicize, graph and mail them in an easy manner."
|
10
|
+
gem.email = "jan.zimmek@web.de"
|
11
|
+
gem.homepage = "http://github.com/jzimmek/report_me"
|
12
|
+
gem.authors = ["Jan Zimmek"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
4
15
|
|
5
|
-
|
6
|
-
|
7
|
-
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/*_test.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
8
26
|
|
9
|
-
|
10
|
-
|
11
|
-
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
12
38
|
end
|
13
39
|
|
14
|
-
|
15
|
-
|
16
|
-
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
if File.exist?('VERSION.yml')
|
46
|
+
config = YAML.load(File.read('VERSION.yml'))
|
47
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
48
|
+
else
|
49
|
+
version = ""
|
50
|
+
end
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "report_me #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
56
|
end
|
18
57
|
|
19
|
-
# vim: syntax=Ruby
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.3
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'activerecord'
|
2
|
+
|
3
|
+
module ReportMe
|
4
|
+
class Report
|
5
|
+
|
6
|
+
attr_reader :name
|
7
|
+
|
8
|
+
def initialize(name)
|
9
|
+
@name = name
|
10
|
+
end
|
11
|
+
|
12
|
+
def source(&block)
|
13
|
+
@source = block
|
14
|
+
end
|
15
|
+
|
16
|
+
def sql(von, bis)
|
17
|
+
@source.call(von, bis)
|
18
|
+
end
|
19
|
+
|
20
|
+
def table_name(period)
|
21
|
+
"#{name}_#{period}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def table_exist?(period)
|
25
|
+
ActiveRecord::Base.connection.select_value("show tables like '#{table_name(period)}'") != nil
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'activerecord'
|
2
|
+
require 'report_me/report'
|
3
|
+
|
4
|
+
module ReportMe
|
5
|
+
class ReportFactory
|
6
|
+
|
7
|
+
def self.create(&block)
|
8
|
+
rme = ReportMe::ReportFactory.new
|
9
|
+
rme.instance_eval(&block)
|
10
|
+
rme.run
|
11
|
+
rme
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@reports = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def periods(today = DateTime.now)
|
19
|
+
|
20
|
+
periods = []
|
21
|
+
|
22
|
+
[:today, :day, :week, :calendar_week, :month, :calendar_month].each do |period|
|
23
|
+
|
24
|
+
von, bis = case period
|
25
|
+
when :today
|
26
|
+
[today, today]
|
27
|
+
when :day
|
28
|
+
[today - 1.day, today - 1.day]
|
29
|
+
when :week
|
30
|
+
[today - 1.day - 1.week, today - 1.day]
|
31
|
+
when :calendar_week
|
32
|
+
n = today - 1.day
|
33
|
+
[n - n.cwday + 1, n - n.cwday + 7]
|
34
|
+
when :month
|
35
|
+
[today - 1.day - 1.month, today - 1.day]
|
36
|
+
when :calendar_month
|
37
|
+
n = today - 1.month
|
38
|
+
[n.beginning_of_month, n.end_of_month]
|
39
|
+
end
|
40
|
+
|
41
|
+
periods << {:name => period, :von => von, :bis => bis}
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
periods
|
46
|
+
end
|
47
|
+
|
48
|
+
def report_information_table_name
|
49
|
+
"report_informations"
|
50
|
+
end
|
51
|
+
|
52
|
+
def report_information_table_name_exist?
|
53
|
+
ActiveRecord::Base.connection.select_value("show tables like '#{report_information_table_name}'") != nil
|
54
|
+
end
|
55
|
+
|
56
|
+
def report_exists?(name, von, bis)
|
57
|
+
ActiveRecord::Base.connection.select_value("select 1 from #{report_information_table_name} where report = '#{name}' and von = '#{von}' and bis = '#{bis}'") != nil
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def run
|
62
|
+
|
63
|
+
debug = true
|
64
|
+
|
65
|
+
unless report_information_table_name_exist?
|
66
|
+
ddl = <<-SQL
|
67
|
+
create
|
68
|
+
table report_informations
|
69
|
+
(
|
70
|
+
report varchar(255) not null,
|
71
|
+
von datetime not null,
|
72
|
+
bis datetime not null,
|
73
|
+
created_at datetime not null,
|
74
|
+
primary key (report)
|
75
|
+
)
|
76
|
+
ENGINE=InnoDB default CHARSET=utf8;
|
77
|
+
SQL
|
78
|
+
ActiveRecord::Base.connection.execute(ddl)
|
79
|
+
end
|
80
|
+
|
81
|
+
if debug
|
82
|
+
# just for testing
|
83
|
+
ActiveRecord::Base.connection.execute("truncate #{report_information_table_name};")
|
84
|
+
end
|
85
|
+
|
86
|
+
periods.each do |period|
|
87
|
+
|
88
|
+
@reports.each do |r|
|
89
|
+
|
90
|
+
von = period[:von].strftime("%Y-%m-%d 00:00:00")
|
91
|
+
bis = period[:bis].strftime("%Y-%m-%d 23:59:59")
|
92
|
+
|
93
|
+
table_name = r.table_name(period[:name])
|
94
|
+
|
95
|
+
if debug
|
96
|
+
# drop and create table while in testing mode
|
97
|
+
ActiveRecord::Base.connection.execute("drop table if exists #{table_name};")
|
98
|
+
end
|
99
|
+
|
100
|
+
table_exist = r.table_exist?(period[:name])
|
101
|
+
sql = r.sql(von, bis)
|
102
|
+
report_exist = report_exists?(table_name, von, bis)
|
103
|
+
|
104
|
+
puts "report: #{r.name}_#{period[:name]} :: #{report_exist}"
|
105
|
+
|
106
|
+
|
107
|
+
unless table_exist
|
108
|
+
ActiveRecord::Base.connection.execute("create table #{table_name} ENGINE=InnoDB default CHARSET=utf8 as #{sql} limit 0;")
|
109
|
+
end
|
110
|
+
|
111
|
+
puts sql
|
112
|
+
|
113
|
+
if !report_exist || period[:name] == :today
|
114
|
+
ActiveRecord::Base.transaction do
|
115
|
+
exec("insert into #{report_information_table_name} values ('#{table_name}', '#{von}', '#{bis}', now());") unless report_exist
|
116
|
+
|
117
|
+
if period[:name] == :today
|
118
|
+
exec("truncate #{table_name};")
|
119
|
+
end
|
120
|
+
|
121
|
+
exec("insert into #{table_name} #{sql};")
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def exec(sql)
|
131
|
+
ActiveRecord::Base.connection.execute(sql)
|
132
|
+
end
|
133
|
+
|
134
|
+
def report(name, &block)
|
135
|
+
r = ReportMe::Report.new(name)
|
136
|
+
r.instance_eval(&block)
|
137
|
+
|
138
|
+
@reports << r
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
end
|
data/lib/report_me.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require 'report_me/
|
1
|
+
require 'report_me/report_factory'
|
data/report_me.gemspec
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{report_me}
|
5
|
+
s.version = "0.0.3"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Jan Zimmek"]
|
9
|
+
s.date = %q{2009-06-23}
|
10
|
+
s.description = %q{ReportMe is a thin ruby wrapper around your reporting sql queries which empowers you to automate, historicize, graph and mail them in an easy manner.}
|
11
|
+
s.email = %q{jan.zimmek@web.de}
|
12
|
+
s.extra_rdoc_files = [
|
13
|
+
"LICENSE",
|
14
|
+
"README.rdoc"
|
15
|
+
]
|
16
|
+
s.files = [
|
17
|
+
".document",
|
18
|
+
".gitignore",
|
19
|
+
"LICENSE",
|
20
|
+
"README.rdoc",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"lib/report_me.rb",
|
24
|
+
"lib/report_me/report.rb",
|
25
|
+
"lib/report_me/report_factory.rb",
|
26
|
+
"report_me.gemspec",
|
27
|
+
"test/report_me_test.rb",
|
28
|
+
"test/test_helper.rb"
|
29
|
+
]
|
30
|
+
s.homepage = %q{http://github.com/jzimmek/report_me}
|
31
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = %q{1.3.4}
|
34
|
+
s.summary = %q{Ruby wrapper to automate sql reports}
|
35
|
+
s.test_files = [
|
36
|
+
"test/report_me_test.rb",
|
37
|
+
"test/test_helper.rb"
|
38
|
+
]
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
45
|
+
else
|
46
|
+
end
|
47
|
+
else
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ReportMeTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
|
7
|
+
ActiveRecord::Base.connection.execute "drop table if exists employees"
|
8
|
+
ActiveRecord::Base.connection.execute <<-SQL
|
9
|
+
create
|
10
|
+
table employees
|
11
|
+
(
|
12
|
+
id bigint auto_increment,
|
13
|
+
name varchar(255),
|
14
|
+
age bigint,
|
15
|
+
created_at datetime,
|
16
|
+
primary key (id)
|
17
|
+
)
|
18
|
+
SQL
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_employee_report_factory
|
22
|
+
|
23
|
+
ReportMe::ReportFactory.create do
|
24
|
+
report :employees do
|
25
|
+
source do |von, bis|
|
26
|
+
<<-SQL
|
27
|
+
select
|
28
|
+
date(created_at) as datum,
|
29
|
+
count(1) as anzahl
|
30
|
+
from
|
31
|
+
employees
|
32
|
+
where
|
33
|
+
created_at between '#{von}' and '#{bis}'
|
34
|
+
group by
|
35
|
+
date(created_at)
|
36
|
+
SQL
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
ActiveRecord::Base.connection.execute "truncate #{f.table_name(:number_of_employees)}"
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
should "probably rename this file and start testing for real" do
|
46
|
+
end
|
47
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
-
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
-
|
3
1
|
require 'rubygems'
|
4
|
-
require '
|
5
|
-
require '
|
6
|
-
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
7
4
|
require 'report_me'
|
8
5
|
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
8
|
+
|
9
|
+
|
10
|
+
class Test::Unit::TestCase
|
11
|
+
end
|
12
|
+
|
9
13
|
ActiveRecord::Base.establish_connection(:adapter => "mysql", :database => "mysql", :username => "root", :password => "root", :host => "localhost", :port => 3306)
|
10
14
|
|
11
15
|
ActiveRecord::Base.connection.execute "drop database if exists report_me_test"
|
12
|
-
ActiveRecord::Base.connection.execute "create database report_me_test"
|
13
|
-
|
14
|
-
require 'test/unit'
|
16
|
+
ActiveRecord::Base.connection.execute "create database report_me_test"
|
metadata
CHANGED
@@ -1,54 +1,45 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jzimmek-report_me
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Jan Zimmek
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-06-23 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
16
|
-
name: hoe
|
17
|
-
type: :development
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 1.12.2
|
24
|
-
version:
|
14
|
+
dependencies: []
|
15
|
+
|
25
16
|
description: ReportMe is a thin ruby wrapper around your reporting sql queries which empowers you to automate, historicize, graph and mail them in an easy manner.
|
26
|
-
email:
|
27
|
-
- jan.zimmek@web.de
|
17
|
+
email: jan.zimmek@web.de
|
28
18
|
executables: []
|
29
19
|
|
30
20
|
extensions: []
|
31
21
|
|
32
22
|
extra_rdoc_files:
|
33
|
-
-
|
34
|
-
-
|
35
|
-
- README.txt
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
36
25
|
files:
|
37
|
-
-
|
38
|
-
-
|
39
|
-
-
|
40
|
-
- README.
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
41
30
|
- Rakefile
|
31
|
+
- VERSION
|
42
32
|
- lib/report_me.rb
|
43
|
-
- lib/report_me/
|
33
|
+
- lib/report_me/report.rb
|
34
|
+
- lib/report_me/report_factory.rb
|
35
|
+
- report_me.gemspec
|
36
|
+
- test/report_me_test.rb
|
44
37
|
- test/test_helper.rb
|
45
|
-
|
46
|
-
|
47
|
-
homepage: http://github.com/jzimmek/report_me/tree/master
|
38
|
+
has_rdoc: false
|
39
|
+
homepage: http://github.com/jzimmek/report_me
|
48
40
|
post_install_message:
|
49
41
|
rdoc_options:
|
50
|
-
- --
|
51
|
-
- README.txt
|
42
|
+
- --charset=UTF-8
|
52
43
|
require_paths:
|
53
44
|
- lib
|
54
45
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -65,11 +56,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
56
|
version:
|
66
57
|
requirements: []
|
67
58
|
|
68
|
-
rubyforge_project:
|
59
|
+
rubyforge_project:
|
69
60
|
rubygems_version: 1.2.0
|
70
61
|
signing_key:
|
71
62
|
specification_version: 3
|
72
|
-
summary:
|
63
|
+
summary: Ruby wrapper to automate sql reports
|
73
64
|
test_files:
|
65
|
+
- test/report_me_test.rb
|
74
66
|
- test/test_helper.rb
|
75
|
-
- test/test_report_me.rb
|
data/History.txt
DELETED
data/Manifest.txt
DELETED
data/README
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
README
|
data/README.txt
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
= report_me
|
2
|
-
|
3
|
-
http://github.com/jzimmek/report_me/tree/master
|
4
|
-
|
5
|
-
== DESCRIPTION:
|
6
|
-
|
7
|
-
ReportMe is a thin ruby wrapper around your reporting sql queries which empowers you to automate, historicize, graph and mail them in an easy manner.
|
8
|
-
|
9
|
-
== FEATURES/PROBLEMS:
|
10
|
-
|
11
|
-
== SYNOPSIS:
|
12
|
-
|
13
|
-
== REQUIREMENTS:
|
14
|
-
|
15
|
-
== INSTALL:
|
16
|
-
|
17
|
-
* sudo gem install jzimmek-report_me
|
18
|
-
|
19
|
-
== LICENSE:
|
20
|
-
|
21
|
-
(The MIT License)
|
22
|
-
|
23
|
-
Copyright (c) 2009 Jan Zimmek
|
24
|
-
|
25
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
26
|
-
a copy of this software and associated documentation files (the
|
27
|
-
'Software'), to deal in the Software without restriction, including
|
28
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
29
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
30
|
-
permit persons to whom the Software is furnished to do so, subject to
|
31
|
-
the following conditions:
|
32
|
-
|
33
|
-
The above copyright notice and this permission notice shall be
|
34
|
-
included in all copies or substantial portions of the Software.
|
35
|
-
|
36
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
37
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
38
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
39
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
40
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
41
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
42
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/report_me/factory.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
module ReportMe
|
5
|
-
class Factory
|
6
|
-
|
7
|
-
VERSION = '0.0.2'
|
8
|
-
|
9
|
-
@@reports = {}
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
yield(self)
|
13
|
-
self
|
14
|
-
end
|
15
|
-
|
16
|
-
def create_report(name, &block)
|
17
|
-
|
18
|
-
puts "create_report #{name}"
|
19
|
-
|
20
|
-
@@reports[name] = block
|
21
|
-
|
22
|
-
# create the reporting table if not already exists
|
23
|
-
unless ActiveRecord::Base.connection.table_exists?(table_name(name))
|
24
|
-
sql = yield(" created_at between '1900-01-01' and timestampadd(second, -1, curdate()) ")
|
25
|
-
sql = "create table #{table_name(name)} as #{sql} limit 0"
|
26
|
-
exec(sql)
|
27
|
-
end
|
28
|
-
|
29
|
-
synchronize(name)
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
def table_name(name)
|
34
|
-
name = name.to_s.downcase.strip
|
35
|
-
table_name = "report_#{name}"
|
36
|
-
end
|
37
|
-
|
38
|
-
def synchronize(name)
|
39
|
-
|
40
|
-
block = @@reports[name]
|
41
|
-
|
42
|
-
# fill reporting table
|
43
|
-
sql = block.call(" created_at between (select ifnull(timestampadd(day, 1, max(datum)), '1900-01-01') from #{table_name(name)}) and timestampadd(second, -1, curdate()) ")
|
44
|
-
exec("insert into #{table_name(name)}\n #{sql}")
|
45
|
-
end
|
46
|
-
|
47
|
-
def exec(sql)
|
48
|
-
puts "exec: #{sql}"
|
49
|
-
ActiveRecord::Base.connection.execute(sql)
|
50
|
-
end
|
51
|
-
|
52
|
-
|
53
|
-
end
|
54
|
-
end
|
data/test/test_report_me.rb
DELETED
@@ -1,131 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class TestReportMe < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
def setup
|
6
|
-
|
7
|
-
ActiveRecord::Base.connection.execute "drop table if exists employees"
|
8
|
-
ActiveRecord::Base.connection.execute <<-SQL
|
9
|
-
create
|
10
|
-
table employees
|
11
|
-
(
|
12
|
-
id bigint auto_increment,
|
13
|
-
name varchar(255),
|
14
|
-
age bigint,
|
15
|
-
created_at datetime,
|
16
|
-
primary key (id)
|
17
|
-
)
|
18
|
-
SQL
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
def create_employee_report_factory
|
24
|
-
|
25
|
-
f = ReportMe::Factory.new do |f|
|
26
|
-
f.create_report :number_of_employees do |where_condition|
|
27
|
-
<<-SQL
|
28
|
-
select
|
29
|
-
date(created_at) as datum,
|
30
|
-
count(1) as anzahl
|
31
|
-
from
|
32
|
-
employees
|
33
|
-
where
|
34
|
-
#{where_condition}
|
35
|
-
group by
|
36
|
-
date(created_at)
|
37
|
-
SQL
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
ActiveRecord::Base.connection.execute "truncate #{f.table_name(:number_of_employees)}"
|
42
|
-
|
43
|
-
return f
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
test "creation of ddl without any data" do
|
48
|
-
f = create_employee_report_factory
|
49
|
-
assert_equal 0, ActiveRecord::Base.connection.select_one("select count(1) as cnt from #{f.table_name(:number_of_employees)}")["cnt"].to_i
|
50
|
-
end
|
51
|
-
|
52
|
-
test "should result in a count of 1 in the report table and with anzahl 1" do
|
53
|
-
f = create_employee_report_factory
|
54
|
-
|
55
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan', 28, '2009-01-01 00:00:00')"
|
56
|
-
f.synchronize(:number_of_employees)
|
57
|
-
|
58
|
-
assert_equal 1, ActiveRecord::Base.connection.select_one("select count(1) as cnt from #{f.table_name(:number_of_employees)} where datum = '2009-01-01'")["cnt"].to_i
|
59
|
-
assert_equal 1, ActiveRecord::Base.connection.select_one("select anzahl from #{f.table_name(:number_of_employees)} where datum = '2009-01-01'")["anzahl"].to_i
|
60
|
-
end
|
61
|
-
|
62
|
-
test "should result in a count of 2 in the report table but with anzahl 2" do
|
63
|
-
f = create_employee_report_factory
|
64
|
-
|
65
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan', 28, '2009-01-01 00:00:00')"
|
66
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan2', 28, '2009-01-01 00:00:00')"
|
67
|
-
f.synchronize(:number_of_employees)
|
68
|
-
|
69
|
-
assert_equal 1, ActiveRecord::Base.connection.select_one("select count(1) as cnt from #{f.table_name(:number_of_employees)} where datum = '2009-01-01'")["cnt"].to_i
|
70
|
-
assert_equal 2, ActiveRecord::Base.connection.select_one("select anzahl from #{f.table_name(:number_of_employees)} where datum = '2009-01-01'")["anzahl"].to_i
|
71
|
-
end
|
72
|
-
|
73
|
-
test "should be anzahl=1 for 2009-01-01 and anzahl=1 for 2009-01-02 " do
|
74
|
-
f = create_employee_report_factory
|
75
|
-
|
76
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan', 28, '2009-01-01 00:00:00')"
|
77
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan2', 28, '2009-01-02 00:00:00')"
|
78
|
-
f.synchronize(:number_of_employees)
|
79
|
-
|
80
|
-
assert_equal 1, ActiveRecord::Base.connection.select_one("select anzahl from #{f.table_name(:number_of_employees)} where datum = '2009-01-01'")["anzahl"].to_i
|
81
|
-
assert_equal 1, ActiveRecord::Base.connection.select_one("select anzahl from #{f.table_name(:number_of_employees)} where datum = '2009-01-02'")["anzahl"].to_i
|
82
|
-
end
|
83
|
-
|
84
|
-
test "should be anzahl=1 for 2009-01-01 and anzahl=2 for 2009-01-02 " do
|
85
|
-
f = create_employee_report_factory
|
86
|
-
|
87
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan', 28, '2009-01-01 00:00:00')"
|
88
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan2', 28, '2009-01-02 00:00:00')"
|
89
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan3', 28, '2009-01-02 00:00:00')"
|
90
|
-
f.synchronize(:number_of_employees)
|
91
|
-
|
92
|
-
assert_equal 1, ActiveRecord::Base.connection.select_one("select anzahl from #{f.table_name(:number_of_employees)} where datum = '2009-01-01'")["anzahl"].to_i
|
93
|
-
assert_equal 2, ActiveRecord::Base.connection.select_one("select anzahl from #{f.table_name(:number_of_employees)} where datum = '2009-01-02'")["anzahl"].to_i
|
94
|
-
end
|
95
|
-
|
96
|
-
test "should not update anzahl for already existing dates" do
|
97
|
-
f = create_employee_report_factory
|
98
|
-
|
99
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan', 28, '2009-01-01 00:00:00')"
|
100
|
-
f.synchronize(:number_of_employees)
|
101
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan2', 28, '2009-01-01 00:00:00')"
|
102
|
-
f.synchronize(:number_of_employees)
|
103
|
-
|
104
|
-
assert_equal 1, ActiveRecord::Base.connection.select_one("select anzahl from #{f.table_name(:number_of_employees)} where datum = '2009-01-01'")["anzahl"].to_i
|
105
|
-
end
|
106
|
-
|
107
|
-
test "should only process entries at least one day older than the latest entry in the report table" do
|
108
|
-
f = create_employee_report_factory
|
109
|
-
|
110
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan', 28, '2009-01-01 00:00:00')"
|
111
|
-
f.synchronize(:number_of_employees)
|
112
|
-
|
113
|
-
assert_equal 1, ActiveRecord::Base.connection.select_one("select anzahl from #{f.table_name(:number_of_employees)} where datum = '2009-01-01'")["anzahl"].to_i
|
114
|
-
|
115
|
-
# only 12 hours later but at the same day
|
116
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan2', 28, '2009-01-01 12:00:00')"
|
117
|
-
f.synchronize(:number_of_employees)
|
118
|
-
|
119
|
-
assert_equal 1, ActiveRecord::Base.connection.select_one("select anzahl from #{f.table_name(:number_of_employees)} where datum = '2009-01-01'")["anzahl"].to_i
|
120
|
-
|
121
|
-
# 24 hours later - next day
|
122
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan2', 28, '2009-01-02 00:00:00')"
|
123
|
-
ActiveRecord::Base.connection.execute "insert into employees values (null, 'jan3', 28, '2009-01-02 00:00:00')"
|
124
|
-
f.synchronize(:number_of_employees)
|
125
|
-
|
126
|
-
assert_equal 1, ActiveRecord::Base.connection.select_one("select anzahl from #{f.table_name(:number_of_employees)} where datum = '2009-01-01'")["anzahl"].to_i
|
127
|
-
assert_equal 2, ActiveRecord::Base.connection.select_one("select anzahl from #{f.table_name(:number_of_employees)} where datum = '2009-01-02'")["anzahl"].to_i
|
128
|
-
|
129
|
-
end
|
130
|
-
|
131
|
-
end
|