Module: Eco::API::UseCases::GraphQL::Helpers::Location::Command

Includes:
Base, Optimizations, Language::AuxiliarLogger
Included in:
Samples::Location::Command::DSL
Defined in:
lib/eco/api/usecases/graphql/helpers/location/command.rb,
lib/eco/api/usecases/graphql/helpers/location/command/result.rb,
lib/eco/api/usecases/graphql/helpers/location/command/results.rb

Defined Under Namespace

Modules: Optimizations Classes: Diff, Diffs, Result, Results

Constant Summary

Constants included from Optimizations

Optimizations::DEFAULT_COMMANDS_PER_PAGE, Optimizations::DEFAULT_FORCE_CONTINUE

Constants included from Base::TreeTracking

Base::TreeTracking::TAGTREE_BACKUP

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Attributes included from Base::TreeTracking

#current_tree, #previous_tree

Instance Method Summary collapse

Methods included from Optimizations

#commands_payload_without_structure_block, #commands_per_page, #default_tree_tracking_mode, #force_continue?, #scope_commands_block

Methods included from Base

#live_tree, #session_live_tree, #tagtree_id, #target_structure_id, #target_structure_id_const

Methods included from Language::AuxiliarLogger

#log

Methods included from Base::TreeTracking

#backup_tree, #track_current_tree, #track_current_tree?

Instance Method Details

#apply_commands(input, &block) ⇒ Object

Actual GraphQL request



10
11
12
# File 'lib/eco/api/usecases/graphql/helpers/location/command.rb', line 10

def apply_commands(input, &block)
  graphql.locationStructure.applyCommands(input: input, &block)
end

#input(commands, force_continue: force_continue?) ) ⇒ Object

With given the commands, it generates the input of the endpoint mutation.

Parameters:

  • commands (Array<Hash>)


16
17
18
19
20
21
22
23
# File 'lib/eco/api/usecases/graphql/helpers/location/command.rb', line 16

def input(commands, force_continue: force_continue?)
  {
    clientMutationId: "",
    id:               target_structure_id,
    force:            force_continue,
    commands:         commands
  }
end

#sliced_batches(batch_input, size: commands_per_page, desc: :input, track_tree_mode: default_tree_tracking_mode) ⇒ Object

Returns see #with_sliced_input.

Parameters:

  • track_tree_mode (Symbol) (defaults to: default_tree_tracking_mode)

    when should updates happen

Returns:

  • see #with_sliced_input



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/eco/api/usecases/graphql/helpers/location/command.rb', line 27

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})..."
    log(: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

#with_sliced_input(input_data, size: commands_per_page) ⇒ Array<Array>

Returns pairs of sliced_input and response thereof.

Parameters:

  • input_data (Hash)

    input for the endpoint mutation.ApplyCommandsToLocationStructure.

Returns:

  • (Array<Array>)

    pairs of sliced_input and response thereof.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/eco/api/usecases/graphql/helpers/location/command.rb', line 68

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