jzimmek-report_me 0.0.2

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/History.txt ADDED
@@ -0,0 +1,8 @@
1
+ === 0.0.2 / 2009-05-27
2
+
3
+ * distribute gem
4
+
5
+ === 0.0.1 / 2009-05-27
6
+
7
+ * initial developer release
8
+
data/Manifest.txt ADDED
@@ -0,0 +1,10 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README
4
+ README.txt
5
+ Rakefile
6
+ report_me.gemspec
7
+ lib/report_me.rb
8
+ lib/report_me/factory.rb
9
+ test/test_helper.rb
10
+ test/test_report_me.rb
data/README ADDED
@@ -0,0 +1 @@
1
+ README
data/README.txt ADDED
@@ -0,0 +1,42 @@
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/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # -*- ruby -*-
2
+
3
+ $LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
4
+
5
+ require 'rubygems'
6
+ require 'hoe'
7
+ require 'report_me'
8
+
9
+ Hoe.new('report_me', ReportMe::Factory::VERSION) do |p|
10
+ # p.rubyforge_name = 'report_mex' # if different than lowercase project name
11
+ p.developer('jzimmek', 'jan.zimmek@web.de')
12
+ end
13
+
14
+ task :cultivate do
15
+ system "touch Manifest.txt; rake check_manifest | grep -v .git | grep -v \"(in \" | patch"
16
+ system "rake debug_gem | grep -v \"(in \" > `basename \\`pwd\\``.gemspec"
17
+ end
18
+
19
+ # vim: syntax=Ruby
@@ -0,0 +1,54 @@
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/lib/report_me.rb ADDED
@@ -0,0 +1 @@
1
+ require 'report_me/factory'
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ require 'rubygems'
4
+ require 'active_support'
5
+ require 'active_support/test_case'
6
+
7
+ require 'report_me'
8
+
9
+ ActiveRecord::Base.establish_connection(:adapter => "mysql", :database => "mysql", :username => "root", :password => "root", :host => "localhost", :port => 3306)
10
+
11
+ 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'
@@ -0,0 +1,131 @@
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
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jzimmek-report_me
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - jzimmek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
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:
25
+ 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
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README
40
+ - README.txt
41
+ - Rakefile
42
+ - lib/report_me.rb
43
+ - lib/report_me/factory.rb
44
+ - test/test_helper.rb
45
+ - test/test_report_me.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/jzimmek/report_me/tree/master
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --main
51
+ - README.txt
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: report_me
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: 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.
73
+ test_files:
74
+ - test/test_helper.rb
75
+ - test/test_report_me.rb