eco-helpers 3.0.21 → 3.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/.idea/.gitignore +10 -0
  3. data/CHANGELOG.md +44 -2
  4. data/eco-helpers.gemspec +5 -3
  5. data/lib/eco/api/common/class_helpers.rb +2 -1
  6. data/lib/eco/api/common/class_hierarchy.rb +6 -9
  7. data/lib/eco/api/common/loaders/case_base.rb +12 -4
  8. data/lib/eco/api/common/loaders/config/workflow/cases_extension.rb +53 -0
  9. data/lib/eco/api/common/loaders/config/workflow/mailer.rb +2 -14
  10. data/lib/eco/api/common/loaders/config/workflow.rb +4 -0
  11. data/lib/eco/api/common/loaders/error_handler.rb +3 -2
  12. data/lib/eco/api/common/loaders/policy.rb +3 -2
  13. data/lib/eco/api/common/loaders/use_case/cli_identify.rb +28 -0
  14. data/lib/eco/api/common/loaders/use_case/target_model.rb +25 -0
  15. data/lib/eco/api/common/loaders/use_case/type.rb +37 -0
  16. data/lib/eco/api/common/loaders/use_case.rb +16 -43
  17. data/lib/eco/api/error/handler.rb +6 -4
  18. data/lib/eco/api/error/handlers.rb +12 -5
  19. data/lib/eco/api/microcases/people/apply_changes/set_account/account_excluded.rb +34 -0
  20. data/lib/eco/api/microcases/people/apply_changes/set_account.rb +28 -0
  21. data/lib/eco/api/microcases/people/apply_changes/set_core/core_excluded.rb +28 -0
  22. data/lib/eco/api/microcases/people/apply_changes/set_core.rb +27 -0
  23. data/lib/eco/api/microcases/people/apply_changes/set_core_with_supervisor.rb +38 -0
  24. data/lib/eco/api/microcases/people/apply_changes/set_supervisor.rb +69 -0
  25. data/lib/eco/api/microcases/people/apply_changes.rb +19 -0
  26. data/lib/eco/api/microcases/people/fetch/with_each.rb +109 -0
  27. data/lib/eco/api/microcases/people/fetch/with_each_leaver.rb +33 -0
  28. data/lib/eco/api/microcases/people/fetch/with_each_present.rb +42 -0
  29. data/lib/eco/api/microcases/people/fetch/with_each_starter.rb +43 -0
  30. data/lib/eco/api/microcases/people/fetch/with_each_subordinate.rb +41 -0
  31. data/lib/eco/api/microcases/people/fetch/with_supervisor.rb +46 -0
  32. data/lib/eco/api/microcases/people/fetch.rb +23 -0
  33. data/lib/eco/api/microcases/people/integrity/fix_default_group.rb +39 -0
  34. data/lib/eco/api/microcases/people/integrity/fix_filter_tags.rb +55 -0
  35. data/lib/eco/api/microcases/people/integrity/refresh_default_tag.rb +32 -0
  36. data/lib/eco/api/microcases/people/integrity.rb +17 -0
  37. data/lib/eco/api/microcases/people/macro/take_email.rb +141 -0
  38. data/lib/eco/api/microcases/people/macro.rb +13 -0
  39. data/lib/eco/api/microcases/people/manage/cache.rb +36 -0
  40. data/lib/eco/api/microcases/people/manage/load.rb +90 -0
  41. data/lib/eco/api/microcases/people/manage/refresh.rb +59 -0
  42. data/lib/eco/api/microcases/people/manage/search.rb +118 -0
  43. data/lib/eco/api/microcases/people/manage.rb +19 -0
  44. data/lib/eco/api/microcases/people/preserve/default_tag.rb +29 -0
  45. data/lib/eco/api/microcases/people/preserve/filter_tags.rb +34 -0
  46. data/lib/eco/api/microcases/people/preserve/policy_groups.rb +36 -0
  47. data/lib/eco/api/microcases/people/preserve.rb +17 -0
  48. data/lib/eco/api/microcases/people.rb +21 -0
  49. data/lib/eco/api/microcases/person_update.rb +1 -1
  50. data/lib/eco/api/microcases/s3upload_targets.rb +1 -1
  51. data/lib/eco/api/microcases/with_each_contractor_present.rb +40 -0
  52. data/lib/eco/api/microcases.rb +3 -23
  53. data/lib/eco/api/organization/people/multiple_search_results.rb +48 -0
  54. data/lib/eco/api/organization/{people_similarity.rb → people/similarity.rb} +89 -72
  55. data/lib/eco/api/organization/people.rb +40 -66
  56. data/lib/eco/api/organization.rb +0 -1
  57. data/lib/eco/api/policies/policy.rb +7 -5
  58. data/lib/eco/api/policies.rb +9 -4
  59. data/lib/eco/api/session/batch/feedback/generate.rb +60 -0
  60. data/lib/eco/api/session/batch/feedback/getters.rb +28 -0
  61. data/lib/eco/api/session/batch/feedback/identifiers.rb +20 -0
  62. data/lib/eco/api/session/batch/feedback/job_dependencies.rb +45 -0
  63. data/lib/eco/api/session/batch/feedback/request_stat.rb +27 -0
  64. data/lib/eco/api/session/batch/feedback/request_stats.rb +270 -0
  65. data/lib/eco/api/session/batch/feedback.rb +45 -119
  66. data/lib/eco/api/session/batch/job/sets.rb +23 -0
  67. data/lib/eco/api/session/batch/job/type.rb +22 -0
  68. data/lib/eco/api/session/batch/job.rb +8 -18
  69. data/lib/eco/api/session/batch/jobs.rb +6 -2
  70. data/lib/eco/api/session/batch/launcher/mode_size.rb +1 -1
  71. data/lib/eco/api/session/batch/launcher/retry.rb +4 -4
  72. data/lib/eco/api/session/batch/launcher.rb +2 -2
  73. data/lib/eco/api/session/batch/policies.rb +4 -4
  74. data/lib/eco/api/session/batch/searcher.rb +2 -1
  75. data/lib/eco/api/session/batch.rb +0 -1
  76. data/lib/eco/api/session/config/workflow.rb +19 -9
  77. data/lib/eco/api/usecases/base_case/model.rb +54 -0
  78. data/lib/eco/api/usecases/base_case/type.rb +52 -0
  79. data/lib/eco/api/usecases/base_case.rb +5 -29
  80. data/lib/eco/api/usecases/base_io/chaining.rb +48 -0
  81. data/lib/eco/api/usecases/base_io/validations.rb +114 -0
  82. data/lib/eco/api/usecases/base_io.rb +65 -115
  83. data/lib/eco/api/usecases/cli/dsl.rb +3 -1
  84. data/lib/eco/api/usecases/default/people/treat/analyse_people_case.rb +7 -4
  85. data/lib/eco/api/usecases/default/people/treat/supers_cyclic_identify_case.rb +1 -3
  86. data/lib/eco/api/usecases/default/people/treat/supers_hierarchy_case.rb +1 -5
  87. data/lib/eco/api/usecases/default_cases/entries_to_csv_case.rb +2 -2
  88. data/lib/eco/api/usecases/default_cases/hris_case.rb +0 -2
  89. data/lib/eco/api/usecases/default_cases/samples/sftp.rb +3 -0
  90. data/lib/eco/api/usecases/default_cases/samples.rb +1 -1
  91. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +3 -6
  92. data/lib/eco/api/usecases/graphql/base.rb +3 -2
  93. data/lib/eco/api/usecases/graphql/helpers/base/case_env.rb +1 -1
  94. data/lib/eco/api/usecases/graphql/helpers/base/error_handling.rb +3 -3
  95. data/lib/eco/api/usecases/graphql/helpers/contractors/base/load.rb +17 -0
  96. data/lib/eco/api/usecases/graphql/helpers/contractors/base.rb +8 -0
  97. data/lib/eco/api/usecases/graphql/helpers/contractors.rb +6 -0
  98. data/lib/eco/api/usecases/graphql/helpers/location/base.rb +5 -4
  99. data/lib/eco/api/usecases/graphql/helpers/location/command.rb +2 -2
  100. data/lib/eco/api/usecases/graphql/helpers.rb +1 -0
  101. data/lib/eco/api/usecases/graphql/samples/contractors/dsl.rb +5 -0
  102. data/lib/eco/api/usecases/graphql/samples/contractors.rb +17 -0
  103. data/lib/eco/api/usecases/graphql/samples/location/service/base.rb +1 -1
  104. data/lib/eco/api/usecases/graphql/samples/location.rb +1 -1
  105. data/lib/eco/api/usecases/graphql/samples.rb +1 -0
  106. data/lib/eco/api/usecases/ooze_samples/ooze_base_case.rb +0 -1
  107. data/lib/eco/api/usecases/ooze_samples/ooze_run_base_case.rb +8 -8
  108. data/lib/eco/api/usecases/{default_cases/samples → samples/drivers}/cli/sftp_cli.rb +1 -1
  109. data/lib/eco/api/usecases/samples/drivers/cli/url_pull_cli.rb +15 -0
  110. data/lib/eco/api/usecases/{default_cases/samples/sftp_case.rb → samples/drivers/sftp_sample.rb} +8 -8
  111. data/lib/eco/api/usecases/samples/drivers/url_pull_sample.rb +110 -0
  112. data/lib/eco/api/usecases/samples/drivers.rb +13 -0
  113. data/lib/eco/api/usecases/samples.rb +11 -0
  114. data/lib/eco/api/usecases/use_case/chainer.rb +12 -0
  115. data/lib/eco/api/usecases/use_case.rb +32 -23
  116. data/lib/eco/api/usecases/use_case_chain/chaining.rb +88 -0
  117. data/lib/eco/api/usecases/use_case_chain.rb +17 -62
  118. data/lib/eco/api/usecases/use_case_io/chain.rb +24 -0
  119. data/lib/eco/api/usecases/use_case_io.rb +17 -22
  120. data/lib/eco/api/usecases.rb +15 -6
  121. data/lib/eco/assets.rb +1 -1
  122. data/lib/eco/cli/config/filters/people_filters.rb +6 -5
  123. data/lib/eco/cli/config/input.rb +6 -2
  124. data/lib/eco/cli/config/use_cases.rb +4 -2
  125. data/lib/eco/cli_default/people_filters.rb +1 -0
  126. data/lib/eco/cli_default/workflow.rb +149 -138
  127. data/lib/eco/data/mapper.rb +51 -19
  128. data/lib/eco/language/methods/dsl_able.rb +10 -0
  129. data/lib/eco/version.rb +1 -1
  130. metadata +87 -35
  131. data/lib/eco/api/microcases/account_excluded.rb +0 -26
  132. data/lib/eco/api/microcases/core_excluded.rb +0 -20
  133. data/lib/eco/api/microcases/fix_default_group.rb +0 -33
  134. data/lib/eco/api/microcases/fix_filter_tags.rb +0 -49
  135. data/lib/eco/api/microcases/people_cache.rb +0 -30
  136. data/lib/eco/api/microcases/people_load.rb +0 -84
  137. data/lib/eco/api/microcases/people_refresh.rb +0 -53
  138. data/lib/eco/api/microcases/people_search.rb +0 -112
  139. data/lib/eco/api/microcases/preserve_default_tag.rb +0 -23
  140. data/lib/eco/api/microcases/preserve_filter_tags.rb +0 -28
  141. data/lib/eco/api/microcases/preserve_policy_groups.rb +0 -30
  142. data/lib/eco/api/microcases/refresh_default_tag.rb +0 -26
  143. data/lib/eco/api/microcases/set_account.rb +0 -18
  144. data/lib/eco/api/microcases/set_core.rb +0 -17
  145. data/lib/eco/api/microcases/set_core_with_supervisor.rb +0 -32
  146. data/lib/eco/api/microcases/set_supervisor.rb +0 -63
  147. data/lib/eco/api/microcases/take_email_from_account.rb +0 -129
  148. data/lib/eco/api/microcases/with_each.rb +0 -103
  149. data/lib/eco/api/microcases/with_each_leaver.rb +0 -27
  150. data/lib/eco/api/microcases/with_each_present.rb +0 -36
  151. data/lib/eco/api/microcases/with_each_starter.rb +0 -37
  152. data/lib/eco/api/microcases/with_each_subordinate.rb +0 -33
  153. data/lib/eco/api/microcases/with_supervisor.rb +0 -35
  154. data/lib/eco/api/session/batch/request_stats.rb +0 -266
