eco-helpers 2.0.34 → 2.0.38

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: 420f5871108edb8c66a534f60a4f8303352245a5080c68398ccd1dc34974a606
4
- data.tar.gz: c176a43c046adf47520d18af6db7a92225786625051f2982b6ffdd1540c722e7
3
+ metadata.gz: 0e84f7c290837d063ed13e63165dadaf272219105227d90b983b9af16d338fe5
4
+ data.tar.gz: 38ea16433f7a5689cfdcbde0b3bd4f1065e8adb9e742493c097a87c684b97b86
5
5
  SHA512:
6
- metadata.gz: 1e1b2828dc5ba988364eb04f9433fff624694d6371ba5e3aa80bc4d4ee3c6cc62eb90cd2693a1e324a630043e19666d1499e5e8fdfff69c03adccd777e387494
7
- data.tar.gz: 5b2d6ba34b14b4164512e3ffd5c65ab2c4edac3c0452ff4dcd422b32537fcff47c244f85b815c4f55b0d313b546cf786dabe49d76cc5feaf374b1039cdc7c266
6
+ metadata.gz: cef2930cc5941db9baab1a726fd04fb6186b57d9498cb8112589b2b12e40e53ae061ca2b68f062a197111ba85f47acafcf512f4aa3559e043b924412b23ba038
7
+ data.tar.gz: e79992f8ed299014a4c30b62c18773c1741a1961c4777560bbd137a60adc265920a3d35b18085256b42ce6cf467b9d4a950ba06e28adf50770a71aa94b3236ad
data/CHANGELOG.md CHANGED
@@ -1,7 +1,58 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
- ## [2.0.34] - 2021-08-xx
4
+ ## [2.0.38] - 2021-09-07
5
+
6
+ ### Added
7
+ - `Eco::CSV::Table` some more helpers to manage input `csv`s
8
+ - `#transform_headers` that allows to pass a block to decide the new name
9
+ - `#merge_same_header_names` that allows to deal with value of columns with same header name
10
+ - `#duplicated_header_names` that identifies repeated header names
11
+
12
+ ### Changed
13
+ - Changed the `workflow` to prevent re-cache of people when `people` was not used
14
+
15
+ ### Fixed
16
+
17
+ ## [2.0.37] - 2021-09-03
18
+
19
+ ### Added
20
+ ### Changed
21
+
22
+ ### Fixed
23
+ - `Eco::API::UseCases::OozeSamples::OozeBaseCase`
24
+ - `#update_ooze` only count dry run for changed pages (to have better feedack)
25
+ - stop downloading the people manager
26
+ - `Eco::API::UseCases::OozeSamples::RegisterUpdateCase`
27
+ - prevent queuing non page objects
28
+
29
+ ## [2.0.36] - 2021-08-31
30
+
31
+ ### Fixed
32
+ - `Eco::API::UseCases::OozeSamples::OozeBaseCase`
33
+ - `#update_ooze` only count dry run for changed pages (to have better feedack)
34
+
35
+ ## [2.0.35] - 2021-08-30
36
+
37
+ ### Added
38
+ - `Eco::API::UseCases::OozeSamples::OozeBaseCase`
39
+ - `#new_target` optional warning when switching `target`
40
+ - `block` that is called when switching target and there were unsaved changes
41
+ - `#dirty?` helper method to check if there are changes
42
+ - `#before_loading_new_target` hook to allow child classes to anticipate when the `target` is going to change
43
+
44
+ ### Changed
45
+ - `Eco::API::UseCases::OozeSamples::Helpers::Shortcuts#object_reference` improved reference text
46
+ - `Eco::API::UseCases::OozeSamples::RegisterUpdateCase`
47
+ - added resilience when switching target (i.e. from one stage to another stage in same entry)
48
+ - added `batch_queue`
49
+
50
+ ### Fixed
51
+ - `Eco::API::UseCases::OozeSamples::RegisterUpdateCase`
52
+ - actual `batch_size` updates
53
+ - actual safe switch of target (i.e. against another stage of same ooze)
54
+
55
+ ## [2.0.34] - 2021-08-28
5
56
 
6
57
  ### Added
7
58
  - `Eco::API::UseCases::OozeSamples::OozeBaseCase` refactored ooze base case
@@ -12,8 +63,6 @@ All notable changes to this project will be documented in this file.
12
63
  - `Eco::API::UseCases::OozeSamples::OozeUpdateCase` remove unnecessary code and helpers that are now inherited from `OoozeBaseCase`
