eco-helpers 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/eco/api.rb +1 -0
- data/lib/eco/api/common.rb +1 -0
- data/lib/eco/api/common/class_helpers.rb +33 -0
- data/lib/eco/api/common/people.rb +1 -0
- data/lib/eco/api/common/people/person_attribute_parser.rb +52 -0
- data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +2 -2
- data/lib/eco/api/common/people/person_parser.rb +16 -10
- data/lib/eco/api/common/version_patches.rb +2 -3
- data/lib/eco/api/common/version_patches/ecoportal_api.rb +4 -0
- data/lib/eco/api/common/version_patches/{base_model.rb → ecoportal_api/base_model.rb} +0 -0
- data/lib/eco/api/common/version_patches/{external_person.rb → ecoportal_api/external_person.rb} +0 -0
- data/lib/eco/api/common/version_patches/{internal_person.rb → ecoportal_api/internal_person.rb} +0 -0
- data/lib/eco/api/common/version_patches/hash.rb +2 -0
- data/lib/eco/api/common/version_patches/hash/deep_merge.rb +34 -0
- data/lib/eco/api/error.rb +133 -0
- data/lib/eco/api/error/handler.rb +19 -0
- data/lib/eco/api/error/handlers.rb +22 -0
- data/lib/eco/api/organization/people.rb +11 -11
- data/lib/eco/api/organization/policy_groups.rb +8 -0
- data/lib/eco/api/policies.rb +26 -3
- data/lib/eco/api/policies/policy.rb +4 -5
- data/lib/eco/api/session.rb +27 -18
- data/lib/eco/api/session/batch.rb +12 -6
- data/lib/eco/api/session/batch/errors.rb +134 -0
- data/lib/eco/api/session/batch/job.rb +213 -0
- data/lib/eco/api/session/batch/jobs.rb +72 -0
- data/lib/eco/api/session/batch/jobs_groups.rb +85 -0
- data/lib/eco/api/session/batch/status.rb +133 -0
- data/lib/eco/api/session/config.rb +36 -14
- data/lib/eco/api/session/config/base_config.rb +2 -0
- data/lib/eco/api/session/config/people.rb +8 -0
- data/lib/eco/api/session/config/post_launch.rb +58 -0
- data/lib/eco/api/session/config/workflow.rb +189 -0
- data/lib/eco/api/session/task.rb +49 -6
- data/lib/eco/api/usecases.rb +137 -2
- data/lib/eco/api/usecases/base_case.rb +20 -8
- data/lib/eco/api/usecases/base_io.rb +97 -0
- data/lib/eco/api/usecases/default_case.rb +19 -0
- data/lib/eco/api/usecases/default_cases.rb +2 -2
- data/lib/eco/api/usecases/default_cases/change_email_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/create_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/create_details_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/delete_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/hris_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/new_email_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/new_id_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/recover_db_case.rb +11 -8
- data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/reinvite_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/remove_account_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/to_csv_case.rb +4 -4
- data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +3 -3
- data/lib/eco/api/usecases/default_cases/update_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/update_details_case.rb +2 -2
- data/lib/eco/api/usecases/default_cases/upsert_case.rb +2 -2
- data/lib/eco/api/usecases/use_case.rb +23 -36
- data/lib/eco/api/usecases/use_case_chain.rb +14 -24
- data/lib/eco/api/usecases/use_case_io.rb +23 -75
- data/lib/eco/assets.rb +11 -11
- data/lib/eco/cli.rb +37 -0
- data/lib/eco/cli/config.rb +63 -1
- data/lib/eco/cli/config/default.rb +15 -0
- data/lib/eco/cli/config/default/filters.rb +69 -0
- data/lib/eco/cli/config/default/input.rb +21 -0
- data/lib/eco/cli/config/default/options.rb +47 -0
- data/lib/eco/cli/config/default/people.rb +39 -0
- data/lib/eco/cli/config/default/usecases.rb +63 -0
- data/lib/eco/cli/config/default/workflow.rb +86 -0
- data/lib/eco/cli/config/input.rb +40 -0
- data/lib/eco/cli/config/options_set.rb +35 -0
- data/lib/eco/cli/config/people_filters.rb +38 -0
- data/lib/eco/cli/config/use_cases.rb +87 -0
- data/lib/eco/cli/scripting/args_helpers.rb +10 -4
- data/lib/eco/cli/scripting/argument.rb +6 -0
- data/lib/eco/language/models/collection.rb +1 -0
- data/lib/eco/version.rb +1 -1
- metadata +32 -12
- data/lib/eco/api/policies/base_policy.rb +0 -14
- data/lib/eco/api/policies/used_policies.rb +0 -37
- data/lib/eco/api/session/batch_job.rb +0 -215
- data/lib/eco/api/session/batch_jobs.rb +0 -62
- data/lib/eco/api/session/batch_status.rb +0 -205
- data/lib/eco/api/session/job_groups.rb +0 -75
- data/lib/eco/api/usecases/use_group.rb +0 -124
- data/lib/eco/cli/config/options.rb +0 -11
@@ -78,11 +78,11 @@ module Eco
|
|
78
78
|
person(id: id, external_id: external_id, email: email, strict: strict)
|
79
79
|
end
|
80
80
|
|
81
|
-
def uniq(include_unsearchable: false)
|
81
|
+
def uniq(strict: false, include_unsearchable: false)
|
82
82
|
init_caches
|
83
83
|
unsearchable = []
|
84
84
|
newFrom to_a.each_with_object([]) do |person, people|
|
85
|
-
if found = find(person)
|
85
|
+
if found = find(person, strict: strict)
|
86
86
|
people << found
|
87
87
|
else
|
88
88
|
unsearchable << person
|
@@ -92,19 +92,19 @@ module Eco
|
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
95
|
+
def merge(data, strict: false, uniq: true)
|
96
|
+
list = uniq ? exclude_people(data, strict: strict).to_a : to_a
|
97
|
+
data = data.to_a unless data.is_a?(Array)
|
98
|
+
newFrom list + data
|
99
|
+
end
|
100
100
|
|
101
|
-
def exclude(object)
|
102
|
-
exclude_people(into_a(object))
|
101
|
+
def exclude(object, strict: false)
|
102
|
+
exclude_people(into_a(object), strict: strict)
|
103
103
|
end
|
104
104
|
|
105
|
-
def exclude_people(list)
|
105
|
+
def exclude_people(list, strict: false)
|
106
106
|
discarded = list.map do |person|
|
107
|
-
find(person)
|
107
|
+
find(person, strict: strict)
|
108
108
|
end.compact
|
109
109
|
newFrom to_a - discarded
|
110
110
|
end
|
data/lib/eco/api/policies.rb
CHANGED
@@ -1,10 +1,33 @@
|
|
1
1
|
module Eco
|
2
2
|
module API
|
3
|
-
|
3
|
+
class Policies < Eco::API::UseCases
|
4
|
+
attr_reader :policies
|
5
|
+
|
6
|
+
def add(policy)
|
7
|
+
raise "Expected Eco::API::Policies::Policy object. Given: #{policy}" unless policy.is_a?(Eco::API::Policies::Policy)
|
8
|
+
super(policy)
|
9
|
+
end
|
10
|
+
|
11
|
+
def define(name, type: :transform, &block)
|
12
|
+
Eco::API::Policies::Policy.new(name, type: type, root: self, &block).tap do |policy|
|
13
|
+
add(policy)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def launch(people:, session:, options: {})
|
18
|
+
|
19
|
+
args = {
|
20
|
+
people: people,
|
21
|
+
session: session,
|
22
|
+
options: options
|
23
|
+
}
|
24
|
+
|
25
|
+
items.map do |policy|
|
26
|
+
policy.launch(**args)
|
27
|
+
end
|
28
|
+
end
|
4
29
|
end
|
5
30
|
end
|
6
31
|
end
|
7
32
|
|
8
|
-
require_relative 'policies/base_policy'
|
9
33
|
require_relative 'policies/policy'
|
10
|
-
require_relative 'policies/used_policies'
|
@@ -1,16 +1,15 @@
|
|
1
1
|
module Eco
|
2
2
|
module API
|
3
|
-
|
3
|
+
class Policies
|
4
4
|
class Policy < Eco::API::UseCases::UseCase
|
5
|
-
|
6
|
-
|
5
|
+
@types = [:transform]
|
6
|
+
|
7
7
|
def initialize(name, type: :transform, root:, &block)
|
8
|
-
raise "Undefine usecase type #{type}. Please, use any of #{TYPES}" unless self.class.valid_type?(type)
|
9
8
|
super(name, type: type, root: root, &block)
|
10
9
|
end
|
11
10
|
|
12
11
|
def root=(value)
|
13
|
-
raise "Root should be a
|
12
|
+
raise "Root should be a Eco::API::Policies object. Given: #{value}" if !value.is_a?(Eco::API::Policies)
|
14
13
|
@root = value
|
15
14
|
end
|
16
15
|
|
data/lib/eco/api/session.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Eco
|
2
2
|
module API
|
3
3
|
class Session < Common::Session::BaseSession
|
4
|
-
attr_reader :batch
|
4
|
+
attr_reader :batch
|
5
5
|
attr_reader :usecases
|
6
6
|
attr_accessor :schema
|
7
7
|
|
@@ -16,12 +16,8 @@ module Eco
|
|
16
16
|
|
17
17
|
@batch = Batch.new(enviro)
|
18
18
|
@task = Task.new(enviro)
|
19
|
-
@job_groups = JobGroups.new(enviro)
|
20
|
-
|
21
|
-
@usecases = Eco::API::UseCases::DefaultCases.new.merge(config.usecases)
|
22
19
|
|
23
20
|
self.schema = config.people.default_schema || schemas.first
|
24
|
-
@policy_groups = config.policy_groups
|
25
21
|
|
26
22
|
presets_custom_file = config.people.presets_custom
|
27
23
|
presets_map_file = config.people.presets_map
|
@@ -112,6 +108,10 @@ module Eco
|
|
112
108
|
end
|
113
109
|
end
|
114
110
|
|
111
|
+
def export(*args)
|
112
|
+
@entry_factory.export(*args)
|
113
|
+
end
|
114
|
+
|
115
115
|
def new_person(**keyed_args)
|
116
116
|
@person_factory.new(**keyed_args)
|
117
117
|
end
|
@@ -126,25 +126,38 @@ module Eco
|
|
126
126
|
end
|
127
127
|
end
|
128
128
|
|
129
|
-
def export(*args)
|
130
|
-
@entry_factory.export(*args)
|
131
|
-
end
|
132
|
-
|
133
129
|
def csv_entries(file)
|
134
130
|
return entries(file: file, format: :csv)
|
135
131
|
end
|
136
132
|
|
137
133
|
def discarded_entries
|
134
|
+
return @discarded_entries if instance_variable_defined?(:@discarded_entries)
|
138
135
|
file = config.people.discarded_file
|
139
136
|
file = file_manager.dir.file(file)
|
140
|
-
csv_entries(file)
|
137
|
+
@discarded_entries = csv_entries(file)
|
141
138
|
end
|
142
139
|
|
143
|
-
def
|
140
|
+
def workflow(io:)
|
141
|
+
config.workflow.tap do |wf|
|
142
|
+
yield(wf, io) if block_given?
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def usecases
|
147
|
+
@usecases ||= Eco::API::UseCases::DefaultCases.new.merge(config.usecases)
|
148
|
+
end
|
149
|
+
|
150
|
+
def post_launch
|
151
|
+
@post_launch ||= config.post_launch.select(usecases)
|
152
|
+
end
|
153
|
+
|
154
|
+
def process_case(name, io: nil, type: nil, **params)
|
144
155
|
args = { session: self }.merge(params)
|
145
|
-
|
146
|
-
|
147
|
-
|
156
|
+
usecases.case(name, type: type).launch(io: io, **args)
|
157
|
+
end
|
158
|
+
|
159
|
+
def job_groups
|
160
|
+
@job_groups ||= Batch::JobsGroups.new(enviro)
|
148
161
|
end
|
149
162
|
|
150
163
|
def job_group(name, order: :last)
|
@@ -166,8 +179,4 @@ end
|
|
166
179
|
|
167
180
|
require_relative 'session/config'
|
168
181
|
require_relative 'session/batch'
|
169
|
-
require_relative 'session/batch_status'
|
170
182
|
require_relative 'session/task'
|
171
|
-
require_relative 'session/batch_job'
|
172
|
-
require_relative 'session/batch_jobs'
|
173
|
-
require_relative 'session/job_groups'
|
@@ -23,9 +23,9 @@ module Eco
|
|
23
23
|
# @option params [String] :per_page the number of people included per each batch api request.
|
24
24
|
# @option params [String] :q some text to search. Omit this parameter to target all the people.
|
25
25
|
# @return [Array<People>] all the people based on `params`
|
26
|
-
def get_people(people = nil, params: {})
|
27
|
-
return launch(people, method: :get, params: params) if people.is_a?(Enumerable)
|
28
|
-
return get(params: params)
|
26
|
+
def get_people(people = nil, params: {}, silent: false)
|
27
|
+
return launch(people, method: :get, params: params, silent: silent) if people.is_a?(Enumerable)
|
28
|
+
return get(params: params, silent: silent)
|
29
29
|
end
|
30
30
|
|
31
31
|
# launches a batch of `method` type using `people` and the specified `params`
|
@@ -36,7 +36,7 @@ module Eco
|
|
36
36
|
# @param method [Symbol] the method to launch the batch api request with.
|
37
37
|
# @param params [Hash] api request options.
|
38
38
|
# @option params [String] :per_page the number of people included per each batch api request.
|
39
|
-
# @return [
|
39
|
+
# @return [Batch::Status] the `status` of this batch launch.
|
40
40
|
def launch(people, method:, params: {} , silent: false)
|
41
41
|
batch_from(people, method: method, params: params, silent: silent)
|
42
42
|
end
|
@@ -87,7 +87,7 @@ module Eco
|
|
87
87
|
private
|
88
88
|
|
89
89
|
def new_status(queue, method)
|
90
|
-
|
90
|
+
Batch::Status.new(enviro, queue: queue, method: method)
|
91
91
|
end
|
92
92
|
|
93
93
|
def get(params: {}, silent: false)
|
@@ -174,7 +174,7 @@ module Eco
|
|
174
174
|
done += slice.length
|
175
175
|
end # next slice
|
176
176
|
|
177
|
-
status.
|
177
|
+
status.errors.print unless silent
|
178
178
|
return status
|
179
179
|
end
|
180
180
|
|
@@ -182,3 +182,9 @@ module Eco
|
|
182
182
|
end
|
183
183
|
end
|
184
184
|
end
|
185
|
+
|
186
|
+
require_relative 'batch/job'
|
187
|
+
require_relative 'batch/status'
|
188
|
+
require_relative 'batch/errors'
|
189
|
+
require_relative 'batch/jobs'
|
190
|
+
require_relative 'batch/jobs_groups'
|
@@ -0,0 +1,134 @@
|
|
1
|
+
module Eco
|
2
|
+
module API
|
3
|
+
class Session
|
4
|
+
class Batch
|
5
|
+
class Errors
|
6
|
+
|
7
|
+
attr_reader :status
|
8
|
+
|
9
|
+
def initialize(status:)
|
10
|
+
"Expected Batch::Status as root. Given: #{status.class}" unless status.is_a?(Batch::Status)
|
11
|
+
@status = status
|
12
|
+
end
|
13
|
+
|
14
|
+
def queue
|
15
|
+
status.queue
|
16
|
+
end
|
17
|
+
|
18
|
+
def method
|
19
|
+
status.method
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_index(*args)
|
23
|
+
status.to_index(*args)
|
24
|
+
end
|
25
|
+
|
26
|
+
def session
|
27
|
+
status.session
|
28
|
+
end
|
29
|
+
|
30
|
+
def logger
|
31
|
+
status.logger
|
32
|
+
end
|
33
|
+
|
34
|
+
def any?
|
35
|
+
queue.any? {|query| !status[query].success?}
|
36
|
+
end
|
37
|
+
|
38
|
+
def entries
|
39
|
+
queue.each_with_index.map do |query, i|
|
40
|
+
unless status[i]
|
41
|
+
msg = "Error: query with no response. You might have duplicated entries in your queue.\n"
|
42
|
+
msg += "Queue length: #{queue.length}; Queue elements class: #{queue.first.class}\n"
|
43
|
+
msg += "Query with no response. Person: #{person_ref(query)}\n"
|
44
|
+
queue.map do |entry|
|
45
|
+
if [:id, :external_id, :email].any? {|attr| (v = get_attr(entry, attr)) && v == get_attr(query, attr)}
|
46
|
+
msg += "It could be this peson entry (idx: #{to_index(entry)}): #{person_ref(entry)}\n"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
raise msg
|
50
|
+
end
|
51
|
+
|
52
|
+
status[i].success? ? nil : query
|
53
|
+
end.compact
|
54
|
+
end
|
55
|
+
|
56
|
+
def errors
|
57
|
+
entries.each_with_object([]) do |entry, arr|
|
58
|
+
if errs = status[entry].body["errors"]
|
59
|
+
errs.each do |msg|
|
60
|
+
arr.push({
|
61
|
+
type: klass = Eco::API::Error.get_type(msg),
|
62
|
+
err: klass.new(err_msg: msg, entry: entry, session: session),
|
63
|
+
entry: entry
|
64
|
+
})
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def by_type
|
71
|
+
errors.group_by do |h|
|
72
|
+
h[:type]
|
73
|
+
end.transform_values do |arr|
|
74
|
+
arr.map {|h| h[:entry]}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def count
|
79
|
+
entries.length
|
80
|
+
end
|
81
|
+
|
82
|
+
def str(key)
|
83
|
+
msg = ""
|
84
|
+
unless status.success?(key)
|
85
|
+
i = to_index(key)
|
86
|
+
entry = queue.to_a[i]
|
87
|
+
response = status[i]
|
88
|
+
msg = "Error #{response.status}: #{response.body}\n"
|
89
|
+
msg += "-- Failed to batch #{method} (entry #{i+1}). Person: #{person_ref(entry)}"
|
90
|
+
end
|
91
|
+
msg
|
92
|
+
end
|
93
|
+
|
94
|
+
def strs
|
95
|
+
by_type.values.flatten(1).each_with_object([]) do |query, msgs|
|
96
|
+
msgs.push(str(query))
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def print_one(key)
|
101
|
+
unless status.success?(key)
|
102
|
+
logger.error(str(key))
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def print
|
107
|
+
msgs = strs
|
108
|
+
if msgs.length > 0
|
109
|
+
logger.info()
|
110
|
+
logger.error("There were #{msgs.length} errors:\n" + msgs.join("\n"))
|
111
|
+
else
|
112
|
+
logger.info("There were no errors for the current batch '#{method}'!! ;)")
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
|
118
|
+
def person_ref(entry)
|
119
|
+
"(id: '#{get_attr(entry, :id)}') '#{get_attr(entry, :name)}' ('#{get_attr(entry, :external_id)}': '#{get_attr(entry, :email)}')"
|
120
|
+
end
|
121
|
+
|
122
|
+
def get_attr(entry, attr)
|
123
|
+
if entry.respond_to?(attr.to_sym)
|
124
|
+
entry.public_send(attr.to_sym)
|
125
|
+
elsif entry.is_a?(Hash)
|
126
|
+
entry["#{attr}"]
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,213 @@
|
|
1
|
+
module Eco
|
2
|
+
module API
|
3
|
+
class Session
|
4
|
+
class Batch
|
5
|
+
class Job < API::Common::Session::BaseSession
|
6
|
+
@types = [:get, :create, :update, :delete]
|
7
|
+
@sets = [:core, :details, :account]
|
8
|
+
|
9
|
+
class << self
|
10
|
+
attr_reader :types, :sets
|
11
|
+
|
12
|
+
def valid_type?(value)
|
13
|
+
types.include?(value)
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid_sets?(value)
|
17
|
+
sts = [value].flatten
|
18
|
+
sts.all? { |s| sets.include?(s) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_reader :name, :type, :status
|
23
|
+
attr_reader :usecase
|
24
|
+
|
25
|
+
def initialize(e, name:, type:, sets:, usecase: nil)
|
26
|
+
raise "A name is required to refer a job. Given: #{name}" if !name
|
27
|
+
raise "Type should be one of #{self.class.types}. Given: #{type}" if !self.class.valid_type?(type)
|
28
|
+
raise "Sets should be some of #{self.class.sets}. Given: #{sets}" if !self.class.valid_sets?(sets)
|
29
|
+
raise "usecase must be a Eco::API::UseCases::UseCase object. Given: #{usecase.class}" if usecase && !usecase.is_a?(Eco::API::UseCases::UseCase)
|
30
|
+
super(e)
|
31
|
+
|
32
|
+
@name = name
|
33
|
+
@type = type
|
34
|
+
@usecase = usecase
|
35
|
+
@sets = [sets].flatten.compact
|
36
|
+
reset
|
37
|
+
end
|
38
|
+
|
39
|
+
def reset
|
40
|
+
@queue = []
|
41
|
+
@queue_hash = {}
|
42
|
+
@callbacks = {}
|
43
|
+
@pending = true
|
44
|
+
@status = nil
|
45
|
+
end
|
46
|
+
|
47
|
+
def usecase?
|
48
|
+
!!usecase
|
49
|
+
end
|
50
|
+
|
51
|
+
def options
|
52
|
+
usecase?? usecase.options : {}
|
53
|
+
end
|
54
|
+
|
55
|
+
def signature
|
56
|
+
"Batch job \"#{name}\" ['#{type.to_s.upcase}': #{sets_title}]"
|
57
|
+
end
|
58
|
+
|
59
|
+
def match?(type:, sets:)
|
60
|
+
sets = [sets].flatten
|
61
|
+
type == self.type && (sets.order == @sets.order)
|
62
|
+
end
|
63
|
+
|
64
|
+
def pending?
|
65
|
+
@pending
|
66
|
+
end
|
67
|
+
|
68
|
+
# Adds an entry(ies) to the job queue.
|
69
|
+
# @param entry [Person, Enumberable<Person>] the person(s) we want to update, carrying the changes to be done.
|
70
|
+
# @param unique [Boolean] specifies if repeated entries should be avoided in the queue.
|
71
|
+
# @yield [person] callback before launching the batch job request against the server.
|
72
|
+
# @yeldparam param [Person] current person object that that should be treated by the callback before launching the batch.
|
73
|
+
# @return [Void]
|
74
|
+
def add(entry, unique: true, &block)
|
75
|
+
case entry
|
76
|
+
when Enumerable
|
77
|
+
entry.each {|e| add(e, unique: unique, &block)}
|
78
|
+
else
|
79
|
+
unless !entry
|
80
|
+
unless unique && @queue_hash.key?(entry)
|
81
|
+
@queue_hash[entry] = true
|
82
|
+
@queue.push(entry)
|
83
|
+
@callbacks[entry] = Proc.new if block_given?
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def people(input = @queue)
|
90
|
+
Eco::API::Organization::People.new(input)
|
91
|
+
end
|
92
|
+
|
93
|
+
def launch(simulate: false)
|
94
|
+
queue = processed_queue
|
95
|
+
launch_feedback(queue, simulate ? 2500 : 800)
|
96
|
+
|
97
|
+
if !simulate
|
98
|
+
if queue.length > 0
|
99
|
+
backup_update(queue)
|
100
|
+
@status = session.batch.launch(queue, method: type)
|
101
|
+
@status.root = self
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
post_launch(queue: queue, simulate: simulate)
|
106
|
+
|
107
|
+
logger.info("Simulate: this would have launched: '#{type}'") if simulate
|
108
|
+
@pending = false
|
109
|
+
return @status
|
110
|
+
end
|
111
|
+
|
112
|
+
private
|
113
|
+
|
114
|
+
def processed_queue
|
115
|
+
@queue.each {|e| @callbacks[e].call(e) if @callbacks.key?(e) }
|
116
|
+
apply_policies(@queue).select {|e| !as_update(e).empty?}
|
117
|
+
end
|
118
|
+
|
119
|
+
def post_launch(queue: [], simulate: false)
|
120
|
+
if !simulate && @status
|
121
|
+
@status.queue.map do |entry|
|
122
|
+
if @status.success?(entry)
|
123
|
+
entry.consolidate! if entry.respond_to?(:consolidate!)
|
124
|
+
#else # do not entry.reset! (keep track on changes still)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
# launch_error handlers
|
128
|
+
handlers = session.config.error_handlers
|
129
|
+
if @status.errors.any? && !handlers.empty?
|
130
|
+
err_types = @status.errors.by_type
|
131
|
+
handlers.each do |hd|
|
132
|
+
if entries = err_types[hd.name]
|
133
|
+
hd.launch(people: people(entries), session: session, options: options)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
elsif simulate
|
138
|
+
queue.map do |entry|
|
139
|
+
entry.consolidate! if entry.respond_to?(:consolidate!)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def apply_policies(pre_queue)
|
145
|
+
pre_queue.tap do |entries|
|
146
|
+
policies = session.config.policies
|
147
|
+
unless policies.empty?
|
148
|
+
policies.launch(people: people(entries), session: session, options: options)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def as_update(entry)
|
154
|
+
hash = entry if entry.is_a?(Hash)
|
155
|
+
if only_ids?
|
156
|
+
hash = entry.as_json.slice("id", "external_id", "email")
|
157
|
+
else
|
158
|
+
if entry.is_a?(Ecoportal::API::V1::Person)
|
159
|
+
hash = entry.as_update
|
160
|
+
if hfields = hash.dig("details", "fields")
|
161
|
+
hash["details"]["fields"] = hfields.map do |fld|
|
162
|
+
fld.merge!("alt_id" => entry.details.get_field(fld["id"]).alt_id) if entry.details
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
fields = hash&.dig('details', 'fields')
|
168
|
+
fields&.map! { |fld| fld&.slice("id", "alt_id", "value") }
|
169
|
+
end
|
170
|
+
hash || {}
|
171
|
+
end
|
172
|
+
|
173
|
+
def only_ids?
|
174
|
+
[:delete, :get].include?(type)
|
175
|
+
end
|
176
|
+
|
177
|
+
def sets_title
|
178
|
+
"#{@sets.map {|s| s.to_s}.join(", ")}"
|
179
|
+
end
|
180
|
+
|
181
|
+
def launch_feedback(data, max_chars = 800)
|
182
|
+
if !data || !data.is_a?(Enumerable) || data.empty?
|
183
|
+
logger.warn("#{"*" * 20} Nothing for #{signature} so far :) #{"*" * 20}")
|
184
|
+
return
|
185
|
+
end
|
186
|
+
header = ("*" * 20) + " #{signature} - Feedback Sample " + ("*" * 20)
|
187
|
+
logger.info(header)
|
188
|
+
|
189
|
+
sample_length = 1
|
190
|
+
sample = data.slice(0, 20).map do |entry|
|
191
|
+
update = as_update(entry)
|
192
|
+
max_chars -= update.pretty_inspect.length
|
193
|
+
sample_length += 1 if max_chars > 0
|
194
|
+
update
|
195
|
+
end
|
196
|
+
|
197
|
+
logger.info("#{sample.slice(0, sample_length).pretty_inspect}")
|
198
|
+
logger.info("#{type.to_s.upcase} length: #{data.length}")
|
199
|
+
logger.info("*" * header.length)
|
200
|
+
end
|
201
|
+
|
202
|
+
def backup_update(data)
|
203
|
+
data_body = data.map { |u| as_update(u) }
|
204
|
+
dir = config.people.requests_folder
|
205
|
+
file = File.join(dir, "#{type}_data.json")
|
206
|
+
file_manager.save_json(data_body, file, :timestamp)
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|