CSC/ECE 517 Fall 2022 - E2253.Refactor delayed mailer.rb and scheduled task.rb: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 62: Line 62:
**delete_from_delayed_queue
**delete_from_delayed_queue
**delete_from_scheduled_set
**delete_from_scheduled_set
* '''Problem 2''': Duplicate code found within the create and edit methods within assignments_controller.rb
* '''Problem 2''': Duplicate code found within the create and update_assignment_form methods within assignments_controller.rb
<pre>
  def create
    ...
    ques_array = assignment_form_params[:assignment_questionnaire]
    due_array = assignment_form_params[:due_date]
    ques_array.each do |cur_questionnaire|
      cur_questionnaire[:assignment_id] = exist_assignment.id.to_s
    end
    due_array.each do |cur_due|
      cur_due[:parent_id] = exist_assignment.id.to_s
    end
    assignment_form_params[:assignment_questionnaire] = ques_array
    assignment_form_params[:due_date] = due_array
    ...
  end
 
  def update_assignment_form(exist_assignment)
    questionnaire_array = assignment_form_params[:assignment_questionnaire]
    questionnaire_array.each { |cur_questionnaire| cur_questionnaire[:assignment_id] = exist_assignment.id.to_s }
    assignment_form_params[:assignment_questionnaire]
    due_array = assignment_form_params[:due_date]
    due_array.each { |cur_due| cur_due[:parent_id] = exist_assignment.id.to_s }
    assignment_form_params[:due_date]
    @assignment_form.update(assignment_form_params, current_user)
  end
</pre>
* '''Solution''': Create two methods to create the arrays for due dates and questionnaires.
* '''Solution''': Create two methods to create the arrays for due dates and questionnaires.
===New Implementation===
===New Implementation===
Line 93: Line 119:


<pre>
<pre>
  def create
    ...
    assignment_form_params[:assignment_questionnaire] = assign_questionnaire_array
    assignment_form_params[:due_date] = assign_due_date_array
    ...
  end
  def update_assignment_form(exist_assignment)
    assignment_form_params[:assignment_questionnaire] = assign_questionnaire_array
    assignment_form_params[:due_date] = assign_due_date_array
    @assignment_form.update(assignment_form_params, current_user)
  end
   # creates array of questionnaires
   # creates array of questionnaires
   def assign_questionnaire_array
   def assign_questionnaire_array
Line 111: Line 150:
* Added comments to methods that lacked clarity in order to improve the readability
* Added comments to methods that lacked clarity in order to improve the readability
* Changed method names to improve readibility
* Changed method names to improve readibility
**  
** ''get_time_diff_btw_due_date_and_now''    is now    ''get_time_diff_between_due_date_and_now''    within    ''assignment_form.rb''
 
* Changed variable names to improve readability
** ''diff_btw_time_left_and_threshold''    is now    ''diff_between_time_left_and_threshold''    within    ''assignment_form.rb''
** ''dd''    is now    ''dueDate''    within    ''assignment_form.rb''    within    ''update''
===Automated Testing using RSPEC===
===Automated Testing using RSPEC===
The current version of expertiza did not have any test for VersionsController. Using the test driven development(TDD) approach, we have added an exhaustive set of RSPEC tests for VersionsController, to test all the modifications we have done to the code of the controller class. The tests use double and stub features of rspec-rails gem, to fake the log in by different users - Administrator, Instructor, Student etc. The tests can be executed "rpec spec" command as shown below.
The current version of expertiza included tests for the assignments_form and the mail_worker, but they were far from extensive. Thus we added a few tests of our own.
<pre>
<pre>
user-expertiza $rspec spec
.
.
.
Finished in 5.39 seconds (files took 25.33 seconds to load)
66 examples, 0 failures


Randomized with seed 19254
.
.
</pre>
</pre>
===Testing from UI===
Following are a few testcases with respectto our code changes that can be tried from UI:
1. To go to versions index page, type in the following url after logging in:
  http://152.46.16.81:3000/versions
2. After logging in as student/instructor or admin : Try accessing the  new, create, edit, update actions. These actions are not allowed to any of the users.
  http://152.46.16.81:3000/versions/new
  This calls the new action. In the current production version of expertiza, it is unhandled and application gives a default 404 page.
3. Another feature that can be tested from UI is Pagination. Try searching for a user's versions and see if the results are paginated or not. Search here:
  http://152.46.16.81:3000/versions/search
4. Visit the same URL as step 3, you should see only the students under that instructor in the users dropdown.
===References===
===References===


Line 149: Line 166:
#[http://bit.ly/myexpertiza  Demo link]  
#[http://bit.ly/myexpertiza  Demo link]  
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]
#[http://wikis.lib.ncsu.edu/index.php/Expertiza Expertiza project documentation wiki]
#[https://relishapp.com/rspec Rspec Documentation]
#Clean Code: A handbook of agile software craftsmanship. Author: Robert C Martin

Revision as of 23:07, 25 October 2022

E2253. Refactoring the Delayed Mailer and Scheduler

This page provides a description of the Expertiza based OSS project.



About Expertiza

Expertiza is an open source project based on Ruby on Rails framework. Expertiza is an all inclusive web service that allows instructors to create, edit and manage assignments. This includes the functionality for topic selection and team formation across various project and assignments. Expertiza allows for a variety of submission types including URL and PDF. Expertiza also allows the instructor to assign peer reviews and team assessments.

Problem Statement

