Skip to content

Commit

Permalink
updated documentation, fixed bug in processing of data for end/beginn…
Browse files Browse the repository at this point in the history
…ing of year
  • Loading branch information
marcoow committed Feb 2, 2009
1 parent 8aa9bac commit d1aa1be
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 103 deletions.
2 changes: 1 addition & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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 aggregation :sum
* :aggregation - The aggregation to use (either :count or :sum); when using :sum, :value_column must also be specified
* :grouping - The period records are grouped on (:hour, :day, :week, :month)
* :grouping - The period records are grouped on (:hour, :day, :week, :month); <b>Beware that reports_as_sparkline treats weeks as starting on monday!</b>
* :limit - The number of periods to get (see :grouping)
* :conditions - Conditions like in ActiveRecord::Base#find; only records that match there conditions are reported on
* :cumulate - Sets whether to cumulate the numbers (instead of [1, 2, 3] returns [1, 3, 6])
Expand Down
2 changes: 1 addition & 1 deletion lib/kvlr/reports_as_sparkline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module ClassMethods
# * <tt>:date_column</tt> - The name of the date column on that the records are aggregated
# * <tt>:value_column</tt> - The name of the column that holds the value to sum for aggregation :sum
# * <tt>:aggregation</tt> - The aggregation to use (either :count or :sum); when using :sum, :value_column must also be specified
# * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month)
# * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month); <b>Beware that reports_as_sparkline treats weeks as starting on monday!</b>
# * <tt>:limit</tt> - The number of periods to get (see :grouping)
# * <tt>:conditions</tt> - Conditions like in ActiveRecord::Base#find; only records that match there conditions are reported on
# * <tt>:live_data</tt> - Specified whether data for the current reporting period is read; if :live_data is true, you will experience a performance hit since the request cannot be satisfied from the cache only (defaults to false)
Expand Down
67 changes: 41 additions & 26 deletions lib/kvlr/reports_as_sparkline/grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,13 @@ def identifier
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(&:to_i)
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

Expand All @@ -58,14 +41,46 @@ def to_sql(date_column) #: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(&:to_i)
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
"DATE_FORMAT(#{date_column}, '%Y/%m/%d/%H')"
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
Expand All @@ -78,7 +93,7 @@ def sqlite_format(date_column)
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
Expand Down
4 changes: 2 additions & 2 deletions lib/kvlr/reports_as_sparkline/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Report
# * <tt>:date_column</tt> - The name of the date column on that the records are aggregated
# * <tt>:value_column</tt> - The name of the column that holds the value to sum for aggregation :sum
# * <tt>:aggregation</tt> - The aggregation to use (either :count or :sum); when using :sum, :value_column must also be specified
# * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month)
# * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month); <b>Beware that reports_as_sparkline treats weeks as starting on monday!</b>
# * <tt>:limit</tt> - The number of periods to get (see :grouping)
# * <tt>:conditions</tt> - Conditions like in ActiveRecord::Base#find; only records that match there conditions are reported on
# * <tt>:live_data</tt> - Specified whether data for the current reporting period is read; if :live_data is true, you will experience a performance hit since the request cannot be satisfied from the cache only (defaults to false)
Expand All @@ -42,7 +42,7 @@ def initialize(klass, name, options = {})
# ==== Options
# * <tt>:limit</tt> - The number of periods to get
# * <tt>:conditions</tt> - Conditions like in ActiveRecord::Base#find; only records that match there conditions are reported on (<b>Beware that when you specify conditions here, caching will be disabled</b>)
# * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month)
# * <tt>:grouping</tt> - The period records are grouped on (:hour, :day, :week, :month); <b>Beware that reports_as_sparkline treats weeks as starting on monday!</b>
# * <tt>:live_data</tt> - Specified whether data for the current reporting period is read; if :live_data is true, you will experience a performance hit since the request cannot be satisfied from the cache only (defaults to false)
def run(options = {})
ensure_valid_options(options, :run)
Expand Down
4 changes: 3 additions & 1 deletion rdoc/classes/Kvlr/ReportsAsSparkline/ClassMethods.html
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ <h4>Options</h4>

