This repository was archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 572
Moves RosterEntry update to background job #2233
Open
shaunakpp
wants to merge
28
commits into
master
Choose a base branch
from
roster-add-active-job
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
3f9f889
Moves RosterEntry creation to background job
e0844ae
add pending specs for new job
9204d77
Add specs and squash bugs for the AddStudentsToRosterJob
8ff70e0
Merge branch 'master' into roster-add-active-job
83664cc
fix specs and job based on failing specs
8f03fa3
satisfy linter
1836324
Merge branch 'master' into roster-add-active-job
186d742
add hidden fields for action cable reference
07c1bb7
add a flash message to tell that update is in progress
c82937f
add counts to display total students and unlinked github accounts
c857342
Make linter happy
a29d7e1
Merge branch 'master' into roster-add-active-job
900e9e1
Merge branch 'master' into roster-add-active-job
23b6673
Merge branch 'master' into roster-add-active-job
bc82744
Merge branch 'master' into roster-add-active-job
b74a320
move progress bar to modal
f7a9dcc
move js code from channel js to roster_setup
234cec7
disable form while upload in progress
641a331
show number of entries added in message
6e60450
Merge branch 'master' into roster-add-active-job
0ea16a6
remove committed changes from a different stash
be2f56c
fix linter cries
62fe84d
fix outdated specs
e274ea1
Fix test with new message
stephaniegiang 9f81fbb
Apply suggestions from code review
stephaniegiang 060eb90
Merge branch 'roster-add-active-job' of https://github.com/education/…
shaunakpp 2087b58
Merge pull request #2402 from shaunakpp/roster-add-active-job
stephaniegiang f4e0f82
Merge branch 'master' into roster-add-active-job
stephaniegiang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
(function(){ | ||
var PROGRESS_HALF_LIFE = 1000; | ||
var setup_roster_update_cable, | ||
progress_asymptotically, | ||
display_message, | ||
set_progress, | ||
display_progress_bar, | ||
initialize_progress; | ||
|
||
var progress_complete = false; | ||
setup_roster_update_cable = function(){ | ||
var roster_id = $("#current_roster_id").val(); | ||
var user_id = $("#current_user_id").val(); | ||
App.add_students_to_roster = App.cable.subscriptions.create({ | ||
channel: "AddStudentsToRosterChannel", | ||
roster_id: roster_id, | ||
user_id: user_id | ||
}, | ||
{ | ||
connected: function() { | ||
// Called when the subscription is ready for use on the server | ||
}, | ||
|
||
disconnected: function() { | ||
// Called when the subscription has been terminated by the server | ||
}, | ||
|
||
received: function(data) { | ||
// Called when there's incoming data on the websocket for this channel | ||
if(data.status == "completed"){ | ||
progress_complete = true; | ||
set_progress(100); | ||
display_message(data.message); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
progress_asymptotically = function() { | ||
recursive_progress_asymptotically = function(recursive_callback, counter) { | ||
var progress; | ||
var remaining = 100/counter; | ||
progress = 100 - (100/counter); | ||
$(document) | ||
.find(".roster-update-progress-bar") | ||
.animate( | ||
{ width: progress.toFixed() + "%" }, | ||
{ duration: PROGRESS_HALF_LIFE * counter } | ||
); | ||
setTimeout(function() { | ||
if(!progress_complete){ | ||
recursive_callback(recursive_callback, counter + 1); | ||
} | ||
}, | ||
PROGRESS_HALF_LIFE * counter | ||
); | ||
}; | ||
recursive_progress_asymptotically(recursive_progress_asymptotically, 1); | ||
}; | ||
|
||
display_message = function(message){ | ||
$(".roster-update-message").removeAttr("hidden"); | ||
$("#roster-progress").text(message); | ||
}; | ||
|
||
set_progress = function(percent) { | ||
$(".roster-update-progress-bar").stop(true, false); | ||
if (percent === 0) { | ||
$(".roster-update-progress-bar").css("width", 0); | ||
} else if (percent) { | ||
$(".roster-update-progress-bar").animate({width: percent + "%"}); | ||
} | ||
}; | ||
|
||
toggle_roster_form = function(disable_fields){ | ||
var text_area = $("#entries-field"); | ||
var csv_button = $("#file-upload"); | ||
if(disable_fields){ | ||
text_area.attr("disabled", "disabled"); | ||
csv_button.addClass("disabled"); | ||
}else{ | ||
text_area.removeAttr("disabled"); | ||
csv_button.removeClass("disabled"); | ||
} | ||
}; | ||
|
||
ready = (function(){ | ||
$("#add-students-roster-form").on("ajax:beforeSend", function(){ | ||
toggle_roster_form(true); | ||
$('.roster-update-progress').removeAttr("hidden"); | ||
progress_asymptotically(); | ||
}); | ||
|
||
$("#add-students-roster-form").on("ajax:complete", function(){ | ||
toggle_roster_form(false); | ||
$("#entries-field").val(""); | ||
}); | ||
|
||
$(document).on('closing', '[data-remodal-id=new-student-modal]', function (e) { | ||
set_progress("0"); | ||
$('.roster-update-progress').attr("hidden", "hidden"); | ||
}); | ||
|
||
setup_roster_update_cable(); | ||
}); | ||
|
||
$(document).ready(ready); | ||
|
||
}).call(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddStudentsToRosterChannel < ApplicationCable::Channel | ||
def self.channel(user_id:, roster_id:) | ||
"#{channel_name}_#{user_id}_#{roster_id}" | ||
end | ||
|
||
def subscribed | ||
stream_from self.class.channel(roster_id: params[:roster_id], user_id: current_user.id) | ||
end | ||
|
||
def unsubscribed | ||
# Any cleanup needed when channel is unsubscribed | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddStudentsToRosterJob < ApplicationJob | ||
queue_as :roster | ||
|
||
ROSTER_UPDATE_SUCCESSFUL = "Roster successfully updated." | ||
ROSTER_UPDATE_FAILED = "Could not add any students to roster, please try again." | ||
ROSTER_UPDATE_PARTIAL_SUCCESS = "Could not add following students:" | ||
|
||
# Takes an array of identifiers and creates a | ||
# roster entry for each. Omits duplicates, and | ||
# | ||
# rubocop:disable AbcSize | ||
# rubocop:disable MethodLength | ||
def perform(identifiers, roster, user, lms_user_ids = []) | ||
channel = AddStudentsToRosterChannel.channel(roster_id: roster.id, user_id: user.id) | ||
|
||
identifiers = add_suffix_to_duplicates!(identifiers, roster) | ||
invalid_roster_entries = | ||
identifiers.zip(lms_user_ids).map do |identifier, lms_user_id| | ||
roster_entry = RosterEntry.create(identifier: identifier, roster: roster, lms_user_id: lms_user_id) | ||
roster_entry.identifier if roster_entry.errors.include?(:identifier) | ||
end.compact! | ||
|
||
message = build_message(invalid_roster_entries, identifiers) | ||
entries_created = identifiers.count - invalid_roster_entries.count | ||
if lms_user_ids.present? && entries_created.positive? | ||
GitHubClassroom.statsd.increment("roster_entries.lms_imported", by: entries_created) | ||
end | ||
ActionCable.server.broadcast(channel, message: message, status: "completed") | ||
end | ||
# rubocop:enable AbcSize | ||
# rubocop:enable MethodLength | ||
|
||
def add_suffix_to_duplicates!(identifiers, roster) | ||
existing_roster_entries = RosterEntry.where(roster: roster).pluck(:identifier) | ||
RosterEntry.add_suffix_to_duplicates( | ||
identifiers: identifiers, | ||
existing_roster_entries: existing_roster_entries | ||
) | ||
end | ||
|
||
# rubocop:disable MethodLength | ||
def build_message(invalid_roster_entries, identifiers) | ||
if invalid_roster_entries.empty? | ||
ROSTER_UPDATE_SUCCESSFUL + " #{identifiers.count} roster #{'entries'.pluralize(identifiers.count)} were added." | ||
elsif invalid_roster_entries.size == identifiers.size | ||
ROSTER_UPDATE_FAILED | ||
else | ||
formatted_students = | ||
invalid_roster_entries.map do |invalid_roster_entry| | ||
"#{invalid_roster_entry} \n" | ||
end.join("") | ||
"#{ROSTER_UPDATE_PARTIAL_SUCCESS} \n#{formatted_students}" | ||
end | ||
end | ||
# rubocop:enable MethodLength | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
- [boom, 3] | ||
- [create_repository, 3] | ||
- [porter_status, 3] | ||
- [roster, 3] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: this
split
is the same as before but it feels odd that we are splitting on\r\n
I would have expected only\n
and then a trim on the identifiers.