Class: UpdateRepo::Metrics

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/update_repo/metrics.rb

Overview

Class : Metrics. This class takes care of storing the metrics for processed, failures, etc.

Instance Method Summary collapse

Methods included from Helpers

#trunc_dir

Constructor Details

#initializeinstance

Constructor for the Metrics class. def initialize(logger)



16
17
18
19
20
# File 'lib/update_repo/metrics.rb', line 16

def initialize
  @metrics = { processed: 0, skipped: 0, failed: 0, updated: 0,
               unchanged: 0, start_time: 0, failed_list: [],
               warning: 0 }
end

Instance Method Details

#[](key) ⇒ various

Read the metric ‘key’

Parameters:

  • key (symbol)

    the key to read

Returns:

  • (various)

    Return the value for hash key ‘key’



25
26
27
# File 'lib/update_repo/metrics.rb', line 25

def [](key)
  @metrics[key]
end

#[]=(key, value) ⇒ value

Set the metric ‘key’ to ‘value’

Parameters:

  • key (symbol)

    the key to set

  • value (symbol)

    set ‘key’ to this value.

Returns:

  • (value)

    Return the value set.



33
34
35
# File 'lib/update_repo/metrics.rb', line 33

def []=(key, value)
  @metrics[key] = value
end

#load_errors(config) ⇒ void

This method returns an undefined value.

loads an error file (if exists) into the @metrics.

Parameters:

  • config (instance)

    of Config class



58
59
60
61
# File 'lib/update_repo/metrics.rb', line 58

def load_errors(config)
  path = config.config_path + '.errors'
  @metrics[:failed_list] = YAML.load_file(path) if File.exist?(path)
end

#save_errors(config) ⇒ void

This method returns an undefined value.

This will save any (git) errors encountered to a file, so they can be reprinted again at a later date. If no errors, then delete any previously existing error file.

Parameters:

  • config (instance)

    of Config class



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/update_repo/metrics.rb', line 42

def save_errors(config)
  # get the location of the config file, we'll use the same dir
  # and base name
  path = config.config_path + '.errors'
  if @metrics[:failed_list].empty?
    # delete any existing  file if we have no errors
    File.delete(path) if File.exist?(path)
  else
    # otherwise save  the errors to file
    File.open(path, 'w') { |file| file.write @metrics[:failed_list].to_yaml }
  end
end