Class: Eco::API::MicroCases
- Inherits:
-
Common::Session::BaseSession
- Object
- Common::Session::BaseSession
- Eco::API::MicroCases
- Defined in:
- lib/eco/api/microcases.rb,
lib/eco/api/microcases/set_core.rb,
lib/eco/api/microcases/with_each.rb,
lib/eco/api/microcases/people_load.rb,
lib/eco/api/microcases/set_account.rb,
lib/eco/api/microcases/people_cache.rb,
lib/eco/api/microcases/core_excluded.rb,
lib/eco/api/microcases/people_search.rb,
lib/eco/api/microcases/strict_search.rb,
lib/eco/api/microcases/people_refresh.rb,
lib/eco/api/microcases/set_supervisor.rb,
lib/eco/api/microcases/fix_filter_tags.rb,
lib/eco/api/microcases/with_supervisor.rb,
lib/eco/api/microcases/account_excluded.rb,
lib/eco/api/microcases/s3upload_targets.rb,
lib/eco/api/microcases/with_each_leaver.rb,
lib/eco/api/microcases/append_usergroups.rb,
lib/eco/api/microcases/fix_default_group.rb,
lib/eco/api/microcases/with_each_present.rb,
lib/eco/api/microcases/with_each_starter.rb,
lib/eco/api/microcases/refresh_default_tag.rb,
lib/eco/api/microcases/preserve_default_tag.rb,
lib/eco/api/microcases/preserve_filter_tags.rb,
lib/eco/api/microcases/with_each_subordinate.rb,
lib/eco/api/microcases/preserve_policy_groups.rb,
lib/eco/api/microcases/set_core_with_supervisor.rb
Instance Attribute Summary
Attributes inherited from Common::Session::BaseSession
#api, #config, #environment, #file_manager, #logger, #session
Instance Method Summary collapse
-
#account_excluded(person, options) ⇒ Array<String>
The account parameters that should not be included.
-
#append_usergroups(entry, person, options) ⇒ Object
It preserves the usergroups of
personand appends those defined inpolicy_group_idsof theentry. -
#core_excluded(person, options) ⇒ Array<String>
The core parameters that should not be included.
-
#fix_default_group(entry, person, options) ⇒ Object
If defined, it sets the default usergroup, only when the
policy_group_idswas not part of the input data. -
#fix_filter_tags(person, options) ⇒ Object
Helper that makes sure the custom
tagsare preserved. - #micro ⇒ Object
-
#people_cache(filename = enviro.config.people.cache) ⇒ Eco::API::Organization::People
Helper to locally cache the people manager.
-
#people_load(filename = enviro.config.people.cache, modifier: [:newest, :api]) ⇒ Eco::API::Organization::People
Helper to load
Peoplethat works in different phases: 1. -
#people_refresh(people:, include_created: true) ⇒ Eco::API::Organization::People
Helper to obtain all the elements of
peopleanew from the People Manager. -
#people_search(data, options: {}, silent: true) ⇒ Eco::API::Organization::People
Helper to search/obtain people from
dataagainst the server (People Manager). -
#preserve_default_tag(person, options) ⇒ String
Helper to preserve the original
default_tag. -
#preserve_filter_tags(person, options, keep_new: false) ⇒ Array<String>
Helper to preserve the original filter tags.
-
#preserve_policy_groups(person, options, keep_new: false) ⇒ String
Helper to preserve the original
policy_group_ids. -
#refresh_default_tag(entry, person, options) ⇒ Object
When the input data, or
entry, does not provide thedefault_tag, it sets thedefault_tagof the user following some criteria. -
#s3upload_targets ⇒ Array<String>
Helper to upload target files to
S3. - #set_account(entry, person, options) ⇒ Object
-
#set_core(entry, person, options) ⇒ Object
Sets all the core details, but the supervisor.
-
#set_core_with_supervisor(entry, person, people, supers_job, options) ⇒ Object
Sets all the core details, but the supervisor.
-
#set_supervisor(person, sup_id, people, options) {|supervisor_id| ... } ⇒ Object
Unique access point to set the
supervisor_idvalue on a person. -
#strict_search?(options) ⇒ Boolean
When trying to find an
personwith given a sourceentry, it states if such a search should bestrictorsoft. -
#with_each(entries, people, options, append_created: true) {|entry, person| ... } ⇒ Eco::API::Organization::People
Finds each entry of
entriesinpeopleand runs a block. -
#with_each_leaver(entries, people, options) {|person| ... } ⇒ Eco::API::Organization::People
Detects who has left the organization and
yields them one by one to the given block. -
#with_each_present(entries, people, options, log_starter: false) {|entry, person| ... } ⇒ Eco::API::Organization::People
Finds those in
entriesthat already exist in the organization (people) andyields them one by one to the given block. -
#with_each_starter(entries, people, options, log_present: false, append_created: true) {|entry, person| ... } ⇒ Eco::API::Organization::People
Detects who in the
entriesis new in the organization andyields them one by one to the given block. -
#with_each_subordinate(supervisor, people) {|subordinate| ... } ⇒ Eco::API::Organization::People
Finds all the subordinates of
supervisor. -
#with_supervisor(value, people, strict: false) {|supervisor| ... } ⇒ nil, Ecoportal::API::V1::Person
Finds the supervisor among
peopleby using thesupervisor_idofvalue.
Methods inherited from Common::Session::BaseSession
#enviro=, #fatal, #fm, #initialize, #mailer, #mailer?, #s3uploader, #s3uploader?, #sftp, #sftp?
Constructor Details
This class inherits a constructor from Eco::API::Common::Session::BaseSession
Instance Method Details
#account_excluded(person, options) ⇒ Array<String>
Returns the account parameters that should not be included.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/eco/api/microcases/account_excluded.rb', line 7 def account_excluded(person, ) [].tap do |account_excluded| unless person.new? if .dig(:exclude, :policy_groups) account_excluded.push("policy_group_ids") end if .dig(:exclude, :default_tag) account_excluded.push("default_tag") end if .dig(:exclude, :login_providers) account_excluded.push("login_provider_ids") end end end end |
#append_usergroups(entry, person, options) ⇒ Object
It preserves the usergroups of person and appends those defined in policy_group_ids of the entry
8 9 10 11 12 13 14 |
# File 'lib/eco/api/microcases/append_usergroups.rb', line 8 def append_usergroups(entry, person, ) unless .dig(:exclude, :account) if person.account person.account.policy_group_ids |= entry.policy_group_ids end end end |
#core_excluded(person, options) ⇒ Array<String>
by default supervisor_id is always excluded.
Returns the core parameters that should not be included.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/eco/api/microcases/core_excluded.rb', line 8 def core_excluded(person, ) ["supervisor_id"].tap do |core_excluded| unless person.new? exclusions = ["name", "external_id", "email", "filter_tags"].select do |attr| .dig(:exclude, attr.to_sym) end core_excluded.concat(exclusions) end end end |
#fix_default_group(entry, person, options) ⇒ Object
If defined, it sets the default usergroup, only when the policy_group_ids was not part of the input data.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/eco/api/microcases/fix_default_group.rb', line 8 def fix_default_group(entry, person, ) unless .dig(:exclude, :account) unless .dig(:exclude, :policy_groups) && !person.new? end_pg_ids = person.account.policy_group_ids if person.account_added? && __def_usergroup_id && !entry.policy_group_ids? # on account creation, if missing policy_group_ids column in the input # use default_usergroup, if it's defined end_pg_ids = [__def_usergroup_id] end person.account.policy_group_ids = end_pg_ids end end end |
#fix_filter_tags(person, options) ⇒ Object
- this feature is essential to preserve custom
tagsin users that have register tags. - for this to work out, it requires a
tagtreeto be defined.
Helper that makes sure the custom tags are preserved.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/eco/api/microcases/fix_filter_tags.rb', line 13 def (person, ) if session.tagtree unless !person.new? && (.dig(:exclude, :core) || .dig(:exclude, :filter_tags)) person. = session.tagtree.( initial: person.original_doc["filter_tags"] || [], final: person., preserve_custom: (), add_custom: () ) end end end |
#micro ⇒ Object
5 6 7 |
# File 'lib/eco/api/microcases.rb', line 5 def micro self end |
#people_cache(filename = enviro.config.people.cache) ⇒ Eco::API::Organization::People
Helper to locally cache the people manager.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/eco/api/microcases/people_cache.rb', line 7 def people_cache(filename = enviro.config.people.cache) logger.info("Going to get all the people via API") start = Time.now people = session.batch.get_people secs = (Time.now - start).round(3) cnt = people.count per_sec = (cnt.to_f / secs).round(2) logger.info("Loaded #{cnt} people in #{secs} seconds (#{per_sec} people/sec)") file = file_manager.save_json(people, filename, :timestamp) logger.info("#{people.length} people loaded and saved locally to #{file}.") Eco::API::Organization::People.new(people) end |
#people_load(filename = enviro.config.people.cache, modifier: [:newest, :api]) ⇒ Eco::API::Organization::People
filenamewill be relative to the working directory (the one of the sessionenviroset by the user).
Helper to load People that works in different phases:
- first tries to get the newest cached file that follows
filenamepattern- if not the newest, it tries to find the specific filename
- if it succeeds to identif a cached file, it loads it
- if it fails, it tries to get people from the server
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/eco/api/microcases/people_load.rb', line 18 def people_load(filename = enviro.config.people.cache, modifier: [:newest, :api]) modifier = [modifier].flatten load_file = [:file, :newest].any? {|flag| modifier.include?(flag)} case when filename && load_file if file = people_load_filename(filename, newest: modifier.include?(:newest)) file_manager.load_json(file).tap do |people| logger.info("#{people&.length} people loaded from file #{file}") if people.is_a?(Array) end else logger.error("could not find the file #{file_manager.dir.file(filename)}") exit unless modifier.include?(:api) people_load(modifier: modifier - [:newest, :file]) end when modifier.include?(:api) logger.info("Going to get all the people via API") start = Time.now session.batch.get_people.tap do |people| secs = (Time.now - start).round(3) cnt = people.count per_sec = (cnt.to_f / secs).round(2) logger.info("Loaded #{cnt} people in #{secs} seconds (#{per_sec} people/sec)") if modifier.include?(:save) && people && people.length > 0 file = file_manager.save_json(people, filename, :timestamp) logger.info("#{people.length } people saved to file #{file}.") end end end.yield_self do |people| Eco::API::Organization::People.new(people) end end |
#people_refresh(people:, include_created: true) ⇒ Eco::API::Organization::People
this helper is normally used to run consecutive usecases, where data needs refresh.
Helper to obtain all the elements of people anew from the People Manager.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/eco/api/microcases/people_refresh.rb', line 9 def people_refresh(people:, include_created: true) ini = people.length if include_created session.job_groups.find_jobs(type: :create).map do |job| people = people.merge(job.people) end end created = people.length - ini msg = "Going to refresh #{people.length} people with server data" msg += " (including #{created} that were created)" if created > 0 logger.info(msg) start = Time.now entries = session.batch.get_people(people, silent: true) secs = (Time.now - start).round(3) cnt = entries.count per_sec = (cnt.to_f / secs).round(2) logger.info("Re-loaded #{cnt} people (out of #{people.length}) in #{secs} seconds (#{per_sec} people/sec)") missing = people.length - entries.length logger.error("Missed to obtain #{missing} people during the refresh") if missing > 0 Eco::API::Organization::People.new(entries) end |
#people_search(data, options: {}, silent: true) ⇒ Eco::API::Organization::People
- this helper is normally used to get partial part of the people manager.
- therefore, normally used with delta input files (files with only the differences).
Helper to search/obtain people from data against the server (People Manager).
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/eco/api/microcases/people_search.rb', line 12 def people_search(data, options: {}, silent: true) session.logger.info("Going to api get #{data.length} entries...") start = Time.now people = session.batch.search(data, silent: silent).yield_self do |status| secs = (Time.now - start).round(3) Eco::API::Organization::People.new(status.people).tap do |people| cnt = people.count per_sec = (cnt.to_f / secs).round(2) msg = "... could get #{cnt} people (out of #{data.length} entries) in #{secs} seconds (#{per_sec} people/sec)" session.logger.info(msg) end end # get the supervisors of found people (current supervisors) supers = people_search_prepare_supers_request(people) if supers.length > 0 session.logger.info(" Going to api get #{supers.length} current supervisors...") start = Time.now people = session.batch.search(supers, silent: silent).yield_self do |status| secs = (Time.now - start).round(3) found = status.people cnt = found.count per_sec = (cnt.to_f / secs).round(2) msg = "... could find #{cnt} current supers (out of #{supers.length}) in #{secs} seconds (#{per_sec} people/sec)" session.logger.info(msg) people.merge(found, strict: micro.strict_search?()) end end # get the supervisors referred in the input data (future supervisors) supers = people_search_prepare_supers_request(data, people) if supers.length > 0 session.logger.info(" Going to api get #{supers.length} supervisors as per input entries...") start = Time.now people = session.batch.search(supers, silent: silent).yield_self do |status| secs = (Time.now - start).round(3) found = status.people cnt = found.count per_sec = (cnt.to_f / secs).round(2) msg = "... could find #{cnt} input supers (out of #{supers.length}) in #{secs} seconds (#{per_sec} people/sec)" session.logger.info(msg) people.merge(found, strict: micro.strict_search?()) end end session.logger.info("Finally got #{people.length} people (out of #{data.length} entries)") people end |
#preserve_default_tag(person, options) ⇒ String
- It only works if the original value of
default_tagwas not empty
Helper to preserve the original default_tag.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/eco/api/microcases/preserve_default_tag.rb', line 10 def preserve_default_tag(person, ) if account = person.account if account.as_update.key?("default_tag") if original = person.original_doc.dig("account", "default_tag") person.account.default_tag = original end end end person.account&.default_tag end |
#preserve_filter_tags(person, options, keep_new: false) ⇒ Array<String>
- It only works if the original value of
filter_tagswas not empty
Helper to preserve the original filter tags.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/eco/api/microcases/preserve_filter_tags.rb', line 11 def (person, , keep_new: false) if person.as_update.key?("filter_tags") if original = person.original_doc["filter_tags"] unless original.empty? if keep_new person. += original else person. = original end end end end person. end |
#preserve_policy_groups(person, options, keep_new: false) ⇒ String
- It only works if the original value of
policy_group_idswas not empty
Helper to preserve the original policy_group_ids.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/eco/api/microcases/preserve_policy_groups.rb', line 11 def preserve_policy_groups(person, , keep_new: false) if account = person.account if account.as_update.key?("policy_group_ids") if original = person.original_doc.dig("account", "policy_group_ids") unless original.empty? if keep_new person.account.policy_group_ids += original else person.account.policy_group_ids = original end end end end end person.account&.policy_group_ids end |
#refresh_default_tag(entry, person, options) ⇒ Object
it assumes default_tag has been already set to person.account
When the input data, or entry, does not provide the default_tag,
it sets the default_tag of the user following some criteria
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/eco/api/microcases/refresh_default_tag.rb', line 10 def refresh_default_tag(entry, person, ) if person.account unless .dig(:exclude, :account) unless .dig(:exclude, :filter_tags) || .dig(:exclude, :default_tag) || entry&.default_tag? if session.tagtree person.account.default_tag = session.tagtree.default_tag(*person.) else = person. || [] person.account.default_tag = .first unless .length > 1 end end end end end |
#s3upload_targets ⇒ Array<String>
Helper to upload target files to S3.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/eco/api/microcases/s3upload_targets.rb', line 6 def s3upload_targets [].tap do |paths| session.config.s3storage.target_files.each_with_object(paths) do |file, arr| arr.push(session.s3upload(file: file)) end session.config.s3storage.target_directories.each_with_object(paths) do |folder, arr| arr.concat(session.s3upload(directory: folder)) end session.config.s3storage.target_file_patterns.each_with_object(paths) do |pattern, arr| filenames = [] case pattern when Regexp Dir.entries(".").sort.each do |file| next unless File.file?(file) # Skip directories filenames.push(file) if file =~ pattern end when String Dir.glob(pattern).sort.each do |file| next unless File.file?(file) # Skip directories filenames.push(file) end else # missconfiguration end filenames.each do |file| arr.push(session.s3upload(file: file)) end end end end |
#set_account(entry, person, options) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/eco/api/microcases/set_account.rb', line 7 def set_account(entry, person, ) unless .dig(:exclude, :account) entry.set_account(person, exclude: micro.account_excluded(person, )) person.account.send_invites = [:send_invites] if .key?(:send_invites) micro.refresh_default_tag(entry, person, ) micro.fix_default_group(entry, person, ) end end |
#set_core(entry, person, options) ⇒ Object
supervisor_id requires a special treatment, and therefore is always excluded.
Sets all the core details, but the supervisor.
9 10 11 12 13 14 |
# File 'lib/eco/api/microcases/set_core.rb', line 9 def set_core(entry, person, ) unless .dig(:exclude, :core) && !person.new? entry.set_core(person, exclude: micro.core_excluded(person, )) micro.(person, ) end end |
#set_core_with_supervisor(entry, person, people, supers_job, options) ⇒ Object
supervisor_id requires a special treatment, and therefore is always excluded.
Sets all the core details, but the supervisor.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/eco/api/microcases/set_core_with_supervisor.rb', line 11 def set_core_with_supervisor(entry, person, people, supers_job, ) unless .dig(:exclude, :core) && !person.new? micro.set_core(entry, person, ) if entry.supervisor_id? micro.set_supervisor(person, entry.supervisor_id, people, ) do |unknown_id| # delay setting supervisor if does not exit supers_job.add(person) do |person| micro.set_supervisor(person, unknown_id, people, ) end end end end end |
#set_supervisor(person, sup_id, people, options) {|supervisor_id| ... } ⇒ Object
Unique access point to set the supervisor_id value on a person.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/eco/api/microcases/set_supervisor.rb', line 11 def set_supervisor(person, sup_id, people, ) unless .dig(:exclude, :core) || .dig(:exclude, :supervisor) cur_id = person.supervisor_id cur_super = cur_id && with_supervisor(cur_id, people) micro.with_supervisor(sup_id, people) do |new_super| if !sup_id person.supervisor_id = nil descrease_subordinates(cur_super) elsif new_super && id = new_super.id person.supervisor_id = id descrease_subordinates(cur_super) increase_subordinates(new_super) elsif !block_given? descrease_subordinates(cur_super) person.supervisor_id = sup_id else yield(sup_id) if block_given? end end end end |
#strict_search?(options) ⇒ Boolean
strictsearches ignore the email when the sourceentryhas anexternal_idspecified.- see related command line options
-search-strictand-search-soft
When trying to find an person with given a source entry, it states if such a search should be strict or soft.
10 11 12 13 14 15 |
# File 'lib/eco/api/microcases/strict_search.rb', line 10 def strict_search?() strict_config = session.config.people.strict_search? strict_option = .dig(:search, :strict) soft_option = .dig(:search, :soft) && !strict_option (strict_config || strict_option) && !soft_option end |
#with_each(entries, people, options, append_created: true) {|entry, person| ... } ⇒ Eco::API::Organization::People
- it also links to
person.entrythe input data entry.
Finds each entry of entries in people and runs a block.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/eco/api/microcases/with_each.rb', line 15 def with_each(entries, people, , append_created: true) @_skip_all_multiple_results = false people_copy = people.newFrom(people.to_a) entries.each_with_object([]) do |entry, scoped| begin unless person = people_copy.find(entry, strict: micro.strict_search?()) person = session.new_person.tap do |person| people << person if append_created end end rescue Eco::API::Organization::People::MultipleSearchResults => e unless @_skip_all_multiple_results msg = "\n * When searching this Entry: #{entry.to_s(:identify)}" person = _with_each_prompt_to_select_user(e.(msg), entry: entry) end end if person person.entry = entry yield(entry, person) if block_given? scoped << person end end.yield_self {|all_people| people.newFrom all_people.uniq} end |
#with_each_leaver(entries, people, options) {|person| ... } ⇒ Eco::API::Organization::People
to be used only when the input file is the full DB
Detects who has left the organization and yield s them one by one to the given block
12 13 14 15 16 17 18 19 20 |
# File 'lib/eco/api/microcases/with_each_leaver.rb', line 12 def with_each_leaver(entries, people, ) leavers = people.map do |person| unless entries.find(person, strict: micro.strict_search?()) yield(person) if block_given? person end end.compact people.newFrom leavers end |
#with_each_present(entries, people, options, log_starter: false) {|entry, person| ... } ⇒ Eco::API::Organization::People
- it also links to
person.entrythe input dataentry.
Finds those in entries that already exist in the organization (people) and yield s them one by one to the given block.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/eco/api/microcases/with_each_present.rb', line 15 def with_each_present(entries, people, , log_starter: false) found = [] micro.with_each(entries, people, ) do |entry, person| if person.new? if log_starter session.logger.error("This person does not exist: #{entry.to_s(:identify)}") end next end found << person yield(entry, person) if block_given? end people.newFrom found end |
#with_each_starter(entries, people, options, log_present: false, append_created: true) {|entry, person| ... } ⇒ Eco::API::Organization::People
- it also links to
person.entrythe input dataentry.
Detects who in the entries is new in the organization and yield s them one by one to the given block.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/eco/api/microcases/with_each_starter.rb', line 16 def with_each_starter(entries, people, , log_present: false, append_created: true) starters = [] micro.with_each(entries, people, , append_created: append_created) do |entry, person| if !person.new? if log_present session.logger.error("This person (id: '#{person.id}') already exists: #{entry.to_s(:identify)}") end next end starters << person yield(entry, person) if block_given? end people.newFrom starters end |
#with_each_subordinate(supervisor, people) {|subordinate| ... } ⇒ Eco::API::Organization::People
if supervisor is nil, it will return all people with no supervisor.
Finds all the subordinates of supervisor.
11 12 13 14 15 16 17 |
# File 'lib/eco/api/microcases/with_each_subordinate.rb', line 11 def with_each_subordinate(supervisor, people) people.supervisor_id(_person_id(supervisor, people)).tap do |subordinates| subordinates.each do |subordinate| yield(subordinate) if block_given? end end end |
#with_supervisor(value, people, strict: false) {|supervisor| ... } ⇒ nil, Ecoportal::API::V1::Person
Finds the supervisor among people by using the supervisor_id of value.
12 13 14 15 16 17 18 |
# File 'lib/eco/api/microcases/with_supervisor.rb', line 12 def with_supervisor(value, people, strict: false) if sup_id = with_supervisor_supervisor_id(value) people.person(id: sup_id, external_id: sup_id, email: sup_id, strict: strict) end.tap do |supervisor| yield(supervisor) if block_given? end end |