Skip to content

Commit

Permalink
Pagination (#369) (#404)
Browse files Browse the repository at this point in the history
* Adds POST route for creating new assignments via API (#338)

* Add API key and sample GET and POST

* Implement POST API route for creating assignments

* Add API key authentication handler

* Run rubocop on assignments_controller

* Reduce linter-flagged issues

* Add API Phase 1 documentation (#336)

* Add API Phase 1 documentation

* Api/sub sim (#331)

* Added API for GET all submission similarities of an assignment

* Fix extra render when uploading invalid zip file (#342)

* Fix extra render when uploading invalid zip file

* Revert "Fix extra render when uploading invalid zip file (#342)" (#344)

This reverts commit d6a720f.

* Fix extra render when uploading invalid zip file

* Add API route for GET Pair of flagged submission details (#341)

* Add tests and bug fixes for api/v1/submission_similarities index and show

* Add RSpec tests for POST /api/v1/courses/{course_id}/assignment/

* Update similarity percentage format it doc

* Remove unused files and routes

* Update/ Clarify API doc

* added pagination

* added to use the will_paginate gem

* Added such that limit has higher precedence over pagination

---------

Co-authored-by: Michael Gau <[email protected]>
Co-authored-by: sibinhho99 <[email protected]>
Co-authored-by: florentianayuwono <[email protected]>
  • Loading branch information
4 people authored Jan 15, 2024
1 parent 8bcd107 commit 1475bcb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
77 changes: 46 additions & 31 deletions app/controllers/api/v1/submission_similarities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,34 @@ def render_submission_similarities
render json: { error: 'Assignment does not exist' }, status: :bad_request
return
end
# Check if the assignment has associated submission files.

submission_similarities = assignment.submission_similarities

if assignment.submissions.empty?
render json: { status: 'empty' }, status: :ok
return
end

# Determine process status of assignment
submission_similarity_process = assignment.submission_similarity_process

case submission_similarity_process.status
when SubmissionSimilarityProcess::STATUS_RUNNING, SubmissionSimilarityProcess::STATUS_WAITING
render json: { status: 'processing' }, status: :ok
return
render_processing_status
when SubmissionSimilarityProcess::STATUS_ERRONEOUS
render json: { status: 'error', message: 'SSID is busy or under maintenance. Please try again later.' },
status: :service_unavailable
return
render_error_status
else
submission_similarities = apply_filters(submission_similarities)
render_paginated_or_limited_submission_similarities(submission_similarities)
end
end

submission_similarities = assignment.submission_similarities
def render_filtered_submission_similarities(submission_similarities)
submission_similarities = apply_filters(submission_similarities)

### Filtering Code
render_paginated_submission_similarities(submission_similarities)
end

def apply_filters(submission_similarities)
# Apply the threshold filter
if params[:threshold].present?
threshold_value = params[:threshold].to_f
Expand All @@ -98,27 +105,36 @@ def render_submission_similarities
submission_similarities = submission_similarities.limit(limit_value)
end

# Apply the page filter
if params[:page].present?
per_page = params[:limit].present? ? limit_value : 20 # Default per page value is 20, limit to use a page size
page_number = params[:page].to_i
submission_similarities = submission_similarities.offset(per_page * (page_number - 1))
submission_similarities
end

def render_paginated_or_limited_submission_similarities(submission_similarities)
if params[:limit].present?
render_json_response(submission_similarities)
else
# Set the default per page value to 20 for pagination
per_page = 20

# Sort the submission_similarities by similarity in descending order by default
submission_similarities = submission_similarities.order(similarity: :desc)

submission_similarities = submission_similarities.paginate(page: params[:page], per_page: per_page)

render_json_response(submission_similarities)
end
end

# Process subnission similarities into readable format for returning via JSON
result_submission_similarities = []
def render_json_response(submission_similarities)
render json: { status: 'processed', submissionSimilarities: submission_similarities }, status: :ok
end

submission_similarities.each { |submission_similarity|
result_submission_similarities.append( {
submissionSimilarityID: submission_similarity.id,
student1ID: submission_similarity.submission1.student_id,
student2ID: submission_similarity.submission2.student_id,
similarity: submission_similarity.similarity
}
)
}
def render_processing_status
render json: { status: 'processing' }, status: :ok
end

render json: { status: 'processed', submissionSimilarities: result_submission_similarities }, status: :ok
def render_error_status
render json: { status: 'error', message: 'SSID is busy or under maintenance. Please try again later.' },
status: :service_unavailable
end

def render_pair_of_flagged_submissions
Expand All @@ -132,16 +148,15 @@ def render_pair_of_flagged_submissions
return
end

max_similarity_percentage = submission_similarity.similarity
matches = []

submission_similarity.similarity_mappings.each do |similarity|
matches.append(
{
student1StartLine: similarity.start_line1 + 1,
student1EndLine: similarity.end_line1 + 1,
student2StartLine: similarity.start_line2 + 1,
student2EndLine: similarity.end_line2 + 1,
numOfMatchingStatements: similarity.statement_count
student1: similarity.line_range1_string,
student2: similarity.line_range2_string,
statementCount: similarity.statement_count
}
)
end
Expand Down
2 changes: 2 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand Down

0 comments on commit 1475bcb

Please sign in to comment.