13
64
  - `Eco::API::UseCases::OozeSamples::OozeFromDocCase` remove unnecessary code
14
65
 
15
- ### Fixed
16
-
17
66
 
18
67
  ## [2.0.33] - 2021-08-16
19
68
 
data/eco-helpers.gemspec CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
31
31
  spec.add_development_dependency "redcarpet", ">= 3.5.1", "< 3.6"
32
32
 
33
33
  spec.add_dependency 'ecoportal-api', '>= 0.8.3', '< 0.9'
34
- spec.add_dependency 'ecoportal-api-v2', '>= 0.8.11', '< 0.9'
34
+ spec.add_dependency 'ecoportal-api-v2', '>= 0.8.13', '< 0.9'
35
35
  spec.add_dependency 'aws-sdk-s3', '>= 1.83.0', '< 2'
36
36
  spec.add_dependency 'aws-sdk-ses', '>= 1.36.0', '< 2'
37
37
  spec.add_dependency 'dotenv', '>= 2.7.6', '< 2.8'
@@ -59,16 +59,16 @@ module Eco
59
59
  "".tap do |ref|
60
60
  case obj
61
61
  when Ecoportal::API::V2::Page::Stage
62
- ref << "Stage "
62
+ ref << "Stage"
63
63
  when Ecoportal::API::V2::Pages::PageStage
64
- ref << "StagePage "
64
+ ref << "Page (#{obj.id}) (#{object_reference(obj.current_stage)})"
65
65
  when Ecoportal::API::V2::Page
66
- ref << "Page "
66
+ ref << "Page"
67
67
  end
68
- ref << "'#{obj.name}' " if obj.respond_to?(:name)
68
+ ref << " '#{obj.name}'" if obj.respond_to?(:name)
69
69
  end
70
70
  end
71
-
71
+
72
72
  end
73
73
  end
74
74
  end
@@ -9,8 +9,10 @@ class Eco::API::UseCases::OozeSamples::OozeBaseCase < Eco::API::Common::Loaders:
9
9
  attr_reader :target
10
10
 
11
11
  SAVE_PATCH = "ooze_patch_update.json"
12
+ DRY_COUNT = 5
12
13
 
13
14
  def main(session, options, usecase)
15
+ options[:end_get] = false
14
16
  raise "You need to inherit from this class ('#{self.class}') and call super with a block" unless block_given?
15
17
  @session = session; @options = options; @usecase = usecase
16
18
  @target = nil
@@ -26,8 +28,13 @@ class Eco::API::UseCases::OozeSamples::OozeBaseCase < Eco::API::Common::Loaders:
26
28
 
27
29
  private
28
30
 
31
+ # Hook method to use before the target is switched
32
+ def before_loading_new_target(ooze_id)
33
+ end
34
+
29
35
  def ooze(ooze_id = nil, stage_id: nil)
30
36
  return target unless ooze_id
37
+ before_loading_new_target(ooze_id)
31
38
  apiv2.pages.get(ooze_id, stage_id: stage_id).tap do |ooze|
32
39
  if ooze
33
40
  new_target(ooze)
@@ -47,6 +54,7 @@ class Eco::API::UseCases::OozeSamples::OozeBaseCase < Eco::API::Common::Loaders:
47
54
 
48
55
  if stg = ooze.stages[id_name] || ooze.stages.get_by_name(id_name)
49
56
  return ooze if ooze.respond_to?(:current_stage_id) && (ooze.current_stage_id == stg.id)
57
+ before_loading_new_target(ooze_id)
50
58
  return apiv2.pages.get(ooze_id, stage_id: stg.id).tap do |stage|
51
59
  if stage
52
60
  new_target(stage)
@@ -61,12 +69,13 @@ class Eco::API::UseCases::OozeSamples::OozeBaseCase < Eco::API::Common::Loaders:
61
69
 
62
70
  def update_ooze(ooze = target)
63
71
  if !options[:simulate]
72
+ return unless dirty?(ooze)
64
73
  apiv2.pages.update(ooze).tap do |response|
65
74
  if response.success?
66
75
  ooze.consolidate!
67
- logger.info("Successful update of '#{ooze.name}' (#{ooze.id})")
76
+ logger.info("Successful update of #{object_reference(ooze)}")
68
77
  else
