eco-helpers 2.6.1 → 2.6.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5b781e23d7538417c84762d636b8e10281adce9e5228cef561edf199aba5334
4
- data.tar.gz: ab7284a1124a8bece11d8d72e8bdc2f532d746e1da3cfdc264270885d6f867d9
3
+ metadata.gz: '0904af2dd434adc72c1ca3e889d4e0b11595ce991f6de1c997f613d80c8866ce'
4
+ data.tar.gz: b21748a878327830d5c813340ab2fcfe69ebace8137480bc9dfc42f290296cbf
5
5
  SHA512:
6
- metadata.gz: eb069c529b1f318f700672676178c52921b06dcaf2600c1b0f6c3ab5b388f781c17630a0ae4d0b815f206bc74cff8a528ab3f6df53ce5ea9068416b40174b3f2
7
- data.tar.gz: 0fe617fa64e3bf666438ac6a8b6bf35eac83c9552280d16f9070443fe7e7b5e7e4c0e28185b5bcf853e6eac40fc465bb29d55c3112621b9d3d979dcac81436bc
6
+ metadata.gz: 2bccc140b8ec44d41aaccdffde8e5b51a1c755fc75dcede5e08526be6daed4482a5f733ceba6a75155f1dee74dbc8e2edc52ee9f2ab55f7ebf6521acfd3aac47
7
+ data.tar.gz: b362a14bf0a872ee04928ace6c05600df37ecfd56cb754b5f2a0cc3a417115cc8d9a889ce5f40de80ed41f281b16005eed78eadf04a330dc6fa80066baa56e63
data/CHANGELOG.md CHANGED
@@ -1,6 +1,28 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [2.6.2] - 2024-02-23
5
+
6
+ ### Added
7
+ - `Eco::API::Session::Config::TagTree`: added **benchmarks** to locations structure live retrieval
8
+ - To all **Ooze** cases that use register update
9
+ - `Eco::API::UseCases::OozeSamples::Helpers::Creatable` native helpers
10
+ - to draft entries
11
+ - `Eco::API::UseCases::OozeSamples::RegisterMigrationCase` -> removed code double-up due to this addition to the register update case.
12
+ - `Eco::API::Common::Loaders::Base` added **methods**
13
+ - `#simulate?` and `#dry_run?`
14
+ - `Eco::API::UseCases::OozeSamples::RegisterUpdateCase`
15
+ - added **kpis** attempted updates and attempted creations
16
+
17
+ ### Changed
18
+ - `Eco::API::UseCases::OozeSamples::RegisterUpdateCase`
19
+ - `#main` allows to tell it to just `yield`
20
+
21
+ ### Fixed
22
+ - `Eco::Data::Locations::NodeDiff::Selectors` `any?` (typo)
23
+ - `Eco::API::UseCases::OozeSamples::TargetOozesUpdateCase`
24
+ - fixed **kpis** tracking
25
+
4
26
  ## [2.6.1] - 2024-02-11
5
27
 
6
28
  ### Added
@@ -32,6 +32,11 @@ module Eco
32
32
 
33
33
  private
34
34
 
35
+ def simulate?
36
+ options.dig(:simulate) || options.dig(:dry_run)
37
+ end
38
+ alias_method :dry_run?, :simulate?
39
+
35
40
  def session
36
41
  @session ||= ASSETS.session
37
42
  end
@@ -93,9 +93,16 @@ module Eco
93
93
  includeArchivedNodes: include_archived
94
94
  }.merge(kargs)
95
95
 
96
+ start = Time.now
96
97
  return nil unless tree = graphql.currentOrganization.locationStructure(**kargs, &block)
98
+ end_time = Time.now
99
+ secs = (end_time - start).round(3)
97
100
 
