Class: CoalescingPanda::Workers::CourseMiner
- Inherits:
-
Object
- Object
- CoalescingPanda::Workers::CourseMiner
- Defined in:
- app/models/coalescing_panda/workers/course_miner.rb
Constant Summary collapse
- SUPPORTED_MODELS =
ORDER MATTERS!!
[:sections, :users, :enrollments, :assignment_groups, :group_categories, :assignments, :submissions, :groups, :group_memberships]
- COMPLETED_STATUSES =
['Completed', 'Error']
- RUNNING_STATUSES =
['Queued', 'Started']
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#assignment_group_ids ⇒ Object
Returns the value of attribute assignment_group_ids.
-
#assignment_ids ⇒ Object
Returns the value of attribute assignment_ids.
-
#batch ⇒ Object
Returns the value of attribute batch.
-
#course ⇒ Object
Returns the value of attribute course.
-
#course_section_ids ⇒ Object
Returns the value of attribute course_section_ids.
-
#enrollment_ids ⇒ Object
Returns the value of attribute enrollment_ids.
-
#group_ids ⇒ Object
Returns the value of attribute group_ids.
-
#group_membership_ids ⇒ Object
Returns the value of attribute group_membership_ids.
-
#options ⇒ Object
Returns the value of attribute options.
-
#user_ids ⇒ Object
Returns the value of attribute user_ids.
Instance Method Summary collapse
- #api_client ⇒ Object
- #delete_collection(query, hard_delete = false, field = 'workflow_state') ⇒ Object
- #delete_object(object, hard_delete = false, field = 'workflow_state') ⇒ Object
- #enrollment_states ⇒ Object
-
#initialize(course, options = []) ⇒ CourseMiner
constructor
A new instance of CourseMiner.
- #process_api_data(key) ⇒ Object
- #setup_batch ⇒ Object
- #should_download? ⇒ Boolean
- #standard_attributes(record, attributes) ⇒ Object
- #start(forced = false) ⇒ Object
- #sync_assignment_groups(collection) ⇒ Object
- #sync_assignments(collection) ⇒ Object
- #sync_enrollments(collection) ⇒ Object
- #sync_group_categories(collection) ⇒ Object
- #sync_group_memberships(collection) ⇒ Object
- #sync_groups(collection) ⇒ Object
- #sync_sections(collection) ⇒ Object
- #sync_submissions(collection) ⇒ Object
- #sync_users(collection) ⇒ Object
Constructor Details
#initialize(course, options = []) ⇒ CourseMiner
Returns a new instance of CourseMiner.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 8 def initialize(course, = []) @course = course @account = course.account @options = @batch = setup_batch @course_section_ids = [] @enrollment_ids = [] @assignment_ids = [] @assignment_group_ids = [] @group_ids = [] @group_membership_ids = [] @user_ids = [] end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
6 7 8 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 6 def account @account end |
#assignment_group_ids ⇒ Object
Returns the value of attribute assignment_group_ids.
6 7 8 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 6 def assignment_group_ids @assignment_group_ids end |
#assignment_ids ⇒ Object
Returns the value of attribute assignment_ids.
6 7 8 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 6 def assignment_ids @assignment_ids end |
#batch ⇒ Object
Returns the value of attribute batch.
6 7 8 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 6 def batch @batch end |
#course ⇒ Object
Returns the value of attribute course.
6 7 8 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 6 def course @course end |
#course_section_ids ⇒ Object
Returns the value of attribute course_section_ids.
6 7 8 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 6 def course_section_ids @course_section_ids end |
#enrollment_ids ⇒ Object
Returns the value of attribute enrollment_ids.
6 7 8 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 6 def enrollment_ids @enrollment_ids end |
#group_ids ⇒ Object
Returns the value of attribute group_ids.
6 7 8 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 6 def group_ids @group_ids end |
#group_membership_ids ⇒ Object
Returns the value of attribute group_membership_ids.
6 7 8 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 6 def group_membership_ids @group_membership_ids end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 6 def @options end |
#user_ids ⇒ Object
Returns the value of attribute user_ids.
6 7 8 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 6 def user_ids @user_ids end |
Instance Method Details
#api_client ⇒ Object
33 34 35 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 33 def api_client @api_client ||= Bearcat::Client.new(prefix: account.settings[:base_url], token: account.settings[:account_admin_api_token]) end |
#delete_collection(query, hard_delete = false, field = 'workflow_state') ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 271 def delete_collection(query, hard_delete = false, field = 'workflow_state') if @options.include?(:soft_delete) && !hard_delete #failsafe in case something was missed begin query.reorder('').update_all(field.to_sym => 'deleted') rescue => e Rails.logger.error("Error deleting with soft delete, attempting hard") delete_collection(query, true) end else query.destroy_all end end |
#delete_object(object, hard_delete = false, field = 'workflow_state') ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 285 def delete_object(object, hard_delete = false, field = 'workflow_state') if @options.include?(:soft_delete) && !hard_delete begin object.update_attributes(field.to_sym => 'deleted') rescue => e Rails.logger.error("Error deleting with soft delete, attempting hard") delete_object(object, true) end else object.destroy end end |
#enrollment_states ⇒ Object
113 114 115 116 117 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 113 def enrollment_states enrollment_state = ['active', 'invited'] enrollment_state << 'completed' if @options.include?(:include_complete) enrollment_state end |
#process_api_data(key) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 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 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 68 def process_api_data(key) case key when :assignment_groups collection = api_client.list_assignment_groups(course.canvas_course_id).all_pages!(100) sync_assignment_groups(collection) when :sections collection = api_client.course_sections(course.canvas_course_id).all_pages!(100) sync_sections(collection) when :users collection = api_client.list_course_users(course.canvas_course_id, { 'enrollment_state' => enrollment_states }).all_pages!(100) sync_users(collection) when :enrollments collection = api_client.course_enrollments(course.canvas_course_id, {'state' => enrollment_states}).all_pages!(100) sync_enrollments(collection) when :assignments collection = api_client.assignments(course.canvas_course_id).all_pages!(100) sync_assignments(collection) when :submissions collection = api_client.course_submissions( course.canvas_course_id, { 'student_ids' => ['all'], 'enrollment_state' => 'active', } ).all_pages!(100) sync_submissions(collection) when :groups collection = api_client.course_groups(course.canvas_course_id).all_pages!(100) sync_groups(collection) when :group_categories collection = api_client.list_group_categories('courses', course.canvas_course_id).all_pages!(100) sync_group_categories(collection) when :group_memberships collection = [] course.groups.each do |group| api_client.list_group_memberships(group.canvas_group_id).all_pages!(100).each do |group_memberships| collection << group_memberships end end sync_group_memberships(collection) else raise "API METHOD DOESN'T EXIST" end end |
#setup_batch ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 22 def setup_batch batch = account.canvas_batches.where(context: course).first if batch.present? and RUNNING_STATUSES.include?(batch.status) batch else batch = account.canvas_batches.create(context: course, status: "Queued") end batch.update_attributes(options: ) batch end |
#should_download? ⇒ Boolean
60 61 62 63 64 65 66 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 60 def should_download? return true unless account.settings[:canvas_download_interval].present? return true unless last_completed_batch = account.canvas_batches.where(context: course, status: 'Completed').order('updated_at ASC').first should_download = last_completed_batch.updated_at < Time.zone.now - account.settings[:canvas_download_interval].minutes batch.update_attributes(status: 'Canceled') unless should_download should_download end |
#standard_attributes(record, attributes) ⇒ Object
315 316 317 318 319 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 315 def standard_attributes(record, attributes) new_attributes = attributes.dup new_attributes.delete('id') new_attributes.delete_if { |key, value| !record.attributes.include?(key) } end |
#start(forced = false) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 37 def start(forced = false) unless forced return unless batch.status == 'Queued' # don't start if there is already a running job return unless should_download? end begin batch.update_attributes(status: "Started", percent_complete: 0) index = 1 SUPPORTED_MODELS.each do |model_key| next unless .include?(model_key) process_api_data(model_key.to_sym) percent_complete = (index/(.count.nonzero? || 1).to_f * 100).round(1) batch.update_attributes(percent_complete: percent_complete) index += 1 end batch.update_attributes(status: "Completed", percent_complete: 100) rescue => e batch.update_attributes(status: "Error", message: e.) end end |
#sync_assignment_groups(collection) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 119 def sync_assignment_groups(collection) collection.each do |values| begin values['canvas_assignment_group_id'] = values['id'].to_s assignment_group = course.assignment_groups.where(canvas_assignment_group_id: values['canvas_assignment_group_id']).first_or_initialize assignment_group.assign_attributes(standard_attributes(assignment_group, values)) assignment_group.save(validate: false) assignment_group_ids << assignment_group.id rescue => e Rails.logger.error "Error syncing assignment group: #{values} Error: #{e}" end end delete_collection(course.assignment_groups.where.not(id: assignment_group_ids)) end |
#sync_assignments(collection) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 198 def sync_assignments(collection) collection.each do |values| begin values['canvas_assignment_id'] = values['id'].to_s assignment = course.assignments.where(canvas_assignment_id: values['canvas_assignment_id']).first_or_initialize assignment_group = course.assignment_groups.find_by(canvas_assignment_group_id: values['assignment_group_id']) group_category = course.group_categories.find_by(canvas_group_category_id: values['group_category_id']) assignment.coalescing_panda_assignment_group_id = assignment_group.id if assignment_group assignment.coalescing_panda_group_category_id = group_category.id if group_category assignment.assign_attributes(standard_attributes(assignment, values)) assignment.save(validate: false) assignment_ids << assignment.id rescue => e Rails.logger.error "Error syncing assignment: #{values} Error: #{e}" end end course.assignments.where.not(id: assignment_ids).each do |assignment| delete_collection(assignment.submissions) delete_object(assignment) end end |
#sync_enrollments(collection) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 174 def sync_enrollments(collection) collection.each do |values| begin values['canvas_enrollment_id'] = values['id'].to_s section = course.sections.find_by(canvas_section_id: values['course_section_id'].to_s) enrollment = section.enrollments.where(canvas_enrollment_id: values['canvas_enrollment_id']).first_or_initialize enrollment.section = course.sections.find_by(canvas_section_id: values['course_section_id'].to_s) enrollment.user = account.users.find_by(canvas_user_id: values['user_id'].to_s) values['workflow_state'] = values["enrollment_state"] values['enrollment_type'] = values['type'] enrollment.assign_attributes(standard_attributes(enrollment, values)) enrollment.save!(validate: false) enrollment_ids << enrollment.id rescue => e Rails.logger.error "Error syncing enrollment: #{values} Error: #{e}" end end removed_enrollments = course.enrollments.where.not(id: enrollment_ids) removed_enrollments.each do |enrollment| delete_collection(course.submissions.where(coalescing_panda_user_id: enrollment.user.id)) end delete_collection(removed_enrollments) end |
#sync_group_categories(collection) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 235 def sync_group_categories(collection) collection.each do |values| begin values['canvas_group_category_id'] = values['id'].to_s values.delete('context_type') #assume only course for now category = course.group_categories.where(canvas_group_category_id: values['canvas_group_category_id']).first_or_initialize category.assign_attributes(standard_attributes(category, values)) category.save(validate: false) rescue => e Rails.logger.error "Error syncing group categories: #{values} Error: #{e.} - #{e.backtrace}" end end end |
#sync_group_memberships(collection) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 298 def sync_group_memberships(collection) collection.each do |values| begin values['canvas_group_membership_id'] = values['id'].to_s group_membership = course.group_memberships.where(canvas_group_membership_id: values['canvas_group_membership_id']).first_or_initialize group_membership.group = course.groups.find_by(canvas_group_id: values['group_id'].to_s) group_membership.user = course.users.find_by(canvas_user_id: values['user_id'].to_s) group_membership.assign_attributes(standard_attributes(group_membership, values)) group_membership.save(validate: false) group_membership_ids << group_membership.id rescue => e Rails.logger.error "Error syncing group memebership: #{values} Error: #{e}" end end delete_collection(course.group_memberships.where.not(id: group_membership_ids)) end |
#sync_groups(collection) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 249 def sync_groups(collection) collection.each do |values| begin values['canvas_group_id'] = values['id'].to_s group = course.groups.where(canvas_group_id: values['canvas_group_id']).first_or_initialize group_category = course.group_categories.find_by(canvas_group_category_id: values['group_category_id']) if values['leader'] group.leader = course.users.find_by(canvas_user_id: values['leader']['id'].to_s) else group.leader = nil end group.coalescing_panda_group_category_id = group_category.id if group_category group.assign_attributes(standard_attributes(group, values)) group.save(validate: false) group_ids << group.id rescue => e Rails.logger.error "Error syncing group: #{values} Error: #{e}" end end delete_collection(course.groups.where.not(id: group_ids)) end |
#sync_sections(collection) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 134 def sync_sections(collection) collection.each do |values| begin values['course_section_id'] = values['id'].to_s section = course.sections.where(canvas_section_id: values['course_section_id']).first_or_initialize section.assign_attributes(standard_attributes(section, values)) section.sis_id = values['sis_section_id'] section.save(validate: false) course_section_ids << section.id rescue => e Rails.logger.error "Error syncing section: #{values} Error: #{e}" end end delete_collection(course.sections.where.not(id: course_section_ids)) end |
#sync_submissions(collection) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 220 def sync_submissions(collection) collection.each do |values| begin values['canvas_submission_id'] = values['id'].to_s submission = course.submissions.where(canvas_submission_id: values['canvas_submission_id']).first_or_initialize submission.user = course.users.find_by(canvas_user_id: values['user_id'].to_s) submission.assignment = course.assignments.find_by(canvas_assignment_id: values['assignment_id'].to_s) submission.assign_attributes(standard_attributes(submission, values)) submission.save(validate: false) rescue => e Rails.logger.error "Error syncing submission: #{values} Error: #{e}" end end end |
#sync_users(collection) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'app/models/coalescing_panda/workers/course_miner.rb', line 150 def sync_users(collection) collection.each do |values| begin values['canvas_user_id'] = values["id"].to_s user = account.users.where(canvas_user_id: values['canvas_user_id']).first_or_initialize user.coalescing_panda_lti_account_id = account.id user.assign_attributes(standard_attributes(user, values)) user.sis_id = values['sis_user_id'].to_s user.save(validate: false) user_ids << user.id rescue => e Rails.logger.error "Error syncing user: #{values} Error: #{e}" end end removed_users = course.users.where.not(id: user_ids) removed_users.each do |user| user.enrollments.each do |enrollment| delete_collection(course.submissions.where(coalescing_panda_user_id: enrollment.user.id)) delete_object(enrollment) end end delete_collection(removed_users) end |