class Eco::CliDefault::People < Eco::API::Common::Loaders::CliConfig MAX_GET_PARTIAL = 12_000 class << self attr_reader :options, :session attr_writer :get_full, :get_partial def get_options # rubocop:disable Naming/AccessorMethodName get = options.dig(:people, :get) get = {} if get.nil? get end def no_get? get_options == false end def from_remote? return false unless get_options get_options[:from] == :remote end def from_local? return false unless get_options get_options[:from] == :local end def get_full? return @get_full if instance_variable_defined?(:@get_full) return false unless from_remote? get_options[:type] == :full end def get_partial? return @get_partial if instance_variable_defined?(:@get_partial) return false unless from_remote? @get_partial = (get_options[:type] == :partial) end def get_by_file? return false unless from_local? get_options[:type] == :file end def source_file return unless get_by_file? get_options[:file] end def switch_to_full_remote! self.get_full = true self.get_partial = false options.deep_merge!(people: { get: { from: :remote, type: :full } }) end def switch_to_full_local! self.get_full = true self.get_partial = false options.deep_merge!(people: { get: { from: :local, type: :full } }) end def optimize_get_partial!(input) return unless get_partial? msg = "To use -get-partial (partial updates), you need to use -entries-from" raise msg unless input.is_a?(Enumerable) return unless input.count > MAX_GET_PARTIAL msg = "(Optimization) " msg << "Switching from partial to full people download. " msg << "Input (#{input.count}) surpases MAX_GET_PARTIAL " msg << "(#{MAX_GET_PARTIAL}) entries." session.log(:info) { msg } switch_to_full_remote! end end people do |input, session, options| @options = options @session = session # -get-partial: validate input present and under max optimize_get_partial!(input) if get_partial? if no_get? Eco::API::Organization::People.new([]) elsif get_full? # -get-people session.micro.people_cache elsif get_partial? # -get-partial session.micro.people_search(input, options: options) elsif get_by_file? # -people-from-backup session.micro.people_load(source_file, modifier: :file) else switch_to_full_local! people = session.micro.people_load(modifier: %i[newest save]) if people.empty? switch_to_full_remote! people = session.micro.people_cache end people end end end