98
- Eco::API::Organization::TagTree.new(tree.treeify, id: tree.id, name: tree.name)
101
+ Eco::API::Organization::TagTree.new(tree.treeify, id: tree.id, name: tree.name).tap do |eco_tree|
102
+ cnt = eco_tree.count
103
+ per_sec = (cnt.to_f / secs).round(2)
104
+ session_logger.info("Loaded #{cnt} location nodes in #{secs} seconds (#{per_sec} nodes/sec)")
105
+ end
99
106
  end
100
107
 
101
108
  # Retrieves all the location structures of the organisation
@@ -110,11 +117,18 @@ module Eco
110
117
  includeArchivedNodes: include_archived
111
118
  }.merge(kargs)
112
119
 
120
+ start = Time.now
113
121
  next unless trees = graphql.currentOrganization.locationStructures(**kargs, &block)
122
+ end_time = Time.now
123
+ secs = (end_time - start).round(3)
124
+ cnt = 0
114
125
  trees.each do |tree|
115
126
  eco_tree = Eco::API::Organization::TagTree.new(tree.treeify, id: tree.id, name: tree.name)
127
+ cnt += eco_tree.count
116
128
  eco_trees.push(eco_tree)
117
129
  end
130
+ per_sec = (cnt.to_f / secs).round(2)
131
+ session_logger.info("Loaded #{cnt} location nodes in #{secs} seconds (#{per_sec} nodes/sec)")
118
132
  end
119
133
  end
120
134
 
@@ -0,0 +1,62 @@
1
+ module Eco
2
+ module API
3
+ class UseCases
4
+ class OozeSamples
5
+ module Helpers
6
+ # set of helpers to create entries from a template
7
+ module Creatable
8
+ module InstanceMethods
9
+ private
10
+
11
+ def creating_new_page(draft_reference = "new entry", template_id:)
12
+ page_id = nil
13
+ drafting_entry(template_id) do |draft|
14
+ yield(draft) if block_given?
15
+
16
+ if page_id = create_entry(draft, reference: draft_reference)
17
+ log(:info) { "Page '#{page_id}' created successfully -- #{draft_reference}" }
18
+ elsif options.dig(:dry_run)
19
+ log(:info) { "Simulated launch for #{draft_reference}" }
20
+ end
21
+ end
22
+ page_id
23
+ end
24
+
25
+ # @return [Page] a draft of `template_id` (still not saved)
26
+ def drafting_entry(template_id)
27
+ raise ArgumentError, "Expecting block, but not given" unless block_given?
28
+ draft = apiv2.pages.get_new(template_id)
29
+ yield(draft)
30
+ end
31
+
32
+ # Does the actual creation of the entry
33
+ def create_entry(draft, reference: "new entry from #{draft&.template_id}")
34
+ with_rescue(reference) do
35
+ if result = create_ooze(draft, template_id: draft.template_id)
36
+ return result.page_id
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ class << self
43
+ def included(base)
44
+ super
45
+ validate_base_type!(base)
46
+ base.include(InstanceMethods)
47
+ end
48
+
49
+ def validate_base_type!(base)
50
+ return super if defined?(super)
51
+ msg = "#{self} can only be included in Eco::API::UseCases::OozeSamples::RegisterUpdateCase"
52
+ msg << "\nCan't be included in #{base}"
53
+ raise LoadError, msg unless base <= Eco::API::UseCases::OozeSamples::RegisterUpdateCase
54
+ true
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,59 @@
1
+ module Eco
2
+ module API
3
+ class UseCases
4
+ class OozeSamples
5
+ module Helpers
6
+ module Rescuable
7
+ module InstanceMethods
8
+ private
9
+
10
+ # Helper to prevent script from stopping
11
+ def with_rescue(reference)
12
+ raise ArgumentError, "Expecting block, but not given" unless block_given?
13
+ yield
14
+ rescue StandardError => e
15
+ log(:error) {
16
+ [reference, e.message].join(' => \n')
17
+ }
18
+ lines = []
19
+ lines << "\nThere was an error. Choose one option:\n"
20
+ lines << " (C) - Continue/resume. Just ignore this one."
21
+ lines << " (A) - Abort. Just break this run."
22
+
23
+ session.prompt_user('Type one option (C/a):', explanation: lines.join("\n"), default: 'C') do |res|
24
+ res = res.upcase
25
+ case
26
+ when res.start_with?("A")
27
+ raise
28
+ else res.start_with?("C")
29
+ log(:warn) {
30
+ msg = "Script resumed after error..."
31
+ msg << "\n • #{reference}"
32
+ }
33
+ nil
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ class << self
40
+ def included(base)
41
+ super
42
+ validate_base_type!(base)
43
+ base.include(InstanceMethods)
44
+ end
45
+
46
+ def validate_base_type!(base)
47
+ return super if defined?(super)
48
+ msg = "#{self} can only be included in Eco::API::Common::Loaders::Base"
49
+ msg << "\nCan't be included in #{base}"
50
+ raise LoadError, msg unless base <= Eco::API::Common::Loaders::Base
51
+ true
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,5 +1,7 @@
1
1
  require_relative 'helpers/shortcuts'
2
2
  require_relative 'helpers/filters'
3
+ require_relative 'helpers/rescuable'
4
+ require_relative 'helpers/creatable'
3
5
 
4
6
  module Eco
5
7
  module API
@@ -4,6 +4,7 @@ class Eco::API::UseCases::OozeSamples::OozeBaseCase < Eco::API::Common::Loaders:
4
4
  type :other
5
5
 
6
6
  include Eco::API::UseCases::OozeSamples::Helpers
7
+ include Eco::API::UseCases::OozeSamples::Helpers::Rescuable
7
8
 
8
9
  attr_reader :target
9
10
 
@@ -145,7 +146,7 @@ class Eco::API::UseCases::OozeSamples::OozeBaseCase < Eco::API::Common::Loaders:
145
146
  # It fill update the ooze only if it's dirty (it carries changes)
146
147
  # @return [Boolean, Response] `false` if there was not request against the server, `Response` otherwise
147
148
  def update_ooze(ooze = target)
148
- if options[:simulate]
149
+ if dry_run?
149
150
  dry_run_feedback(ooze)
150
151
  false
151
152
  else
@@ -66,8 +66,8 @@ class Eco::API::UseCases::OozeSamples::RegisterMigrationCase < Eco::API::UseCase
66
66
 
67
67
  def process_ooze
68
68
  with_target_stage(self.class::SOURCE_STAGE) do |source|
69
- drafting_entry(self.class::TEMPLATE_ID) do |draft|
70
- draft_reference = object_reference(source)
69
+ draft_reference = object_reference(source)
70
+ creating_new_page(draft_reference, template_id: elf.class::TEMPLATE_ID) do |draft|
71
71
  draft.base_tags << ["IMPORTED", "MIGRATED"]
72
72
 
73
73
  pairing_tracking = copy_pairings(source, draft)
@@ -89,13 +89,6 @@ class Eco::API::UseCases::OozeSamples::RegisterMigrationCase < Eco::API::UseCase
89
89
  if msg = pairing_tracking.report_dst_unpaired(only_required: false, exclude_ximport: !self.class::REPORT_DST_XIMPORT)
90
90
  logger.warn(msg)
91
91
  end
92
-
93
- if page_id = create_entry(draft, reference: draft_reference)
94
- csv << [source.id, page_id]
95
- logger.info("Page '#{page_id}' created successfully.")
96
- elsif options.dig(:dry_run)
97
- logger.info("Simulated launch for #{draft_reference}")
98
- end
99
92
  end
100
93
  end
101
94
  end
@@ -153,23 +146,6 @@ class Eco::API::UseCases::OozeSamples::RegisterMigrationCase < Eco::API::UseCase
153
146
  "#{sym}_callback".to_sym
154
147
  end
155
148
 
156
- def create_entry(draft, reference:, from: self.class::TEMPLATE_ID)
157
- with_rescue(reference) do
158
- if !options[:dry_run]
159
- create_ooze(draft, template_id: from).page_id
160
- else
161
- logger.info("Creation sample follows...")
162
- dry_run_feedback(draft)
163
- false
164
- end
165
- end
166
- end
167
-
168
- def drafting_entry(template_id = selfTEMPLATE_ID)
169
- draft = apiv2.pages.get_new(template_id)
170
- yield(draft)
171
- end
172
-
173
149
  def with_target_stage(name)
174
150
  if stg = target_stage(name)
175
151
  yield(stg)
@@ -4,6 +4,8 @@
4
4
  # - You need to define the `process_ooze` method
5
5
  # This case expects `options[:source][:register_id]`
6
6
  class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::OozeSamples::OozeBaseCase
7
+ include Eco::API::UseCases::OozeSamples::Helpers::Creatable
8
+
7
9
  class << self
8
10
  # @return [Integer] the number of pages to be processed in each batch
9
11
  def batch_size(size = nil)
@@ -18,31 +20,25 @@ class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::
18
20
 
19
21
  attr_reader :retrieved_oozes, :non_retrieved_oozes
20
22
  attr_reader :total_search_oozes, :dupped_search_oozes
21
- attr_reader :ooze_result_ids, :updated_oozes, :failed_update_oozes, :created_oozes
23
+ attr_reader :ooze_result_ids
24
+ attr_reader :updated_oozes, :failed_update_oozes, :attempted_ooze_updates
25
+ attr_reader :created_oozes, :ooze_create_attempts
22
26
 
23
- def main(session, options, usecase, &block)
24
- @retrieved_oozes = 0
25
- @non_retrieved_oozes = 0
26
- @dupped_search_oozes = 0
27
- @ooze_result_ids = {}
28
- @updated_oozes = 0
29
- @failed_update_oozes = 0
30
- @created_oozes = 0
27
+ def main(session, options, usecase, mode: :legacy, &block)
28
+ init_kpis
31
29
 
32
30
  super(session, options, usecase) do
33
- with_each_entry do |ooze|
34
- process_ooze(&block)
31
+ if mode == :legacy
32
+ with_each_entry do |ooze|
33
+ process_ooze(&block)
34
+ end
35
+ elsif mode == :delegate
36
+ raise "You need to pass a block when running main in `:delegate` mode" unless block_given?
37
+ yield
35
38
  end
36
39
  end
37
- msg = "Run end:\n"
38
- msg += " • Search results: #{total_search_oozes}\n"
39
- msg += " • Duplicated search results #{dupped_search_oozes}\n"
40
- msg += " • Retrieved a total of #{retrieved_oozes}\n"
41
- msg += " • Could not get #{non_retrieved_oozes} oozes.\n"
42
- msg += " • Updated #{updated_oozes} oozes.\n"
43
- msg += " - Failed update on #{failed_update_oozes} oozes.\n"
44
- msg += " • Created #{created_oozes} oozes.\n"
45
- logger.info(msg)
40
+
41
+ log_kpis
46
42
  end
47
43
 
48
44
  def process_ooze(ooze = target)
@@ -51,29 +47,6 @@ class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::
51
47
 
52
48
  private
53
49
 
54
- def with_rescue(reference)
55
- begin
56
- yield
57
- rescue StandardError => e
58
- logger.error([reference, e.message].join(" => \n"))
59
- lines = []
60
- lines << "\nThere was an error. Choose one option:\n"
61
- lines << ' (C) - Continue/resume. Just ignore this one.'
62
- lines << ' (A) - Abort. Just break this run.'
63
-
64
- session.prompt_user('Type one option (C/a):', explanation: lines.join("\n"), default: 'C') do |res|
65
- res = res.upcase
66
- case
67
- when res.start_with?("A")
68
- raise
69
- else res.start_with?("C")
70
- logger.warn "Script resumed after error..."
71
- nil
72
- end
73
- end
74
- end
75
- end
76
-
77
50
  def before_loading_new_target(ooze_id)
