Class: Bootprint::Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/bootprint/matrix.rb

Overview

Compares two or more snapshots to find consensus values and outlier environments.

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(snapshots) ⇒ Matrix

Returns a new instance of Matrix.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
# File 'lib/bootprint/matrix.rb', line 25

def initialize(snapshots)
  @snapshots = snapshots.to_h.transform_keys(&:to_s).transform_values do |snapshot|
    snapshot.is_a?(Snapshot) ? snapshot.semantic_data : snapshot
  end.freeze
  raise ArgumentError, "matrix requires at least two named snapshots" if @snapshots.length < 2
end

Instance Attribute Details

#snapshotsObject (readonly)

Returns the value of attribute snapshots.



23
24
25
# File 'lib/bootprint/matrix.rb', line 23

def snapshots
  @snapshots
end

Instance Method Details

#clean?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/bootprint/matrix.rb', line 56

def clean?
  entries.empty?
end

#entriesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bootprint/matrix.rb', line 32

def entries
  @entries ||= begin
    flattened = snapshots.transform_values { |data| flatten(data) }
    paths = flattened.values.flat_map(&:keys).uniq.sort.reject { |path| ignored?(path) }
    paths.filter_map do |path|
      values = {}
      missing = []
      flattened.each do |name, data|
        data.key?(path) ? values[name] = data[path] : missing << name
      end
      distinct = values.values.map { |value| canonical(value) }.uniq.length
      next if distinct <= 1 && missing.empty?

      consensus = consensus_value(values, snapshots.length)
      outliers = if consensus.nil?
                   snapshots.keys.sort
                 else
                   (values.filter_map { |name, value| name unless value == consensus } + missing).sort
                 end
      Entry.new(path:, values: values.sort.to_h, missing: missing.sort, consensus:, outliers:)
    end
  end
end

#outlier_countsObject



60
61
62
63
64
# File 'lib/bootprint/matrix.rb', line 60

def outlier_counts
  counts = snapshots.keys.to_h { |name| [name, 0] }
  entries.each { |entry| entry.outliers.each { |name| counts[name] += 1 } }
  counts.sort.to_h
end

#to_hObject



66
67
68
69
70
71
72
73
74
# File 'lib/bootprint/matrix.rb', line 66

def to_h
  {
    "schema" => 1,
    "environments" => snapshots.keys.sort,
    "clean" => clean?,
    "outlier_counts" => outlier_counts,
    "entries" => entries.map(&:to_h)
  }
end