mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
Merge pull request #3377 from alphagov/zero-case-performance-page
Fix division by zero error on performance page
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from sqlalchemy.dialects.postgresql import insert
|
from sqlalchemy.dialects.postgresql import insert
|
||||||
|
from sqlalchemy.sql.expression import case
|
||||||
|
|
||||||
from app import db
|
from app import db
|
||||||
from app.dao.dao_utils import autocommit
|
from app.dao.dao_utils import autocommit
|
||||||
@@ -37,8 +38,13 @@ def get_processing_time_percentage_for_date_range(start_date, end_date):
|
|||||||
FactProcessingTime.bst_date.cast(db.Text).label("date"),
|
FactProcessingTime.bst_date.cast(db.Text).label("date"),
|
||||||
FactProcessingTime.messages_total,
|
FactProcessingTime.messages_total,
|
||||||
FactProcessingTime.messages_within_10_secs,
|
FactProcessingTime.messages_within_10_secs,
|
||||||
((FactProcessingTime.messages_within_10_secs / FactProcessingTime.messages_total.cast(
|
case([
|
||||||
db.Float)) * 100).label("percentage")
|
(
|
||||||
|
FactProcessingTime.messages_total > 0,
|
||||||
|
((FactProcessingTime.messages_within_10_secs / FactProcessingTime.messages_total.cast(db.Float)) * 100)
|
||||||
|
),
|
||||||
|
(FactProcessingTime.messages_total == 0, 100.0)
|
||||||
|
]).label("percentage")
|
||||||
).filter(
|
).filter(
|
||||||
FactProcessingTime.bst_date >= start_date,
|
FactProcessingTime.bst_date >= start_date,
|
||||||
FactProcessingTime.bst_date <= end_date
|
FactProcessingTime.bst_date <= end_date
|
||||||
|
|||||||
@@ -70,3 +70,29 @@ def test_get_processing_time_percentage_for_date_range(notify_db_session):
|
|||||||
assert results[0].messages_total == 3
|
assert results[0].messages_total == 3
|
||||||
assert results[0].messages_within_10_secs == 2
|
assert results[0].messages_within_10_secs == 2
|
||||||
assert round(results[0].percentage, 1) == 66.7
|
assert round(results[0].percentage, 1) == 66.7
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_processing_time_percentage_for_date_range_handles_zero_cases(notify_db_session):
|
||||||
|
create_process_time(
|
||||||
|
bst_date='2021-02-21',
|
||||||
|
messages_total=0,
|
||||||
|
messages_within_10_secs=0
|
||||||
|
)
|
||||||
|
create_process_time(
|
||||||
|
bst_date='2021-02-22',
|
||||||
|
messages_total=10,
|
||||||
|
messages_within_10_secs=0
|
||||||
|
)
|
||||||
|
|
||||||
|
results = get_processing_time_percentage_for_date_range('2021-02-21', '2021-02-22')
|
||||||
|
|
||||||
|
assert len(results) == 2
|
||||||
|
assert results[0].date == '2021-02-21'
|
||||||
|
assert results[0].messages_total == 0
|
||||||
|
assert results[0].messages_within_10_secs == 0
|
||||||
|
assert results[0].percentage == 100.0
|
||||||
|
|
||||||
|
assert results[1].date == '2021-02-22'
|
||||||
|
assert results[1].messages_total == 10
|
||||||
|
assert results[1].messages_within_10_secs == 0
|
||||||
|
assert results[1].percentage == 0.0
|
||||||
|
|||||||
Reference in New Issue
Block a user