CSC/ECE 517 Spring 2022 - E2220: Refactor reputation web service controller

From Expertiza_Wiki
Jump to navigation Jump to search

This page provides information about the refactoring work done on the reputation web service controller as part of the OSS project.

Team

Mentor

  • Naman Shrimali (nshrima)

Team Members (MissingSemicolon)

  • Eshwar Chandra Vidhyasagar Theda (ethedla)
  • Gokul Krishna Koganti (gkogant)
  • Krishna Saurabh Vankadaru (kvankad)

Expertiza

Expertiza is an assignment management portal. This portal can be used by instructors to create assignments. It can be used by students to submit their academic works and peer review others' works. Expertiza allows students to create, join and work in teams.

Background of reputation web service controller

Expertiza provides a way for students to peer review others' works. The motive of this provision is to encourage students to learn more while reviewing others' works and utilize the reviews to reflect on the grades. However, to depend on the peer reviews, the reviews' credibility has to be established. This credibility can be evaluated through reputation scores computed by reputation systems. These systems are deployed as web services and facilitate the calculation of past and present assignment scores for peer review research.

In the project's scope, we focus on the aspect of refactoring the file reputation_web_service_controller.rb. This is a controller file used for the calculation of reputation scores. This is measured based on how close the current review scores are to the other.

Issues identified

  1. Some methods are violating the single responsibility principle, i.e, some methods are doing more than one tasks. These methods must be broken down such that each method performs only one task.
  2. In send_post_request, references to specific assignments, such as 724, 735, and 756 are no longer relevant and must be removed.
  3. Some method names followed bad naming convention. These method names are general and are in noun forms. Specific relevant names must be used as method names.
  4. Detailed method descriptions are needed to be inserted into the code as comments.
  5. There is a lot of unused/commented code, which should be removed.
  6. Some spelling mistakes are identified in the code.

Enhancements

Functionality Fixes

The controller we received to perform refactoring work on, was not in a working condition. We found many issues pertaining to the below mentioned fixes and had to debug the file extensively. Later refactoring work is done on the debugged and working code.

  1. The case of when a post request to reputation web service fails is not handled properly, resulting in a long error message on the view. This error has been handled to display formatted error on the page.
    Commit link: https://github.com/krishnasaurabh/expertiza/pull/15/commits/84c9f30f9ee29759b5a41c9d11cda53c298fe49b

  2. Commented out the part of encryption and decryption as the public key files are not available.
    Commit link: https://github.com/krishnasaurabh/expertiza/pull/15/commits/db83d497dd284cf0700d219f1b225f2d6c508ac8

  3. In send_post_request (old version, currently prepare_request_body), init_header has been removed and requested & response JSON has been displayed in the beautify JSON for the implemented algorithms. This is done by using constant objects instead of URL strings.
    Commit link: https://github.com/krishnasaurabh/expertiza/pull/17/commits/f343438383d56d577648ef4dd91de4fc3501449a

  4. When the control is redirected to the client, in the older versions, the data was being passed through the use of instance variables. Now, the parameters are being passed using flash.
    Commit link: https://github.com/krishnasaurabh/expertiza/pull/15/commits/bf2ab75eb7b25e6d0b79791610beb371383b6042

Refactoring

Introduced single responsibility principle to the functions

All the functions are modified in a way to concur with the single responsibility principle i.e, each function performs only one considerable task.

The following functions violated SRP:

  1. The db_query method not only queries, but calculates sums.
  2. The json_generator method not only generates json, but also queries the database based on the ‘type’ it receives.
  3. The send_post_request not only sends a post request, but generates a body based on parameters received, encrypts the data, hits the post request, decrypts the response data, and updates participants.

SRP is introduced in the following ways:

  1. The db_query method is split into calculate_peer_review_grade and get_peer_reviews methods.
    get_peer_reviews uses get_peer_reviews_for_responses method, which further calls get_valid_answers_for_response, get_max_question_score , calculate_peer_review_grade methods.
    Commit link: https://github.com/krishnasaurabh/expertiza/pull/5/commits/3c5b2b5e33675aaf121a954180eee11f31a8cb36

  2. The json_generator method is split into generate_json_body, generate_json_for_peer_reviews, generate_json_for_quiz_scores.These functions further use helper methods to abide by the SRP principle.
    generate_json_for_peer_reviews calls get_peer_reviews and generate_json_body methods.
    get_peer_reviews calls get_peer_reviews_for_responses method.
    generate_json_for_quiz_scores calls get_quiz_score and generate_json_body methods.
    get_quiz_score calls get_ids_list and get_scores methods.
    Commit link: https://github.com/krishnasaurabh/expertiza/pull/3/commits/3c5b2b5e33675aaf121a954180eee11f31a8cb36

  3. The send_post_request is split into prepare_request_body and process_response_body methods.
    prepare_request_method uses add_expert_grades, add_quiz_scores, add_hamer_reputation_values, add_lauw_reputation_values, encrypt_request_body, format_into_JSON to prepare the request body.
    process_response_body makes use of decrypt_response and update_participants to process the response received.
    Commit link: https://github.com/krishnasaurabh/expertiza/pull/3/commits/3c5b2b5e33675aaf121a954180eee11f31a8cb36

