added specs for Grouping.to_sql - reportable - Unnamed repository; edit this fi… | |
Log | |
Files | |
Refs | |
README | |
--- | |
commit 7f02d5870811625ca57691a29a3c9f749203f550 | |
parent 627ddcc78052dbd58ea36f1d405deff5c6f8801d | |
Author: marcoow <[email protected]> | |
Date: Mon, 8 Dec 2008 22:48:15 +0800 | |
added specs for Grouping.to_sql | |
Signed-off-by: Marco Otte-Witte <[email protected]> | |
Diffstat: | |
M lib/kvlr/reports_as_sparkline/grou… | 26 ++++++++++++-------------- | |
M lib/kvlr/reports_as_sparkline/repo… | 23 ++++++++++++++++------- | |
M spec/other/grouping_spec.rb | 80 +++++++++++++++++++++++++++++… | |
M spec/other/report_cache_spec.rb | 3 +++ | |
4 files changed, 111 insertions(+), 21 deletions(-) | |
--- | |
diff --git a/lib/kvlr/reports_as_sparkline/grouping.rb b/lib/kvlr/reports_as_sp… | |
@@ -4,10 +4,10 @@ module Kvlr #:nodoc: | |
class Grouping | |
- @@allowed_groupings = [:month, :week, :day, :hour] | |
+ @@allowed_groupings = | |
def initialize(grouping) | |
- raise ArgumentError.new("Argument grouping must be one of #{@@allowed_… | |
+ raise ArgumentError.new("Invalid grouping #{grouping}") unless [:hour,… | |
@identifier = grouping | |
end | |
@@ -17,6 +17,8 @@ module Kvlr #:nodoc: | |
def to_reporting_period(date_time) | |
return case @identifier | |
+ when :hour | |
+ DateTime.new(date_time.year, date_time.month, date_time.day, date_… | |
when :day | |
date_time.to_date | |
when :week | |
@@ -24,13 +26,13 @@ module Kvlr #:nodoc: | |
Date.new(date_time.year, date_time.month, date_time.day) | |
when :month | |
Date.new(date_time.year, date_time.month) | |
- when :hour | |
- DateTime.new(date_time.year, date_time.month, date_time.day, date_… | |
end | |
end | |
def first_reporting_period(limit) | |
return case @identifier | |
+ when :hour | |
+ to_reporting_period(Time.now - limit.hours) | |
when :day | |
to_reporting_period(Time.now - limit.days) | |
when :week | |
@@ -38,14 +40,10 @@ module Kvlr #:nodoc: | |
when :month | |
date = Time.now - limit.months | |
Date.new(date.year, date.month, 1) | |
- when :hour | |
- to_reporting_period(Time.now - limit.hours) | |
end | |
end | |
def to_sql(date_column_name) | |
- #TODO: DATE_FORMAT's format string is different on different RDBMs => … | |
- # => this can be implemented using ActiveRecord::Base.connection.class | |
return case ActiveRecord::Base.connection.class.to_s | |
when 'ActiveRecord::ConnectionAdapters::MysqlAdapter' | |
mysql_format(date_column_name) | |
@@ -60,40 +58,40 @@ module Kvlr #:nodoc: | |
def mysql_format(date_column_name) | |
return case @identifier | |
+ when :hour | |
+ "DATE_FORMAT(#{date_column_name}, '%Y/%m/%d/%H')" | |
when :day | |
"DATE_FORMAT(#{date_column_name}, '%Y/%m/%d')" | |
when :week | |
"DATE_FORMAT(#{date_column_name}, '%Y-%u')" | |
when :month | |
"DATE_FORMAT(#{date_column_name}, '%Y/%m')" | |
- when :hour | |
- "DATE_FORMAT(#{date_column_name}, '%Y/%m/%d/%H')" | |
end | |
end | |
def sqlite_format(date_column_name) | |
return case @identifier | |
+ when :hour | |
+ "strftime('%Y/%m/%d/%H', #{date_column_name})" | |
when :day | |
"strftime('%Y/%m/%d', #{date_column_name})" | |
when :week | |
"strftime('%Y-%W', #{date_column_name})" | |
when :month | |
"strftime('%Y/%m', #{date_column_name})" | |
- when :hour | |
- "strftime('%Y/%m/%d/%H', #{date_column_name})" | |
end | |
end | |
def postgresql_format(date_column_name) | |
return case @identifier | |
+ when :hour | |
+ "date_trunc('hour', #{date_column_name})" | |
when :day | |
"date_trunc('day', #{date_column_name})" | |
when :week | |
"date_trunc('week', #{date_column_name})" | |
when :month | |
"date_trunc('month', #{date_column_name})" | |
- when :hour | |
- "date_trunc('hour', #{date_column_name})" | |
end | |
end | |
diff --git a/lib/kvlr/reports_as_sparkline/report_cache.rb b/lib/kvlr/reports_a… | |
@@ -10,11 +10,20 @@ module Kvlr #:nodoc: | |
self.transaction do | |
cached_data = self.find( | |
:all, | |
- :conditions => { :model_name => report.klass.to_s, :report_name =>… | |
+ :conditions => { | |
+ :model_name => report.klass.to_s, | |
+ :report_name => report.name.to_s, | |
+ :grouping => report.grouping.identifier.to_s, | |
+ :aggregation => report.aggregation.to_s | |
+ }, | |
:limit => limit, | |
:order => 'reporting_period DESC' | |
) | |
- last_reporting_period_to_read = cached_data.empty? ? report.grouping… | |
+ last_reporting_period_to_read = if cached_data.empty? | |
+ report.grouping.first_reporting_period(limit) | |
+ else | |
+ report.grouping.to_reporting_period(cached_data.last.reporting_p… | |
+ end | |
new_data = yield(last_reporting_period_to_read) | |
return update_cache(new_data, cached_data, report) | |
end | |
@@ -30,12 +39,12 @@ module Kvlr #:nodoc: | |
end | |
for row in (new_data[rows_to_write] || []) | |
self.create!( | |
- :model_name => report.klass.to_s, | |
- :report_name => report.name.to_s, | |
- :grouping => report.grouping.identifier.to_s, | |
- :aggregation => report.aggregation.to_s, | |
+ :model_name => report.klass.to_s, | |
+ :report_name => report.name.to_s, | |
+ :grouping => report.grouping.identifier.to_s, | |
+ :aggregation => report.aggregation.to_s, | |
:reporting_period => report.grouping.to_reporting_period(DateTim… | |
- :value => row[1] | |
+ :value => row[1] | |
) | |
end | |
result = cached_data.map { |cached| [cached.reporting_period, cached… | |
diff --git a/spec/other/grouping_spec.rb b/spec/other/grouping_spec.rb | |
@@ -51,4 +51,84 @@ describe Kvlr::ReportsAsSparkline::Grouping do | |
end | |
+ describe '.to_sql' do | |
+ | |
+ describe 'for MySQL' do | |
+ | |
+ before do | |
+ ActiveRecord::Base.connection.class.stub!(:to_s).and_return('ActiveRec… | |
+ end | |
+ | |
+ it 'should use DATE_FORMAT with format string "%Y/%m/%d/%H" for grouping… | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:hour).send(:to_sql, 'created_a… | |
+ end | |
+ | |
+ it 'should use DATE_FORMAT with format string "%Y/%m/%d" for grouping :d… | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:day).send(:to_sql, 'created_at… | |
+ end | |
+ | |
+ it 'should use DATE_FORMAT with format string "%Y-%u" for grouping :week… | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:week).send(:to_sql, 'created_a… | |
+ end | |
+ | |
+ it 'should use DATE_FORMAT with format string "%Y/%m" for grouping :mont… | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:month).send(:to_sql, 'created_… | |
+ end | |
+ | |
+ end | |
+ | |
+ describe 'for PostgreSQL' do | |
+ | |
+ before do | |
+ ActiveRecord::Base.connection.class.stub!(:to_s).and_return('ActiveRec… | |
+ end | |
+ | |
+ it 'should use date_trunc with trunc "hour" for grouping :hour' do | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:hour).send(:to_sql, 'created_a… | |
+ end | |
+ | |
+ it 'should use date_trunc with trunc "day" for grouping :day' do | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:day).send(:to_sql, 'created_at… | |
+ end | |
+ | |
+ it 'should use date_trunc with trunc "week" for grouping :week' do | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:week).send(:to_sql, 'created_a… | |
+ end | |
+ | |
+ it 'should use date_trunc with trunc "month" for grouping :month' do | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:month).send(:to_sql, 'created_… | |
+ end | |
+ | |
+ end | |
+ | |
+ describe 'for SQLite3' do | |
+ | |
+ before do | |
+ ActiveRecord::Base.connection.class.stub!(:to_s).and_return('ActiveRec… | |
+ end | |
+ | |
+ it 'should use strftime with format string "%Y/%m/%d/%H" for grouping :h… | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:hour).send(:to_sql, 'created_a… | |
+ end | |
+ | |
+ it 'should use strftime with format string "%Y/%m/%d" for grouping :day'… | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:day).send(:to_sql, 'created_at… | |
+ end | |
+ | |
+ it 'should use strftime with format string "%Y-%W" for grouping :week' do | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:week).send(:to_sql, 'created_a… | |
+ end | |
+ | |
+ it 'should use strftime with format string "%Y/%m" for grouping :month' … | |
+ Kvlr::ReportsAsSparkline::Grouping.new(:month).send(:to_sql, 'created_… | |
+ end | |
+ | |
+ end | |
+ | |
+ end | |
+ | |
end | |
+ | |
+class ActiveRecord::ConnectionAdapters::MysqlAdapter; end | |
+class ActiveRecord::ConnectionAdapters::SQLite3Adapter; end | |
+class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter; end | |
diff --git a/spec/other/report_cache_spec.rb b/spec/other/report_cache_spec.rb | |
@@ -98,4 +98,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do | |
end | |
+ describe '#update_cache' do | |
+ end | |
+ | |
end |