Sha256: f4193cc42d092fd15c3f7a9c5082c0448453d6222d9fd378407716a0cfc88b23

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

require 'update_repo/version'
require 'update_repo/helpers'
require 'yaml'

module UpdateRepo
  # Class : Metrics.
  # This class takes care of storing the metrics for processed, failures, etc.
  class Metrics
    include Helpers

    # Constructor for the Metrics class.
    # @return [instance] Instance of the Metrics class
    # def initialize(logger)
    def initialize
      @metrics = { processed: 0, skipped: 0, failed: 0, updated: 0,
                   unchanged: 0, start_time: 0, failed_list: [],
                   warning: 0 }
    end

    # Read the metric 'key'
    # @param key [symbol] the key to read
    # @return [various] Return the value for hash key 'key'
    def [](key)
      @metrics[key]
    end

    # Set the metric 'key' to 'value'
    # @param key [symbol] the key to set
    # @param value [symbol] set 'key' to this value.
    # @return [value] Return the value set.
    def []=(key, value)
      @metrics[key] = value
    end

    # 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.
    # @param config [instance] of Config class
    # @return [void]
    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

    # loads an error file (if exists) into the @metrics[:failed_list].
    # @param config [instance] of Config class
    # @return [void]
    def load_errors(config)
      path = "#{config.config_path}.errors"
      @metrics[:failed_list] = YAML.load_file(path) if File.exist?(path)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
update_repo-0.11.3 lib/update_repo/metrics.rb