Removed unnecessary references in the send_post_request body

In send_post_request, there are references to specific assignments, such as 724, 735, and 756. They were put in to gather data for a paper published in 2015. They are no longer relevant and are removed. Relevant tests are also removed.

Commit link: https://github.com/krishnasaurabh/expertiza/commit/d5d0a6de203b9643bcb1750328261ea6e9abdde5

Changed method names to make them more meaningful

Method names should be verbs, and should say what the method does. Method names should not be so general that they could apply to many different methods. Bad method names are observed in the methods, db_query and json_generator. These method names are too general and needed changing.

Function descriptions

In the older version, controller's functions did not have function descriptions. In the current version, a detailed functional description has been added to each method. Additionally, descriptions include what parameters are accepted and what is returned. Proper convention is followed in accordance with the industry standards for the method descriptions, parameters and return specifications.

Commit link: https://github.com/krishnasaurabh/expertiza/pull/20/commits/d8c86b0c0a073c894e0700e6f4f971a1ec845cee

Removed unnecessary code

Commented code blocks that are unused are found and deleted from the controller's body. RSA Encryption and Decryption are not being used for now. Such code is commented out for potential future use.

Commit link: https://github.com/expertiza/expertiza/pull/2291/commits/673fe2099991c21237c59c71d7ee51f063e55b70

Corrected spelling mistakes

Only one instance of spelling error has been identified i.e, dimention. It has been corrected.

Commit link: https://github.com/expertiza/expertiza/pull/2226/commits/efc4b4dcbd5d3b1f783d660f70ac18abfb451ab7

Used map functionality instead of for loop to make the code compact

In some functions table's records are replaced with the record's ID. This is done by looping the table and changing each record. We improved this by using the map functionality to map the records with their respective IDs.

Commit link: https://github.com/krishnasaurabh/expertiza/commit/36bc4fa30d7c6548cb64e59ebca2a3afe4c9c4a9

Test Plan

Manual Testing

To test the changes manually use the following link to access the reputation web service endpoint.
http://152.7.177.155:8080/reputation_web_service/client
Username : instructor6
Password : password


Enter assignment id which is a mandatory field, the secondary assignment id is optional. Enter the round number for which we want to calculate the reputation for (1-3)

Select the type of algorithm to calculate the reputation for the peer reviews.

Based on the type of algorithm selected, some of the additional information is automatically passed. Other additional information is optional.

Click send request (This might take a few minutes, please be patient)

On clicking the send request, the JSON for the post request to the reputation web service is formed, and then sent.
On receiving the response, we parse it and update the reputation of participants of the assignment provided.
The request and response JSONs can be seen in both minified and beautified versions.
There is also a summary of the choices selected above the beautified JSONs.

Testing via rspec


Only a few methods and flows have automated test cases. Adding test cases is not in the scope of this project, and will be taken up later, to make the entire controller robust.
However, we have made sure that all the existing test cases are not broken and working as they are supposed to be.

To run the automated rspec cases run the below command to verify if all the test cases are working.

 rspec spec/controllers/reputation_web_service_controller_spec.rb 

Future Work

  1. Client view has checkboxes for choosing between the action to be performed. But only the first checked action is being considered. This has to be fixed by the use of radio buttons.
  2. Hamer and Lauw actions are not resulting in any functional implementations. These algorithms have to be implemented.
  3. Need to fix the usage of assignment_id and another_assignment_id for the Hamer and Lauw algorithms
  4. Client is a bad method name and needs to be changed.
  5. Formatting string into JSON can be improved. Instead of prepending characters like '{', dictionaries can be used and then converted into JSON.

GitHub Repository and Pull Request links

Link to the Expertiza repository: https://github.com/expertiza/expertiza
Link to the forked repository: https://github.com/krishnasaurabh/expertiza/tree/beta
Link to the pull request: https://github.com/expertiza/expertiza/pull/2291