@@ -0,0 +1,270 @@
1
+ module Eco
2
+ module API
3
+ class Session
4
+ class Batch
5
+ class Feedback
6
+ # @attr_reader count [Integer] the total number of requests
7
+ # @attr_reader stats [Hash] plain `Hash` with the number of requests that include an attribute
8
+ class RequestStats
9
+ CORE_ATTRS = Eco::API::Common::People::PersonParser::CORE_ATTRS
10
+ ACCOUNT_ATTRS = (Eco::API::Common::People::PersonParser::ACCOUNT_ATTRS + ['permissions_custom']).uniq
11
+ DETAILS_ATTRS = ['fields'].freeze
12
+ BLANKED_PREFIX = 'blanked_'.freeze
13
+ DETAILS_FIELDS = 'details_fields'.freeze
14
+
15
+ class << self
16
+ def valid_type?(type)
17
+ Eco::API::Session::Batch::Job.valid_type?(type.to_sym)
18
+ end
19
+
20
+ def core_attrs(stats: false, all: false)
21
+ CORE_ATTRS.dup.tap do |attrs|
22
+ next unless stats || all
23
+
24
+ attrs.unshift('core')
25
+ attrs.concat(blank_attrs(CORE_ATTRS))
26
+ end
27
+ end
28
+
29
+ def account_attrs(stats: false, all: false)
30
+ ACCOUNT_ATTRS.dup.tap do |attrs|
31
+ next unless stats || all
32
+
33
+ attrs.unshift('account_remove')
34
+ attrs.unshift('account') if all
35
+ attrs.concat(blank_attrs(ACCOUNT_ATTRS))
36
+ end
37
+ end
38
+
39
+ def details_attrs(stats: false, all: false)
40
+ DETAILS_ATTRS.dup.tap do |attrs|
41
+ next unless stats || all
42
+
43
+ attrs.unshift('details_remove')
44
+ attrs.unshift('details') if all
45
+ end
46
+ end
47
+
48
+ def blanked_prefix(attr = nil)
49
+ @blanked_prefix ||= BLANKED_PREFIX
50
+ return @blanked_prefix unless attr
51
+
52
+ "#{blanked_prefix}#{attr}"
53
+ end
54
+
55
+ def blanked_prefix=(value)
56
+ @blanked_prefix = value || blanked_prefix
57
+ end
58
+
59
+ def blank_attrs(attrs)
60
+ attrs.map {|attr| "#{blanked_prefix}#{attr}"}
61
+ end
62
+ end
63
+
64
+ attr_reader :type, :count
65
+
66
+ def initialize(type:, requests: [])
67
+ msg = "Type should be one of #{Eco::API::Session::Batch::Job.types}. Given: #{type}"
68
+ raise msg unless self.class.valid_type?(type.to_sym)
69
+
70
+ @type = type.to_sym
71
+ @count = requests&.length
72
+ @stats = build(requests)
73
+ end
74
+
75
+ def to_h
76
+ @stats
77
+ end
78
+
79
+ def message(percent: false)
80
+ lines = []
81
+ lines << pairs_to_line(core_pairs(percent: percent))
82
+ lines << pairs_to_line(account_pairs(percent: percent))
83
+ lines << pairs_to_line(details_pairs(percent: percent))
84
+ lines.join("\n")
85
+ end
86
+
87
+ private
88
+
89
+ def attr_value(attr, percent: false, total: count, details: false)
90
+ target = details ? (@stats[DETAILS_FIELDS] || {}) : @stats
91
+ i = target[attr.to_s]
92
+ return i unless percent
93
+
94
+ percentage(i, total: total)
95
+ end
96
+
97
+ def core(percent: false)
98
+ attr_value('core', percent: percent)
99
+ end
100
+
101
+ def account(percent: false)
102
+ attr_value('account', percent: percent)
103
+ end
104
+
105
+ def details(percent: false)
106
+ attr_value('details', percent: percent)
107
+ end
108
+
109
+ def stats
110
+ @stats ||= Hash.new(0)
111
+ end
112
+
113
+ def build(requests)
114
+ stats.tap do |stats|
115
+ stats[type] = count
116
+
117
+ no_requests =
118
+ !requests ||
119
+ !requests.is_a?(Enumerable) ||
120
+ requests.empty?
121
+
122
+ next if no_requests
123
+
124
+ stats[DETAILS_FIELDS] = Hash.new(0)
125
+
126
+ requests.each do |request|
127
+ add_core_stats(stats, request || {})
128
+ add_account_stats(stats, request || {})
129
+ add_details_stats(stats, request || {})
130
+ end
131
+ end
132
+ end
133
+
134
+ def attrs_to_stat(stats, hash, attrs)
135
+ stats.tap do |st|
136
+ attrs.each do |attr|
137
+ next unless hash.key?(attr)
138
+
139
+ st[attr] += 1
140
+ next unless blanked_value?(hash[attr])
141
+
142
+ st[blanked_prefix(attr)]+= 1
143
+ end
144
+ end
145
+ end
146
+
147
+ def add_core_stats(stats, request)
148
+ any_core = !(request.keys & core_attrs).empty? # rubocop:disable Style/ArrayIntersect
149
+ stats['core'] += 1 if any_core
150
+
151
+ attrs_to_stat(stats, request, core_attrs)
152
+ end
153
+
154
+ def add_account_stats(stats, request)
155
+ return unless request.key?('account')
156
+
157
+ stats['account'] += 1
158
+ stats['account_remove'] += 1 unless request['account']
159
+
160
+ attrs_to_stat(stats, request['account'] || {}, account_attrs)
161
+ end
162
+
163
+ def add_details_stats(stats, request)
164
+ return unless request.key?('details')
165
+
166
+ stats['details'] += 1
167
+ stats['details_remove'] += 1 unless request['details']
168
+
169
+ det_attrs = {}
170
+ if (fields = request.dig('details', 'fields'))
171
+ stats['fields'] += fields.length
172
+
173
+ det_attrs = fields.each_with_object(det_attrs) do |fld, hash|
174
+ hash[fld['alt_id']] = fld['value']
175
+ end
176
+ end
177
+
178
+ attrs_to_stat(stats[DETAILS_FIELDS], det_attrs, det_attrs.keys)
179
+ end
180
+
181
+ def core_pairs(percent: false)
182
+ cattrs = core_attrs + blank_attrs(core_attrs)
183
+ [['core', core(percent: percent)]] + pairs(cattrs, percent: percent, total: core)
184
+ end
185
+
186
+ def account_pairs(percent: false)
187
+ aattrs = ['account_remove'] + account_attrs + blank_attrs(account_attrs)
188
+ [['account', account(percent: percent)]] + pairs(aattrs, percent: percent, total: account)
189
+ end
190
+
191
+ def details_pairs(percent: false)
192
+ det_pairs = [['details', details(percent: percent)]]
193
+ det_pairs += [['fields', fields_average]] if attr_value('fields') && fields_average
194
+ det_pairs += pairs(['details_remove'], percent: percent, total: details)
195
+ det_pairs += pairs(details_field_attrs, percent: percent, total: details, details: true)
196
+ det_pairs
197
+ end
198
+
199
+ def pairs(attrs, percent: false, total: count, details: false)
200
+ attrs.map do |a|
201
+ value = attr_value(a, percent: percent, total: total, details: details)
202
+ value.positive? ? [a, value] : nil
203
+ end.compact
204
+ end
205
+
206
+ def pairs_to_line(pairs, percent: false)
207
+ key_val_delimiter = ': '
208
+ attr_delimiter = ' ++ '
209
+
210
+ pairs.map do |p|
211
+ [p.first.to_s, "#{p.last}#{percent ? '%' : ''}"].join(key_val_delimiter)
212
+ end.join(attr_delimiter)
213
+ end
214
+
215
+ def core_attrs
216
+ @core_attrs ||= self.class.core_attrs
217
+ end
218
+
219
+ def account_attrs
220
+ @account_attrs ||= self.class.account_attrs
221
+ end
222
+
223
+ def details_attrs
224
+ @details_attrs ||= self.class.details_attrs
225
+ end
226
+
227
+ def details_field_attrs
228
+ @stats[DETAILS_FIELDS].keys
229
+ end
230
+
231
+ def blank_attrs(attrs)
232
+ self.class.blank_attrs(attrs)
233
+ end
234
+
235
+ def blanked_prefix(attr = nil)
236
+ self.class.blanked_prefix(attr)
237
+ end
238
+
239
+ def blanked_value?(value)
240
+ case value
241
+ when nil, false
242
+ true
243
+ when Numeric
244
+ value.zero?
245
+ when Array
246
+ value.compact.empty?
247
+ when String
248
+ value.to_s.strip.empty?
249
+ end
250
+ end
251
+
252
+ def fields_average
253
+ fields_num = attr_value('fields')
254
+ return unless fields_num && (total = details).positive?
255
+
256
+ (fields_num / total.to_f).round(2)
257
+ end
258
+
259
+ def percentage(num, total: count)
260
+ return unless num
261
+
262
+ total ||= count
263
+ (num.to_f / total * 100).round(2)
264
+ end
265
+ end
266
+ end
267
+ end
268
+ end
269
+ end
270
+ end
@@ -4,151 +4,76 @@ module Eco
4
4
  class Batch
