Class: Eco::API::Session::Task

Inherits:
Common::Session::BaseSession show all
Defined in:
lib/eco/api/session/task.rb

Overview

Set of helpers to do most common tasks

Constant Summary collapse

NEWEST_FILE_MODE =
[:newest, :last_file, :previous]
LOAD_FILE_MODE =
[:file]
SAVE_FILE_MODE =
[:save]
API_MODE =
[:api, :api_get]

Instance Attribute Summary

Attributes inherited from Common::Session::BaseSession

#api, #config, #environment, #file_manager, #logger, #session

Instance Method Summary collapse

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

#file_people(filename = enviro.config.people.cache) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/eco/api/session/task.rb', line 11

def file_people(filename = enviro.config.people.cache)
  logger.info("Going to get all the people via API")
  people = session.batch.get_people
  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

#load_people(filename = enviro.config.people.cache, modifier: [:newest, :api]) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/eco/api/session/task.rb', line 81

def load_people(filename = enviro.config.people.cache, modifier: [:newest, :api])
  modifier = [modifier].flatten
  people = []
  case
  when !!filename && (load_file?(modifier) || newest_file?(modifier))
    case
    when newest_file?(modifier)
      # search input file based on pattern (in case the name has a timestamp)

      file = file_manager.dir.newest_file(file: filename)
      logger.info("previous file found: #{file}") if !!file
    else
      file = file_manager.dir.file(filename)
    end

    if !file
      logger.error("could not find the file #{file_manager.dir.file(filename)}")
      exit if !use_api?(modifier)
      people = self.load_people(modifier: modifier - NEWEST_FILE_MODE - LOAD_FILE_MODE)
    else
      people = file_manager.load_json(file)
      if !!people && people.is_a?(Array)
        logger.info("#{people&.length} people loaded from file #{file}")
      end
    end
  when use_api?(modifier)
    # no previous file: use API to get all people

    logger.info("Going to get all the people via API")
    people = session.batch.get_people

    if save_file?(modifier) && people && people.length > 0
      file = file_manager.save_json(people, filename, :timestamp)
      logger.info("#{people.length } people saved to file #{file}.")
    end
  end
  Eco::API::Organization::People.new(people)
end

#people_refresh(people:, include_created: true) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/eco/api/session/task.rb', line 19

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
  session.logger.info(msg)
  status  = session.batch.get_people(people, silent: true)
  entries = status.people

  missing = people.length - entries.length
  session.logger.error("Missed to obtain #{missing} people during the refresh") if missing > 0

  Eco::API::Organization::People.new(status.people)
end

#s3upload_targetsObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/eco/api/session/task.rb', line 118

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

#search(data, options: {}, silent: true) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/eco/api/session/task.rb', line 40

def search(data, options: {}, silent: true)
  strict_search = session.config.people.strict_search? && (!options[:search]&.key?(:strict) || options.dig(:search, :strict))

  # to scope people to be fresh data got via api

  session.logger.info("going to api get #{data.length} entries...")

  status = session.batch.search(data, silent: silent)
  people = Eco::API::Organization::People.new(status.people)

  # get the supervisors

  supers = people.each_with_object([]) do |person, sup|
    if sup_id = person.supervisor_id
      spr = {"id" => sup_id}
      sup.push(spr) unless sup.include?(spr) || people.person(id: sup_id, external_id: sup_id)
    end
  end

  if supers.length > 0
    session.logger.info("going to api get #{supers.length} current supervisors...")
    status = session.batch.search(supers, silent: silent)
    people = people.merge(status.people, strict: strict_search)
  end

  supers = data.each_with_object([]) do |entry, sup|
    if entry.respond_to?(:supervisor_id) && !entry.supervisor_id.to_s.strip.empty?
      sup_id = entry.supervisor_id
      spr = {"id" => entry.supervisor_id}
      sup.push(spr) unless sup.include?(spr) || people.person(id: sup_id, external_id: sup_id)
    end
  end

  if supers.length > 0
    session.logger.info("going to api get #{supers.length} supervisors as per entries...")
    status = session.batch.search(supers, silent: silent)
    people = people.merge(status.people, strict: strict_search)
  end

  session.logger.info("could get #{people.length} people (out of #{data.length} entries)")
  people
end