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

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

Overview

Logic to: Create tags remap csv table batch design

Constant Summary collapse

REMAP_LOC_IDS_FOLDER =
'cache'.freeze
REMAP_LOC_IDS_FILENAME =
'remap_loc_ids.csv'.freeze

Instance Attribute Summary collapse

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Instance Attribute Details

#tags_remap_csv_fileObject

Returns the value of attribute tags_remap_csv_file.



6
7
8
# File 'lib/eco/api/usecases/graphql/samples/location/command/track_changed_ids.rb', line 6

def tags_remap_csv_file
  @tags_remap_csv_file
end

Instance Method Details

#close_handling_tags_remap_csvObject

Note:

this method used to only work if we could run cummulative dry-runs to the back-end. However, after RS P3, as mappings are one-to-one (not many-to-many per row), we can just display the mappings in dry-run as well.

Generates the file



31
32
33
34
35
36
37
38
39
40
# File 'lib/eco/api/usecases/graphql/samples/location/command/track_changed_ids.rb', line 31

def close_handling_tags_remap_csv
  if tags_remap_table.any?
    puts "REMAP LOC IDs CSV (content):"
    puts tags_remap_table
    true
  else
    log(:info) { "Remap location ids NOT needed :)" }
    false
  end
end

#generate_tags_remap_csv(filename = tags_remap_csv_full_filename) ⇒ Object

Generates the final tags remap file



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/eco/api/usecases/graphql/samples/location/command/track_changed_ids.rb', line 72

def generate_tags_remap_csv(filename = tags_remap_csv_full_filename)
  return nil if tags_remap_table.empty?
  timestamp_file(filename).tap do |file|
    CSV.open(file, 'w') do |csv|
      csv << %w[prev_node_ids new_node_ids]
      tags_remap_table.each do |tags_remap|
        csv << tags_remap.to_csv_row
      end
    end
    log(:info) { "Generated file '#{file}'" }
  end
end

#tags_remap_classObject



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

def tags_remap_class
  Eco::API::UseCases::GraphQL::Helpers::Location::TagsRemap
end

#tags_remap_csv_full_filenameObject



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

def tags_remap_csv_full_filename
  folder   = self.class::REMAP_LOC_IDS_FOLDER
  filename = self.class::REMAP_LOC_IDS_FILENAME
  File.join(folder, filename)
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>



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

def tags_remap_table
  @tags_remap_table ||= tags_remap_class.new
end

#timestamp_file(filename, enviro_relative: true) ⇒ Object

Makes the file relative to the enviro



86
87
88
89
# File 'lib/eco/api/usecases/graphql/samples/location/command/track_changed_ids.rb', line 86

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, ref_tree = nil) ⇒ Boolean

Note:

The only update operation that generate tag remaps is :id (or :id_name).

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

Returns:

  • (Boolean)

    whether new maps were inserted to the tracking table



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eco/api/usecases/graphql/samples/location/command/track_changed_ids.rb', line 47

def update_tags_remap_table(results, stage, ref_tree = nil)
  return false unless %i[id id_name].include?(stage)

  msg = "Expecting CommandResults object. Given: #{results.class}"
  raise msg unless results.is_a?(request_results_class)

  target = simulate?? results.results : results.applied

  target.each do |result|
    prev_id, new_id = result.command_input_data.values_at(:nodeId, :newId)
    next if new_id.nil? # not an id change
    next if prev_id == new_id

    tags_remap_table << [[prev_id], [new_id]]

    # next unless ref_tree.is_a?(Eco::API::Organization::TagTree)
    # next unless ref_tree.tag?(new_id)

    # msg  = "Node '#{prev_id}' was updated to '#{new_id}', "
    # msg << "but in current structure '#{new_id}' is not present"
    # log(:warn) { msg }
  end
end