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.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eco/api.rb +1 -0
  3. data/lib/eco/api/common.rb +1 -0
  4. data/lib/eco/api/common/class_helpers.rb +33 -0
  5. data/lib/eco/api/common/people.rb +1 -0
  6. data/lib/eco/api/common/people/person_attribute_parser.rb +52 -0
  7. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +2 -2
  8. data/lib/eco/api/common/people/person_parser.rb +16 -10
  9. data/lib/eco/api/common/version_patches.rb +2 -3
  10. data/lib/eco/api/common/version_patches/ecoportal_api.rb +4 -0
  11. data/lib/eco/api/common/version_patches/{base_model.rb → ecoportal_api/base_model.rb} +0 -0
  12. data/lib/eco/api/common/version_patches/{external_person.rb → ecoportal_api/external_person.rb} +0 -0
  13. data/lib/eco/api/common/version_patches/{internal_person.rb → ecoportal_api/internal_person.rb} +0 -0
  14. data/lib/eco/api/common/version_patches/hash.rb +2 -0
  15. data/lib/eco/api/common/version_patches/hash/deep_merge.rb +34 -0
  16. data/lib/eco/api/error.rb +133 -0
  17. data/lib/eco/api/error/handler.rb +19 -0
  18. data/lib/eco/api/error/handlers.rb +22 -0
  19. data/lib/eco/api/organization/people.rb +11 -11
  20. data/lib/eco/api/organization/policy_groups.rb +8 -0
  21. data/lib/eco/api/policies.rb +26 -3
  22. data/lib/eco/api/policies/policy.rb +4 -5
  23. data/lib/eco/api/session.rb +27 -18
  24. data/lib/eco/api/session/batch.rb +12 -6
  25. data/lib/eco/api/session/batch/errors.rb +134 -0
  26. data/lib/eco/api/session/batch/job.rb +213 -0
  27. data/lib/eco/api/session/batch/jobs.rb +72 -0
  28. data/lib/eco/api/session/batch/jobs_groups.rb +85 -0
  29. data/lib/eco/api/session/batch/status.rb +133 -0
  30. data/lib/eco/api/session/config.rb +36 -14
  31. data/lib/eco/api/session/config/base_config.rb +2 -0
  32. data/lib/eco/api/session/config/people.rb +8 -0
  33. data/lib/eco/api/session/config/post_launch.rb +58 -0
  34. data/lib/eco/api/session/config/workflow.rb +189 -0
  35. data/lib/eco/api/session/task.rb +49 -6
  36. data/lib/eco/api/usecases.rb +137 -2
  37. data/lib/eco/api/usecases/base_case.rb +20 -8
  38. data/lib/eco/api/usecases/base_io.rb +97 -0
  39. data/lib/eco/api/usecases/default_case.rb +19 -0
  40. data/lib/eco/api/usecases/default_cases.rb +2 -2
  41. data/lib/eco/api/usecases/default_cases/change_email_case.rb +2 -2
  42. data/lib/eco/api/usecases/default_cases/create_case.rb +2 -2
  43. data/lib/eco/api/usecases/default_cases/create_details_case.rb +2 -2
  44. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +2 -2
  45. data/lib/eco/api/usecases/default_cases/delete_case.rb +2 -2
  46. data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +2 -2
  47. data/lib/eco/api/usecases/default_cases/hris_case.rb +2 -2
  48. data/lib/eco/api/usecases/default_cases/new_email_case.rb +2 -2
  49. data/lib/eco/api/usecases/default_cases/new_id_case.rb +2 -2
  50. data/lib/eco/api/usecases/default_cases/recover_db_case.rb +11 -8
  51. data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +2 -2
  52. data/lib/eco/api/usecases/default_cases/reinvite_case.rb +2 -2
  53. data/lib/eco/api/usecases/default_cases/remove_account_case.rb +2 -2
  54. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +2 -2
  55. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +2 -2
  56. data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +2 -2
  57. data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +2 -2
  58. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +4 -4
  59. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +3 -3
  60. data/lib/eco/api/usecases/default_cases/update_case.rb +2 -2
  61. data/lib/eco/api/usecases/default_cases/update_details_case.rb +2 -2
  62. data/lib/eco/api/usecases/default_cases/upsert_case.rb +2 -2
  63. data/lib/eco/api/usecases/use_case.rb +23 -36
  64. data/lib/eco/api/usecases/use_case_chain.rb +14 -24
  65. data/lib/eco/api/usecases/use_case_io.rb +23 -75
  66. data/lib/eco/assets.rb +11 -11
  67. data/lib/eco/cli.rb +37 -0
  68. data/lib/eco/cli/config.rb +63 -1
  69. data/lib/eco/cli/config/default.rb +15 -0
  70. data/lib/eco/cli/config/default/filters.rb +69 -0
  71. data/lib/eco/cli/config/default/input.rb +21 -0
  72. data/lib/eco/cli/config/default/options.rb +47 -0
  73. data/lib/eco/cli/config/default/people.rb +39 -0
  74. data/lib/eco/cli/config/default/usecases.rb +63 -0
  75. data/lib/eco/cli/config/default/workflow.rb +86 -0
  76. data/lib/eco/cli/config/input.rb +40 -0
  77. data/lib/eco/cli/config/options_set.rb +35 -0
  78. data/lib/eco/cli/config/people_filters.rb +38 -0
  79. data/lib/eco/cli/config/use_cases.rb +87 -0
  80. data/lib/eco/cli/scripting/args_helpers.rb +10 -4
  81. data/lib/eco/cli/scripting/argument.rb +6 -0
  82. data/lib/eco/language/models/collection.rb +1 -0
  83. data/lib/eco/version.rb +1 -1
  84. metadata +32 -12
  85. data/lib/eco/api/policies/base_policy.rb +0 -14
  86. data/lib/eco/api/policies/used_policies.rb +0 -37
  87. data/lib/eco/api/session/batch_job.rb +0 -215
  88. data/lib/eco/api/session/batch_jobs.rb +0 -62
  89. data/lib/eco/api/session/batch_status.rb +0 -205
  90. data/lib/eco/api/session/job_groups.rb +0 -75
  91. data/lib/eco/api/usecases/use_group.rb +0 -124
  92. data/lib/eco/cli/config/options.rb +0 -11
