module Eco::API::UseCases::GraphQL::Helpers::Location::Command class Results attr_reader :input, :response def initialize(input, response) @input = input @response = response end def stats msg = '' msg << " • Errored: #{errored.count} #{first_err_str}\n" if errored? msg << " • Applied: #{errored.count} #{last_okay_str}\n" if some_applied? msg << " • Pending: #{pending.count}\n" if some_pending? msg end # Was this configured to force-continue on command error? def force? input[:force] end # # Offers a summary. If anything went wrong, it's `false`. # # If everything went right, it's `true`. # def ok? # response&.ok # end # Overal errors (i.e. ID clashes between different structures) def error response&.error end def error? !!error end def success? !error? && results.all?(&:success?) end def results @results ||= input_commands.zip(response_results).each_with_object([]) do |(i, r), results| results << Result.new(i, r) end end def count results.count end def input_result(input) results_by_input[input] end def input_idx(input) results.index(input_result(input)) end def idx(result) results.index(result) end def errored @errored ||= results.select(&:error?) end def errored? errored.any? end def first_errored errored.first end def first_errored_idx idx(first_errored) end def applied @applied ||= results.select(&:applied?) end def applied? results.all?(&:applied?) end def some_applied? applied.count.positive? end def last_applied applied.last end def last_applied_idx idx(last_applied) end def pending @pending ||= results.select(&:pending?) end def some_pending? !pending.empty? end private def results_by_input @results_by_input ||= results.each_with_object({}) do |r, h| h[r.input] = r end end def input_commands input[:commands] end def response_results response&.results || [] end # First error is relevant only if request was NOT forced to continue. def first_err_str return '' if force? pre_str = 'stopped on node' pre_str = 'first error' unless force? "(#{pre_str}: '#{first_errored&.node_id}' - idx: #{first_errored_idx})" end # Last successfully applied is relevant only if request was NOT forced to continue. def last_okay_str return '' if force? "(last node done: '#{last_applied&.node_id}' - idx: #{last_applied_idx})" end end end