Introduction
Introduction Statistics Contact Development Disclaimer Help
updated documentation, fixed bug in processing of data for end/beginning of yea…
Log
Files
Refs
README
---
commit d1aa1be511b8c85872d238b84faa356ea3231baf
parent 8aa9bac80b0716c60fd58a5f78cba96077608b12
Author: Marco Otte-Witte <[email protected]>
Date: Mon, 2 Feb 2009 14:32:36 +0100
updated documentation, fixed bug in processing of data for end/beginning of year
Diffstat:
M README.rdoc | 2 +-
M lib/kvlr/reports_as_sparkline.rb | 2 +-
M lib/kvlr/reports_as_sparkline/grou… | 67 +++++++++++++++++++--------…
M lib/kvlr/reports_as_sparkline/repo… | 4 ++--
M rdoc/classes/Kvlr/ReportsAsSparkli… | 4 +++-
M rdoc/classes/Kvlr/ReportsAsSparkli… | 6 ++++--
M rdoc/classes/Kvlr/ReportsAsSparkli… | 52 ++++++++++++++++-----------…
M rdoc/created.rid | 2 +-
M rdoc/files/README_rdoc.html | 5 +++--
M rdoc/files/lib/kvlr/reports_as_spa… | 2 +-
M rdoc/files/lib/kvlr/reports_as_spa… | 2 +-
M rdoc/files/lib/kvlr/reports_as_spa… | 2 +-
M rdoc/files/lib/kvlr/reports_as_spa… | 2 +-
M spec/boot.rb | 2 +-
M spec/classes/grouping_spec.rb | 22 +++++++++++-----------
M spec/classes/report_spec.rb | 88 ++++++++++++++++++++++-------…
16 files changed, 161 insertions(+), 103 deletions(-)
---
diff --git a/README.rdoc b/README.rdoc
@@ -10,7 +10,7 @@ to it with the following options:
* :date_column - The name of the date column on that the records are aggregated
* :value_column - The name of the column that holds the value to sum for aggre…
* :aggregation - The aggregation to use (either :count or :sum); when using :s…
-* :grouping - The period records are grouped on (:hour, :day, :week, :month)
+* :grouping - The period records are grouped on (:hour, :day, :week, :month); …
* :limit - The number of periods to get (see :grouping)
* :conditions - Conditions like in ActiveRecord::Base#find; only records that …
* :cumulate - Sets whether to cumulate the numbers (instead of [1, 2, 3] retur…
diff --git a/lib/kvlr/reports_as_sparkline.rb b/lib/kvlr/reports_as_sparkline.rb
@@ -19,7 +19,7 @@ module Kvlr #:nodoc:
# * <tt>:date_column</tt> - The name of the date column on that the reco…
# * <tt>:value_column</tt> - The name of the column that holds the value…
# * <tt>:aggregation</tt> - The aggregation to use (either :count or :su…
- # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day,…
+ # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day,…
# * <tt>:limit</tt> - The number of periods to get (see :grouping)
# * <tt>:conditions</tt> - Conditions like in ActiveRecord::Base#find; o…
# * <tt>:live_data</tt> - Specified whether data for the current reporti…
diff --git a/lib/kvlr/reports_as_sparkline/grouping.rb b/lib/kvlr/reports_as_sp…
@@ -18,30 +18,13 @@ module Kvlr #:nodoc:
end
def date_parts_from_db_string(db_string) #:nodoc:
- if ActiveRecord::Base.connection.adapter_name =~ /postgres/i
- case @identifier
- when :hour
- return (db_string[0..9].split('-') + [db_string[11..12]]).map(&:…
- when :day
- return db_string[0..9].split('-').map(&:to_i)
- when :week
- parts = db_string[0..9].split('-').map(&:to_i)
- date = Date.new(parts[0], parts[1], parts[2])
- return [date.year, date.cweek]
- when :month
- return db_string[0..6].split('-')[0..1].map(&:to_i)
- end
- else
- parts = db_string.split('/').map(&:to_i)
- if ActiveRecord::Base.connection.adapter_name =~ /mysql/i
- if @identifier == :week && parts[1] > 52
- parts[0] += 1
- parts[1] = 1
- end
- return parts
- end
- parts[1] += 1 if @identifier == :week
- parts
+ return case ActiveRecord::Base.connection.adapter_name
+ when /mysql/i
+ from_mysql_db_string(db_string)
+ when /sqlite/i
+ from_sqlite_db_string(db_string)
+ when /postgres/i
+ from_postgresql_db_string(db_string)
end
end
@@ -58,6 +41,38 @@ module Kvlr #:nodoc:
private
+ def from_mysql_db_string(db_string)
+ if @identifier == :week
+ parts = [db_string[0..3], db_string[4..5]].map(&:to_i)
+ else
+ db_string.split('/').map(&:to_i)
+ end
+ end
+
+ def from_sqlite_db_string(db_string)
+ if @identifier == :week
+ parts = db_string.split('-').map(&:to_i)
+ date = Date.new(parts[0], parts[1], parts[2])
+ return [date.cwyear, date.cweek]
+ end
+ db_string.split('/').map(&:to_i)
+ end
+
+ def from_postgresql_db_string(db_string)
+ case @identifier
+ when :hour
+ return (db_string[0..9].split('-') + [db_string[11..12]]).map(&:…
+ when :day
+ return db_string[0..9].split('-').map(&:to_i)
+ when :week
+ parts = db_string[0..9].split('-').map(&:to_i)
+ date = Date.new(parts[0], parts[1], parts[2])
+ return [date.cwyear, date.cweek]
+ when :month
+ return db_string[0..6].split('-')[0..1].map(&:to_i)
+ end
+ end
+
def mysql_format(date_column)
return case @identifier
when :hour
@@ -65,7 +80,7 @@ module Kvlr #:nodoc:
when :day
"DATE_FORMAT(#{date_column}, '%Y/%m/%d')"
when :week
- "DATE_FORMAT(#{date_column}, '%Y/%u')"
+ "YEARWEEK(#{date_column}, 3)"
when :month
"DATE_FORMAT(#{date_column}, '%Y/%m')"
end
@@ -78,7 +93,7 @@ module Kvlr #:nodoc:
when :day
"strftime('%Y/%m/%d', #{date_column})"
when :week
- "strftime('%Y/%W', #{date_column})"
+ "date(#{date_column}, 'weekday 0')"
when :month
"strftime('%Y/%m', #{date_column})"
end
diff --git a/lib/kvlr/reports_as_sparkline/report.rb b/lib/kvlr/reports_as_spar…
@@ -16,7 +16,7 @@ module Kvlr #:nodoc:
# * <tt>:date_column</tt> - The name of the date column on that the reco…
# * <tt>:value_column</tt> - The name of the column that holds the value…
# * <tt>:aggregation</tt> - The aggregation to use (either :count or :su…
- # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day,…
+ # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day,…
# * <tt>:limit</tt> - The number of periods to get (see :grouping)
# * <tt>:conditions</tt> - Conditions like in ActiveRecord::Base#find; o…
# * <tt>:live_data</tt> - Specified whether data for the current reporti…
@@ -42,7 +42,7 @@ module Kvlr #:nodoc:
# ==== Options
# * <tt>:limit</tt> - The number of periods to get
# * <tt>:conditions</tt> - Conditions like in ActiveRecord::Base#find; o…
- # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day,…
+ # * <tt>:grouping</tt> - The period records are grouped on (:hour, :day,…
# * <tt>:live_data</tt> - Specified whether data for the current reporti…
def run(options = {})
ensure_valid_options(options, :run)
diff --git a/rdoc/classes/Kvlr/ReportsAsSparkline/ClassMethods.html b/rdoc/clas…
@@ -139,7 +139,9 @@ when using :sum, :value_column must also be specified
</li>
<li><tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week,
-:month)
+:month); <b>Beware that <a
+href="ClassMethods.html#M000002">reports_as_sparkline</a> treats weeks as
+starting on monday!</b>
</li>
<li><tt>:limit</tt> - The number of periods to get (see :grouping)
diff --git a/rdoc/classes/Kvlr/ReportsAsSparkline/Report.html b/rdoc/classes/Kv…
@@ -192,7 +192,8 @@ when using :sum, :value_column must also be specified
</li>
<li><tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week,
-:month)
+:month); <b>Beware that reports_as_sparkline treats weeks as starting on
+monday!</b>
</li>
<li><tt>:limit</tt> - The number of periods to get (see :grouping)
@@ -261,7 +262,8 @@ you specify conditions here, caching will be disabled</b>)
</li>
<li><tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week,
-:month)
+:month); <b>Beware that reports_as_sparkline treats weeks as starting on
+monday!</b>
</li>
<li><tt>:live_data</tt> - Specified whether data for the current reporting
diff --git a/rdoc/classes/Kvlr/ReportsAsSparkline/ReportingPeriod.html b/rdoc/c…
@@ -246,19 +246,19 @@ hour/day/month/year)
onclick="toggleCode('M000008-source');return false;">[Source]</a><…
<div class="method-source-code" id="M000008-source">
<pre>
- <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/report…
-53: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword k…
-54: <span class="ruby-keyword kw">return</span> <span class="ruby-keyw…
-55: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
-56: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
-57: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
-58: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
-59: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
-60: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
-61: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
-62: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
-63: <span class="ruby-keyword kw">end</span>
-64: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/report…
+54: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword k…
+55: <span class="ruby-keyword kw">return</span> <span class="ruby-keyw…
+56: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
+57: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
+58: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
+59: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
+60: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
+61: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
+62: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
+63: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
+64: <span class="ruby-keyword kw">end</span>
+65: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
@@ -283,19 +283,19 @@ hour/day/month/year)
onclick="toggleCode('M000009-source');return false;">[Source]</a><…
<div class="method-source-code" id="M000009-source">
<pre>
- <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/report…
-67: <span class="ruby-keyword kw">def</span> <span class="ruby-identifie…
-68: <span class="ruby-keyword kw">return</span> <span class="ruby-keyw…
-69: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
-70: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
-71: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
-72: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
-73: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
-74: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
-75: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
-76: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
-77: <span class="ruby-keyword kw">end</span>
-78: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/report…
+68: <span class="ruby-keyword kw">def</span> <span class="ruby-identifie…
+69: <span class="ruby-keyword kw">return</span> <span class="ruby-keyw…
+70: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
+71: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
+72: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
+73: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
+74: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
+75: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
+76: <span class="ruby-keyword kw">when</span> <span class="ruby-iden…
+77: <span class="ruby-keyword kw">self</span>.<span class="ruby-id…
+78: <span class="ruby-keyword kw">end</span>
+79: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
diff --git a/rdoc/created.rid b/rdoc/created.rid
@@ -1 +1 @@
-Wed, 21 Jan 2009 13:09:39 +0100
+Mon, 02 Feb 2009 14:31:59 +0100
diff --git a/rdoc/files/README_rdoc.html b/rdoc/files/README_rdoc.html
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Tue Jan 20 19:59:51 +0100 2009</td>
+ <td>Mon Feb 02 11:51:38 +0100 2009</td>
</tr>
</table>
</div>
@@ -93,7 +93,8 @@ aggregation :sum
:sum, :value_column must also be specified
</li>
-<li>:grouping - The period records are grouped on (:hour, :day, :week, :month)
+<li>:grouping - The period records are grouped on (:hour, :day, :week, :month);
+<b>Beware that reports_as_sparkline treats weeks as starting on monday!</b>
</li>
<li>:limit - The number of periods to get (see :grouping)
diff --git a/rdoc/files/lib/kvlr/reports_as_sparkline/grouping_rb.html b/rdoc/f…
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Thu Jan 15 15:32:06 +0100 2009</td>
+ <td>Mon Feb 02 14:24:37 +0100 2009</td>
</tr>
</table>
</div>
diff --git a/rdoc/files/lib/kvlr/reports_as_sparkline/report_rb.html b/rdoc/fil…
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Tue Jan 20 12:21:17 +0100 2009</td>
+ <td>Mon Feb 02 11:52:42 +0100 2009</td>
</tr>
</table>
</div>
diff --git a/rdoc/files/lib/kvlr/reports_as_sparkline/reporting_period_rb.html …
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Tue Jan 20 18:50:13 +0100 2009</td>
+ <td>Mon Feb 02 14:00:53 +0100 2009</td>
</tr>
</table>
</div>
diff --git a/rdoc/files/lib/kvlr/reports_as_sparkline_rb.html b/rdoc/files/lib/…
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Wed Jan 21 13:08:56 +0100 2009</td>
+ <td>Mon Feb 02 11:52:04 +0100 2009</td>
</tr>
</table>
</div>
diff --git a/spec/boot.rb b/spec/boot.rb
@@ -19,5 +19,5 @@ FileUtils.mkdir_p File.join(File.dirname(__FILE__), 'log')
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), 'log'…
databases = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'db', 'databa…
-ActiveRecord::Base.establish_connection(databases['sqlite3'])
+ActiveRecord::Base.establish_connection(databases['sqlite'])
load(File.join(File.dirname(__FILE__), 'db', 'schema.rb'))
diff --git a/spec/classes/grouping_spec.rb b/spec/classes/grouping_spec.rb
@@ -26,8 +26,8 @@ describe Kvlr::ReportsAsSparkline::Grouping do
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…
+ it 'should use YEARWEEK with mode 3 for grouping :week' do
+ Kvlr::ReportsAsSparkline::Grouping.new(:week).send(:to_sql, 'created_a…
end
it 'should use DATE_FORMAT with format string "%Y/%m" for grouping :mont…
@@ -66,8 +66,8 @@ describe Kvlr::ReportsAsSparkline::Grouping do
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…
+ it 'should use date with mode "weekday 0" 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' …
@@ -94,9 +94,9 @@ describe Kvlr::ReportsAsSparkline::Grouping do
end
- it 'should split the string with "/" and increment the week by 1 for gro…
- db_string = '2008/2'
- expected = [2008, 3]
+ it 'should split the string with "-" and return teh calendar year and we…
+ db_string = '2008-2-1'
+ expected = [2008, 5]
Kvlr::ReportsAsSparkline::Grouping.new(:week).date_parts_from_db_strin…
end
@@ -133,7 +133,7 @@ describe Kvlr::ReportsAsSparkline::Grouping do
ActiveRecord::Base.connection.stub!(:adapter_name).and_return('MySQL')
end
- for grouping in [[:hour, '2008/12/31/12'], [:day, '2008/12/31'], [:week,…
+ for grouping in [[:hour, '2008/12/31/12'], [:day, '2008/12/31'], [:month…
it "should split the string with '/' for grouping :#{grouping[0].to_s}…
Kvlr::ReportsAsSparkline::Grouping.new(grouping[0]).date_parts_from_…
@@ -141,9 +141,9 @@ describe Kvlr::ReportsAsSparkline::Grouping do
end
- it 'should split the string with "/", set the week to 1 and increment th…
- db_string = '2008/53'
- expected = [2009, 1]
+ it 'should use the first 4 numbers for the year and the last 2 numbers f…
+ db_string = '200852'
+ expected = [2008, 52]
Kvlr::ReportsAsSparkline::Grouping.new(:week).date_parts_from_db_strin…
end
diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb
@@ -70,30 +70,6 @@ describe Kvlr::ReportsAsSparkline::Report do
describe "for grouping #{grouping.to_s}" do
- before(:all) do
-
- end
-
-
-
-
-
- after(:all) do
- User.destroy_all
- end
-
- after(:each) do
- Kvlr::ReportsAsSparkline::ReportCache.destroy_all
- end
-
- end
-
- end
-
- for grouping in [:hour, :day, :week, :month] do
-
- describe "for grouping #{grouping.to_s}" do
-
[true, false].each do |live_data|
describe "with :live_data = #{live_data}" do
@@ -211,7 +187,69 @@ describe Kvlr::ReportsAsSparkline::Report do
end
- after(:each) do
+ describe 'for grouping week with data ranging over two years' do
+
+ describe 'with the first week of the second year belonging to the first …
+
+ before(:all) do
+ User.create!(:login => 'test 1', :created_at => DateTime.new(2008, 1…
+ User.create!(:login => 'test 2', :created_at => DateTime.new(2008, 1…
+ User.create!(:login => 'test 3', :created_at => DateTime.new(2009, 1…
+ User.create!(:login => 'test 4', :created_at => DateTime.new(2009, 1…
+ User.create!(:login => 'test 5', :created_at => DateTime.new(2009, 1…
+
+ Time.stub!(:now).and_return(DateTime.new(2009, 1, 25))
+ end
+
+ it 'should return correct data for aggregation :count' do
+ @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,
+ :aggregation => :count,
+ :grouping => :week,
+ :limit => 10
+ )
+ result = @report.run.to_a
+
+ result[9][1].should == 0.0
+ result[8][1].should == 1.0
+ result[7][1].should == 1.0
+ result[6][1].should == 2.0
+ result[5][1].should == 1.0
+ end
+
+ end
+
+ describe 'with the first week of the second year belonging to the second…
+
+ before(:all) do
+ User.create!(:login => 'test 1', :created_at => DateTime.new(2009, 1…
+ User.create!(:login => 'test 2', :created_at => DateTime.new(2009, 1…
+ User.create!(:login => 'test 3', :created_at => DateTime.new(2010, 1…
+ User.create!(:login => 'test 4', :created_at => DateTime.new(2010, 1…
+ User.create!(:login => 'test 5', :created_at => DateTime.new(2010, 1…
+
+ Time.stub!(:now).and_return(DateTime.new(2010, 1, 25))
+ end
+
+ it 'should return correct data for aggregation :count' do
+ @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations,
+ :aggregation => :count,
+ :grouping => :week,
+ :limit => 10
+ )
+ result = @report.run.to_a
+
+ result[9][1].should == 0.0
+ result[8][1].should == 1.0
+ result[7][1].should == 1.0
+ result[6][1].should == 2.0
+ result[5][1].should == 1.0
+ end
+
+ end
+
+ end
+
+ after do
Kvlr::ReportsAsSparkline::ReportCache.destroy_all
end
You are viewing proxied material from jay.scot. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.