5
5
  # @attr_reader job [Eco::API::Session::Batch::Job] `batch job` the feedback is associated with
6
6
  class Feedback
7
+ require_relative 'feedback/job_dependencies'
8
+ require_relative 'feedback/request_stat'
9
+ require_relative 'feedback/generate'
7
10
 
8
- class << self
9
- def person_ref(entry)
10
- row_str = (row = get_row(entry)) ? "(row: #{row}) " : nil
11
- "#{row_str}(id: '#{get_attr(entry, :id)}') '#{get_attr(entry, :name)}' ('#{get_attr(entry, :external_id)}': '#{get_attr(entry, :email)}')"
12
- end
11
+ include JobDependencies
12
+ include RequestStat
13
+ include Generate
13
14
 
14
- def get_attr(entry, attr)
15
- if entry.respond_to?(attr.to_sym)
16
- entry.public_send(attr.to_sym)
17
- elsif entry.is_a?(Hash)
18
- entry["#{attr}"]
19
- end
20
- end
15
+ require_relative 'feedback/getters'
16
+ require_relative 'feedback/identifiers'
21
17
 
22
- def get_row(value)
23
- case value
24
- when Eco::API::Common::People::PersonEntry
25
- value.idx
26
- when Ecoportal::API::V1::Person
27
- get_row(value.entry)
28
- end
29
- end
30
- end
31
-
32
- attr_reader :job
18
+ extend Getters
19
+ extend Identifiers
33
20
 
