Class: Bootprint::Matrix
- Inherits:
-
Object
- Object
- Bootprint::Matrix
- 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
-
#snapshots ⇒ Object
readonly
Returns the value of attribute snapshots.
Instance Method Summary collapse
- #clean? ⇒ Boolean
- #entries ⇒ Object
-
#initialize(snapshots) ⇒ Matrix
constructor
A new instance of Matrix.
- #outlier_counts ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(snapshots) ⇒ Matrix
Returns a new instance of Matrix.
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
#snapshots ⇒ Object (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
56 57 58 |
# File 'lib/bootprint/matrix.rb', line 56 def clean? entries.empty? end |
#entries ⇒ Object
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_counts ⇒ Object
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_h ⇒ Object
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 |