module Eco::API::UseCases::GraphQL::Helpers::Location module Command include Eco::Language::AuxiliarLogger include Eco::API::UseCases::GraphQL::Helpers::Location::Base require_relative 'command/optimizations' include Eco::API::UseCases::GraphQL::Helpers::Location::Command::Optimizations # Actual GraphQL request def apply_commands(input, &block) graphql.locationStructure.applyCommands(input: input, &block) 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 # @param track_tree_mode [Symbol] when should updates happen # @return see #with_sliced_input def sliced_batches( batch_input, size: commands_per_page, desc: :input, track_tree_mode: default_tree_tracking_mode ) 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 curr_block = scope_commands_block(page, pages, track_tree_mode: track_tree_mode) response = apply_commands(sliced_input, &curr_block) 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' require_relative 'command/diff' require_relative 'command/diffs'