69
- logger.error("Could not update '#{ooze.name}' (#{ooze.id}): #{response.body}")
78
+ logger.error("Could not update #{object_reference(ooze)} (created_at: #{ooze.created_at}: #{response.body}")
70
79
  end
71
80
  end
72
81
  else
@@ -76,17 +85,26 @@ class Eco::API::UseCases::OozeSamples::OozeBaseCase < Eco::API::Common::Loaders:
76
85
  end
77
86
  end
78
87
  backup_patch!(ooze)
79
- exit(0) if dry_count > 5
88
+ exit(0) if dirty?(ooze) && dry_count > DRY_COUNT
80
89
  end
81
90
  end
82
91
 
83
- def new_target(object)
84
- if target && patch_doc(target)["page"]
85
- logger.warn "You you are switching to a new target #{object_reference(object)} after doing unsaved changes to #{object_reference(target)}"
92
+ def new_target(object, warn_pending_changes: true)
93
+ if dirty?(target)
94
+ if warn_pending_changes
95
+ msg = "You you are switching to a new target #{object_reference(object)}"
96
+ msg += " after doing unsaved changes to #{object_reference(target)}"
97
+ logger.warn msg
98
+ end
99
+ yield(target) if block_given?
86
100
  end
87
101
  @target = object
88
102
  end
89
103
 
104
+ def dirty?(object)
105
+ object && patch_doc(object)["page"]
106
+ end
107
+
90
108
  def backup_patch!(ooze = target)
91
109
  unless patch = (patch_doc(ooze) || {})["page"]
92
110
  logger.info "No changes to update for #{object_reference(ooze)}."
@@ -6,11 +6,11 @@
6
6
  class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::OozeSamples::OozeBaseCase
7
7
 
8
8
  class << self
9
- attr_reader :batch_size
10
-
11
9
  # @return [Integer] the number of pages to be processed in each batch
12
- def batch_size
13
- @batch_size || 25
10
+ def batch_size(size = nil)
11
+ @batch_size ||= 25
12
+ return @batch_size unless size
13
+ @batch_size = size
14
14
  end
15
15
  end
16
16
 
@@ -31,39 +31,98 @@ class Eco::API::UseCases::OozeSamples::RegisterUpdateCase < Eco::API::UseCases::
31
31
 
32
32
  private
33
33
 
34
+ def before_loading_new_target(ooze_id)
35
+ if pending = queue_shift(ooze_id)
36
+ update_ooze(pending)
37
+ end
38
+ end
39
+
40
+ def new_target(object, warn_pending_changes: false)
41
+ enqueue(object)
42
+ super(object, warn_pending_changes: warn_pending_changes) do |pending|
43
+ #enqueue(pending)
44
+ end
45
+ end
46
+
34
47
  def with_each_entry
35
48
  batched_search_results do |page_results|
36
- batched_oozes = page_results.map do |page_result|
37
- # TODO: Logics to get the correct PageStage
38
- if ooze = new_target(ooze(page_result.id))
39
- yield(ooze)
49
+ page_results.each do |page_result|
50
+ if pending = queue_shift(page_result.id)
51
+ if dirty?(pending)
52
+ msg = "Inconsistent search results. "
53
+ msg << "Launching update on '#{object_reference(pending)}' to be able to queue it back"
54
+ console.warn msg
55
+ update_ooze(pending)
56
+ end
57
+ end
58
+ if ooz = ooze(page_result.id)
59
+ yield(ooz)
40
60
  end
41
- ooze
42
61
  end
43
- update_oozes(batched_oozes)
62
+ update_oozes
44
63
  end
45
64
  end
46
65
 
66
+ def enqueue(object)
67
+ return unless object && object.respond_to?(:id)
68
+ unless object.is_a?(Ecoportal::API::V2::Page) or object.is_a?(Ecoportal::API::V2::Pages::PageStage)
69
+ raise "Queuing is just for entries. Expecting Page or PageStage. Given: #{object.class}"
70
+ end
71
+ if elem = queue_get(object.id)
72
+ msg = "Something is wrong. Native case 'RegisterUpdateCase' is "
73
+ if (elem != object) && dirty?(elem)
74
+ msg << "trying to queue different objects with same page id:\n"
75
+ msg << " • already queued (changes will go): #{object_reference(elem)}\n"
76
+ msg << " • tried to queue (lost changes): #{object_reference(object)}"
77
+ logger.warn msg
78
+ end
79
+ else
80
+ batch_queue << object unless batch_queue.include?(object)
81
+ end
82
+ end
83
+
84
+ def queue_shift(id = nil)
85
+ if id
86
+ batch_queue.delete(queue_get(id))
87
+ else
88
+ batch_queue.shift
89
+ end
90
+ end
91
+
92
+ def queue_get(id)
93
+ batch_queue.find {|e| e.id == id}
94
+ end
95
+
96
+ def batch_queue
97
+ @batch_queue ||= []
98
+ end
99
+
47
100
  def batched_search_results
