Class: MetricFu::Metric

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetric

Returns a new instance of Metric.



9
10
11
12
13
# File 'lib/metric_fu/metric.rb', line 9

def initialize
  self.enabled = false
  @libraries = Set.new
  @configured_run_options = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (protected)

Enable using a syntax such as metric.foo = ‘foo’

by catching the missing method here,
checking if :foo is a key in the default_run_options, and
setting the key/value in the @configured_run_options hash

TODO: See if we can do this without a method_missing



110
111
112
113
114
115
116
117
# File 'lib/metric_fu/metric.rb', line 110

def method_missing(method, *args)
  key = method_to_attr(method)
  if default_run_options.has_key?(key)
    configured_run_options[key] = args.first
  else
    raise "#{key} is not a valid configuration option"
  end
end

Instance Attribute Details

#activatedObject

Returns the value of attribute activated.



7
8
9
# File 'lib/metric_fu/metric.rb', line 7

def activated
  @activated
end

#enabledObject

Returns the value of attribute enabled.



7
8
9
# File 'lib/metric_fu/metric.rb', line 7

def enabled
  @enabled
end

Class Method Details

.enabled_metricsObject



91
92
93
# File 'lib/metric_fu/metric.rb', line 91

def self.enabled_metrics
  metrics.select { |metric| metric.enabled && metric.activated }.sort_by { |metric| metric.name  == :hotspots ? 1 : 0 }
end

.get_metric(name) ⇒ Object



95
96
97
# File 'lib/metric_fu/metric.rb', line 95

def self.get_metric(name)
  metrics.find { |metric|metric.name.to_s == name.to_s }
end

.metricsObject

ensure :hotspots runs last

Returns:

  • all subclassed metrics [Array<MetricFu::Metric>]



87
88
89
# File 'lib/metric_fu/metric.rb', line 87

def self.metrics
  @metrics
end

Instance Method Details

#activateObject

TODO: Confirm this catches load errors from requires in subclasses, such as for flog



20
21
22
23
24
25
26
# File 'lib/metric_fu/metric.rb', line 20

def activate
  MetricFu.metrics_require { default_metric_library_paths }
  @libraries.each { |library| require(library) }
  self.activated = true
rescue LoadError => e
  mf_log "#{name} metric not activated, #{e.message}"
end

#configured_run_optionsObject



70
71
72
# File 'lib/metric_fu/metric.rb', line 70

def configured_run_options
  @configured_run_options
end

#default_run_argsObject



42
43
44
# File 'lib/metric_fu/metric.rb', line 42

def default_run_args
  run_options.map { |k, v| "--#{k} #{v}" }.join(" ")
end

#default_run_optionsHash

Returns default metric run options.

Returns:

  • (Hash)

    default metric run options



75
76
77
# File 'lib/metric_fu/metric.rb', line 75

def default_run_options
  not_implemented
end

#enableObject



15
16
17
# File 'lib/metric_fu/metric.rb', line 15

def enable
  self.enabled = true
end

#gem_nameObject



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

def gem_name
  name
end

#has_graph?Hash

Returns metric_options.

Returns:

  • (Hash)

    metric_options



80
81
82
# File 'lib/metric_fu/metric.rb', line 80

def has_graph?
  not_implemented
end

#nameObject

Returns metric name [Symbol].

Returns:

  • metric name [Symbol]



29
30
31
# File 'lib/metric_fu/metric.rb', line 29

def name
  not_implemented
end

#runObject



46
47
48
# File 'lib/metric_fu/metric.rb', line 46

def run
  not_implemented
end

#run_external(args = default_run_args) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/metric_fu/metric.rb', line 50

def run_external(args = default_run_args)
  runner = GemRun.new(
    gem_name: gem_name.to_s,
    metric_name: name.to_s,
    # version: ,
    args: args,
  )
  stdout, stderr, status = runner.run
  # TODO: do something with the stderr
  # for now, just acknowledge we got it
  unless stderr.empty?
    STDERR.puts "STDERR from #{gem_name}:\n#{stderr}"
  end
  # TODO: status.success? is not reliable for distinguishing
  # between a successful run of the metric and problems
  # found by the metric. Talk to other metrics about this.
  MetricFu.logger.debug "#{gem_name} ran with #{status.success? ? 'success' : 'failure'} code #{status.exitstatus}"
  stdout
end

#run_optionsObject

Returns metric run options [Hash].

Returns:

  • metric run options [Hash]



38
39
40
# File 'lib/metric_fu/metric.rb', line 38

def run_options
  default_run_options.merge(configured_run_options)
end