Skip to content

fix: O3-5485 Fix NaN return from computeAverageWaitTimeInMinutes when no entries have endedAt#99

Open
sanks011 wants to merge 1 commit intoopenmrs:mainfrom
sanks011:O3-5485-queue-utils-compute-average-wait-time-in-minutes-returns-na-n-when-all-queue-entries-have-null-ended-at
Open

fix: O3-5485 Fix NaN return from computeAverageWaitTimeInMinutes when no entries have endedAt#99
sanks011 wants to merge 1 commit intoopenmrs:mainfrom
sanks011:O3-5485-queue-utils-compute-average-wait-time-in-minutes-returns-na-n-when-all-queue-entries-have-null-ended-at

Conversation

@sanks011
Copy link

Summary

QueueUtils.computeAverageWaitTimeInMinutes() returns Double.NaN when all queue entries
in the list have a null endedAt date. This causes the /ws/rest/v1/queue-entry-metric and
/ws/rest/v1/queue-metrics REST endpoints to return NaN in JSON responses - which is
invalid per RFC 7159 and breaks frontend metric display.

Root Cause

The method computes totalWaitTime / numEntries without guarding against numEntries == 0.
When every entry is skipped (because endedAt is null), this evaluates to 0.0 / 0 which
per JLS §15.17.2 produces Double.NaN — no exception is thrown.

Fix

Added a numEntries > 0 guard before the division. When no entries have both startedAt
and endedAt populated, the method now returns 0.0 instead of NaN.

Tests Added

  • computeAverageWaitTime_shouldReturnZeroForNullList
  • computeAverageWaitTime_shouldReturnZeroForEmptyList
  • computeAverageWaitTime_shouldReturnZeroWhenAllEntriesHaveNullEndedAt
  • computeAverageWaitTime_shouldReturnCorrectAverageForEntriesWithEndedAt
  • computeAverageWaitTime_shouldSkipEntriesWithNullEndedAt

##Related
https://openmrs.atlassian.net/jira/software/c/projects/O3/boards/37?selectedIssue=O3-5485

@sanks011 sanks011 changed the title fix:O3-5485 Fix NaN return from computeAverageWaitTimeInMinutes when no entries have endedAt fix: O3-5485 Fix NaN return from computeAverageWaitTimeInMinutes when no entries have endedAt Mar 12, 2026
@vimla01
Copy link

vimla01 commented Mar 20, 2026

I have Tested this locally and this looks correct. It fixes the NaN case by returning 0.0 when no matching entries have endedAt, and the added tests cover the expected edge cases.

small note : a shorter branch name can make manual fetch/checkouts a bit easier.

@sanks011 sanks011 closed this Mar 21, 2026
@sanks011 sanks011 deleted the O3-5485-queue-utils-compute-average-wait-time-in-minutes-returns-na-n-when-all-queue-entries-have-null-ended-at branch March 21, 2026 16:43
@sanks011 sanks011 restored the O3-5485-queue-utils-compute-average-wait-time-in-minutes-returns-na-n-when-all-queue-entries-have-null-ended-at branch March 21, 2026 16:44
@sanks011 sanks011 reopened this Mar 21, 2026
@sanks011
Copy link
Author

I have Tested this locally and this looks correct. It fixes the NaN case by returning 0.0 when no matching entries have endedAt, and the added tests cover the expected edge cases.

small note : a shorter branch name can make manual fetch/checkouts a bit easier.

Thanks for testing and the feedback! Good tip on the branch name - I'll keep it shorter next time around. 👍

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.

2 participants