78
51
  if pending = queue_shift(ooze_id)
79
52
  update_ooze(pending).tap do |result|
@@ -95,6 +68,7 @@ class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::
95
68
  end
96
69
  end
97
70
 
71
+ # Main looper (invoked from `main`)
98
72
  def with_each_entry
99
73
  batched_search_results do |page_results|
100
74
  page_results.each do |page_result|
@@ -172,12 +146,26 @@ class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::
172
146
  yield(results) unless results.empty?
173
147
  end
174
148
 
149
+ # Launches the actual request to create the entry
175
150
  def create_ooze(draft, template_id:)
176
- apiv2.pages.create(draft, from: template_id).tap do |result|
177
- @created_oozes += 1
151
+ @ooze_create_attempts += 1
152
+
153
+ if dry_run?
154
+ dry_run_feedback(ooze)
155
+ false
156
+ else
157
+ apiv2.pages.create(draft, from: template_id).tap do |result|
158
+ @created_oozes += 1
159
+ end
178
160
  end
179
161
  end
180
162
 
163
+ # Capture the attempts count
164
+ def update_ooze(*args, **kargs, &block)
165
+ @attempted_ooze_updates += 1 if dirty?(ooze)
166
+ super
167
+ end
168
+
181
169
  def update_oozes(batched_oozes = batch_queue)
182
170
  batched_oozes.each do |ooze|
183
171
  update_ooze(ooze).tap do |result|
@@ -215,10 +203,39 @@ class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::
215
203
  end
216
204
  end
217
205
 
206
+ def init_kpis
207
+ @retrieved_oozes = 0
208
+ @non_retrieved_oozes = 0
209
+ @dupped_search_oozes = 0
210
+ @ooze_result_ids = {}
211
+ @updated_oozes = 0
212
+ @failed_update_oozes = 0
213
+ @attempted_ooze_updates = 0
214
+ @created_oozes = 0
215
+ @ooze_create_attempts = 0
216
+ end
217
+
218
+ def log_kpis
219
+ log(:info) { kpis_message }
220
+ end
221
+
222
+ def kpis_message
223
+ msg = []
224
+ msg << "Run end:"
225
+ msg << " • Search results: #{total_search_oozes}"
226
+ msg << " • Duplicated search results #{dupped_search_oozes}"
227
+ msg << " • Retrieved a total of #{retrieved_oozes}"
228
+ msg << " • Could not get #{non_retrieved_oozes} oozes."
229
+ msg << " • Updated #{updated_oozes} oozes (attempted: #{attempted_ooze_updates})."
230
+ msg << " - Failed update on #{failed_update_oozes} oozes."
231
+ msg << " • Created #{created_oozes} oozes (out of: #{ooze_create_attempts})."
232
+ msg.join("\n")
233
+ end
234
+
218
235
  def search_options
219
236
  @search_options ||= {}.tap do |opts|
220
- opts.merge!(sort: "created_at")
221
- opts.merge!(dir: "asc")
237
+ opts.merge!(sort: 'created_at')
238
+ opts.merge!(dir: 'asc')
222
239
  opts.merge!(query: conf_search) if conf_search
223
240
  opts.merge!(filters: conf_filters)
224
241
  end
@@ -19,7 +19,11 @@ class Eco::API::UseCases::OozeSamples::TargetOozesUpdateCase < Eco::API::UseCase
19
19
  end
20
20
  end
21
21
  if ooz = ooze(id)
22
+ @retrieved_oozes += 1
22
23
  yield(ooz)
24
+ else
25
+ @non_retrieved_oozes += 1
26
+ logger.warn "Could not get page #{id}"
23
27
  end
24
28
  end
25
29
  update_oozes
@@ -42,8 +46,12 @@ class Eco::API::UseCases::OozeSamples::TargetOozesUpdateCase < Eco::API::UseCase
42
46
 
43
47
  def target_ids_preview
44
48
  dups = target_ids.select {|id| target_ids.count(id) > 1}
49
+ @total_search_oozes = target_ids.count
50
+ @dupped_search_oozes = dups.count
51
+
45
52
  dups_str = dups.count > 0 ? "There are #{dups.count} duplicated ids" : "No duplicates detected"
46
53
  msg = "Total target entries: #{target_ids.count} (#{dups_str})"
54
+
47
55
  session.prompt_user("Do you want to proceed (y/N):", explanation: msg, default: "N", timeout: 10) do |res|
48
56
  unless res.upcase.start_with?("Y")
49
57
  puts "..."
@@ -50,30 +50,32 @@ class Eco::Data::Locations::NodeDiff
50
50
  end
51
51
 
52
52
  def diffs_summary
53
- return "There were no differences identified" if diffs.empty?
54
- msg = "Identified #{diffs.count} differences:\n"
53
+ comp = "(#{source2.count} vs #{source1.count} nodes)"
54
+ return "There were no differences identified #{comp}" if diffs.empty?
55
+ msg = []
56
+ msg << "Identified #{diffs.count} differences #{comp}:"
55
57
  msg << when_present(insert, '') do |count|
56
- " • #{count} nodes to insert\n"
58
+ " • #{count} nodes to insert"
57
59
  end
58
60
  msg << when_present(update, '') do |count|
59
- " • #{count} nodes to update\n"
61
+ " • #{count} nodes to update"
60
62
  end
61
63
  # msg << when_present(id, '') do |count|
62
64
  # " • #{count} nodes to change id\n"
63
65
  # end
64
66
  msg << when_present(name, '') do |count|
65
- " • #{count} nodes to change name\n"
67
+ " • #{count} nodes to change name"
66
68
  end
67
69
  msg << when_present(move, '') do |count|
68
- " • #{count} nodes to move\n"
70
+ " • #{count} nodes to move"
69
71
  end
70
72
  msg << when_present(unarchive, '') do |count|
71
- " • #{count} nodes to unarchive\n"
73
+ " • #{count} nodes to unarchive"
72
74
  end
73
75
  msg << when_present(archive, '') do |count|
74
- " • #{count} nodes to archive\n"
76
+ " • #{count} nodes to archive"
75
77
  end
76
- msg
78
+ msg.join("\n")
77
79
  end
78
80
 
79
81
  private
@@ -12,7 +12,7 @@ class Eco::Data::Locations::NodeDiff
12
12
  end
13
13
 
14
14
  define_method methq do
15
- diffs.any(&methq)
15
+ diffs.any?(&methq)
16
16
  end
17
17
  end
18
18
  end
data/lib/eco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eco
2
- VERSION = "2.6.1"
2
+ VERSION = "2.6.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eco-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.1
4
+ version: 2.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
@@ -694,10 +694,12 @@ files:
694
694
  - lib/eco/api/usecases/ooze_cases/export_register_case.rb
695
695
  - lib/eco/api/usecases/ooze_samples.rb
696
696
  - lib/eco/api/usecases/ooze_samples/helpers.rb
697
+ - lib/eco/api/usecases/ooze_samples/helpers/creatable.rb
697
698
  - lib/eco/api/usecases/ooze_samples/helpers/exportable_ooze.rb
698
699
  - lib/eco/api/usecases/ooze_samples/helpers/exportable_register.rb
699
700
  - lib/eco/api/usecases/ooze_samples/helpers/filters.rb
700
701
  - lib/eco/api/usecases/ooze_samples/helpers/ooze_handlers.rb
702
+ - lib/eco/api/usecases/ooze_samples/helpers/rescuable.rb
701
703
  - lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb
702
704
  - lib/eco/api/usecases/ooze_samples/helpers_migration.rb
703
705
  - lib/eco/api/usecases/ooze_samples/helpers_migration/copying.rb