@@ -0,0 +1,72 @@
1
+ module Eco
2
+ module API
3
+ class Session
4
+ class Batch
5
+ class Jobs < API::Common::Session::BaseSession
6
+ attr_reader :name
7
+
8
+ def initialize(e, name:)
9
+ super(e)
10
+ @name = name
11
+ reset
12
+ end
13
+
14
+ def reset
15
+ @jobs = {}
16
+ @callbacks = {}
17
+ end
18
+
19
+ def [](name)
20
+ @jobs[name]
21
+ end
22
+
23
+ def exists?(name)
24
+ @jobs.key?(name)
25
+ end
26
+
27
+ def job(name, type: nil, sets: nil, usecase: nil, &block)
28
+ new(name, type: type, sets: sets, usecase: usecase, &block) unless exists?(name)
29
+ self[name].tap do |job|
30
+ block.call(job) if block
31
+ end
32
+ end
33
+
34
+ def new(name, type:, sets:, usecase: nil)
35
+ fatal "Can't create job named '#{name}' because it already exists." if exists?(name)
36
+
37
+ Batch::Job.new(enviro, name: name, type: type, sets: sets, usecase: usecase).tap do |job|
38
+ @jobs[name] = job
39
+ @callbacks[job] = Proc.new if block_given?
40
+ end
41
+ end
42
+
43
+ def pending?
44
+ @jobs.keys.any? {|key| @jobs[key].pending?}
45
+ end
46
+
47
+ def launch(simulate: false)
48
+ group_status = {}
49
+ @jobs.each do |name, job|
50
+ if job.pending?
51
+ group_status[job] = job_status = job.launch(simulate: simulate)
52
+ callback = @callbacks[job]
53
+ callback.call(job, job_status) if callback
54
+ end
55
+ end
56
+
57
+ return group_status
58
+ end
59
+
60
+ def find_jobs(type:)
61
+ @jobs.each_with_object([]) do |(k, jb), jbs|
62
+ if jb.type == type
63
+ jbs.push(jb)
64
+ end
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,85 @@
1
+ module Eco
2
+ module API
3
+ class Session
4
+ class Batch
5
+ class JobsGroups < API::Common::Session::BaseSession
6
+ DELAY_BETWEEN_GROUPS = 2
7
+
8
+ class << self
9
+ def counter(seconds)
10
+ puts "\n"
11
+ while seconds + 1 > 0 do
12
+ print " Waiting #{seconds}\r"
13
+ $stdout.flush
14
+ seconds -= 1
15
+ sleep 1
16
+ end
17
+ print "#{" " * 40}"
18
+ $stdout.flush
19
+ puts ""
20
+ end
21
+ end
22
+
23
+ def initialize(e)
24
+ super(e)
25
+ reset
26
+ end
27
+
28
+ def reset
29
+ @order = []
30
+ @groups = {}
31
+ @callbacks = {}
32
+ end
33
+
34
+ def [](name)
35
+ @groups[name]
36
+ end
37
+
38
+ def exists?(name)
39
+ @groups.key?(name)
40
+ end
41
+
42
+ def new(name, order: :last)
43
+ fatal "Can't create job group named '#{name}' because it already exists." if exists?(name)
44
+
45
+ Batch::Jobs.new(enviro, name: name).tap do |group|
46
+ @groups[name] = group
47
+
48
+ if order == :last
49
+ @order.push(group)
50
+ else
51
+ @order.unshift(group)
52
+ end
53
+
54
+ @callbacks[group] = Proc.new if block_given?
55
+ end
56
+ end
57
+
58
+ def pending?
59
+ @groups.any? {|group| group.pending?}
60
+ end
61
+
62
+ def launch(simulate: false)
63
+ groups_status = {}
64
+ @order.each.with_index do |group, idx|
65
+ if group.pending?
66
+ groups_status[group] = group_status = group.launch(simulate: simulate)
67
+ callback = @callbacks[group]
68
+ callback.call(group, group_status) if callback
69
+ self.class.counter(DELAY_BETWEEN_GROUPS) if !simulate && idx < @order.length - 1
70
+ end
71
+ end
72
+ return groups_status
73
+ end
74
+
75
+ def find_jobs(type:)
76
+ @groups.each_with_object([]) do |(k, gr), jbs|
77
+ jbs.concat(gr.find_jobs(type: type))
78
+ end
79
+ end
80
+
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,133 @@
1
+ module Eco
2
+ module API
3
+ class Session
4
+ class Batch
5
+ class Status < Common::Session::BaseSession
6
+ @types = [:exact, :search]
7
+
8
+ attr_reader :source_queue
9
+ attr_reader :queue, :method, :type
10
+ attr_reader :root
11
+
12
+ class << self
13
+ attr_reader :types
14
+
15
+ def valid_type?(value)
16
+ types.include?(value)
17
+ end
18
+ end
19
+
20
+ def initialize(e, queue:, method:, type: :exact)
21
+ super(e)
22
+ fatal("In batch operations you must batch an Enumerable. Received: #{queue}") unless queue && queue.is_a?(Enumerable)
23
+
24
+ self.type = type
25
+ @method = method
26
+ @source_queue = queue
27
+
28
+ que = queue.to_a
29
+ que = queue if queue.respond_to?(:uniq)
30
+ if que.length != que.uniq.length
31
+ logger.warn("Please, review your entries-to-query builder, you have repeated entries")
32
+ queue = que.uniq
33
+ end
34
+
35
+ @queue = queue
36
+ @hash = @queue.each_with_index.map do |entry, i|
37
+ [entry, i]
38
+ end.to_h
39
+ @responses = []
40
+ @person_match = []
41
+ @people_match = Array.new(@queue.length, [])
42
+ end
43
+
44
+ def root=(object)
45
+ @root = object
46
+ end
47
+
48
+ def type=(value)
49
+ fatal("Invalid :type '#{value}. You must specify type: as one of #{self.class.types} ") unless self.class.valid_type?(value)
50
+ @type = value
51
+ end
52
+
53
+ def errors
54
+ @errors ||= Batch::Errors.new(status: self)
55
+ end
56
+
57
+ def [](key)
58
+ @responses[to_index(key)]
59
+ end
60
+
61
+
62
+ def []=(key, response)
63
+ @responses[to_index(key)] = response
64
+ end
65
+
66
+ def person(key)
67
+ return self[key].result if success?(key)
68
+ nil
69
+ end
70
+
71
+ def person_match(key)
72
+ @person_match[to_index(key)]
73
+ end
74
+
75
+ def set_person_match(key, person)
76
+ @person_match[to_index(key)] = person
77
+ end
78
+
79
+ def people_match(key)
80
+ @people_match[to_index(key)]
81
+ end
82
+
83
+ def set_people_match(key, people)
84
+ @people_match[to_index(key)] = people
85
+ end
86
+
87
+ def received?(key)
88
+ !!self[key]
89
+ end
90
+
91
+ def success?(key)
92
+ self[key]&.success?
93
+ end
94
+
95
+ def people
96
+ fatal "This batch wasn't a 'get'. Can't obtain people without 'get' method" unless method == :get
97
+ #out = Eco::API::Organization::People.new([])
98
+ if type == :exact
99
+ out = Array(queue.length)
100
+ @responses.each_with_index do |response, i|
101
+ # out = out.merge([response.result]) if respose.success?
102
+ out[i] = response.result if response.success?
103
+ end
104
+ elsif type == :search
105
+ out = []
106
+ queue.each_with_index.map do |entry, i|
107
+ pers = person(entry)
108
+ pers ||= person_match(entry)
109
+ ppl = pers ? [pers] : people_match(entry)
110
+ out += ppl
111
+ # out = out.merge(ppl) unless ppl.empty?
112
+ end
113
+ end
114
+ out
115
+ end
116
+
117
+ def to_index(key)
118
+ key.is_a?(Integer) ? valid_index(index: key) : valid_index(entry: key)
119
+ end
120
+
121
+ def valid_index(index: nil, entry: nil)
122
+ index ||= @hash[entry]
123
+ unless index && index < @queue.length
124
+ fatal "You must provide either the index or the original entry object of the batch"
125
+ end
126
+ index
127
+ end
128
+
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -24,10 +24,6 @@ module Eco
24
24
  yield(self)