34
21
  # @param job [Eco::API::Session::Batch::Job] `batch job` the feedback is associated with
35
22
  def initialize(job:)
36
- raise "A Eco::API::Session::Batch::Job object is required. Given: #{job}" unless job.is_a?(Eco::API::Session::Batch::Job)
37
- @job = job
38
- end
39
-
40
- # @!group Job shortcut methods
41
-
42
- # @see Eco::API::Session::Batch::Job#name
43
- # @return [String] name of the `batch job`
44
- def name
45
- job.name
46
- end
47
-
48
- # @see Eco::API::Session::Batch::Job#type
49
- def type
50
- job.type
51
- end
52
-
53
- # @see Eco::API::Session::Batch::Job#sets
54
- def sets
55
- job.sets
56
- end
57
-
58
- # @see Eco::API::Session::Batch::Job#options
59
- def options
60
- job.options
61
- end
23
+ msg = "A Eco::API::Session::Batch::Job object is required. Given: #{job.class}"
24
+ raise msg unless job.is_a?(Eco::API::Session::Batch::Job)
62
25
 
63
- # @see Eco::API::Session::Batch::Job#requests
64
- def job_requests
65
- job.requests
26
+ @job = job
66
27
  end
67
- # @!endgroup
68
28
 
69
29
  # @!group Pure feedback methods