The following tasks were accomplished in this project:

  • Improved the clarity of code by improving variable and method names.
  • Improved the clarity of code by adding comments.
  • Ensure one method doesn't perform more than one task.
  • Added RSPEC testcases for testing changes done in Assignment Controller

About Assignment Controller

This class is responsible for the creation and implementation of assignment forms. This includes due dates, questionnaires and assigning reviews.

Previous Information can be found here E2144

Files Involved (some changed should not have been changed):

  • app/controllers/assignments_controller.rb
  • app/mailers/mail_worker.rb
  • app/models/assignment_form.rb
  • config/sidekiq.yml
  • spec/models/assignment_form_spec.rb
  • spec/sidekiq_mail_worker_spec.rb
  • spec/spec_helper.rb


Current Implementation

Functionality
  • Instructor should be able to create, edit and assign assignments to all account in their course.
  • TA's should be able to
Drawbacks and Solutions
  • Problem 1:
The method delete_from_delayed_queue performs two tasks
def delete_from_delayed_queue
    queue = Sidekiq::Queue.new('mailers')
    queue.each do |job|
      assignmentId = job.args.first
      job.delete if @assignment.id == assignmentId
    end

    queue = Sidekiq::ScheduledSet.new
    queue.each do |job|
      assignmentId = job.args.first
      job.delete if @assignment.id == assignmentId
    end
  end
  • Solution: The implementation has been changed to include two function that will instead be called back to back:
    • delete_from_delayed_queue
    • delete_from_scheduled_set
  • Problem 2: Duplicate code found within the create and update_assignment_form methods within assignments_controller.rb
  def create
    ...
    ques_array = assignment_form_params[:assignment_questionnaire]
    due_array = assignment_form_params[:due_date]
    ques_array.each do |cur_questionnaire|
      cur_questionnaire[:assignment_id] = exist_assignment.id.to_s
    end
    due_array.each do |cur_due|
      cur_due[:parent_id] = exist_assignment.id.to_s
    end
    assignment_form_params[:assignment_questionnaire] = ques_array
    assignment_form_params[:due_date] = due_array
    ...
  end

  def update_assignment_form(exist_assignment)
    questionnaire_array = assignment_form_params[:assignment_questionnaire]
    questionnaire_array.each { |cur_questionnaire| cur_questionnaire[:assignment_id] = exist_assignment.id.to_s }
    assignment_form_params[:assignment_questionnaire]
    due_array = assignment_form_params[:due_date]
    due_array.each { |cur_due| cur_due[:parent_id] = exist_assignment.id.to_s }
    assignment_form_params[:due_date]
    @assignment_form.update(assignment_form_params, current_user)
  end
  • Solution: Create two methods to create the arrays for due dates and questionnaires.

New Implementation

  • The method delete_from_delayed_queue has been split into 2 methods now.
    • delete_from_delayed_queue- This method will remove the delayed_job from the delayed_jobs queue
    • delete_from_scheduled_set- This method will remove the delayed_job from the scheduled set
  # Deletes the job with id equal to "delayed_job_id" from the delayed_jobs queue
  def delete_from_delayed_queue
    queue = Sidekiq::Queue.new('mailers')
    queue.each do |job|
      assignmentId = job.args.first
      job.delete if @assignment.id == assignmentId
    end
  end

  # Deletes the job with id equal to "delayed_job_id" from the scheduled set
  def delete_from_scheduled_set
    queue = Sidekiq::ScheduledSet.new
    queue.each do |job|
      assignmentId = job.args.first
      job.delete if @assignment.id == assignmentId
    end
  end
  • Created the methods assign_questionnaire_array and assign_due_date_array to handle the creation of the arrays
    • assign_questionnaire_array- This method will create the array of questionnaires
    • delete_from_scheduled_set- This method will create the array of due dates assorted
  def create
    ...
    assignment_form_params[:assignment_questionnaire] = assign_questionnaire_array
    assignment_form_params[:due_date] = assign_due_date_array
    ...
  end

  def update_assignment_form(exist_assignment)
    assignment_form_params[:assignment_questionnaire] = assign_questionnaire_array
    assignment_form_params[:due_date] = assign_due_date_array
    @assignment_form.update(assignment_form_params, current_user)
  end

  # creates array of questionnaires
  def assign_questionnaire_array
    questionnaire_array = assignment_form_params[:assignment_questionnaire]
    questionnaire_array.each { |cur_questionnaire| cur_questionnaire[:assignment_id] = exist_assignment.id.to_s }
    questionnaire_array
  end

  # creates array of due dates
  def assign_due_date_array
    due_array = assignment_form_params[:due_date]
    due_array.each { |cur_due| cur_due[:parent_id] = exist_assignment.id.to_s }
    due_array
  end

Code improvements

  • Added comments to methods that lacked clarity in order to improve the readability
  • Changed method names to improve readibility
    • get_time_diff_btw_due_date_and_now is now get_time_diff_between_due_date_and_now within assignment_form.rb
  • Changed variable names to improve readability
    • diff_btw_time_left_and_threshold is now diff_between_time_left_and_threshold within assignment_form.rb
    • dd is now dueDate within assignment_form.rb within update

Automated Testing using RSPEC

The current version of expertiza included tests for the assignments_form and the mail_worker, but they were far from extensive. Thus we added a few tests of our own.


References

  1. Expertiza on GitHub
  2. GitHub Project Repository Fork
  3. The live Expertiza website
  4. Demo link
  5. Expertiza project documentation wiki