Class: CoalescingPanda::Workers::ProvisioningMiner

Inherits:
Object
  • Object
show all
Defined in:
app/models/coalescing_panda/workers/provisioning_miner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_id) ⇒ ProvisioningMiner

Returns a new instance of ProvisioningMiner.



8
9
10
11
12
13
14
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 8

def initialize()
  @account = Account.find()
  @user_ids = [];
  @course_ids = [];
  @section_ids = [];
  @enrollment_ids = [];
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



6
7
8
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 6

def 
  @account
end

#course_idsObject

Returns the value of attribute course_ids.



6
7
8
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 6

def course_ids
  @course_ids
end

#enrollment_idsObject

Returns the value of attribute enrollment_ids.



6
7
8
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 6

def enrollment_ids
  @enrollment_ids
end

#section_idsObject

Returns the value of attribute section_ids.



6
7
8
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 6

def section_ids
  @section_ids
end

#user_idsObject

Returns the value of attribute user_ids.



6
7
8
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 6

def user_ids
  @user_ids
end

Instance Method Details

#clientObject



16
17
18
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 16

def client
  Bearcat::Client.new(prefix: .api_domain, token: .api_token)
end

#delete_recordsObject



47
48
49
50
51
52
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 47

def delete_records
  User.where.not(id: user_ids).destroy_all
  Course.where.not(id: course_ids).destroy_all
  Section.where.not(id: section_ids).destroy_all
  Enrollment.where.not(id: enrollment_ids).destroy_all
end

#download_report(report_name, report_params) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 70

def download_report(report_name, report_params)
  report = client.start_report(., report_name, report_params)
  report_id = report['id']
  loop do
    sleep 20
    report = client.report_status(., report_name, report_id)
    break if report['status'] == 'complete' || report['status'] == 'error'
  end
  url = "#{.api_domain}/files#{report['attachment']['url'].split('files').last}"
end

#performObject



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
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 20

def perform
  Delayed::Worker.logger.debug "Downloading Provisioning Report"
  params = {
    'parameters[enrollments]' => true, 'parameters[users]' => true,
    'parameters[courses]' => true, 'parameters[sections]' => true,
    'parameters[xlist]' => true, 'parameters[include_deleted]' => true
  }
  url = download_report(:provisioning_csv, params)
  open(url, :allow_redirections => :safe) do |file|
    begin
      Delayed::Worker.logger.debug "Processing Provisioning Report"
      {users: User, courses: Course, sections: Section, enrollments: Enrollment, xlist: Section}.each do |k, v|
        process_zipped_csv_file(file, "#{k}.csv") do |row|
          begin
            id = v.create_from_csv(, row)
            self.instance_variable_get("@#{v.to_s.downcase}_ids") << id if id.present?
          rescue => exception
            Delayed::Worker.logger.debug exception
          end
        end
      end
    ensure
      file.close unless file.nil?
    end
  end
end

#process_csv(csv) ⇒ Object



65
66
67
68
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 65

def process_csv(csv)
  csv_options = {:headers => true, :return_headers => false, :header_converters => :symbol}
  CSV.parse(csv, csv_options) { |row| yield(row) }
end

#process_zipped_csv_file(zip, file) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'app/models/coalescing_panda/workers/provisioning_miner.rb', line 54

def process_zipped_csv_file(zip, file)
  Zip::InputStream.open(zip) do |io|
    while entry = io.get_next_entry
      if entry.name == file #io.read
        process_csv(io.read) { |row| yield(row) }
        break
      end
    end
  end
end