48
101
  raise "Missing block. It yields in slices of #{self.class.batch_size} results" unless block_given?
49
102
  results_preview
50
- i = 0; results = []
103
+ results = []
51
104
  apiv2.registers.search(register_id, search_options) do |page_result|
52
- i += 1
53
105
  results << page_result
54
- if i <= self.class.batch_size
106
+ if results.length >= self.class.batch_size
55
107
  yield(results)
56
108
  results = []
57
- i = 0
58
109
  end
59
110
  end
60
111
  yield(results) unless results.empty?
61
112
  end
62
113
 
63
- def update_oozes(batched_oozes)
114
+ def update_oozes(batched_oozes = batch_queue)
64
115
  batched_oozes.each do |ooze|
65
116
  update_ooze(ooze)
66
117
  end
118
+ batched_oozes.clear
119
+ end
120
+
121
+ def backup_patch!(ooze = target)
122
+ unless patch = (patch_doc(ooze) || {})["page"]
123
+ logger.info "No changes to update for #{object_reference(ooze)}."
124
+ return
125
+ end
67
126
  end
68
127
 
69
128
  def results_preview
@@ -146,8 +146,11 @@ ASSETS.cli.config do |config|
146
146
  get_people = io.options.dig(:people, :get)
147
147
  partial_update = get_people && get_people.dig(:type) == :partial
148
148
  unless !io.options[:end_get] || io.options[:dry_run] || partial_update
149
- people = io.session.micro.people_cache
150
- io = io.new(people: people)
149
+ # Prevent getting people when there were no use cases that used them
150
+ unless io.people
151
+ people = io.session.micro.people_cache
152
+ io = io.new(people: people)
153
+ end
151
154
  end
152
155
  io
153
156
  end
data/lib/eco/csv/table.rb CHANGED
@@ -15,6 +15,46 @@ module Eco
15
15
  end
16
16
  end
17
17
 
18
+ # @return [Eco::CSV::Table]
19
+ def transform_headers
20
+ header = self.headers
21
+ cols = self.columns
22
+ cols.each do |col|
23
+ col[0] = yield(col.first)
24
+ end
25
+ columns_to_table(cols)
26
+ end
27
+
28
+ # @return [Eco::CSV::Table]
29
+ def merge_same_header_names
30
+ dups = self.duplicated_header_names
31
+ out_rows = self.map do |row|
32
+ row.to_a.each_with_object({}) do |(name, value), out|
33
+ if dups.include?(name) && out.key?(name)
34
+ if block_given?
35
+ yield(value, out[name], name)
36
+ else
37
+ # resolve
38
+ value || out[name]
39
+ end
40
+ elsif out.key?(name)
41
+ out[name]
42
+ else
43
+ value
44
+ end.tap do |final_value|
45
+ out[name] = final_value
46
+ end
47
+ end
48
+ end
49
+ self.class.new(out_rows)
50
+ end
51
+
52
+ # @return [Array<String>] list of duplicated header names
53
+ def duplicated_header_names
54
+ header = self.headers
55
+ header.select {|e| header.count(e) > 1}.uniq
56
+ end
57
+
18
58
  # @return [Eco::CSV::Table]
19
59
  def transform_values
20
60
  transformed_rows = rows.map do |row|
data/lib/eco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eco
2
- VERSION = "2.0.34"
2
+ VERSION = "2.0.38"
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.0.34
4
+ version: 2.0.38
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura
@@ -136,7 +136,7 @@ dependencies:
136
136
  requirements:
137
137
  - - ">="
138
138
  - !ruby/object:Gem::Version
139
- version: 0.8.11
139
+ version: 0.8.13
140
140
  - - "<"
141
141
  - !ruby/object:Gem::Version
142
142
  version: '0.9'
@@ -146,7 +146,7 @@ dependencies:
146
146
  requirements:
147
147
  - - ">="
148
148
  - !ruby/object:Gem::Version
149
- version: 0.8.11
149
+ version: 0.8.13
150
150
  - - "<"
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0.9'