</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)
Expand Down
6 changes: 4 additions & 2 deletions rdoc/classes/Kvlr/ReportsAsSparkline/Report.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ <h4>Options</h4>

</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)
Expand Down Expand Up @@ -261,7 +262,8 @@ <h4>Options</h4>

</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
Expand Down
52 changes: 26 additions & 26 deletions rdoc/classes/Kvlr/ReportsAsSparkline/ReportingPeriod.html
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,19 @@ <h3 class="section-bar">Public Instance methods</h3>
onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000008-source">
<pre>
<span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/reporting_period.rb, line 53</span>
53: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">next</span>
54: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@grouping</span>.<span class="ruby-identifier">identifier</span>
55: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:hour</span>
56: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">hour</span>)
57: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:day</span>
58: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">day</span>)
59: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:week</span>
60: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">week</span>)
61: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:month</span>
62: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">month</span>)
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/reporting_period.rb, line 54</span>
54: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">next</span>
55: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@grouping</span>.<span class="ruby-identifier">identifier</span>
56: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:hour</span>
57: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">hour</span>)
58: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:day</span>
59: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">day</span>)
60: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:week</span>
61: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">week</span>)
62: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:month</span>
63: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">month</span>)
64: <span class="ruby-keyword kw">end</span>
65: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
Expand All @@ -283,19 +283,19 @@ <h3 class="section-bar">Public Instance methods</h3>
onclick="toggleCode('M000009-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000009-source">
<pre>
<span class="ruby-comment cmt"># File lib/kvlr/reports_as_sparkline/reporting_period.rb, line 67</span>
67: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">previous</span>
68: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@grouping</span>.<span class="ruby-identifier">identifier</span>
69: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:hour</span>
70: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">hour</span>)
71: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:day</span>
72: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">day</span>)
73: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:week</span>
74: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">week</span>)
75: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:month</span>
76: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">month</span>)
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/reporting_period.rb, line 68</span>
68: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">previous</span>
69: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@grouping</span>.<span class="ruby-identifier">identifier</span>
70: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:hour</span>
71: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">hour</span>)
72: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:day</span>
73: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">day</span>)
74: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:week</span>
75: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">week</span>)
76: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:month</span>
77: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@grouping</span>, <span class="ruby-ivar">@date_time</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>.<span class="ruby-identifier">month</span>)
78: <span class="ruby-keyword kw">end</span>
79: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion rdoc/created.rid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Wed, 21 Jan 2009 13:09:39 +0100
Mon, 02 Feb 2009 14:31:59 +0100
5 changes: 3 additions & 2 deletions rdoc/files/README_rdoc.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h1>README.rdoc</h1>
</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>
Expand Down Expand Up @@ -93,7 +93,8 @@ <h2>Usage</h2>
: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)
Expand Down
2 changes: 1 addition & 1 deletion rdoc/files/lib/kvlr/reports_as_sparkline/grouping_rb.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h1>grouping.rb</h1>
</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>
Expand Down
2 changes: 1 addition & 1 deletion rdoc/files/lib/kvlr/reports_as_sparkline/report_rb.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h1>report.rb</h1>
</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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h1>reporting_period.rb</h1>
</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>
Expand Down
2 changes: 1 addition & 1 deletion rdoc/files/lib/kvlr/reports_as_sparkline_rb.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h1>reports_as_sparkline.rb</h1>
</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>
Expand Down
2 changes: 1 addition & 1 deletion spec/boot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), 'log', 'spec.log'))

databases = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'db', 'database.yml')))
ActiveRecord::Base.establish_connection(databases['sqlite3'])
ActiveRecord::Base.establish_connection(databases['sqlite'])
load(File.join(File.dirname(__FILE__), 'db', 'schema.rb'))
Loading

0 comments on commit d1aa1be

Please sign in to comment.