70
30
 
71
- # Slightly modifies the behaviour of `Ecoportal::API::Common::BaseModel#as_update`, so schema details fields show the `alt_id`
31
+ # Slightly modifies the behaviour of `Ecoportal::API::Common::BaseModel#as_update`,
32
+ # so schema details fields show the `alt_id`.
72
33
  # It also fixes possible patch updates that are incomplete or unnecessary.
73
34
  # @note for better feedback
74
35
  # @param entry [Hash, Ecoportal::API::V1::Person, Ecoportal::API::Internal::Person]
75
36
  # @param add_feedback [Boolean] if `true` it tweak the hash update with additional data.
76
37
  def as_update(entry, add_feedback: true)
77
- case
78
- when entry.is_a?(Hash)
79
- hash = entry
80
- else #entry.is_a?(Ecoportal::API::V1::Person)
81
- if only_ids?
82
- hash = entry.as_json.slice("id", "external_id", "email")
83
- else
84
- hash = entry.as_update
85
-
86
- if add_feedback && details = hash["details"]
87
- if hfields = details["fields"]
88
- hfields.each do |fld|
89
- fld.merge!("alt_id" => entry.details.get_field(fld["id"]).alt_id)
90
- end
91
- end
92
- end
38
+ return entry if entry.is_a?(Hash)
39
+
40
+ # assuming entry.is_a?(Ecoportal::API::V1::Person)
41
+
42
+ if only_ids?
43
+ # @todo entry.respond_to?(:as_json)
44
+ hash = entry.as_json.slice('id', 'external_id', 'email')
45
+ else
46
+ # @todo entry.respond_to?(:as_update)
47
+ hash = entry.as_update
93
48
 
