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

Includes:
Base, 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

Classes: Result, Results

Constant Summary collapse

DEFAULT_COMMANDS_PER_PAGE =
45
DEFAULT_FORCE_CONTINUE =
false

Constants included from Base

Base::TAGTREE_BACKUP

Instance Attribute Summary

Attributes included from Base

#current_tree, #previous_tree

Attributes included from Base::CaseEnv

#options, #session

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Base

#backup_tree, #live_tree, #tagtree_id, #target_structure_id, #track_current_tree

Methods included from Base

#backup, #exit_error, #graphql

Methods included from Base::CaseEnv

#config, #simulate?

Methods included from Language::AuxiliarLogger

#log

Instance Method Details

#commands_per_pageObject

Prevents each request from timing out



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

def commands_per_page
  if self.class.const_defined?(:COMMANDS_PER_PAGE)
    self.class::COMMANDS_PER_PAGE
  else
    DEFAULT_COMMANDS_PER_PAGE
  end
end

#force_continue?Boolean

Whether to stop or continue on command fail

Returns:

  • (Boolean)


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

def force_continue?
  if self.class.const_defined?(:FORCE_CONTINUE)
    self.class::FORCE_CONTINUE
  else
    DEFAULT_FORCE_CONTINUE
  end
end

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

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

Parameters:

  • commands (Array<Hash>)


29
30
31
32
33
34
35
36
# File 'lib/eco/api/usecases/graphql/helpers/location/command.rb', line 29

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, logging: true) ⇒ Object

Returns see #with_sliced_input.

Returns:

  • see #with_sliced_input



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
65
66
67
68
69
70
# File 'lib/eco/api/usecases/graphql/helpers/location/command.rb', line 39

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

#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.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/eco/api/usecases/graphql/helpers/location/command.rb', line 74

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