25
25
  end
26
26
 
27
- def cli
28
- self["cli"] ||= Eco::CLI::Config.new
29
- end
30
-
31
27
  def apis
32
28
  self["apis"] ||= Eco::API::Session::Config::Apis.new(config: self)
33
29
  end
@@ -140,7 +136,6 @@ module Eco
140
136
  def require(file = nil, match: nil)
141
137
  if match
142
138
  file_manager.dir.dir_files(pattern: match).each do |file|
143
- #pp "#{File.expand_path(file)}"
144
139
  require_relative File.expand_path(file)
145
140
  end
146
141
  else
@@ -159,8 +154,9 @@ module Eco
159
154
  end
160
155
 
161
156
  def locations_mapper
157
+ return @locations_mapper if instance_variable_defined?(:@locations_mapper)
162
158
  file = file_manager.newest(location_codes)
163
- @locations_mapper ||= Eco::Data::Mapper.new(file_manager.load_json(file), internal: :first)
159
+ @locations_mapper = Eco::Data::Mapper.new(file_manager.load_json(file), internal: :first)
164
160
  end
165
161
 
166
162
  def tagtree=(file)
@@ -193,6 +189,10 @@ module Eco
193
189
  end
194
190
 
195
191
  # PEOPLE
192
+ def default_usergroup=(value)
193
+ people.default_usergroup = value
194
+ end
195
+
196
196
  def discarded_people_file=(value)
