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

Add try/except + rollback for fact notifications #2397

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions app/dao/fact_notification_status_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from flask import current_app
from sqlalchemy import Date, case, func
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.exc import IntegrityError
from sqlalchemy.sql.expression import extract, literal
from sqlalchemy.types import DateTime, Integer

Expand Down Expand Up @@ -106,29 +106,42 @@ def query_for_fact_status_data(table, start_date, end_date, notification_type, s


def update_fact_notification_status(data, process_day, service_ids=None):
table = FactNotificationStatus.__table__
if service_ids:
FactNotificationStatus.query.filter(
FactNotificationStatus.bst_date == process_day, FactNotificationStatus.service_id.in_(service_ids)
).delete()
else:
FactNotificationStatus.query.filter(FactNotificationStatus.bst_date == process_day).delete()

for row in data:
stmt = insert(table).values(
bst_date=process_day,
template_id=row.template_id,
service_id=row.service_id,
job_id=row.job_id,
notification_type=row.notification_type,
key_type=row.key_type,
notification_status=row.status,
notification_count=row.notification_count,
billable_units=row.billable_units,
)
db.session.connection().execute(stmt)
# Prepare bulk insert data
bulk_insert_data = [
{
"bst_date": process_day,
"template_id": row.template_id,
"service_id": row.service_id,
"job_id": row.job_id,
"notification_type": row.notification_type,
"key_type": row.key_type,
"notification_status": row.status,
"notification_count": row.notification_count,
"billable_units": row.billable_units,
}
for row in data
]
try:
if bulk_insert_data:
db.session.bulk_insert_mappings(FactNotificationStatus, bulk_insert_data)
db.session.commit()

except IntegrityError as e:
db.session.rollback()
current_app.logger.error(f"Integrity error in update_fact_notification_status: {e}")
raise
except Exception as e:
db.session.rollback()
current_app.logger.error(f"Unexpected error in update_fact_notification_status: {e}")
raise


def fetch_notification_status_for_service_by_month(start_date, end_date, service_id):
filters = [
Expand Down
Loading