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 can only work if we can run cummulative dry-runs to the back-end. This is only possible using a draft, which is not that desired.

Generates the file



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

def close_handling_tags_remap_csv
  return false if simulate?
  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



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

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



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

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

  results.applied.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