94
- if account = hash["account"]
95
- if account.keys == ["send_invites"] && !account["send_invites"]
96
- hash.delete("account")
97
- hash.delete("id") if hash.keys == ["id"]
98
- end
49
+ details = hash['details']
50
+ h_fields = details && details['fields']
51
+
52
+ if add_feedback && h_fields
53
+ h_fields.each do |fld|
54
+ fld.merge!('alt_id' => entry.details.get_field(fld['id']).alt_id)
99
55
  end
100
56
  end
101
- end
102
- hash || {}
103
- end
104
57
 
105
- # @note if `requests` is not provided, it uses the last requests of the parent `Batch::Job` `job`
106
- # @param requests [Enumerable<Hash>] raw requests as they would be sent to the _Server_
107
- # @return [Eco::API::Session::Batch::RequestStats] the stats object of the current requests
108
- def request_stats(requests = nil)
109
- requests ||= job.requests
110
- return @request_stats if @request_stats && requests == job.requests
111
- @request_stats ||= Eco::API::Session::Batch::RequestStats.new(type: type, requests: requests)
112
- end
58
+ account = hash['account']
59
+ invites = account && account.keys == ['send_invites']
60
+ ninvites = invites && !account['send_invites']
113
61
 
114
- # Generates the lines of feedback of the current requests
115
- # @note if `requests` is not provided, it uses the last requests of the parent `Batch::Job` `job`
116
- # @param requests [Enumerable<Hash>] raw requests as they would be sent to the _Server_
117
- # @param max_chars [Integer] the max number of characters for the current feedback message
118
- # @param only_stats [Boolean] whether or not should only include a brief summary of stats
119
- # @return [String] the feedback message
120
- def generate(requests = nil, max_chars: 800, only_stats: false)
121
- requests ||= job.requests
122
- [].tap do |msg|
123
- if !requests || !requests.is_a?(Enumerable) || requests.empty?
124
- msg << "#{"*" * 10} Nothing for #{signature} so far :) #{"*" * 10}"
125
- else
126
- header = "#{"*"*10} #{signature} - Feedback Sample #{"*"*10}"
127
- msg << header unless only_stats
128
- unless only_stats
129
- sample_length = 1
130
- sample = requests.slice(0, 20).map do |request|
131
- max_chars -= request.pretty_inspect.length
132
- sample_length += 1 if max_chars > 0
133
- request
134
- end
135
- msg << "#{sample.slice(0, sample_length).pretty_inspect}"
136
- end
137
- stats_str = "#{"+"*3} #{type.to_s.upcase} length: #{requests.length} #{"+"*3} STATS (job '#{name}') #{"+"*3}"
138
- msg << "#{"-"*stats_str.length}" if only_stats
139
- msg << stats_str
140
- msg << "#{request_stats(requests).message}"
141
- msg << "#{"-"*stats_str.length}" if only_stats
142
- msg << "*" * header.length unless only_stats
62
+ if ninvites
63
+ hash.delete('account')
64
+ hash.delete('id') if hash.keys == ['id']
143
65
  end
