Module: Eco::API::UseCases::GraphQL::Samples::Location::Command::Results

Includes:
Helpers::Location::Base
Included in:
DSL
Defined in:
lib/eco/api/usecases/graphql/samples/location/command/results.rb

Overview

Logic to:

  1. Track-down results and errors
  2. Create tags remap csv table batch design

Constant Summary

Constants included from Helpers::Location::Base

Helpers::Location::Base::TAGTREE_BACKUP

Instance Attribute Summary collapse

Attributes included from Helpers::Location::Base

#current_tree, #previous_tree

Attributes included from Helpers::Base

#options, #session

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Helpers::Location::Base

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

Methods included from Helpers::Base

#backup, #config, #exit_error, #graphql, #simulate?

Methods included from Language::AuxiliarLogger

#log

Instance Attribute Details

#errorObject

Returns the value of attribute error.



8
9
10
# File 'lib/eco/api/usecases/graphql/samples/location/command/results.rb', line 8

def error
  @error
end

#exceptionObject

Returns the value of attribute exception.



8
9
10
# File 'lib/eco/api/usecases/graphql/samples/location/command/results.rb', line 8

def exception
  @exception
end

#tags_remap_csv_fileObject

Returns the value of attribute tags_remap_csv_file.



9
10
11
# File 'lib/eco/api/usecases/graphql/samples/location/command/results.rb', line 9

def tags_remap_csv_file
  @tags_remap_csv_file
end

Instance Method Details

#generate_tags_remap_csv(filename = "cache/remap_tags.csv") ⇒ Object

Generates the final tags remap file



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/eco/api/usecases/graphql/samples/location/command/results.rb', line 106

def generate_tags_remap_csv(filename = "cache/remap_tags.csv")
  return nil if tags_remap_table.empty?
  timestamp_file(filename).tap do |file|
    CSV.open(file, 'w') do |csv|
      csv << ["source_tags", "destination_tags"]
      tags_remap_table.each do |(src_tags, dst_tags)|
        csv << [src_tags.join('|'), dst_tags.join('|')]
      end
    end
    log(:info) { "Generated file '#{file}'" }
  end
end

#page_errors?(page_results, page, pages, done, total, stage: nil) ⇒ Boolean

Note:

it gives feedback on where an error has occurred.

Errors tracking/logging.

Parameters:

  • page_results (Eco::API::UseCases::GraphQL::Helpers::Locations::Commands::CommandResults)
  • stage (Symbol) (defaults to: nil)

    used when we launch an update in different phases (i.e. rename, move, etc.)

Returns:

  • (Boolean)

    whether or not there was an error



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
# File 'lib/eco/api/usecases/graphql/samples/location/command/results.rb', line 37

def page_errors?(page_results, page, pages, done, total, stage: nil)
  raise "Expecting CommandResults object. Given: #{page_results.class}" unless page_results.is_a?(request_results_class)
  stage_str   = stage ? "'#{stage}' " : ''
  fingerprint = "#{stage_str}#{page} (of #{pages})"
  errored     = false

  if page_results.error?
    errored = true
    log(:error) { "Error on #{fingerprint}: #{page_results.error.doc.pretty_inspect}" }
  end

  if page_results.applied?
    log(:info) { "Success on #{fingerprint}: #{done} (of #{total}) commands applied!" }
  elsif page_results.errored?
    errored = true
    msg     = "Some command failed on #{fingerprint}:\n#{page_results.stats}"
    unless force_continue?
      first_errored = page_results.first_errored
      msg << "The error(s) - #{first_errored.error_msg}\n"
    end
    log(:error) { msg }
  end

  errored
end

#request_results_classObject



17
18
19
# File 'lib/eco/api/usecases/graphql/samples/location/command/results.rb', line 17

def request_results_class
  Eco::API::UseCases::GraphQL::Helpers::Location::Command::Results
end

#rescuedObject



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

def rescued
  yield
rescue StandardError => e
  log(:error) { self.exception ||= e.patch_full_message }
end

#resultsObject

Capture results



22
23
24
# File 'lib/eco/api/usecases/graphql/samples/location/command/results.rb', line 22

def results
  @results ||= {}
end

#tags_remap_tableArray<Array>

The maps of tags to be used in batch remap tags

Returns:

  • (Array<Array>)

    source/destination pairs of Array<String>



28
29
30
# File 'lib/eco/api/usecases/graphql/samples/location/command/results.rb', line 28

def tags_remap_table
  @tags_remap_table ||= []
end

#timestamp_file(filename, enviro_relative: true) ⇒ Object

Makes the file relative to the enviro



120
121
122
123
# File 'lib/eco/api/usecases/graphql/samples/location/command/results.rb', line 120

def timestamp_file(filename, enviro_relative: true)
  filename = session.file_manager.dir.file(filename) if enviro_relative
  Eco::Data::Files.timestamp_file(filename)
end

#update_tags_remap_table(results, stage) ⇒ Object

Note:
  1. This requires to have available the current_tree locations structure
    • Fortunatelly this is being tracked, as it is returned as payload of the response.
  2. Based on the assumption that the order of the commands (stages) happens like this:
    • :unarchive, :id_name, :insert, :move, :archive
  3. The only update operations that generate tag remaps are :id (or :id_name) and :move.

Based on commands that succeded, and the batch stage, it tracks the tag remaps that should be batches against existing pages



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/eco/api/usecases/graphql/samples/location/command/results.rb', line 71

def update_tags_remap_table(results, stage)
  return false if [:unarchive, :archive].include?(stage)
  raise "Expecting CommandResults object. Given: #{results.class}" unless results.is_a?(request_results_class)
  results.applied.each do |result|
    case stage
    when :id, :id_name
      prev_id, curr_id = result.command_input_data.values_at(:nodeId, :newId)

      unless current_tree.tag?(curr_id)
        msg  = "Node '#{prev_id}' was updated to '#{curr_id}', "
        msg << "but in current structure '#{curr_id}' is not present"
        log(:warn) { msg }
      end

      tags_remap_table << [[prev_id], [curr_id]]
    when :move
      node_id, parent_id = result.command_input_data.values_at(:nodeId, :parentId)
      prev_node = previous_tree.node(node_id)
      curr_node = current_tree.node(node_id)
      lost_tags = prev_node.path - curr_node.path
      new_tags  = curr_node.path - prev_node.path

      curr_parent = curr_node.parent.top? ? nil : curr_node.parent
      unless curr_parent&.id == parent_id
        msg  = "Node '#{node_id}' was moved uner '#{parent_id}', "
        msg << "but in current structure has parent '#{curr_parent&.id}'"
        log(:warn) { msg }
      end

      tags_remap_table << [lost_tags.unshift(node_id), new_tags.unshift(node_id)]
    end
  end
end