lib/eco/api/microcases/people_load.rb in eco-helpers-1.5.14 vs lib/eco/api/microcases/people_load.rb in eco-helpers-1.5.15
- old
+ new
@@ -16,30 +16,38 @@
# - `:save` if it is supposed to cache/save the data locally once obtained people from the server (`:api`)
# @return [Eco::API::Organization::People] the `People` object with the data.
def people_load(filename = enviro.config.people.cache, modifier: [:newest, :api])
modifier = [modifier].flatten
load_file = [:file, :newest].any? {|flag| modifier.include?(flag)}
- people = 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")
- session.batch.get_people.tap do |people|
- 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
- Eco::API::Organization::People.new(people)
+ 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
+ cnt = people.count
+ per_sec = (cnt.to_f / secs).floor
+ 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
private
def people_load_filename(filename, newest: false)