144
- end.join("\n")
66
+ end
67
+
68
+ hash || {}
145
69
  end
70
+
146
71
  # @!endgroup
147
72
 
148
73
  private
149
74
 
150
75
  def only_ids?
151
- [:delete, :get].include?(type)
76
+ %i[delete get].include?(type)
152
77
  end
153
78
 
154
79
  def signature
@@ -156,11 +81,12 @@ module Eco
156
81
  end
157
82
 
158
83
  def sets_title
159
- "#{sets.map {|s| s.to_s}.join(", ")}"
84
+ sets.map(&:to_s).join(', ')
160
85
  end
161
-
162
86
  end
163
87
  end
164
88
  end
165
89
  end
166
90
  end
91
+
92
+ require_relative 'feedback/request_stats'
@@ -0,0 +1,23 @@
1
+ class Eco::API::Session::Batch::Job
2
+ module Sets
3
+ SETS = %i[core details account].freeze
4
+
5
+ class << self
6
+ def included(base)
7
+ super
8
+ base.extend ClassMethods
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def valid_sets?(value)
14
+ sts = [value].flatten
15
+ sts.all? { |s| sets.include?(s) }
16
+ end
17
+
18
+ def sets
19
+ self::SETS
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ class Eco::API::Session::Batch::Job
2
+ module Type
3
+ TYPES = %i[get create update delete].freeze
4
+
5
+ class << self
6
+ def included(base)
7
+ super
8
+ base.extend ClassMethods
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def valid_type?(value)
14
+ types.include?(value)
15
+ end
16
+
17
+ def types
18
+ self::TYPES
19
+ end
20
+ end
21
+ end
22
+ end
@@ -14,23 +14,13 @@ module Eco
14
14
  # @attr_reader feedback [Eco::API::Session::Batch::Feedback] helper class
15
15
  # for feedback and end-user decision making
16
16
  class Job < Eco::API::Common::Session::BaseSession
17
- include Eco::Language::Methods::DslAble
18
-
19
- @types = %i[get create update delete]
20
- @sets = %i[core details account]
17
+ require_relative 'job/type'
18
+ require_relative 'job/sets'
21
19
 
22
- class << self
23
- attr_reader :types, :sets
24
-
25
- def valid_type?(value)
26
- types.include?(value)
27
- end
20
+ include Eco::Language::Methods::DslAble
28
21
 
29
- def valid_sets?(value)
30
- sts = [value].flatten
31
- sts.all? { |s| sets.include?(s) }
32
- end
33
- end
22
+ include Type
23
+ include Sets
34
24
 
35
25
  attr_reader :name, :type, :sets
36
26
  attr_reader :usecase
@@ -197,7 +187,7 @@ module Eco
197
187
  # - only include the entries that, after all above, still hold pending changes
198
188
  # (`!as_update.empty?`) to be launched as update
199
189
  # 2. pre launch checks against the `requests`:
200
- # - it generates `stats` (`Eco::API::Session::Batch::RequestStats`) out of the requests
190
+ # - it generates `stats` (`Eco::API::Session::Batch::Feedback::RequestStats`) out of the requests
201
191
  # - if there is a batch policy declared for the current job `type`, it
202
192
  # checks compliance against `stats` (see {Eco::API::Session::Batch::Policies}),
203
193
  # - a non-compliant batch will stop the current session by raising an `Exception`
@@ -368,7 +358,7 @@ module Eco
368
358
  next if policies.empty?
369
359
 
