Permalink
Browse files

Initial import, not functional or feature complete yet

  • Loading branch information...
mk committed May 11, 2008
0 parents commit 5d9e03aa1f738e273aeca4d0ba7dca3107ec3657
@@ -0,0 +1 @@
+.DS_Store
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Martin Kavalar
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
+
+For more information on reports_as_sparkline, see http://reports_as_sparkline.rubyforge.org
+
+NOTE: Change this information in PostInstall.txt
+You can also delete it if you don't want it.
+
+
@@ -0,0 +1,46 @@
+= reports_as_sparkline
+
+* http://github.com/mk/reports_as_sparkline
+
+== DESCRIPTION:
+
+FIX (describe your package)
+
+== FEATURES/PROBLEMS:
+
+* FIX (list of features or problems)
+
+== SYNOPSIS:
+
+ FIX (code sample of usage)
+
+== REQUIREMENTS:
+
+* edge rails
+
+== INSTALL:
+
+* sudo gem install reports_as_sparkline
+
+== LICENSE:
+
+Copyright (c) 2008 Martin Kavalar
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
+require 'config/requirements'
+require 'config/hoe' # setup Hoe + all gem configuration
+
+Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,73 @@
+require 'reports_as_sparkline/version'
+
+AUTHOR = 'FIXME full name' # can also be an array of Authors
+EMAIL = "FIXME email"
+DESCRIPTION = "description of gem"
+GEM_NAME = 'reports_as_sparkline' # what ppl will type to install your gem
+RUBYFORGE_PROJECT = 'reports_as_sparkline' # The unix name for your project
+HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
+DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
+EXTRA_DEPENDENCIES = [
+# ['activesupport', '>= 1.3.1']
+] # An array of rubygem dependencies [name, version]
+
+@config_file = "~/.rubyforge/user-config.yml"
+@config = nil
+RUBYFORGE_USERNAME = "unknown"
+def rubyforge_username
+ unless @config
+ begin
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
+ rescue
+ puts <<-EOS
+ERROR: No rubyforge config file found: #{@config_file}
+Run 'rubyforge setup' to prepare your env for access to Rubyforge
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
+ EOS
+ exit
+ end
+ end
+ RUBYFORGE_USERNAME.replace @config["username"]
+end
+
+
+REV = nil
+# UNCOMMENT IF REQUIRED:
+# REV = YAML.load(`svn info`)['Revision']
+VERS = ReportsAsSparkline::VERSION::STRING + (REV ? ".#{REV}" : "")
+RDOC_OPTS = ['--quiet', '--title', 'reports_as_sparkline documentation',
+ "--opname", "index.html",
+ "--line-numbers",
+ "--main", "README",
+ "--inline-source"]
+
+class Hoe
+ def extra_deps
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
+ @extra_deps
+ end
+end
+
+# Generate all the Rake tasks
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
+$hoe = Hoe.new(GEM_NAME, VERS) do |p|
+ p.developer(AUTHOR, EMAIL)
+ p.description = DESCRIPTION
+ p.summary = DESCRIPTION
+ p.url = HOMEPATH
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
+ p.test_globs = ["test/**/test_*.rb"]
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
+
+ # == Optional
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
+ #p.extra_deps = EXTRA_DEPENDENCIES
+
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
+ end
+
+CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
+PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
+$hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
+$hoe.rsync_args = '-av --delete --ignore-errors'
+$hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
@@ -0,0 +1,15 @@
+require 'fileutils'
+include FileUtils
+
+require 'rubygems'
+%w[rake hoe newgem rubigen].each do |req_gem|
+ begin
+ require req_gem
+ rescue LoadError
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
+ puts "Installation: gem install #{req_gem} -y"
+ exit
+ end
+end
+
+$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -0,0 +1,3 @@
+# Include hook code here
+require 'reports_as_sparkline'
+require 'asset_tag_helper'
@@ -0,0 +1,33 @@
+require 'active_support'
+require 'active_record'
+#require 'action_view'
+#require 'action_controller'
+
+module ReportsAsSparkline
+ class << self
+ # shortcut for <tt>enable_actionpack; enable_activerecord</tt>
+ def enable
+ enable_actionpack
+ enable_activerecord
+ end
+
+ # mixes in ReportsAsSparkline::ViewHelpers in ActionView::Base
+ def enable_actionpack
+ #return if ActionView::Base.instance_methods.include? 'sparkline_tag'
+ #require 'reports_as_sparkline/view_helpers'
+ #ActionView::Base.class_eval { include ViewHelpers }
+ end
+
+ def enable_activerecord
+ return if false
+ require 'reports_as_sparkline/report'
+ ActiveRecord::Base.send(:include, ReportsAsSparkline)
+ end
+ end
+end
+
+if defined?(ActiveRecord)
+ ReportsAsSparkline.enable
+else
+ raise "Could not find ActiveRecord"
+end
@@ -0,0 +1,181 @@
+module ReportsAsSparkline #:nodoc:
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ class InvalidGroupExpception < Exception
+ end
+
+ class InvalidOperationExpception < Exception
+ end
+
+ class ReportingGroup
+ @@ranges = [:month, :week, :day, :hour]
+
+ attr_reader :group
+
+ def initialize(range)
+ raise Kvlr::ReportsAsSparkline::InvalidGroupExpception unless @@ranges.include?(range.to_sym)
+ @group = range.to_sym
+ end
+
+ def group_sql(attribute)
+ attribute = attribute.to_s
+ raise "No date_column given" if attribute.blank?
+ case @group
+ when :day
+ group_by = "DATE(#{attribute})"
+ when :week
+ group_by = "YEARWEEK(#{attribute})"
+ when :month
+ group_by = "YEAR(#{attribute}) * 100 + MONTH(#{attribute})"
+ when :hour
+ group_by = "DATE(#{attribute}) + HOUR(#{attribute})"
+ end
+ group_by
+ end
+
+ def latest_datetime
+ case @group
+
+ when :day, :week, :month
+ return 1.send(@group).ago.to_date.to_datetime
+ when :hour
+ return 1.day.ago.to_date.to_datetime
+ end
+ end
+
+ def self.default
+ :day
+ end
+ end
+
+ class ReportingOperation
+ @@operations = [:count, :sum]
+
+ attr_reader :operation
+
+ def initialize(op)
+ raise Kvlr::ReportsAsSparkline::InvalidOperationExpception unless @@operations.include?(op.to_sym)
+ @operation = op.to_sym
+ end
+
+ def self.default
+ :count
+ end
+ end
+
+
+ class Report
+
+ @@default_statement_options = {:limit => 100, :operation => ReportingOperation.default, :group => ReportingGroup.default, :date_column => 'created_at'}
+ attr_reader :name, :operation, :date_column, :value_column, :graph_options, :statement_options, :reporting_group
+
+ def initialize(name, options)
+ @name = name.to_sym
+ @value_column = (options[:value_column] || @name).to_sym
+ @statement_options = @@default_statement_options.merge options
+
+ @reporting_group = ReportingGroup.new(@statement_options[:group])
+ @reporting_operation = ReportingOperation.new(@statement_options[:operation])
+ end
+
+ def report(klass, options)
+ statement_options = options.merge(@statement_options)
+ reporting_group = statement_options[:group] != @reporting_group.group ? ReportingGroup.new(statement_options[:group]) : @reporting_group
+ reporting_operation = statement_options[:operation] != @reporting_operation.operation ? ReportingOperation.new(statement_options[:operation]) : @reporting_operation
+
+ conditions = ["model_name = ? AND report_name = ? AND report_range = ?", klass.to_s, name.to_s, reporting_group.group.to_s]
+ newest_report = ReportCache.find(:first, :select => "start, value", :conditions => conditions, :order => "start DESC")
+ newest_value = reporting_group.latest_datetime
+ if newest_report.nil? or newest_report.start < newest_value
+ value_statement = nil
+ case reporting_operation.operation
+ when :sum
+ value_statement = "SUM(#{@value_column})"
+ when :count
+ value_statement = "COUNT(1)"
+ end
+ raise if value_statement.nil?
+
+ where = ["#{reporting_group.group_sql(statement_options[:date_column])} <= \"#{newest_value.to_formatted_s(:db)}\""]
+ where << "#{reporting_group.group_sql(statement_options[:date_column])} > \"#{(newest_report.start).to_formatted_s(:db)}\"" unless newest_report.nil?
+ where = where.join(" AND ")
+
+ query = "INSERT INTO #{ReportCache.table_name} (model_name, report_name, report_range, start, value)
+ (
+ SELECT \"#{klass.to_s}\", \"#{name}\", \"#{reporting_group.group.to_s}\",
+ #{reporting_group.group_sql(statement_options[:date_column])} AS start,
+ #{value_statement} AS value
+ FROM #{klass.table_name}
+ WHERE #{where}
+ GROUP BY start
+ );"
+ ActiveRecord::Base.connection.execute query
+ end
+ data = ReportCache.find(:all, :select => "start, value", :conditions => conditions, :order => "start DESC", :limit => statement_options[:limit])
+ data.collect! {|report| [report.start, report.value] }
+ data.reverse
+ end
+
+ # def generate_report(klass, options)
+ #
+ #
+ # case reporting_operation.operation
+ # when :sum
+ # return klass.sum @value_column, :group => @reporting_group.group_sql(@statement_options[:date_column])
+ # when :count
+ # return klass.count :group => @reporting_group.group_sql(@statement_options[:date_column])
+ # end
+ # end
+ end
+
+
+ class CumulateReport < Kvlr::ReportsAsSparkline::Report
+
+ def report(klass, options)
+ CumulateReport.cumulate!(super(klass, options))
+ end
+
+ protected
+ def self.cumulate!(data)
+ last_item = 0
+ data.collect{ |element|
+ last_item += element[1].to_i
+ [element[0], last_item]
+ }
+ end
+
+ end
+
+ end
+
+ module ClassMethods
+ #
+ # Examples:
+ #
+ # class Game < ActiveRecord::Base
+ # report_as_sparkline :games_per_day
+ # report :games_played_total, :cumulate => :games_played
+ # end
+ # class User < ActiveRecord::Base
+ # report_as_sparkline :registrations, :operation => :count
+ # report_as_sparkline :activations, :date_column => :activated_at, :operation => :count
+ # report_as_sparkline :total_users_report, :cumulate => :registrations
+ # end
+ # class Rake < ActiveRecord::Base
+ # report_as_sparkline :rake, :operation => :sum
+ # end
+ def report_as_sparkline(name, options = {})
+ report = options[:cumulate] ? Kvlr::ReportsAsSparkline::CumulateReport.new(options[:cumulate], options) : Kvlr::ReportsAsSparkline::Report.new(name, options)
+ (class << self; self; end).instance_eval {
+ define_method "#{name.to_s}_report".to_sym do |*args|
+ raise ArgumentError if args.size > 1
+ options = args.first || {}
+ raise ArgumentError unless options.is_a?(Hash)
+ report.report(self, options)
+ end
+ }
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 5d9e03a

Please sign in to comment.