Class: Eco::API::UseCases::OozeSamples::RegisterMigrationCase

Inherits:
RegisterUpdateCase show all
Includes:
HelpersMigration
Defined in:
lib/eco/api/usecases/ooze_samples/register_migration_case.rb

Overview

Note:
  • You can define methods filters and search to change the target entries of the register
  • You can define a json file named after your use case file (i.e. new_training.json) to pair source-destination fields:
    • This file format expects an object {} where keys are field types (i.e. "select", "plain_text") and the value is an array with either:
      1. field label name (String or RegExp body/source),
      2. or an object with key-value being source-destination label names (also supports RegExp body/source)
    • Example: ["severity of .*:", {"type of event:" => "type of incident:"]}
    • "all" as type will target all type of fields.
    • Caution with this! Copying between fields of different types is not yet supported.
  • You may either
    1. define the process_ooze method calling super with a block: super.do ||
    2. of define the function custom_processing(draft, source, fields_tracker: nil)
  • You can define a callback function, paired_fields_post_callback(src_fld, dst_fld, pairing_tracking = nil)
    • It will be called after copying by using any of the pairing methods, and allows to introduce modifications such as selection option mappings
  • You may define a method called excluded_field_hooks with same syntax as the json pairing file
    • This will exclude as source and destination targets any fields matching those patterns.
    • This is specially useful to discard rich text field guidelines (i.e. {"rich_text" => [".*guideline:"]})

Use case to offer the basics to migrate a register by creating new entries This case expects options[:source][:register_id]

Constant Summary collapse

EXCLUDE_XIMPORT =

whether or not it should try to pair src <-> dst ximport fields:

false
REPORT_DST_XIMPORT =

whether or not it should warn on unpaired destination ximport fields:

true
REPORT_SRC_EMTPY =

whether or not it should warn on unpaired source fields that are empty (no data loss):

false
REPORT_SRC_DEINDEX =

whether or not it should warn on unpaired source fields that are deindexed (low potential of data loss):

false

Constants included from Helpers::Filters

Helpers::Filters::FILTER_TIME_FORMAT

Constants inherited from OozeBaseCase

OozeBaseCase::DRY_COUNT, OozeBaseCase::SAVE_PATCH

Instance Attribute Summary collapse

Attributes inherited from RegisterUpdateCase

#created_oozes, #dupped_search_oozes, #failed_update_oozes, #non_retrieved_oozes, #ooze_result_ids, #retrieved_oozes, #total_search_oozes, #updated_oozes

Attributes inherited from OozeBaseCase

#target, #usecase

Instance Method Summary collapse

Methods included from HelpersMigration::Copying

#copy_field_content, #copy_generic_paired_fields, #copy_hooked_fields, #logger, #to_regex, #with_src_dst_field_pair

Methods included from Helpers::Filters

#date_range_filter, #days, #daystart, #field_key_name, #midnight, #previous_sunday, #set_time, #sunday, #tags_filter, #this_monday, #to_date_filter, #today, #weeks

Methods included from Helpers::Shortcuts

#bracked_regex, #clean_question, #is_number?, #non_letters_regex, #normalize_string, #object_reference, #same_string?, #simplify_string, #titleize, #to_i

Methods inherited from RegisterUpdateCase

batch_size

Methods inherited from Common::Loaders::UseCase

#initialize, type, #type

Methods inherited from Common::Loaders::CaseBase

#name, name_only_once!

Methods inherited from Common::Loaders::Base

<=>, created_at, #initialize, #name, set_created_at!

Methods included from Common::ClassHelpers

#class_resolver, #descendants, #descendants?, #inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant

Constructor Details

This class inherits a constructor from Eco::API::Common::Loaders::UseCase

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



44
45
46
# File 'lib/eco/api/usecases/ooze_samples/register_migration_case.rb', line 44

def csv
  @csv
end

#optionsObject (readonly)

Returns the value of attribute options.



43
44
45
# File 'lib/eco/api/usecases/ooze_samples/register_migration_case.rb', line 43

def options
  @options
end

#sessionObject (readonly)

Returns the value of attribute session.



43
44
45
# File 'lib/eco/api/usecases/ooze_samples/register_migration_case.rb', line 43

def session
  @session
end

Instance Method Details

#main(session, options, usecase, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/eco/api/usecases/ooze_samples/register_migration_case.rb', line 46

def main(session, options, usecase, &block)
  @session = session; @options = options
  if options[:dry_run]
    @csv = []
    super(session, options, usecase, &block)
  else
    CSV.open(out_csv_filename, "w") do |csv|
      @csv = csv
      csv << ["src_page_id", "dst_page_id"]
      super(session, options, usecase, &block)
    end
    logger.info("File '#{out_csv_filename}' has been created.")
  end
end

#process_oozeObject

Note:

At this stage, the RegisterMigration case has already populated the draft where source and destination fields(in the draft) could be paired.

This method is expected to finalize the copying/adjustment of the draft entry. def custom_processing(draft, source, fields_tracker: nil) draft.tags << source.tags.to_a # More custom logics end



69
70
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/ooze_samples/register_migration_case.rb', line 69

def process_ooze
  with_target_stage(self.class::SOURCE_STAGE) do |source|
    drafting_entry(self.class::TEMPLATE_ID) do |draft|
      draft_reference = object_reference(source)
      draft.base_tags << ["IMPORTED", "MIGRATED"]

      pairing_tracking = copy_pairings(source, draft)

      if respond_to?(:custom_processing, true)
        custom_processing(draft, source, fields_tracker: pairing_tracking)
      elsif block_given?
        yield(draft, source, fields_tracker: pairing_tracking)
      end

      if msg = pairing_tracking.report_src_unpaired(only_present: !self.class::REPORT_SRC_EMTPY, only_indexed: !self.class::REPORT_SRC_DEINDEX)
        logger.warn(msg)
      end

      if msg = pairing_tracking.report_multi_pairs(only_present: !self.class::REPORT_SRC_EMTPY, only_indexed: !self.class::REPORT_SRC_DEINDEX, dst_exclude_ximport: !self.class::REPORT_DST_XIMPORT)
        logger.warn(msg)
      end

      if msg = pairing_tracking.report_dst_unpaired(only_required: false, exclude_ximport: !self.class::REPORT_DST_XIMPORT)
        logger.warn(msg)
      end

      if page_id = create_entry(draft, reference: draft_reference)
        csv << [source.id, page_id]
        logger.info("Page '#{page_id}' created successfully.")
      elsif options.dig(:dry_run)
        logger.info("Simulated launch for #{draft_reference}")
      end
    end
  end
end