197
197
  people.discarded_file = value
198
198
  end
@@ -228,28 +228,48 @@ module Eco
228
228
  people.parser(format: format, &block)
229
229
  end
230
230
 
231
- # CUSTOM USE CASES
232
231
  def usecases
233
- cases = self["usecases"] ||= Eco::API::UseCases::UseGroup.new
232
+ @usecases = self["usecases"] ||= Eco::API::UseCases.new
234
233
  if block_given?
235
- yield(cases)
234
+ yield(@usecases)
236
235
  self
237
236
  else
238
- cases
237
+ @usecases
239
238
  end
240
239
  end
241
240
 
242
- # API POLICIES
241
+ def post_launch
242
+ self["post_launch"] ||= Eco::API::Session::Config::PostLaunch.new(config: self)
243
+ end
244
+
243
245
  def policies
244
- api_policies = self["policies"] ||= Eco::API::Policies::UsedPolicies.new
246
+ @policies = self["policies"] ||= Eco::API::Policies.new
247
+ if block_given?
248
+ yield(@policies)
249
+ self
250
+ else
251
+ @policies
252
+ end
253
+ end
254
+
255
+ def error_handlers
256
+ @error_handlers = self["error_handlers"] ||= Eco::API::Error::Handlers.new
245
257
  if block_given?