370
360
  policies.launch(
371
- people: entries,
361
+ data: entries,
372
362
  session: session,
373
363
  options: options,
374
364
  job: self
@@ -496,7 +486,7 @@ module Eco
496
486
  log(:debug) { msg }
497
487
 
498
488
  handler.launch(
499
- people: people(entries),
489
+ data: people(entries),
500
490
  session: session,
501
491
  options: options,
502
492
  job: handler_job
@@ -28,7 +28,8 @@ module Eco
28
28
  end
29
29
 
30
30
  def each(&block)
31
- return to_enum(:each) unless block
31
+ return to_enum(:each) unless block_given?
32
+
32
33
  items.each(&block)
33
34
  end
34
35
 
@@ -51,6 +52,7 @@ module Eco
51
52
  # @return [Eco::API::Session::Batch::Job]
52
53
  def job(name, type: nil, sets: nil, usecase: nil, accept_update_with_no_id: false, &block)
53
54
  fatal "Can't give a job post-launch callback &block to an already existing job" if exists?(name)
55
+
54
56
  new(name, type: type, sets: sets, usecase: usecase, accept_update_with_no_id: accept_update_with_no_id, &block) unless exists?(name)
55
57
  self[name]
56
58
  end
@@ -75,7 +77,9 @@ module Eco
75
77
  # @yieldparam status [Eco::API::Session::Batch::Status] the status of the batch job launch.
76
78
  # @return [Eco::API::Session::Batch::Job]
77
79
  def add(job, &block)
78
- fatal "Expected Eco::API::Session::Batch::Job object. Given #{job.class}" unless job.is_a?(Eco::API::Session::Batch::Job)
80
+ msg = "Expected Eco::API::Session::Batch::Job object. Given #{job.class}"
81
+ fatal msg unless job.is_a?(Eco::API::Session::Batch::Job)
82
+
79
83
  @jobs[job.name] = job
80
84
  @callbacks[job] = block if block_given?
81
85
  end
@@ -19,7 +19,7 @@ module Eco
19
19
 
20
20
  # Swaps to batch endpoint on specific errors
21
21
  def batch_mode_on(*error_types, options: self.options, allow_job_mode: true)
22
- msg = "Expecting block. Non given"
22
+ msg = 'Expecting block. Non given'
23
23
  raise ArgumentError, msg unless block_given?
24
24
 
25
25
  in_job_mode = allow_job_mode && job_mode?(options)
@@ -6,7 +6,7 @@ module Eco
6
6
  module Retry
7
7
  def self.included(base)
8
8
  unless base <= Eco::API::Session::Batch::Launcher
9
- msg = "To be included only in Eco::API::Common::Session::BaseSession. "
9
+ msg = 'To be included only in Eco::API::Common::Session::BaseSession. '
10
10
  msg << "Tried on '#{base}'"
11
11
  raise msg
12
12
  end
@@ -23,15 +23,15 @@ module Eco
23
23
 
24
24
  explanation = "#{err}\n"
25
25
  explanation << "You have #{retries_left} retries left."
26
- question = " Do you want to retry (y/N)?"
26
+ question = ' Do you want to retry (Y/n)?'
27
27
 
28
28
  prompt_user(
29
29
  question,
30
- default: "Y",
30
+ default: 'Y',
31
31
  explanation: explanation,
32
32
  timeout: 10
33
33
  ) do |response|
34
- raise unless response.upcase.start_with?("Y")
34
+ raise unless response.upcase.start_with?('Y')
35
35
 
36
36
  puts "\nOkay... let's retry!"
37
37
  offer_retry_on(*error_types, retries_left: retries_left - 1, &block)
@@ -39,9 +39,9 @@ module Eco
39
39
  def batch_from(
40
40
  data,
41
41
  method:,
42
- params: {},
43
42
  silent: false,
44
- options: self.options
43
+ options: self.options,
44
+ **_other_kargs
45
45
  )
46
46
  fatal "Invalid batch method: #{method}." unless valid_method?(method)
47
47
  return unless data.is_a?(Enumerable)
@@ -3,9 +3,9 @@ module Eco
3
3
  class Session
4
4
  class Batch
5
5
  class Policies < Eco::API::Session::Batch::BasePolicy
6
- CORE_ATTRS = Eco::API::Session::Batch::RequestStats.core_attrs(stats: true)
7
- ACCOUNT_ATTRS = Eco::API::Session::Batch::RequestStats.account_attrs(stats: true)
8
- DETAILS_ATTRS = Eco::API::Session::Batch::RequestStats.details_attrs(stats: true)
6
+ CORE_ATTRS = Eco::API::Session::Batch::Feedback::RequestStats.core_attrs(stats: true)
7
+ ACCOUNT_ATTRS = Eco::API::Session::Batch::Feedback::RequestStats.account_attrs(stats: true)
8
+ DETAILS_ATTRS = Eco::API::Session::Batch::Feedback::RequestStats.details_attrs(stats: true)
9
9
 
10
10
  core_model = {core: CORE_ATTRS}
11
11
  account_model = {account: ACCOUNT_ATTRS}
@@ -14,7 +14,7 @@ module Eco
14
14
  TOP_MODEL = Eco::API::Session::Batch::Job.types.each_with_object({}) {|t, h| h[t] = submodel}
15
15
 
16
16
  self.model = TOP_MODEL
17
- policy_attrs *model_attrs
17
+ policy_attrs(*model_attrs)
18
18
  end
19
19
  end
20
20
  end