2021-02-22 15:42:29 +00:00
|
|
|
from sqlalchemy.dialects.postgresql import insert
|
Fix division by zero error on performance page
For preview and staging environments, we often send no messages
in a single day. This is currently causing a `DivisionByZero` error
that is rendering the page with no results. This makes it impossible
to look at preview/staging and see if the performance page is
working correctly or not.
(psycopg2.errors.DivisionByZero) division by zero
[SQL: SELECT CAST(ft_processing_time.bst_date AS TEXT) AS date, ft_processing_time.messages_total AS ft_processing_time_messages_total, ft_processing_time.messages_within_10_secs AS ft_processing_time_messages_within_10_secs, (ft_processing_time.messages_within_10_secs / CAST(ft_processing_time.messages_total AS FLOAT)) * %(param_1)s AS percentage
FROM ft_processing_time
WHERE ft_processing_time.bst_date >= %(bst_date_1)s AND ft_processing_time.bst_date <= %(bst_date_2)s ORDER BY ft_processing_time.bst_date]
[parameters: {'param_1': 100, 'bst_date_1': datetime.date(2021, 11, 12), 'bst_date_2': datetime.date(2021, 11, 19)}]
(Background on this error at: http://sqlalche.me/e/14/9h9h)
I've fixed this by falling back to 100.0% for days we send
no messages. Maybe some argument that it should be N/A rather than
100% but I think it doesn't really matter as this is only
going to affect preview and staging as we will never have a day
sending no messages in production.
2021-11-22 11:11:52 +00:00
|
|
|
from sqlalchemy.sql.expression import case
|
2021-02-22 15:42:29 +00:00
|
|
|
|
|
|
|
|
from app import db
|
2021-04-14 07:11:01 +01:00
|
|
|
from app.dao.dao_utils import autocommit
|
2021-02-22 15:42:29 +00:00
|
|
|
from app.models import FactProcessingTime
|
2024-05-23 13:59:51 -07:00
|
|
|
from app.utils import utc_now
|
2021-02-22 15:42:29 +00:00
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2021-02-22 15:42:29 +00:00
|
|
|
def insert_update_processing_time(processing_time):
|
2023-08-29 14:54:30 -07:00
|
|
|
"""
|
2021-02-22 15:42:29 +00:00
|
|
|
This uses the Postgres upsert to avoid race conditions when two threads try and insert
|
|
|
|
|
at the same row. The excluded object refers to values that we tried to insert but were
|
|
|
|
|
rejected.
|
|
|
|
|
http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#insert-on-conflict-upsert
|
2023-08-29 14:54:30 -07:00
|
|
|
"""
|
2021-02-22 15:42:29 +00:00
|
|
|
table = FactProcessingTime.__table__
|
|
|
|
|
stmt = insert(table).values(
|
2022-11-21 11:49:59 -05:00
|
|
|
local_date=processing_time.local_date,
|
2021-02-22 15:42:29 +00:00
|
|
|
messages_total=processing_time.messages_total,
|
2023-08-29 14:54:30 -07:00
|
|
|
messages_within_10_secs=processing_time.messages_within_10_secs,
|
2021-02-22 15:42:29 +00:00
|
|
|
)
|
|
|
|
|
stmt = stmt.on_conflict_do_update(
|
2022-11-21 11:49:59 -05:00
|
|
|
index_elements=[table.c.local_date],
|
2021-02-22 15:42:29 +00:00
|
|
|
set_={
|
2023-08-29 14:54:30 -07:00
|
|
|
"messages_total": stmt.excluded.messages_total,
|
|
|
|
|
"messages_within_10_secs": stmt.excluded.messages_within_10_secs,
|
2024-05-23 13:59:51 -07:00
|
|
|
"updated_at": utc_now(),
|
2023-08-29 14:54:30 -07:00
|
|
|
},
|
2021-02-22 15:42:29 +00:00
|
|
|
)
|
|
|
|
|
db.session.connection().execute(stmt)
|
2021-03-04 16:10:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_processing_time_percentage_for_date_range(start_date, end_date):
|
2023-08-29 14:54:30 -07:00
|
|
|
query = (
|
|
|
|
|
db.session.query(
|
|
|
|
|
FactProcessingTime.local_date.cast(db.Text).label("date"),
|
|
|
|
|
FactProcessingTime.messages_total,
|
|
|
|
|
FactProcessingTime.messages_within_10_secs,
|
|
|
|
|
case(
|
2024-04-24 16:27:20 -04:00
|
|
|
(
|
|
|
|
|
FactProcessingTime.messages_total > 0,
|
2023-08-29 14:54:30 -07:00
|
|
|
(
|
|
|
|
|
(
|
2024-04-24 16:27:20 -04:00
|
|
|
FactProcessingTime.messages_within_10_secs
|
|
|
|
|
/ FactProcessingTime.messages_total.cast(db.Float)
|
|
|
|
|
)
|
|
|
|
|
* 100
|
2023-08-29 14:54:30 -07:00
|
|
|
),
|
2024-04-24 16:27:20 -04:00
|
|
|
),
|
|
|
|
|
(FactProcessingTime.messages_total == 0, 100.0),
|
2023-08-29 14:54:30 -07:00
|
|
|
).label("percentage"),
|
|
|
|
|
)
|
|
|
|
|
.filter(
|
|
|
|
|
FactProcessingTime.local_date >= start_date,
|
|
|
|
|
FactProcessingTime.local_date <= end_date,
|
|
|
|
|
)
|
|
|
|
|
.order_by(FactProcessingTime.local_date)
|
|
|
|
|
)
|
2021-03-04 16:10:53 +00:00
|
|
|
|
|
|
|
|
return query.all()
|