module Eco::API::UseCases::GraphQL::Helpers::Location module Command include Eco::Language::AuxiliarLogger include Eco::API::UseCases::GraphQL::Helpers::Location::Base DEFAULT_COMMANDS_PER_PAGE = 45 DEFAULT_FORCE_CONTINUE = false # Prevents each request from timing out def commands_per_page if self.class.const_defined?(:COMMANDS_PER_PAGE) self.class::COMMANDS_PER_PAGE else DEFAULT_COMMANDS_PER_PAGE end end # Whether to stop or continue on command fail def force_continue? if self.class.const_defined?(:FORCE_CONTINUE) self.class::FORCE_CONTINUE else DEFAULT_FORCE_CONTINUE end end # With given the commands, it generates the input of the endpoint mutation. # @param commands [Array] def input(commands, force_continue: force_continue?) { clientMutationId: "", id: target_structure_id, force: force_continue, commands: commands } end # @return see #with_sliced_input def sliced_batches(batch_input, size: commands_per_page, desc: :input, logging: true) dry_run_msg = simulate? ? '(dry-run) ' : '' if batch_input[:commands].empty? msg = "#{dry_run_msg}No commands for '#{desc}'." msg << " Skipping batch..." unless simulate? log(:info) { msg } return end done = 0 with_sliced_input(batch_input, size: size) do |sliced_input, page, pages, count, total| msg = "#{dry_run_msg}Launching '#{desc}' request #{page} (of #{pages}) " msg << "with #{count} commands (done #{done} of #{total})..." logger.info { msg } response = nil unless simulate? && !options.dig(:requests, :backup) backup(sliced_input, type: "tree_update_#{desc}_request_#{page}_of_#{pages}") end if simulate? log(:info) { sliced_input.pretty_inspect } if page < 3 else response = graphql.locationStructure.applyCommands(input: sliced_input) backup(response, type: "tree_update_#{desc}_response_#{page}_of_#{pages}") end done += count yield(sliced_input, response, page, pages, done, total) if block_given? end end # @param input_data [Hash] input for the endpoint `mutation.ApplyCommandsToLocationStructure`. # @return [Array] pairs of `sliced_input` and `response` thereof. def with_sliced_input(input_data, size: commands_per_page) comms = input_data[:commands] total = comms.count pages = (total.to_f / size).ceil.to_i page = 1; out = [] comms.each_slice(size) do |comms_slice| sliced_input = input_data.slice(:clientMutationId, :id).merge(commands: comms_slice) yield(sliced_input, page, pages, comms_slice.count, total).tap do |response| out.push([sliced_input, response]) page += 1 end end out end end end require_relative 'command/result' require_relative 'command/results'