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
(Created page with "E2253. Refactor delayed_mailer.rb and scheduled_task.rb This page provides a description of the Expertiza based OSS project.")
 
Line 1: Line 1:
E2253. Refactor delayed_mailer.rb and scheduled_task.rb
==E2253. Refactoring the Delayed Mailer and Scheduler==
This page provides a description of the Expertiza based OSS project.
 
This page provides a description of the Expertiza based OSS project.  
 
 
__TOC__
 
 
===About Expertiza===
 
[http://expertiza.ncsu.edu/ Expertiza] is an open source project based on [http://rubyonrails.org/ 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 [https://expertiza.csc.ncsu.edu/index.php/CSC/ECE_517_Fall_2021_-_E2144._Refactor_delayed_mailer_and_scheduled_task 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
<pre>
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
</pre>
* '''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 edit methods within assignments_controller.rb
* '''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
 
<pre>
  # 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
</pre>
*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
 
<pre>
  # 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
</pre>
 
===Code improvements===
* Added comments to methods that lacked clarity in order to improve the readability
* Changed method names to improve readibility
**
 
===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.
<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>
 
===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===
 
#[https://github.com/expertiza/expertiza Expertiza on GitHub]
#[https://github.com/WintersLt/expertiza GitHub Project Repository Fork]
#[http://expertiza.ncsu.edu/ The live Expertiza website]
#[http://bit.ly/myexpertiza  Demo link]
#[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 22:44, 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 edit methods within assignments_controller.rb
  • 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
  # 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

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.

user-expertiza $rspec spec
.
.
.
Finished in 5.39 seconds (files took 25.33 seconds to load)
66 examples, 0 failures

Randomized with seed 19254
.
.

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

  1. Expertiza on GitHub
  2. GitHub Project Repository Fork
  3. The live Expertiza website
  4. Demo link
  5. Expertiza project documentation wiki
  6. Rspec Documentation
  7. Clean Code: A handbook of agile software craftsmanship. Author: Robert C Martin