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

Included in:
Eco::API::UseCases::GraphQL::Helpers::Location::Command
Defined in:
lib/eco/api/usecases/graphql/helpers/location/command/optimizations.rb

Constant Summary collapse

DEFAULT_COMMANDS_PER_PAGE =
45
DEFAULT_FORCE_CONTINUE =
false

Instance Method Summary collapse

Instance Method Details

#commands_payload_without_structure_blockObject

Note:

this servces the purpose of optimizing/speeding up the requests.

Commands payload without querying Structure



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/eco/api/usecases/graphql/helpers/location/command/optimizations.rb', line 54

def commands_payload_without_structure_block
  proc {
    clientMutationId
    error { # rubocop:disable Style/BlockDelimiters
      message
      conflictingIds
      validationErrors { # rubocop:disable Style/BlockDelimiters
        error
        message
      }
    }
    results { # rubocop:disable Style/BlockDelimiters
      command { # rubocop:disable Style/BlockDelimiters
        id
        state
        __typename
      }
      ok
      error { # rubocop:disable Style/BlockDelimiters
        conflictingIds
        message
        validationErrors { # rubocop:disable Style/BlockDelimiters
          error
          message
        }
      }
    }
  }
end

#commands_per_pageObject

Prevents each request from timing out



35
36
37
38
39
40
41
# File 'lib/eco/api/usecases/graphql/helpers/location/command/optimizations.rb', line 35

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

#default_tree_tracking_modeObject

Available options are:

  • :per_request -> on each request
  • :per_batch -> at the end of each batch / stage (on last page)
  • :once -> only when the script starts to run


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

def default_tree_tracking_mode
  :per_request
end

#force_continue?Boolean

Whether to stop or continue on command fail

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/eco/api/usecases/graphql/helpers/location/command/optimizations.rb', line 44

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

#scope_commands_block(idx, total, track_tree_mode: default_tree_tracking_mode) ⇒ Proc, NilClass

Note:
  1. Gives flexibility on at what time the structure should be retrieved
  2. This increases performacne, as we don't retrieve the full tree on each request unless necessary/specified
  3. nil falls the block back to the ecoportal-api-graphql gem, which has a default block that retrieves the structure

Helper to identify the commands payload block

Returns:

  • (Proc, NilClass)

    the payload block



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eco/api/usecases/graphql/helpers/location/command/optimizations.rb', line 22

def scope_commands_block(idx, total, track_tree_mode: default_tree_tracking_mode)
  case track_tree_mode
  when :per_request
    nil
  when :once
    commands_payload_without_structure_block
  when :per_batch
    return nil if idx == total
    commands_payload_without_structure_block
  end
end