Sha256: 21e0e8b0517619e057dab22fc928c4b9e1bfb82daa593e88427da3820e0f48d9

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

require 'multi_json'
module MetricFu
  class Grapher

    @graphers = []
    # @return all subclassed graphers [Array<MetricFu::Grapher>]
    def self.graphers
      @graphers
    end

    def self.inherited(subclass)
      @graphers << subclass
    end

    def self.get_grapher(metric)
      graphers.find{|grapher|grapher.metric.to_s == metric.to_s}
    end

    BLUFF_GRAPH_SIZE = "1000x600"
    BLUFF_DEFAULT_OPTIONS = <<-EOS
      var g = new Bluff.Line('graph', "#{BLUFF_GRAPH_SIZE}");
      g.theme_37signals();
      g.tooltips = true;
      g.title_font_size = "24px"
      g.legend_font_size = "12px"
      g.marker_font_size = "10px"
    EOS

    attr_accessor :output_directory

    def initialize(opts = {})
      self.output_directory = opts[:output_directory]
    end

    def output_directory
      @output_directory || MetricFu::Io::FileSystem.directory('output_directory')
    end

    def get_metrics(metrics, sortable_prefix)
      not_implemented
    end

    def graph!
      title = send(:title)
      data = send(:data)
      labels = MultiJson.dump(@labels)
      output_filename = send(:output_filename)
      content = <<-EOS
        #{BLUFF_DEFAULT_OPTIONS}
        g.title = '#{title}';
        #{build_data(data)}
        g.labels = #{labels};
        g.draw();
      EOS
      File.open(File.join(self.output_directory, output_filename), 'w') {|f| f << content }
    end

    def title
      not_implemented
    end

    def date
      not_implemented
    end

    def output_filename
      not_implemented
    end

    private

    def build_data(data)
      Array(data).map do |label, datum|
        "g.data('#{label}', [#{datum}]);"
      end.join("\n")
    end

    def not_implemented
      raise "#{__LINE__} in #{__FILE__} from #{caller[0]}"
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
metric_fu-4.8.0 lib/metric_fu/reporting/graphs/grapher.rb
metric_fu-4.7.4 lib/metric_fu/reporting/graphs/grapher.rb
metric_fu-4.7.3 lib/metric_fu/reporting/graphs/grapher.rb
metric_fu-4.7.2 lib/metric_fu/reporting/graphs/grapher.rb
metric_fu-4.7.1 lib/metric_fu/reporting/graphs/grapher.rb
metric_fu-4.7.0 lib/metric_fu/reporting/graphs/grapher.rb