246
- yield(api_policies)
258
+ yield(@error_handlers)
247
259
  self
248
260
  else
249
- api_policies
261
+ @error_handlers
250
262
  end
251
263
  end
252
264
 
265
+ def workflow
266
+ @workflow = self["workflow"] ||= Eco::API::Session::Config::Workflow.new(config: self)
267
+ @workflow.tap do |wf|
268
+ yield(wf) if block_given?
269
+ end
270
+ end
271
+
272
+
253
273
  end
254
274
  end
255
275
  end
@@ -263,3 +283,5 @@ require_relative 'config/mailer'
263
283
  require_relative 'config/s3_storage'
264
284
  require_relative 'config/files'
265
285
  require_relative 'config/people'
286
+ require_relative 'config/post_launch'
287
+ require_relative 'config/workflow'
@@ -3,7 +3,9 @@ module Eco
3
3
  class Session
4
4
  class Config
5
5
  class BaseConfig < Hash
6
+
6
7
  attr_reader :config
8
+
7
9
  def initialize(config:)
8
10
  super(nil)
9
11
  @config = config
@@ -49,6 +49,14 @@ module Eco
49
49
  end
50
50
 
51
51
  # person model
52
+ def default_usergroup=(value)
53
+ self["default_usergroup"] = value
54
+ end
55
+
56
+ def default_usergroup
57
+ self["default_usergroup"]
58
+ end
59
+
52
60
  def default_schema=(name)
53
61
  self["default_schema"] = name
54
62
  end
@@ -0,0 +1,58 @@
1
+ module Eco
2
+ module API
3
+ class Session
4
+ class Config
5
+ class PostLaunch < BaseConfig
6
+ include Enumerable
7
+
8
+ def initialize(config:)
9
+ @postcases = []
10
+ super(config: config)
11
+ end
12
+
13
+ def length
14
+ count
15
+ end
16
+
17
+ def empty?
18
+ count == 0
19
+ end
20
+
21
+ def each(params: {}, &block)
22
+ return to_enum(:each) unless block
23
+ items.each(&block)
24
+ end
25
+
26
+ def items
27
+ @postcases
28
+ end
29
+
30
+ def add(name, type: nil)
31
+ @postcases.push({
32
+ name: name,
33
+ type: type
34
+ })
35
+ end
36
+
37
+ def select(usecases)
38
+ raise "Required Eco::API::UseCases to select the target post cases" unless usecases.is_a?(Eco::API::UseCases)
39
+ each_with_object([]) do |pc, targets|
40
+ targets.push(usecases.case(pc[:name], type: pc[:type]))
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def by_name
47
+ @postcases.group_by {|pc| pc[:name]}
48
+ end
49
+
50
+ def by_type
51
+ @postcases.group_by {|pc| pc[:type]}
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end