Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 500 error if paginated_stats are empty #422

Merged
merged 1 commit into from
Nov 24, 2023
Merged

Conversation

muhammadadeeltajamul
Copy link
Contributor

API was returning 500 when there were no stats in a course.

Ticket Link: INF-1067

Copy link

codecov bot commented Nov 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f408aa2) 96.14% compared to head (cae4f29) 96.15%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #422   +/-   ##
=======================================
  Coverage   96.14%   96.15%           
=======================================
  Files          58       58           
  Lines        4562     4573   +11     
=======================================
+ Hits         4386     4397   +11     
  Misses        176      176           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@AhtishamShahid AhtishamShahid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a little refactor. otherwise, LGTM

api/users.rb Outdated
Comment on lines 74 to 85
if paginated_stats["pagination"].empty?
data = []
num_pages = 0
page = 0
total_count = 0
else
total_count = paginated_stats["pagination"][0]["total_count"]
num_pages = [1, (total_count / per_page.to_f).ceil].max
data = paginated_stats["data"].map do |user_stats|
{
:username => user_stats["username"]
}.merge(user_stats["course_stats"].except(*exclude_from_stats))
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if paginated_stats["pagination"].empty?
data = []
num_pages = 0
page = 0
total_count = 0
else
total_count = paginated_stats["pagination"][0]["total_count"]
num_pages = [1, (total_count / per_page.to_f).ceil].max
data = paginated_stats["data"].map do |user_stats|
{
:username => user_stats["username"]
}.merge(user_stats["course_stats"].except(*exclude_from_stats))
end
data = []
num_pages = 0
page = 0
total_count = 0
if not paginated_stats["pagination"].empty?
total_count = paginated_stats["pagination"][0]["total_count"]
num_pages = [1, (total_count / per_page.to_f).ceil].max
data = paginated_stats["data"].map do |user_stats|
{
:username => user_stats["username"]
}.merge(user_stats["course_stats"].except(*exclude_from_stats))
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@saadyousafarbi
Copy link
Contributor

Just a nit, LGTM!

@@ -521,6 +521,14 @@ def build_structure_and_response(course_id, authors, build_initial_stats = true,
expect(res["user_stats"]).to eq expected_result
end

it "API doesn't fail if user has no activity" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename test to something like "handle stats for user with no activity"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@muhammadadeeltajamul muhammadadeeltajamul merged commit 20cd380 into master Nov 24, 2023
4 checks passed
@muhammadadeeltajamul muhammadadeeltajamul deleted the inf-1067 branch November 24, 2023 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants