| Merge branch 'integration' of [email protected]:marcoow/reports_as_sparkline into … | |
| Log | |
| Files | |
| Refs | |
| README | |
| --- | |
| commit 915e4799ef51997835a38be511f0366edd2053e3 | |
| parent 1eb9f194bfcff7505457cae1f2496c0fb0816893 | |
| Author: Marco Otte-Witte <[email protected]> | |
| Date: Sat, 2 Jan 2010 11:34:12 +0100 | |
| Merge branch 'integration' of [email protected]:marcoow/reports_as_sparkline into … | |
| Diffstat: | |
| M generators/reports_as_sparkline_mi… | 5 ++++- | |
| M lib/simplabs/reports_as_sparkline/… | 5 ++--- | |
| M lib/simplabs/reports_as_sparkline/… | 23 +++++++++++------------ | |
| M lib/simplabs/reports_as_sparkline/… | 14 +++++++++++--- | |
| M spec/classes/report_cache_spec.rb | 48 ++++++++---------------------… | |
| M spec/classes/report_spec.rb | 11 ++++------- | |
| M spec/db/schema.rb | 9 ++++++--- | |
| 7 files changed, 50 insertions(+), 65 deletions(-) | |
| --- | |
| diff --git a/generators/reports_as_sparkline_migration/templates/migration.erb … | |
| @@ -6,6 +6,7 @@ class <%= class_name %> < ActiveRecord::Migration | |
| t.string :report_name, :null => false | |
| t.string :grouping, :null => false | |
| t.string :aggregation, :null => false | |
| + t.string :condition, :null => false | |
| t.float :value, :null => false, :default => 0 | |
| t.datetime :reporting_period, :null => false | |
| @@ -16,13 +17,15 @@ class <%= class_name %> < ActiveRecord::Migration | |
| :model_name, | |
| :report_name, | |
| :grouping, | |
| - :aggregation | |
| + :aggregation, | |
| + :condition | |
| ], :name => :name_model_grouping_agregation | |
| add_index :reports_as_sparkline_cache, [ | |
| :model_name, | |
| :report_name, | |
| :grouping, | |
| :aggregation, | |
| + :condition, | |
| :reporting_period | |
| ], :unique => true, :name => :name_model_grouping_aggregation_period | |
| end | |
| diff --git a/lib/simplabs/reports_as_sparkline/report.rb b/lib/simplabs/reports… | |
| @@ -44,13 +44,12 @@ module Simplabs #:nodoc: | |
| # ==== Options | |
| # * <tt>:grouping</tt> - The period records are grouped on (<tt>:hour</t… | |
| # * <tt>:limit</tt> - The number of reporting periods to get (see <tt>:g… | |
| - # * <tt>:conditions</tt> - Conditions like in <tt>ActiveRecord::Base#fin… | |
| + # * <tt>:conditions</tt> - Conditions like in <tt>ActiveRecord::Base#fin… | |
| # * <tt>:live_data</tt> - Specifies whether data for the current reporti… | |
| # * <tt>:end_date</tt> - When specified, the report will only include da… | |
| def run(options = {}) | |
| - custom_conditions = options.key?(:conditions) | |
| options = options_for_run(options) | |
| - ReportCache.process(self, options, !custom_conditions) do |begin_at, e… | |
| + ReportCache.process(self, options) do |begin_at, end_at| | |
| read_data(begin_at, end_at, options) | |
| end | |
| end | |
| diff --git a/lib/simplabs/reports_as_sparkline/report_cache.rb b/lib/simplabs/r… | |
| @@ -30,21 +30,18 @@ module Simplabs #:nodoc: | |
| }) | |
| end | |
| - def self.process(report, options, cache = true, &block) #:nodoc: | |
| + def self.process(report, options, &block) #:nodoc: | |
| raise ArgumentError.new('A block must be given') unless block_given? | |
| self.transaction do | |
| - cached_data = [] | |
| - if cache | |
| - cached_data = read_cached_data(report, options) | |
| - end | |
| + cached_data = read_cached_data(report, options) | |
| new_data = read_new_data(cached_data, options, &block) | |
| - prepare_result(new_data, cached_data, report, options, cache) | |
| + prepare_result(new_data, cached_data, report, options) | |
| end | |
| end | |
| private | |
| - def self.prepare_result(new_data, cached_data, report, options, cache … | |
| + def self.prepare_result(new_data, cached_data, report, options) | |
| new_data = new_data.map { |data| [ReportingPeriod.from_db_string(opt… | |
| cached_data.map! { |cached| [ReportingPeriod.new(options[:grouping],… | |
| current_reporting_period = ReportingPeriod.current(options[:grouping… | |
| @@ -54,8 +51,8 @@ module Simplabs #:nodoc: | |
| if cached = cached_data.find { |cached| reporting_period == cached… | |
| result << [cached[0].date_time, cached[1]] | |
| else | |
| - new_cached = build_cached_data(report, options[:grouping], repor… | |
| - new_cached.save! if cache | |
| + new_cached = build_cached_data(report, options[:grouping], optio… | |
| + new_cached.save! | |
| result << [reporting_period.date_time, new_cached.value] | |
| end | |
| reporting_period = reporting_period.next | |
| @@ -71,12 +68,13 @@ module Simplabs #:nodoc: | |
| data ? data[1] : 0.0 | |
| end | |
| - def self.build_cached_data(report, grouping, reporting_period, value) | |
| + def self.build_cached_data(report, grouping, condition, reporting_peri… | |
| self.new( | |
| :model_name => report.klass.to_s, | |
| :report_name => report.name.to_s, | |
| :grouping => grouping.identifier.to_s, | |
| :aggregation => report.aggregation.to_s, | |
| + :condition => condition.to_s, | |
| :reporting_period => reporting_period.date_time, | |
| :value => value | |
| ) | |
| @@ -84,11 +82,12 @@ module Simplabs #:nodoc: | |
| def self.read_cached_data(report, options) | |
| conditions = [ | |
| - 'model_name = ? AND report_name = ? AND grouping = ? AND aggregati… | |
| + 'model_name = ? AND report_name = ? AND grouping = ? AND aggregati… | |
| report.klass.to_s, | |
| report.name.to_s, | |
| options[:grouping].identifier.to_s, | |
| - report.aggregation.to_s | |
| + report.aggregation.to_s, | |
| + options[:conditions].to_s | |
| ] | |
| first_reporting_period = get_first_reporting_period(options) | |
| last_reporting_period = get_last_reporting_period(options) | |
| diff --git a/lib/simplabs/reports_as_sparkline/sparkline_tag_helper.rb b/lib/si… | |
| @@ -17,18 +17,26 @@ module Simplabs #:nodoc: | |
| # * <tt>line_color</tt> - The line color of the sparkline (hex code) | |
| # * <tt>fill_color</tt> - The color to fill the area below the sparkline… | |
| # * <tt>labels</tt> - The axes to render lables for (Array of <tt>:x</tt… | |
| + # * <tt>alt</tt> - The HTML img alt tag | |
| + # * <tt>title</tt> - The HTML img title tag | |
| # | |
| # ==== Example | |
| # <tt><%= sparkline_tag(User.registrations_report, :width => 200, :heigh… | |
| def sparkline_tag(data, options = {}) | |
| - options.reverse_merge!({ :width => 300, :height => 34, :line_color => … | |
| + options.reverse_merge!({ :width => 300, :height => 34, :line_color => … | |
| data = data.collect { |d| d[1] } | |
| labels = "" | |
| unless options[:labels].empty? | |
| - labels = "&chxt=#{options[:labels].map(&:to_s).join(',')}&chxr=0,0,#… | |
| + chxr = {} | |
| + options[:labels].each_with_index do |l, i| | |
| + chxr[l] = "#{i}," + ([:x, :t].include?(l) ? "0,#{data.length}" : "… | |
| + end | |
| + labels = "&chxt=#{options[:labels].map(&:to_s).join(',')}&chxr=#{opt… | |
| end | |
| image_tag( | |
| - "http://chart.apis.google.com/chart?cht=ls&chs=#{options[:width]}x#{… | |
| + "http://chart.apis.google.com/chart?cht=ls&chs=#{options[:width]}x#{… | |
| + :alt => options[:alt], | |
| + :title => options[:title] | |
| ) | |
| end | |
| diff --git a/spec/classes/report_cache_spec.rb b/spec/classes/report_cache_spec… | |
| @@ -122,11 +122,12 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
| it 'should read existing data from the cache' do | |
| Simplabs::ReportsAsSparkline::ReportCache.should_receive(:all).once.with( | |
| :conditions => [ | |
| - 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation… | |
| + 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation… | |
| @report.klass.to_s, | |
| @report.name.to_s, | |
| @report.options[:grouping].identifier.to_s, | |
| @report.aggregation.to_s, | |
| + @report.options[:conditions].to_s, | |
| Simplabs::ReportsAsSparkline::ReportingPeriod.first(@report.options[… | |
| ], | |
| :limit => 10, | |
| @@ -140,11 +141,12 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
| end_date = Time.now | |
| Simplabs::ReportsAsSparkline::ReportCache.should_receive(:all).once.with( | |
| :conditions => [ | |
| - 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation… | |
| + 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation… | |
| @report.klass.to_s, | |
| @report.name.to_s, | |
| @report.options[:grouping].identifier.to_s, | |
| @report.aggregation.to_s, | |
| + @report.options[:conditions].to_s, | |
| Simplabs::ReportsAsSparkline::ReportingPeriod.first(@report.options[… | |
| Simplabs::ReportsAsSparkline::ReportingPeriod.new(@report.options[:g… | |
| ], | |
| @@ -160,11 +162,12 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
| Simplabs::ReportsAsSparkline::ReportCache.should_receive(:find).once.wit… | |
| :all, | |
| :conditions => [ | |
| - 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation… | |
| + 'model_name = ? AND report_name = ? AND grouping = ? AND aggregation… | |
| @report.klass.to_s, | |
| @report.name.to_s, | |
| grouping.identifier.to_s, | |
| @report.aggregation.to_s, | |
| + @report.options[:conditions].to_s, | |
| Simplabs::ReportsAsSparkline::ReportingPeriod.first(grouping, 10).da… | |
| ], | |
| :limit => 10, | |
| @@ -181,25 +184,6 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
| [] | |
| end | |
| end | |
| - | |
| - describe 'with cache = false' do | |
| - | |
| - it 'should not read any data from cache' do | |
| - Simplabs::ReportsAsSparkline::ReportCache.should_not_receive(:find) | |
| - | |
| - Simplabs::ReportsAsSparkline::ReportCache.process(@report, @report.opt… | |
| - end | |
| - | |
| - it 'should yield the first reporting period' do | |
| - Simplabs::ReportsAsSparkline::ReportCache.process(@report, @report.opt… | |
| - begin_at.should == Simplabs::ReportsAsSparkline::ReportingPeriod.fir… | |
| - end_at.should == nil | |
| - [] | |
| - end | |
| - end | |
| - | |
| - end | |
| - | |
| end | |
| describe '.prepare_result' do | |
| @@ -217,6 +201,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
| Simplabs::ReportsAsSparkline::ReportCache.should_receive(:build_cached_d… | |
| @report, | |
| @report.options[:grouping], | |
| + @report.options[:conditions], | |
| anything(), | |
| 0.0 | |
| ).and_return(@cached) | |
| @@ -228,12 +213,14 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
| Simplabs::ReportsAsSparkline::ReportCache.should_receive(:build_cached_d… | |
| @report, | |
| @report.options[:grouping], | |
| + @report.options[:conditions], | |
| anything(), | |
| 0.0 | |
| ).and_return(@cached) | |
| Simplabs::ReportsAsSparkline::ReportCache.should_receive(:build_cached_d… | |
| @report, | |
| @report.options[:grouping], | |
| + @report.options[:conditions], | |
| @current_reporting_period.previous, | |
| 1.0 | |
| ).and_return(@cached) | |
| @@ -248,7 +235,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
| end | |
| it 'should return an array of arrays of Dates and Floats' do | |
| - result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result,… | |
| + result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result,… | |
| result.should be_kind_of(Array) | |
| result[0].should be_kind_of(Array) | |
| @@ -259,7 +246,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
| describe 'with :live_data = false' do | |
| before do | |
| - @result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_resu… | |
| + @result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_resu… | |
| end | |
| it 'should return an array of length :limit' do | |
| @@ -276,7 +263,7 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
| before do | |
| options = @report.options.merge(:live_data => true) | |
| - @result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_resu… | |
| + @result = Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_resu… | |
| end | |
| it 'should return an array of length (:limit + 1)' do | |
| @@ -288,17 +275,6 @@ describe Simplabs::ReportsAsSparkline::ReportCache do | |
| end | |
| end | |
| - | |
| - describe 'with cache = false' do | |
| - | |
| - it 'should not save the created Simplabs::ReportsAsSparkline::ReportCach… | |
| - @cached.should_not_receive(:save!) | |
| - | |
| - Simplabs::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_d… | |
| - end | |
| - | |
| - end | |
| - | |
| end | |
| describe '.find_value' do | |
| diff --git a/spec/classes/report_spec.rb b/spec/classes/report_spec.rb | |
| @@ -21,18 +21,16 @@ describe Simplabs::ReportsAsSparkline::Report do | |
| it 'should process the data with the report cache' do | |
| Simplabs::ReportsAsSparkline::ReportCache.should_receive(:process).once.… | |
| @report, | |
| - { :limit => 100, :grouping => @report.options[:grouping], :conditions … | |
| - true | |
| + { :limit => 100, :grouping => @report.options[:grouping], :conditions … | |
| ) | |
| @report.run | |
| end | |
| - it 'should process the data with the report cache and specify cache = fals… | |
| + it 'should process the data with the report cache when custom conditions a… | |
| Simplabs::ReportsAsSparkline::ReportCache.should_receive(:process).once.… | |
| @report, | |
| - { :limit => 100, :grouping => @report.options[:grouping], :conditions … | |
| - false | |
| + { :limit => 100, :grouping => @report.options[:grouping], :conditions … | |
| ) | |
| @report.run(:conditions => { :some => :condition }) | |
| @@ -49,8 +47,7 @@ describe Simplabs::ReportsAsSparkline::Report do | |
| Simplabs::ReportsAsSparkline::Grouping.should_receive(:new).once.with(:m… | |
| Simplabs::ReportsAsSparkline::ReportCache.should_receive(:process).once.… | |
| @report, | |
| - { :limit => 100, :grouping => grouping, :conditions => [], :live_data … | |
| - true | |
| + { :limit => 100, :grouping => grouping, :conditions => [], :live_data … | |
| ) | |
| @report.run(:grouping => :month) | |
| diff --git a/spec/db/schema.rb b/spec/db/schema.rb | |
| @@ -13,6 +13,7 @@ ActiveRecord::Schema.define(:version => 1) do | |
| t.string :report_name, :null => false | |
| t.string :grouping, :null => false | |
| t.string :aggregation, :null => false | |
| + t.string :condition, :null => false | |
| t.float :value, :null => false, :default => 0 | |
| t.datetime :reporting_period, :null => false | |
| @@ -22,14 +23,16 @@ ActiveRecord::Schema.define(:version => 1) do | |
| :model_name, | |
| :report_name, | |
| :grouping, | |
| - :aggregation | |
| - ], :name => :name_model_grouping_agregation_run_limit | |
| + :aggregation, | |
| + :condition | |
| + ], :name => :name_model_grouping_agregation | |
| add_index :reports_as_sparkline_cache, [ | |
| :model_name, | |
| :report_name, | |
| :grouping, | |
| :aggregation, | |
| + :condition, | |
| :reporting_period | |
| - ], :unique => true, :name => :name_model_grouping_aggregation_period_run_lim… | |
| + ], :unique => true, :name => :name_model_grouping_aggregation_period | |
| end |