每天一剂Rails良药之Make Pretty Graphs

rake stats可以生成统计代码行数,类的个数,方法的个数等信息,今天我们看看怎样生成rake stats的饼状图。

1,安装ImageMagick
什么新立得包管理器装一下或者apt-get装一下或者下载binary包安装

2,安装RMagick
sudo gem install rmagick


3,安装Gruff
sudo gem install gruff


4,写生成饼状stats图的controller
class GraphController < ApplicationController

require 'gruff'

  STATS_DIRECTORIES = [
    %w(Helpers              app/helpers),
    %w(Controllers          app/controllers),
    %w(APIs                 app/apis),
    %w(Components           components),
    %w(Functional\ tests    test/functional),
    %w(Models               app/models),
    %w(Unit\ tests          test/unit),
    %w(Libraries            lib/),
    %w(Integration\ tests test/integration)
  ].collect { |name, dir|
      [ name, "#{RAILS_ROOT}/#{dir}" ]
    }.select { |name, dir|
      File.directory?(dir)
    }
  
  def stats
    code_stats = CodeStatistics.new(*STATS_DIRECTORIES)
    statistics = code_stats.instance_variable_get(:@statistics)
    g = Gruff::Pie.new(500)
    g.font = "/Library/Fonts/Arial"
    g.title = "Code Stats"
    g.theme_37signals
    g.legend_font_size = 10
    0xFDD84E.step(0xFF0000, 1500) do |num|
      g.colors << "#%x"   % num
    end
    statistics.each do |key, values|
      g.data(key, [values["codelines"]])
    end
    send_data(g.to_blob,
                 :disposition => ' inline' ,
                 :type => ' image/png' ,
                 :filename => "code_stats.png")
  end

end

访问GraphController的stats方法即可。
但是页面会报错:
NameError in GraphController#stats 
uninitialized constant GraphController::CodeStatistics
RAILS_ROOT: ./script/../config/..

Application Trace | Framework Trace | Full Trace 
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:477:in `const_missing'
#{RAILS_ROOT}/app/controllers/graph_controller.rb:22:in `stats'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:477:in `const_missing'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:1095:in `send'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:1095:in `perform_action_without_filters'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:632:in `call_filter'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:619:in `perform_action_without_benchmark'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/rescue.rb:83:in `perform_action'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:in `send'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:in `process_without_filters'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:624:in `process_without_session_management_support'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/session_management.rb:114:in `process'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:330:in `process'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/dispatcher.rb:41:in `dispatch'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:78:in `process'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/1.8/thread.rb:135:in `synchronize'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:76:in `process'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:618:in `process_client'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in `each'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in `process_client'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `initialize'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `new'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `initialize'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `new'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:271:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in `each'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:127:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/command.rb:211:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:243
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:488:in `load'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:488:in `load'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:342:in `new_constants_in'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:488:in `load'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/servers/mongrel.rb:60
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:495:in `require'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:342:in `new_constants_in'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:495:in `require'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/server.rb:39
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
script/server:3
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:477:in `const_missing'
#{RAILS_ROOT}/app/controllers/graph_controller.rb:22:in `stats'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:1095:in `send'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:1095:in `perform_action_without_filters'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:632:in `call_filter'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:619:in `perform_action_without_benchmark'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/benchmarking.rb:66:in `perform_action_without_rescue'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/rescue.rb:83:in `perform_action'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:in `send'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:430:in `process_without_filters'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/filters.rb:624:in `process_without_session_management_support'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/session_management.rb:114:in `process'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/base.rb:330:in `process'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/dispatcher.rb:41:in `dispatch'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:78:in `process'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/1.8/thread.rb:135:in `synchronize'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:76:in `process'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:618:in `process_client'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in `each'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in `process_client'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `initialize'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `new'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `initialize'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `new'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:271:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in `each'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:127:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/command.rb:211:in `run'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:243
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:488:in `load'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:488:in `load'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:342:in `new_constants_in'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:488:in `load'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/servers/mongrel.rb:60
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:495:in `require'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:342:in `new_constants_in'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.1/lib/active_support/dependencies.rb:495:in `require'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.2/lib/commands/server.rb:39
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
D:/InstantRails-1.6-win/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
script/server:3
Request
Parameters: None

Show session dump

--- 
flash: !map:ActionController::Flash::FlashHash {}


Response
Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"}

不管是Windows还是Linux下均报这个错,不知道怎么回事。

你可能感兴趣的:(linux,Flash,Ruby,Rails,rubygems)