Class: MetricFu::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/metric_fu/generator.rb

Overview

Generator

The Generator class is an abstract class that provides the skeleton for producing different types of metrics.

It drives the production of the metrics through a template method - #generate_result(options={}). This method calls #emit, #analyze and #to_h in order to produce the metrics.

To implement a concrete class to generate a metric, therefore, the class must implement those three methods.

  • #emit should take care of running the metric tool and gathering its output.

  • #analyze should take care of manipulating the output from #emit and making it possible to store it in a programmatic way.

  • #to_h should provide a hash representation of the output from #analyze ready to be serialized into yaml at some point.

Pre-conditions

Based on the class name of the concrete class implementing a Generator, the Generator class will create a ‘metric_directory’ named after the metric under the scratch_directory, where any output from the #emit method should go.

It will also create the output_directory if neccessary, and in general setup the directory structure that the MetricFu system expects.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Generator

Returns a new instance of Generator.



35
36
37
# File 'lib/metric_fu/generator.rb', line 35

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



33
34
35
# File 'lib/metric_fu/generator.rb', line 33

def options
  @options
end

#resultObject (readonly)

Returns the value of attribute result.



33
34
35
# File 'lib/metric_fu/generator.rb', line 33

def result
  @result
end

#templateObject (readonly)

Returns the value of attribute template.



33
34
35
# File 'lib/metric_fu/generator.rb', line 33

def template
  @template
end

Class Method Details

.generatorsObject

Returns all subclassed generators [Array<MetricFu::Generator>].

Returns:

  • all subclassed generators [Array<MetricFu::Generator>]



49
50
51
# File 'lib/metric_fu/generator.rb', line 49

def self.generators
  @generators
end

.get_generator(metric) ⇒ Object



53
54
55
# File 'lib/metric_fu/generator.rb', line 53

def self.get_generator(metric)
  generators.find { |generator|generator.metric.to_s == metric.to_s.downcase }
end

.metricObject



39
40
41
# File 'lib/metric_fu/generator.rb', line 39

def self.metric
  not_implemented
end

.metric_directoryObject

Returns the directory where the Generator will write any output



62
63
64
65
66
67
68
69
# File 'lib/metric_fu/generator.rb', line 62

def self.metric_directory
  @metric_directory ||=
    MetricFu::Metric.get_metric(metric).run_options[:output_directory] ||
    begin
      metric_directory = MetricFu::Io::FileSystem.scratch_directory(metric)
      MetricFu::Utility.mkdir_p(metric_directory, verbose: false)
    end
end

.not_implementedObject



141
142
143
144
145
146
147
148
# File 'lib/metric_fu/generator.rb', line 141

def self.not_implemented
  raise NotImplementedError.new <<-EOF
    Required method #{caller[0]} not implemented in #{__FILE__}.
    This method must be implemented by a concrete class descending
    from Generator.  See generator class documentation for more
    information.
  EOF
end

Instance Method Details

#analyzeObject

:nodoc:



133
134
135
# File 'lib/metric_fu/generator.rb', line 133

def analyze #:nodoc:
  self.class.not_implemented
end

#emitObject

:nodoc:



129
130
131
# File 'lib/metric_fu/generator.rb', line 129

def emit #:nodoc:
  self.class.not_implemented
end

#generate_resultObject

Provides a template method to drive the production of a metric from a concrete implementation of this class. Each concrete class must implement the three methods that this template method calls: #emit, #analyze and #to_h. For more details, see the class documentation.

This template method also calls before_emit, after_emit… etc. methods to allow extra hooks into the processing methods, and help to keep the logic of your Generators clean.



117
118
119
120
121
122
# File 'lib/metric_fu/generator.rb', line 117

def generate_result
  mf_debug "Executing #{metric}"
  emit
  analyze
  to_h
end

#metricObject



43
44
45
# File 'lib/metric_fu/generator.rb', line 43

def metric
  self.class.metric
end

#metric_configObject



85
86
87
# File 'lib/metric_fu/generator.rb', line 85

def metric_config
  MetricFu::Metric.get_metric(metric)
end

#metric_directoryString

The path of the metric directory this class is using.

Returns:

  • (String)


73
74
75
# File 'lib/metric_fu/generator.rb', line 73

def metric_directory
  self.class.metric_directory
end

#remove_excluded_files(paths, globs_to_remove = MetricFu::Io::FileSystem.file_globs_to_ignore) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/metric_fu/generator.rb', line 77

def remove_excluded_files(paths, globs_to_remove = MetricFu::Io::FileSystem.file_globs_to_ignore)
  files_to_remove = []
  globs_to_remove.each do |glob|
    files_to_remove.concat(Dir[glob])
  end
  paths - files_to_remove
end

#round_to_tenths(decimal) ⇒ Object



124
125
126
127
# File 'lib/metric_fu/generator.rb', line 124

def round_to_tenths(decimal)
  decimal = 0.0 if decimal.to_s.eql?("NaN")
  (decimal * 10).round / 10.0
end

#run!(args) ⇒ Object



89
90
91
# File 'lib/metric_fu/generator.rb', line 89

def run!(args)
  metric_config.run_external(args)
end

#silence_streams(*streams) ⇒ Object

temporarily redirect stderr usage: silence_streams(STDERR) { STDERR.puts “something wrong” }



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/metric_fu/generator.rb', line 95

def silence_streams(*streams)
  on_hold = streams.collect { |stream| stream.dup }
  streams.each do |stream|
    stream.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
    stream.sync = true
  end
  yield
ensure
  streams.each_with_index do |stream, i|
    stream.reopen(on_hold[i])
  end
end

#to_hObject

:nodoc:



137
138
139
# File 'lib/metric_fu/generator.rb', line 137

def to_h #:nodoc:
  self.class.not_implemented
end