From a530bab859750dd9034bfe4a7bc5a835c106c24c Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Mon, 14 Oct 2024 11:33:21 -0700 Subject: [PATCH 01/49] update billing dao --- app/dao/annual_billing_dao.py | 34 ++++++++++++++++++++++------------ app/dao/fact_billing_dao.py | 9 +++++---- app/service_invite/rest.py | 2 +- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/app/dao/annual_billing_dao.py b/app/dao/annual_billing_dao.py index 0e4d3b96b..306a2dd86 100644 --- a/app/dao/annual_billing_dao.py +++ b/app/dao/annual_billing_dao.py @@ -1,4 +1,5 @@ from flask import current_app +from sqlalchemy import select, update from app import db from app.dao.dao_utils import autocommit @@ -26,42 +27,51 @@ def dao_create_or_update_annual_billing_for_year( def dao_get_annual_billing(service_id): - return ( - AnnualBilling.query.filter_by( + stmt = ( + select(AnnualBilling) + .filter_by( service_id=service_id, ) .order_by(AnnualBilling.financial_year_start) - .all() ) + return db.session.execute(stmt).scalars().all() @autocommit def dao_update_annual_billing_for_future_years( service_id, free_sms_fragment_limit, financial_year_start ): - AnnualBilling.query.filter( - AnnualBilling.service_id == service_id, - AnnualBilling.financial_year_start > financial_year_start, - ).update({"free_sms_fragment_limit": free_sms_fragment_limit}) + stmt = ( + update(AnnualBilling) + .filter( + AnnualBilling.service_id == service_id, + AnnualBilling.financial_year_start > financial_year_start, + ) + .values({"free_sms_fragment_limit": free_sms_fragment_limit}) + ) + db.session.execute(stmt) + db.session.commit() def dao_get_free_sms_fragment_limit_for_year(service_id, financial_year_start=None): if not financial_year_start: financial_year_start = get_current_calendar_year_start_year() - return AnnualBilling.query.filter_by( + stmt = select(AnnualBilling).filter_by( service_id=service_id, financial_year_start=financial_year_start - ).first() + ) + return db.session.execute(stmt).scalars().first() def dao_get_all_free_sms_fragment_limit(service_id): - return ( - AnnualBilling.query.filter_by( + stmt = ( + select(AnnualBilling) + .filter_by( service_id=service_id, ) .order_by(AnnualBilling.financial_year_start) - .all() ) + return db.session.execute(stmt).scalars().all() def set_default_free_allowance_for_service(service, year_start=None): diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 14d82835b..111a9a053 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -1,7 +1,7 @@ from datetime import date, timedelta from flask import current_app -from sqlalchemy import Date, Integer, and_, desc, func, union +from sqlalchemy import Date, Integer, and_, desc, func, select, union from sqlalchemy.dialects.postgresql import insert from sqlalchemy.sql.expression import case, literal @@ -334,9 +334,8 @@ def query_service_sms_usage_for_year(service_id, year): free_allowance_used = func.least( remaining_free_allowance_before_this_row, this_rows_chargeable_units ) - - return ( - db.session.query( + stmt = ( + select( FactBilling.local_date, FactBilling.notifications_sent, this_rows_chargeable_units.label("chargeable_units"), @@ -346,6 +345,7 @@ def query_service_sms_usage_for_year(service_id, year): free_allowance_used.label("free_allowance_used"), charged_units.label("charged_units"), ) + .select_from(FactBilling) .join(AnnualBilling, AnnualBilling.service_id == service_id) .filter( FactBilling.service_id == service_id, @@ -355,6 +355,7 @@ def query_service_sms_usage_for_year(service_id, year): AnnualBilling.financial_year_start == year, ) ) + return stmt def delete_billing_data_for_service_for_day(process_day, service_id): diff --git a/app/service_invite/rest.py b/app/service_invite/rest.py index f6d9627da..5728b3ed5 100644 --- a/app/service_invite/rest.py +++ b/app/service_invite/rest.py @@ -86,7 +86,7 @@ def _create_service_invite(invited_user, invite_link_host): redis_store.set( f"email-personalisation-{saved_notification.id}", json.dumps(personalisation), - ex=2*24*60*60, + ex=2 * 24 * 60 * 60, ) send_notification_to_queue(saved_notification, queue=QueueNames.NOTIFY) From 287b7d1dec36afeb6b570cb1f93c6143026e214f Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Mon, 14 Oct 2024 11:52:42 -0700 Subject: [PATCH 02/49] update billing dao --- app/dao/fact_billing_dao.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 111a9a053..bd6474ac2 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -845,8 +845,8 @@ def fetch_daily_volumes_for_platform(start_date, end_date): def fetch_daily_sms_provider_volumes_for_platform(start_date, end_date): # query to return the total notifications sent per day for each channel. NB start and end dates are inclusive - daily_volume_stats = ( - db.session.query( + stmt = ( + select( FactBilling.local_date, FactBilling.provider, func.sum(FactBilling.notifications_sent).label("sms_totals"), @@ -860,6 +860,7 @@ def fetch_daily_sms_provider_volumes_for_platform(start_date, end_date): * FactBilling.rate ).label("sms_cost"), ) + .select_from(FactBilling) .filter( FactBilling.notification_type == NotificationType.SMS, FactBilling.local_date >= start_date, @@ -873,10 +874,8 @@ def fetch_daily_sms_provider_volumes_for_platform(start_date, end_date): FactBilling.local_date, FactBilling.provider, ) - .all() ) - - return daily_volume_stats + return db.session.execute(stmt).scalars().all() def fetch_volumes_by_service(start_date, end_date): @@ -885,7 +884,7 @@ def fetch_volumes_by_service(start_date, end_date): year_end_date = int(end_date.strftime("%Y")) volume_stats = ( - db.session.query( + select( FactBilling.local_date, FactBilling.service_id, func.sum( @@ -916,6 +915,7 @@ def fetch_volumes_by_service(start_date, end_date): ) ).label("email_totals"), ) + .select_from(FactBilling) .filter( FactBilling.local_date >= start_date, FactBilling.local_date <= end_date ) @@ -928,18 +928,18 @@ def fetch_volumes_by_service(start_date, end_date): ) annual_billing = ( - db.session.query( + select( func.max(AnnualBilling.financial_year_start).label("financial_year_start"), AnnualBilling.service_id, AnnualBilling.free_sms_fragment_limit, ) + .select_from(AnnualBilling) .filter(AnnualBilling.financial_year_start <= year_end_date) .group_by(AnnualBilling.service_id, AnnualBilling.free_sms_fragment_limit) .subquery() ) - - results = ( - db.session.query( + stmt = ( + select( Service.name.label("service_name"), Service.id.label("service_id"), Service.organization_id.label("organization_id"), @@ -977,7 +977,7 @@ def fetch_volumes_by_service(start_date, end_date): Organization.name, Service.name, ) - .all() ) + results = db.session.execute(stmt).scalars().all() return results From 43e774ce4414dd7d2aec1073f210174e51122536 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Mon, 14 Oct 2024 12:12:31 -0700 Subject: [PATCH 03/49] update billing dao --- app/dao/fact_billing_dao.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index bd6474ac2..616e0e4d1 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -875,7 +875,7 @@ def fetch_daily_sms_provider_volumes_for_platform(start_date, end_date): FactBilling.provider, ) ) - return db.session.execute(stmt).scalars().all() + return db.session.execute(stmt).all() def fetch_volumes_by_service(start_date, end_date): @@ -978,6 +978,6 @@ def fetch_volumes_by_service(start_date, end_date): Service.name, ) ) - results = db.session.execute(stmt).scalars().all() + results = db.session.execute(stmt).all() return results From 961f8f85f062811fb2331a80f48659c7db710515 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Mon, 14 Oct 2024 14:11:12 -0700 Subject: [PATCH 04/49] fix a method --- app/dao/fact_billing_dao.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 616e0e4d1..e7ec89f6e 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -31,7 +31,7 @@ def fetch_sms_free_allowance_remainder_until_date(end_date): ) query = ( - db.session.query( + select( AnnualBilling.service_id.label("service_id"), AnnualBilling.free_sms_fragment_limit, billable_units.label("billable_units"), @@ -40,6 +40,7 @@ def fetch_sms_free_allowance_remainder_until_date(end_date): 0, ).label("sms_remainder"), ) + .select_from(AnnualBilling) .outerjoin( # if there are no ft_billing rows for a service we still want to return the annual billing so we can use the # free_sms_fragment_limit) From fb1c2c1b3adc8b9db87cb4366c592ae8a6899c7b Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 07:17:10 -0700 Subject: [PATCH 05/49] fix a method --- app/dao/fact_billing_dao.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index e7ec89f6e..7ff40a371 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -88,7 +88,7 @@ def fetch_sms_billing_for_all_services(start_date, end_date): sms_cost = chargeable_sms * FactBilling.rate query = ( - db.session.query( + select( Organization.name.label("organization_name"), Organization.id.label("organization_id"), Service.name.label("service_name"), @@ -127,7 +127,7 @@ def fetch_sms_billing_for_all_services(start_date, end_date): .order_by(Organization.name, Service.name) ) - return query.all() + return db.session.execute(query).all() def fetch_billing_totals_for_year(service_id, year): From 18ef32bf4d359fe316d970c87c467e0b42e94d1e Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 07:32:04 -0700 Subject: [PATCH 06/49] fix a test --- tests/app/dao/test_fact_billing_dao.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/app/dao/test_fact_billing_dao.py b/tests/app/dao/test_fact_billing_dao.py index 30f2cd1c3..2291ab9dc 100644 --- a/tests/app/dao/test_fact_billing_dao.py +++ b/tests/app/dao/test_fact_billing_dao.py @@ -671,7 +671,8 @@ def test_fetch_sms_free_allowance_remainder_until_date_with_two_services( rate=0.11, ) - results = fetch_sms_free_allowance_remainder_until_date(datetime(2016, 5, 1)).all() + stmt = fetch_sms_free_allowance_remainder_until_date(datetime(2016, 5, 1)) + results = db.session.execute(stmt).all() assert len(results) == 2 service_result = [row for row in results if row[0] == service.id] assert service_result[0] == (service.id, 10, 2, 8) From c49bfb9341ca6198025fe6f7a67fece119f05782 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 07:54:24 -0700 Subject: [PATCH 07/49] fix a method --- app/dao/fact_billing_dao.py | 146 +++++++++++++++++------------------- 1 file changed, 68 insertions(+), 78 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 7ff40a371..014709d04 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -147,36 +147,29 @@ def fetch_billing_totals_for_year(service_id, year): a rate multiplier. Each subquery returns the same set of columns, which we pick from here before the big union. """ - return ( - db.session.query( - union( - *[ - db.session.query( - query.c.notification_type.label("notification_type"), - query.c.rate.label("rate"), - func.sum(query.c.notifications_sent).label( - "notifications_sent" - ), - func.sum(query.c.chargeable_units).label("chargeable_units"), - func.sum(query.c.cost).label("cost"), - func.sum(query.c.free_allowance_used).label( - "free_allowance_used" - ), - func.sum(query.c.charged_units).label("charged_units"), - ).group_by(query.c.rate, query.c.notification_type) - for query in [ - query_service_sms_usage_for_year(service_id, year).subquery(), - query_service_email_usage_for_year(service_id, year).subquery(), - ] + stmt = select( + union( + *[ + select( + query.c.notification_type.label("notification_type"), + query.c.rate.label("rate"), + func.sum(query.c.notifications_sent).label("notifications_sent"), + func.sum(query.c.chargeable_units).label("chargeable_units"), + func.sum(query.c.cost).label("cost"), + func.sum(query.c.free_allowance_used).label("free_allowance_used"), + func.sum(query.c.charged_units).label("charged_units"), + ).group_by(query.c.rate, query.c.notification_type) + for query in [ + query_service_sms_usage_for_year(service_id, year).subquery(), + query_service_email_usage_for_year(service_id, year).subquery(), ] - ).subquery() - ) - .order_by( - "notification_type", - "rate", - ) - .all() + ] + ).subquery() + ).order_by( + "notification_type", + "rate", ) + return db.session.execute(stmt).all() def fetch_monthly_billing_for_year(service_id, year): @@ -209,63 +202,60 @@ def fetch_monthly_billing_for_year(service_id, year): for d in data: update_fact_billing(data=d, process_day=today) - return ( - db.session.query( - union( - *[ - db.session.query( - query.c.rate.label("rate"), - query.c.notification_type.label("notification_type"), - func.date_trunc("month", query.c.local_date) - .cast(Date) - .label("month"), - func.sum(query.c.notifications_sent).label( - "notifications_sent" - ), - func.sum(query.c.chargeable_units).label("chargeable_units"), - func.sum(query.c.cost).label("cost"), - func.sum(query.c.free_allowance_used).label( - "free_allowance_used" - ), - func.sum(query.c.charged_units).label("charged_units"), - ).group_by( - query.c.rate, - query.c.notification_type, - "month", - ) - for query in [ - query_service_sms_usage_for_year(service_id, year).subquery(), - query_service_email_usage_for_year(service_id, year).subquery(), - ] + stmt = select( + union( + *[ + select( + query.c.rate.label("rate"), + query.c.notification_type.label("notification_type"), + func.date_trunc("month", query.c.local_date) + .cast(Date) + .label("month"), + func.sum(query.c.notifications_sent).label("notifications_sent"), + func.sum(query.c.chargeable_units).label("chargeable_units"), + func.sum(query.c.cost).label("cost"), + func.sum(query.c.free_allowance_used).label("free_allowance_used"), + func.sum(query.c.charged_units).label("charged_units"), + ).group_by( + query.c.rate, + query.c.notification_type, + "month", + ) + for query in [ + query_service_sms_usage_for_year(service_id, year).subquery(), + query_service_email_usage_for_year(service_id, year).subquery(), ] - ).subquery() - ) - .order_by( - "month", - "notification_type", - "rate", - ) - .all() + ] + ).subquery() + ).order_by( + "month", + "notification_type", + "rate", ) + return db.session.execute(stmt).all() def query_service_email_usage_for_year(service_id, year): year_start, year_end = get_calendar_year_dates(year) - return db.session.query( - FactBilling.local_date, - FactBilling.notifications_sent, - FactBilling.billable_units.label("chargeable_units"), - FactBilling.rate, - FactBilling.notification_type, - literal(0).label("cost"), - literal(0).label("free_allowance_used"), - FactBilling.billable_units.label("charged_units"), - ).filter( - FactBilling.service_id == service_id, - FactBilling.local_date >= year_start, - FactBilling.local_date <= year_end, - FactBilling.notification_type == NotificationType.EMAIL, + return ( + select( + FactBilling.local_date, + FactBilling.notifications_sent, + FactBilling.billable_units.label("chargeable_units"), + FactBilling.rate, + FactBilling.notification_type, + literal(0).label("cost"), + literal(0).label("free_allowance_used"), + FactBilling.billable_units.label("charged_units"), + ) + .select_from(FactBilling) + .filter( + FactBilling.service_id == service_id, + FactBilling.local_date >= year_start, + FactBilling.local_date <= year_end, + FactBilling.notification_type == NotificationType.EMAIL, + ) ) From 1fe4ec8b834c8c24cce0b77b4d08eacfcf578fe7 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 08:51:00 -0700 Subject: [PATCH 08/49] fix a delete query --- app/dao/fact_billing_dao.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 014709d04..bd7b987f5 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -1,7 +1,7 @@ from datetime import date, timedelta from flask import current_app -from sqlalchemy import Date, Integer, and_, desc, func, select, union +from sqlalchemy import Date, Integer, and_, delete, desc, func, select, union from sqlalchemy.dialects.postgresql import insert from sqlalchemy.sql.expression import case, literal @@ -355,9 +355,12 @@ def delete_billing_data_for_service_for_day(process_day, service_id): Returns how many rows were deleted """ - return FactBilling.query.filter( + stmt = delete(FactBilling).filter( FactBilling.local_date == process_day, FactBilling.service_id == service_id - ).delete() + ) + result = db.session.execute(stmt) + db.session.commit() + return result.rowcount def fetch_billing_data_for_day(process_day, service_id=None, check_permissions=False): From c86a0d7214a0a6412cf7cbb5da7550f3cd908f9d Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 09:02:52 -0700 Subject: [PATCH 09/49] fix rates query --- app/dao/fact_billing_dao.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index bd7b987f5..095d210e6 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -392,7 +392,7 @@ def fetch_billing_data_for_day(process_day, service_id=None, check_permissions=F def _query_for_billing_data(notification_type, start_date, end_date, service): def _email_query(): return ( - db.session.query( + select( NotificationAllTimeView.template_id, literal(service.id).label("service_id"), literal(notification_type).label("notification_type"), @@ -402,6 +402,7 @@ def _email_query(): literal(0).label("billable_units"), func.count().label("notifications_sent"), ) + .select_from(NotificationAllTimeView) .filter( NotificationAllTimeView.status.in_( NotificationStatus.sent_email_types() @@ -424,7 +425,7 @@ def _sms_query(): ).cast(Integer) international = func.coalesce(NotificationAllTimeView.international, False) return ( - db.session.query( + select( NotificationAllTimeView.template_id, literal(service.id).label("service_id"), literal(notification_type).label("notification_type"), @@ -436,6 +437,7 @@ def _sms_query(): ), func.count().label("notifications_sent"), ) + .select_from(NotificationAllTimeView) .filter( NotificationAllTimeView.status.in_( NotificationStatus.billable_sms_types() @@ -460,12 +462,12 @@ def _sms_query(): } query = query_funcs[notification_type]() - return query.all() + return db.session.execute(query).all() def get_rates_for_billing(): - rates = Rate.query.order_by(desc(Rate.valid_from)).all() - return rates + stmt = select(Rate).order_by(desc(Rate.valid_from)) + return db.session.execute(stmt).all() def get_service_ids_that_need_billing_populated(start_date, end_date): From 333cd1de394708edf09b1687555e194f18ca8b4d Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 09:12:52 -0700 Subject: [PATCH 10/49] try scalars to resolve test failure --- app/dao/fact_billing_dao.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 095d210e6..fa8b43338 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -462,7 +462,7 @@ def _sms_query(): } query = query_funcs[notification_type]() - return db.session.execute(query).all() + return db.session.execute(query).scalars().all() def get_rates_for_billing(): From 223a8f00a6c084ac9c023f998a40e21b10cc3bb1 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 09:28:45 -0700 Subject: [PATCH 11/49] revert scalars --- app/dao/fact_billing_dao.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index fa8b43338..095d210e6 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -462,7 +462,7 @@ def _sms_query(): } query = query_funcs[notification_type]() - return db.session.execute(query).scalars().all() + return db.session.execute(query).all() def get_rates_for_billing(): From f52026204e5cd3e0fffae80c80db2d47e6c9f330 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 10:08:57 -0700 Subject: [PATCH 12/49] revert scalars --- app/dao/fact_billing_dao.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 095d210e6..706540040 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -467,7 +467,7 @@ def _sms_query(): def get_rates_for_billing(): stmt = select(Rate).order_by(desc(Rate.valid_from)) - return db.session.execute(stmt).all() + return db.session.execute(stmt).scalars().all() def get_service_ids_that_need_billing_populated(start_date, end_date): @@ -487,6 +487,9 @@ def get_service_ids_that_need_billing_populated(start_date, end_date): def get_rate(rates, notification_type, date): + print( + f"ENTER get_rate with rates {rates} and notification_type {notification_type}" + ) start_of_day = get_midnight_in_utc(date) if notification_type == NotificationType.SMS: From 579d856efb7be2e28a1036d3792b223f42bd466b Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 10:29:15 -0700 Subject: [PATCH 13/49] revert scalars --- app/dao/fact_billing_dao.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index 706540040..f5d4bbc5d 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -471,8 +471,9 @@ def get_rates_for_billing(): def get_service_ids_that_need_billing_populated(start_date, end_date): - return ( - db.session.query(NotificationHistory.service_id) + stmt = ( + select(NotificationHistory.service_id) + .select_from(NotificationHistory) .filter( NotificationHistory.created_at >= start_date, NotificationHistory.created_at <= end_date, @@ -482,14 +483,11 @@ def get_service_ids_that_need_billing_populated(start_date, end_date): NotificationHistory.billable_units != 0, ) .distinct() - .all() ) + return db.session.execute(stmt).all() def get_rate(rates, notification_type, date): - print( - f"ENTER get_rate with rates {rates} and notification_type {notification_type}" - ) start_of_day = get_midnight_in_utc(date) if notification_type == NotificationType.SMS: @@ -560,7 +558,7 @@ def create_billing_record(data, rate, process_day): def fetch_email_usage_for_organization(organization_id, start_date, end_date): query = ( - db.session.query( + select( Service.name.label("service_name"), Service.id.label("service_id"), func.sum(FactBilling.notifications_sent).label("emails_sent"), @@ -583,7 +581,7 @@ def fetch_email_usage_for_organization(organization_id, start_date, end_date): ) .order_by(Service.name) ) - return query.all() + return db.session.execute(query).all() def fetch_sms_billing_for_organization(organization_id, financial_year): From c2a2dd0e1beb2d3cb0968cb44c4148f6981c7ffa Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 10:57:08 -0700 Subject: [PATCH 14/49] revert scalars --- app/dao/fact_billing_dao.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index f5d4bbc5d..f5fd93089 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -604,7 +604,7 @@ def fetch_sms_billing_for_organization(organization_id, financial_year): sms_cost = func.sum(ft_billing_subquery.c.cost) query = ( - db.session.query( + select( Service.name.label("service_name"), Service.id.label("service_id"), AnnualBilling.free_sms_fragment_limit, @@ -630,7 +630,7 @@ def fetch_sms_billing_for_organization(organization_id, financial_year): .order_by(Service.name) ) - return query.all() + return db.session.execute(query).all() def query_organization_sms_usage_for_year(organization_id, year): @@ -671,7 +671,7 @@ def query_organization_sms_usage_for_year(organization_id, year): ) return ( - db.session.query( + select( Service.id.label("service_id"), FactBilling.local_date, this_rows_chargeable_units.label("chargeable_units"), @@ -746,7 +746,7 @@ def fetch_usage_year_for_organization(organization_id, year): def fetch_billing_details_for_all_services(): billing_details = ( - db.session.query( + select( Service.id.label("service_id"), func.coalesce( Service.purchase_order_number, Organization.purchase_order_number @@ -762,11 +762,12 @@ def fetch_billing_details_for_all_services(): Service.billing_reference, Organization.billing_reference ).label("billing_reference"), ) + .select_from(Service) .outerjoin(Service.organization) .all() ) - return billing_details + return db.session.execute(billing_details).all() def fetch_daily_volumes_for_platform(start_date, end_date): From 597f2b0a8ba1365e3a99f32df5154f6a9d40147b Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 11:17:23 -0700 Subject: [PATCH 15/49] remove all() from statement --- app/dao/fact_billing_dao.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index f5fd93089..b99107a5f 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -764,7 +764,6 @@ def fetch_billing_details_for_all_services(): ) .select_from(Service) .outerjoin(Service.organization) - .all() ) return db.session.execute(billing_details).all() From 6db8fcf2e880e7bcd41355958308e5a7eb9a2f21 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 11:31:18 -0700 Subject: [PATCH 16/49] remove all() from statement --- tests/app/dao/test_fact_billing_dao.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/app/dao/test_fact_billing_dao.py b/tests/app/dao/test_fact_billing_dao.py index 2291ab9dc..11ec97b9f 100644 --- a/tests/app/dao/test_fact_billing_dao.py +++ b/tests/app/dao/test_fact_billing_dao.py @@ -1225,8 +1225,8 @@ def test_query_organization_sms_usage_for_year_handles_multiple_services( ) # ---------- - - result = query_organization_sms_usage_for_year(org.id, 2022).all() + stmt = query_organization_sms_usage_for_year(org.id, 2022) + result = db.session.execute(stmt).all() service_1_rows = [row._asdict() for row in result if row.service_id == service_1.id] service_2_rows = [row._asdict() for row in result if row.service_id == service_2.id] @@ -1296,10 +1296,9 @@ def test_query_organization_sms_usage_for_year_handles_multiple_rates( financial_year_start=current_year, ) - result = [ - row._asdict() - for row in query_organization_sms_usage_for_year(org.id, 2022).all() - ] + stmt = query_organization_sms_usage_for_year(org.id, 2022) + rows = db.session.execute(rows).all() + result = [row._asdict() for row in rows] # al lthe free allowance is used on the first day assert result[0]["local_date"] == date(2022, 4, 29) From b9f1eae7e3ac7c6a5718f5d565e8dd33c9cde948 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 11:34:35 -0700 Subject: [PATCH 17/49] remove all() from statement --- tests/app/dao/test_fact_billing_dao.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/app/dao/test_fact_billing_dao.py b/tests/app/dao/test_fact_billing_dao.py index 11ec97b9f..49c59d48d 100644 --- a/tests/app/dao/test_fact_billing_dao.py +++ b/tests/app/dao/test_fact_billing_dao.py @@ -1297,7 +1297,7 @@ def test_query_organization_sms_usage_for_year_handles_multiple_rates( ) stmt = query_organization_sms_usage_for_year(org.id, 2022) - rows = db.session.execute(rows).all() + rows = db.session.execute(stmt).all() result = [row._asdict() for row in rows] # al lthe free allowance is used on the first day From 2ef49ac95e106a74f1ffdcf801846145366a871d Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 11:46:18 -0700 Subject: [PATCH 18/49] remove all() from statement --- app/dao/fact_billing_dao.py | 7 +++---- tests/app/dao/test_fact_billing_dao.py | 10 ++++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/app/dao/fact_billing_dao.py b/app/dao/fact_billing_dao.py index b99107a5f..132f62bf2 100644 --- a/app/dao/fact_billing_dao.py +++ b/app/dao/fact_billing_dao.py @@ -773,7 +773,7 @@ def fetch_daily_volumes_for_platform(start_date, end_date): # query to return the total notifications sent per day for each channel. NB start and end dates are inclusive daily_volume_stats = ( - db.session.query( + select( FactBilling.local_date, func.sum( case( @@ -820,7 +820,7 @@ def fetch_daily_volumes_for_platform(start_date, end_date): ) aggregated_totals = ( - db.session.query( + select( daily_volume_stats.c.local_date.cast(db.Text).label("local_date"), func.sum(daily_volume_stats.c.sms_totals).label("sms_totals"), func.sum(daily_volume_stats.c.sms_fragment_totals).label( @@ -833,10 +833,9 @@ def fetch_daily_volumes_for_platform(start_date, end_date): ) .group_by(daily_volume_stats.c.local_date) .order_by(daily_volume_stats.c.local_date) - .all() ) - return aggregated_totals + return db.session.execute(aggregated_totals).all() def fetch_daily_sms_provider_volumes_for_platform(start_date, end_date): diff --git a/tests/app/dao/test_fact_billing_dao.py b/tests/app/dao/test_fact_billing_dao.py index 49c59d48d..4b64e6b36 100644 --- a/tests/app/dao/test_fact_billing_dao.py +++ b/tests/app/dao/test_fact_billing_dao.py @@ -3,6 +3,7 @@ import pytest from freezegun import freeze_time +from sqlalchemy import func, select from app import db from app.dao.fact_billing_dao import ( @@ -614,7 +615,8 @@ def test_delete_billing_data(notify_db_session): delete_billing_data_for_service_for_day("2018-01-01", service_1.id) - current_rows = FactBilling.query.all() + stmt = select(FactBilling) + current_rows = db.session.execute(stmt).all() assert sorted(x.billable_units for x in current_rows) == sorted( [other_day.billable_units, other_service.billable_units] ) @@ -974,8 +976,8 @@ def test_fetch_usage_year_for_organization_populates_ft_billing_for_today( free_sms_fragment_limit=10, financial_year_start=current_year, ) - - assert FactBilling.query.count() == 0 + stmt = select(func.count()).select_from(FactBilling) + assert db.session.execute(stmt).scalar() == 0 create_notification(template=template, status=NotificationStatus.DELIVERED) @@ -983,7 +985,7 @@ def test_fetch_usage_year_for_organization_populates_ft_billing_for_today( organization_id=new_org.id, year=current_year ) assert len(results) == 1 - assert FactBilling.query.count() == 1 + assert db.session.execute(stmt).scalar() == 1 @freeze_time("2022-05-01 13:30") From 3b25cfe8bccf2e32cfc8e18654f807ed09e94845 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 12:02:35 -0700 Subject: [PATCH 19/49] remove all() from statement --- tests/app/dao/test_fact_billing_dao.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/app/dao/test_fact_billing_dao.py b/tests/app/dao/test_fact_billing_dao.py index 4b64e6b36..e1331dfe5 100644 --- a/tests/app/dao/test_fact_billing_dao.py +++ b/tests/app/dao/test_fact_billing_dao.py @@ -616,7 +616,7 @@ def test_delete_billing_data(notify_db_session): delete_billing_data_for_service_for_day("2018-01-01", service_1.id) stmt = select(FactBilling) - current_rows = db.session.execute(stmt).all() + current_rows = db.session.execute(stmt).scalars().all() assert sorted(x.billable_units for x in current_rows) == sorted( [other_day.billable_units, other_service.billable_units] ) From 028f55e0b0ff2da6f88b9d231a33d953ee935d5b Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 12:25:10 -0700 Subject: [PATCH 20/49] remove all() from statement --- app/dao/fact_notification_status_dao.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/dao/fact_notification_status_dao.py b/app/dao/fact_notification_status_dao.py index df8e653ee..13a21abf2 100644 --- a/app/dao/fact_notification_status_dao.py +++ b/app/dao/fact_notification_status_dao.py @@ -1,6 +1,6 @@ from datetime import timedelta -from sqlalchemy import Date, case, cast, func, select, union_all +from sqlalchemy import Date, case, cast, delete, func, select, union_all from sqlalchemy.dialects.postgresql import insert from sqlalchemy.orm import aliased from sqlalchemy.sql.expression import extract, literal @@ -33,14 +33,16 @@ def update_fact_notification_status(process_day, notification_type, service_id): end_date = get_midnight_in_utc(process_day + timedelta(days=1)) # delete any existing rows in case some no longer exist e.g. if all messages are sent - FactNotificationStatus.query.filter( + stmt = delete(FactNotificationStatus).filter( FactNotificationStatus.local_date == process_day, FactNotificationStatus.notification_type == notification_type, FactNotificationStatus.service_id == service_id, - ).delete() + ) + db.session.execute(stmt) + db.session.commit() query = ( - db.session.query( + select( literal(process_day).label("process_day"), NotificationAllTimeView.template_id, literal(service_id).label("service_id"), @@ -52,6 +54,7 @@ def update_fact_notification_status(process_day, notification_type, service_id): NotificationAllTimeView.status, func.count().label("notification_count"), ) + .select_from(NotificationAllTimeView) .filter( NotificationAllTimeView.created_at >= start_date, NotificationAllTimeView.created_at < end_date, @@ -86,13 +89,14 @@ def update_fact_notification_status(process_day, notification_type, service_id): def fetch_notification_status_for_service_by_month(start_date, end_date, service_id): - return ( - db.session.query( + stmt = ( + select( func.date_trunc("month", NotificationAllTimeView.created_at).label("month"), NotificationAllTimeView.notification_type, NotificationAllTimeView.status.label("notification_status"), func.count(NotificationAllTimeView.id).label("count"), ) + .select_from(NotificationAllTimeView) .filter( NotificationAllTimeView.service_id == service_id, NotificationAllTimeView.created_at >= start_date, @@ -104,8 +108,8 @@ def fetch_notification_status_for_service_by_month(start_date, end_date, service NotificationAllTimeView.notification_type, NotificationAllTimeView.status, ) - .all() ) + return db.session.execute(stmt).all() def fetch_notification_status_for_service_for_day(fetch_day, service_id): From 5f6894e5aafb434228ace8fe694112ffab6ab98c Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 12:34:24 -0700 Subject: [PATCH 21/49] remove all() from statement --- app/dao/fact_notification_status_dao.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/dao/fact_notification_status_dao.py b/app/dao/fact_notification_status_dao.py index 13a21abf2..9a8093ac4 100644 --- a/app/dao/fact_notification_status_dao.py +++ b/app/dao/fact_notification_status_dao.py @@ -113,14 +113,15 @@ def fetch_notification_status_for_service_by_month(start_date, end_date, service def fetch_notification_status_for_service_for_day(fetch_day, service_id): - return ( - db.session.query( + stmt = ( + select( # return current month as a datetime so the data has the same shape as the ft_notification_status query literal(fetch_day.replace(day=1), type_=DateTime).label("month"), Notification.notification_type, Notification.status.label("notification_status"), func.count().label("count"), ) + .select_from(Notification) .filter( Notification.created_at >= get_midnight_in_utc(fetch_day), Notification.created_at @@ -129,8 +130,8 @@ def fetch_notification_status_for_service_for_day(fetch_day, service_id): Notification.key_type != KeyType.TEST, ) .group_by(Notification.notification_type, Notification.status) - .all() ) + return db.session.execute(stmt).all() def fetch_notification_status_for_service_for_today_and_7_previous_days( @@ -250,7 +251,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days( def fetch_notification_status_totals_for_all_services(start_date, end_date): stats = ( - db.session.query( + select( FactNotificationStatus.notification_type.cast(db.Text).label( "notification_type" ), @@ -258,6 +259,7 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date): FactNotificationStatus.key_type.cast(db.Text).label("key_type"), func.sum(FactNotificationStatus.notification_count).label("count"), ) + .select_from(FactNotificationStatus) .filter( FactNotificationStatus.local_date >= start_date, FactNotificationStatus.local_date <= end_date, @@ -271,7 +273,7 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date): today = get_midnight_in_utc(utc_now()) if start_date <= utc_now().date() <= end_date: stats_for_today = ( - db.session.query( + select( Notification.notification_type.cast(db.Text).label("notification_type"), Notification.status.cast(db.Text), Notification.key_type.cast(db.Text), @@ -286,7 +288,7 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date): ) all_stats_table = stats.union_all(stats_for_today).subquery() query = ( - db.session.query( + select( all_stats_table.c.notification_type, all_stats_table.c.status, all_stats_table.c.key_type, @@ -301,7 +303,7 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date): ) else: query = stats.order_by(FactNotificationStatus.notification_type) - return query.all() + return db.session.execute(query).all() def fetch_notification_statuses_for_job(job_id): From 2cff3fa0fdcc59225ae560e23b50803581913482 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 12:52:44 -0700 Subject: [PATCH 22/49] remove all() from statement --- app/dao/fact_notification_status_dao.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/app/dao/fact_notification_status_dao.py b/app/dao/fact_notification_status_dao.py index 9a8093ac4..e689254b0 100644 --- a/app/dao/fact_notification_status_dao.py +++ b/app/dao/fact_notification_status_dao.py @@ -307,24 +307,25 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date): def fetch_notification_statuses_for_job(job_id): - return ( - db.session.query( + stmt = ( + select( FactNotificationStatus.notification_status.label("status"), func.sum(FactNotificationStatus.notification_count).label("count"), ) + .select_from(FactNotificationStatus) .filter( FactNotificationStatus.job_id == job_id, ) .group_by(FactNotificationStatus.notification_status) - .all() ) + return db.session.execute(stmt).all() def fetch_stats_for_all_services_by_date_range( start_date, end_date, include_from_test_key=True ): stats = ( - db.session.query( + select( FactNotificationStatus.service_id.label("service_id"), Service.name.label("name"), Service.restricted.label("restricted"), @@ -336,6 +337,7 @@ def fetch_stats_for_all_services_by_date_range( FactNotificationStatus.notification_status.cast(db.Text).label("status"), func.sum(FactNotificationStatus.notification_count).label("count"), ) + .select_from(FactNotificationStatus) .filter( FactNotificationStatus.local_date >= start_date, FactNotificationStatus.local_date <= end_date, @@ -360,12 +362,13 @@ def fetch_stats_for_all_services_by_date_range( if start_date <= utc_now().date() <= end_date: today = get_midnight_in_utc(utc_now()) subquery = ( - db.session.query( + select( Notification.notification_type.label("notification_type"), Notification.status.label("status"), Notification.service_id.label("service_id"), func.count(Notification.id).label("count"), ) + .select_from(Notification) .filter(Notification.created_at >= today) .group_by( Notification.notification_type, @@ -377,7 +380,7 @@ def fetch_stats_for_all_services_by_date_range( subquery = subquery.filter(Notification.key_type != KeyType.TEST) subquery = subquery.subquery() - stats_for_today = db.session.query( + stats_for_today = select( Service.id.label("service_id"), Service.name.label("name"), Service.restricted.label("restricted"), @@ -390,7 +393,7 @@ def fetch_stats_for_all_services_by_date_range( all_stats_table = stats.union_all(stats_for_today).subquery() query = ( - db.session.query( + select( all_stats_table.c.service_id, all_stats_table.c.name, all_stats_table.c.restricted, @@ -417,7 +420,7 @@ def fetch_stats_for_all_services_by_date_range( ) else: query = stats - return query.all() + return db.session.execute(query).all() def fetch_monthly_template_usage_for_service(start_date, end_date, service_id): From 9c95e588d1934d8548f98c0a9bd8769a2e55db1f Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 13:13:42 -0700 Subject: [PATCH 23/49] remove all() from statement --- app/dao/fact_notification_status_dao.py | 18 +++++++++--------- .../dao/test_fact_notification_status_dao.py | 13 ++++++++----- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/dao/fact_notification_status_dao.py b/app/dao/fact_notification_status_dao.py index e689254b0..4b238642e 100644 --- a/app/dao/fact_notification_status_dao.py +++ b/app/dao/fact_notification_status_dao.py @@ -426,7 +426,7 @@ def fetch_stats_for_all_services_by_date_range( def fetch_monthly_template_usage_for_service(start_date, end_date, service_id): # services_dao.replaces dao_fetch_monthly_historical_usage_by_template_for_service stats = ( - db.session.query( + select( FactNotificationStatus.template_id.label("template_id"), Template.name.label("name"), Template.template_type.label("template_type"), @@ -461,7 +461,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id): month = get_month_from_utc_column(Notification.created_at) stats_for_today = ( - db.session.query( + select( Notification.template_id.label("template_id"), Template.name.label("name"), Template.template_type.label("template_type"), @@ -490,7 +490,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id): all_stats_table = stats.union_all(stats_for_today).subquery() query = ( - db.session.query( + select( all_stats_table.c.template_id, all_stats_table.c.name, all_stats_table.c.template_type, @@ -511,12 +511,12 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id): ) else: query = stats - return query.all() + return db.session.execute(query).all() def get_total_notifications_for_date_range(start_date, end_date): query = ( - db.session.query( + select( FactNotificationStatus.local_date.label("local_date"), func.sum( case( @@ -550,12 +550,12 @@ def get_total_notifications_for_date_range(start_date, end_date): FactNotificationStatus.local_date >= start_date, FactNotificationStatus.local_date <= end_date, ) - return query.all() + return db.session.execute(query).all() def fetch_monthly_notification_statuses_per_service(start_date, end_date): - return ( - db.session.query( + stmt = ( + select( func.date_trunc("month", FactNotificationStatus.local_date) .cast(Date) .label("date_created"), @@ -648,5 +648,5 @@ def fetch_monthly_notification_statuses_per_service(start_date, end_date): Service.id, FactNotificationStatus.notification_type, ) - .all() ) + return db.session.execute(stmt).all() diff --git a/tests/app/dao/test_fact_notification_status_dao.py b/tests/app/dao/test_fact_notification_status_dao.py index 586c1c3ec..2c0de9014 100644 --- a/tests/app/dao/test_fact_notification_status_dao.py +++ b/tests/app/dao/test_fact_notification_status_dao.py @@ -3,7 +3,9 @@ import pytest from freezegun import freeze_time +from sqlalchemy import func, select +from app import db from app.dao.fact_notification_status_dao import ( fetch_monthly_notification_statuses_per_service, fetch_monthly_template_usage_for_service, @@ -1126,9 +1128,10 @@ def test_update_fact_notification_status_respects_gmt_bst( process_day, NotificationType.SMS, sample_service.id ) - assert ( - FactNotificationStatus.query.filter_by( - service_id=sample_service.id, local_date=process_day - ).count() - == expected_count + stmt = ( + select(func.count()) + .select_from(FactNotificationStatus) + .filter_by(service_id=sample_service.id, local_date=process_day) ) + result = db.session.execute(stmt) + assert result.rowcount == expected_count From f83032c4bc0bfd962bd2385863ce363d889b43a1 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 13:26:13 -0700 Subject: [PATCH 24/49] start on jobs dao --- app/dao/jobs_dao.py | 56 +++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index f4914e423..c5b5cc9e8 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -3,7 +3,7 @@ from datetime import timedelta from flask import current_app -from sqlalchemy import and_, asc, desc, func +from sqlalchemy import and_, asc, desc, func, select from app import db from app.enums import JobStatus @@ -18,36 +18,33 @@ def dao_get_notification_outcomes_for_job(service_id, job_id): - notification_statuses = ( - db.session.query( - func.count(Notification.status).label("count"), Notification.status - ) + stmt = ( + select(func.count(Notification.status).label("count"), Notification.status) .filter(Notification.service_id == service_id, Notification.job_id == job_id) .group_by(Notification.status) - .all() ) + notification_statuses = db.session.execute(stmt).all() if not notification_statuses: - notification_statuses = ( - db.session.query( - FactNotificationStatus.notification_count.label("count"), - FactNotificationStatus.notification_status.label("status"), - ) - .filter( - FactNotificationStatus.service_id == service_id, - FactNotificationStatus.job_id == job_id, - ) - .all() + stmt = select( + FactNotificationStatus.notification_count.label("count"), + FactNotificationStatus.notification_status.label("status"), + ).filter( + FactNotificationStatus.service_id == service_id, + FactNotificationStatus.job_id == job_id, ) + notification_statuses = db.session.execute(stmt).all() return notification_statuses def dao_get_job_by_service_id_and_job_id(service_id, job_id): - return Job.query.filter_by(service_id=service_id, id=job_id).one() + stmt = select(Job).filter_by(service_id=service_id, id=job_id) + return db.session.execute(stmt).scalars().one() def dao_get_unfinished_jobs(): - return Job.query.filter(Job.processing_finished.is_(None)).all() + stmt = select(Job).filter(Job.processing_finished.is_(None)) + return db.session.execute(stmt).all() def dao_get_jobs_by_service_id( @@ -67,8 +64,9 @@ def dao_get_jobs_by_service_id( query_filter.append(Job.created_at >= midnight_n_days_ago(limit_days)) if statuses is not None and statuses != [""]: query_filter.append(Job.job_status.in_(statuses)) + return ( - Job.query.filter(*query_filter) + select(*query_filter) .order_by(Job.processing_started.desc(), Job.created_at.desc()) .paginate(page=page, per_page=page_size) ) @@ -77,21 +75,19 @@ def dao_get_jobs_by_service_id( def dao_get_scheduled_job_stats( service_id, ): - return ( - db.session.query( - func.count(Job.id), - func.min(Job.scheduled_for), - ) - .filter( - Job.service_id == service_id, - Job.job_status == JobStatus.SCHEDULED, - ) - .one() + stmt = select( + func.count(Job.id), + func.min(Job.scheduled_for), + ).filter( + Job.service_id == service_id, + Job.job_status == JobStatus.SCHEDULED, ) + return db.session.execute(stmt).all() def dao_get_job_by_id(job_id): - return Job.query.filter_by(id=job_id).one() + stmt = select(Job).filter_by(id=job_id) + return db.session.execute(stmt).scalars().one() def dao_archive_job(job): From 2919395ad0070e03af8a21afd733b3e7705da8f4 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 13:32:08 -0700 Subject: [PATCH 25/49] start on jobs dao --- app/dao/jobs_dao.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index c5b5cc9e8..8d0fa270d 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -65,11 +65,12 @@ def dao_get_jobs_by_service_id( if statuses is not None and statuses != [""]: query_filter.append(Job.job_status.in_(statuses)) - return ( + stmt =( select(*query_filter) .order_by(Job.processing_started.desc(), Job.created_at.desc()) - .paginate(page=page, per_page=page_size) + ) + return db.session.execute(stmt).paginate(page=page, per_page=page_size) def dao_get_scheduled_job_stats( From 13c84184388da5e72104ab7e040a8c12dad112c3 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 13:37:49 -0700 Subject: [PATCH 26/49] start on jobs dao --- app/dao/jobs_dao.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 8d0fa270d..4d71a8d9d 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -65,10 +65,8 @@ def dao_get_jobs_by_service_id( if statuses is not None and statuses != [""]: query_filter.append(Job.job_status.in_(statuses)) - stmt =( - select(*query_filter) - .order_by(Job.processing_started.desc(), Job.created_at.desc()) - + stmt = select(*query_filter).order_by( + Job.processing_started.desc(), Job.created_at.desc() ) return db.session.execute(stmt).paginate(page=page, per_page=page_size) From 01675ae9cece58faa8f3913d4c9618e94266c50b Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 13:52:23 -0700 Subject: [PATCH 27/49] fix paginate --- app/dao/jobs_dao.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 4d71a8d9d..71031a86d 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -66,9 +66,10 @@ def dao_get_jobs_by_service_id( query_filter.append(Job.job_status.in_(statuses)) stmt = select(*query_filter).order_by( - Job.processing_started.desc(), Job.created_at.desc() + Job.processing_started.desc(), + Job.created_at.desc().limit(page_size).offset(page), ) - return db.session.execute(stmt).paginate(page=page, per_page=page_size) + return db.session.execute(stmt).scalars().all() def dao_get_scheduled_job_stats( From 573098bc618b0a4da1d7af1d49e26d9668700d43 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 14:00:21 -0700 Subject: [PATCH 28/49] fix paginate --- app/dao/jobs_dao.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 71031a86d..72bc6e1c0 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -67,8 +67,7 @@ def dao_get_jobs_by_service_id( stmt = select(*query_filter).order_by( Job.processing_started.desc(), - Job.created_at.desc().limit(page_size).offset(page), - ) + Job.created_at.desc()).limit(page_size).offset(page) return db.session.execute(stmt).scalars().all() From ad608865250dab90cb67312c74ea0f7278d0b195 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 14:31:27 -0700 Subject: [PATCH 29/49] try handmade pagination --- app/dao/jobs_dao.py | 17 +++++++++++++---- app/dao/pagination.py | 13 +++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 app/dao/pagination.py diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 72bc6e1c0..1d2584aa5 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -6,6 +6,7 @@ from sqlalchemy import and_, asc, desc, func, select from app import db +from app.dao.pagination import Pagination from app.enums import JobStatus from app.models import ( FactNotificationStatus, @@ -65,10 +66,18 @@ def dao_get_jobs_by_service_id( if statuses is not None and statuses != [""]: query_filter.append(Job.job_status.in_(statuses)) - stmt = select(*query_filter).order_by( - Job.processing_started.desc(), - Job.created_at.desc()).limit(page_size).offset(page) - return db.session.execute(stmt).scalars().all() + total_items = db.session.execute( + select(func.count()).select_from(*query_filter).scalar_one() + ) + + stmt = ( + select(*query_filter) + .order_by(Job.processing_started.desc(), Job.created_at.desc()) + .limit(page_size) + .offset(page) + ) + items = db.session.execute(stmt).scalars().all() + return Pagination(items, page, page_size, total_items) def dao_get_scheduled_job_stats( diff --git a/app/dao/pagination.py b/app/dao/pagination.py new file mode 100644 index 000000000..247f08fd3 --- /dev/null +++ b/app/dao/pagination.py @@ -0,0 +1,13 @@ +class Pagination: + def __init__(self, items, page, per_page, total): + self.items = items + self.page = page + self.per_page = per_page + self.total = total + self.pages = (total + per_page - 1) // per_page + + def has_next(self): + return self.page < self.pages + + def has_prev(self): + return self.page > 1 From 17cfa38df68305d836c4e17e7ee09d79e6e83dee Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 14:43:24 -0700 Subject: [PATCH 30/49] try handmade pagination --- app/dao/jobs_dao.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 1d2584aa5..5060e8d71 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -67,11 +67,11 @@ def dao_get_jobs_by_service_id( query_filter.append(Job.job_status.in_(statuses)) total_items = db.session.execute( - select(func.count()).select_from(*query_filter).scalar_one() + select(func.count()).select_from(Job).filter(*query_filter).scalar_one() ) stmt = ( - select(*query_filter) + select(Job).filter(*query_filter) .order_by(Job.processing_started.desc(), Job.created_at.desc()) .limit(page_size) .offset(page) From 935b778b22865edcb525f4df1924a32a4ab13b89 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 14:50:49 -0700 Subject: [PATCH 31/49] try handmade pagination --- app/dao/jobs_dao.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 5060e8d71..78cfc50d7 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -67,11 +67,12 @@ def dao_get_jobs_by_service_id( query_filter.append(Job.job_status.in_(statuses)) total_items = db.session.execute( - select(func.count()).select_from(Job).filter(*query_filter).scalar_one() - ) + select(func.count()).select_from(Job).filter(*query_filter) + ).scalar_one() stmt = ( - select(Job).filter(*query_filter) + select(Job) + .filter(*query_filter) .order_by(Job.processing_started.desc(), Job.created_at.desc()) .limit(page_size) .offset(page) From 94b8fc2a34e0888efd8c708f7dd67e863052d441 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 15:01:40 -0700 Subject: [PATCH 32/49] add prev_num to Pagination --- app/dao/pagination.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/dao/pagination.py b/app/dao/pagination.py index 247f08fd3..d3c0d70df 100644 --- a/app/dao/pagination.py +++ b/app/dao/pagination.py @@ -5,6 +5,7 @@ def __init__(self, items, page, per_page, total): self.per_page = per_page self.total = total self.pages = (total + per_page - 1) // per_page + self.prev_num = page - 1 if page > 1 else None def has_next(self): return self.page < self.pages From 9c39de402514cc0b9870a6b1668c7e71284869b5 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Tue, 15 Oct 2024 15:07:53 -0700 Subject: [PATCH 33/49] add next_num to Pagination --- app/dao/pagination.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/dao/pagination.py b/app/dao/pagination.py index d3c0d70df..cf6d8d4bd 100644 --- a/app/dao/pagination.py +++ b/app/dao/pagination.py @@ -6,6 +6,7 @@ def __init__(self, items, page, per_page, total): self.total = total self.pages = (total + per_page - 1) // per_page self.prev_num = page - 1 if page > 1 else None + self.next_num = page + 1 if page < self.pages else None def has_next(self): return self.page < self.pages From 8e3784caee60f221c6dba6579eb53b5823aa5ffb Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Wed, 16 Oct 2024 10:16:04 -0700 Subject: [PATCH 34/49] revert pagination for now --- app/dao/jobs_dao.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 78cfc50d7..cfbe4745e 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -6,7 +6,8 @@ from sqlalchemy import and_, asc, desc, func, select from app import db -from app.dao.pagination import Pagination + +# from app.dao.pagination import Pagination from app.enums import JobStatus from app.models import ( FactNotificationStatus, @@ -66,19 +67,25 @@ def dao_get_jobs_by_service_id( if statuses is not None and statuses != [""]: query_filter.append(Job.job_status.in_(statuses)) - total_items = db.session.execute( - select(func.count()).select_from(Job).filter(*query_filter) - ).scalar_one() - - stmt = ( - select(Job) - .filter(*query_filter) + # total_items = db.session.execute( + # select(func.count()).select_from(Job).filter(*query_filter) + # ).scalar_one() + + # stmt = ( + # select(Job) + # .filter(*query_filter) + # .order_by(Job.processing_started.desc(), Job.created_at.desc()) + # .limit(page_size) + # .offset(page) + # ) + # items = db.session.execute(stmt).scalars().all() + # return Pagination(items, page, page_size, total_items) + + return ( + Job.query.filter(*query_filter) .order_by(Job.processing_started.desc(), Job.created_at.desc()) - .limit(page_size) - .offset(page) + .paginate(page=page, per_page=page_size) ) - items = db.session.execute(stmt).scalars().all() - return Pagination(items, page, page_size, total_items) def dao_get_scheduled_job_stats( From d7700b2b08d6bfd5ce0cb8f93a51f1e0660d5d10 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Wed, 16 Oct 2024 10:48:17 -0700 Subject: [PATCH 35/49] fix test --- app/dao/jobs_dao.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index cfbe4745e..91ed6f493 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -91,6 +91,7 @@ def dao_get_jobs_by_service_id( def dao_get_scheduled_job_stats( service_id, ): + stmt = select( func.count(Job.id), func.min(Job.scheduled_for), @@ -98,7 +99,7 @@ def dao_get_scheduled_job_stats( Job.service_id == service_id, Job.job_status == JobStatus.SCHEDULED, ) - return db.session.execute(stmt).all() + return db.session.execute(stmt).scalars().one() def dao_get_job_by_id(job_id): From 7d3900f3c5dad494e1e5d3afd22a160cb7a4f37d Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Wed, 16 Oct 2024 10:56:08 -0700 Subject: [PATCH 36/49] fix test --- app/dao/jobs_dao.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 91ed6f493..13ee5829d 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -99,7 +99,7 @@ def dao_get_scheduled_job_stats( Job.service_id == service_id, Job.job_status == JobStatus.SCHEDULED, ) - return db.session.execute(stmt).scalars().one() + return db.session.execute(stmt).one() def dao_get_job_by_id(job_id): From 8b3851259951be0cc5344bedad53a0e6f76edfe1 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Wed, 16 Oct 2024 11:30:36 -0700 Subject: [PATCH 37/49] fix pagination maybe --- app/dao/jobs_dao.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 13ee5829d..563eba68f 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -6,8 +6,7 @@ from sqlalchemy import and_, asc, desc, func, select from app import db - -# from app.dao.pagination import Pagination +from app.dao.pagination import Pagination from app.enums import JobStatus from app.models import ( FactNotificationStatus, @@ -67,25 +66,26 @@ def dao_get_jobs_by_service_id( if statuses is not None and statuses != [""]: query_filter.append(Job.job_status.in_(statuses)) - # total_items = db.session.execute( - # select(func.count()).select_from(Job).filter(*query_filter) - # ).scalar_one() - - # stmt = ( - # select(Job) - # .filter(*query_filter) - # .order_by(Job.processing_started.desc(), Job.created_at.desc()) - # .limit(page_size) - # .offset(page) - # ) - # items = db.session.execute(stmt).scalars().all() - # return Pagination(items, page, page_size, total_items) + total_items = db.session.execute( + select(func.count()).select_from(Job).filter(*query_filter) + ).scalar_one() - return ( - Job.query.filter(*query_filter) + offset = (page - 1) * page_size + stmt = ( + select(Job) + .filter(*query_filter) .order_by(Job.processing_started.desc(), Job.created_at.desc()) - .paginate(page=page, per_page=page_size) + .limit(page_size) + .offset(offset) ) + items = db.session.execute(stmt).scalars().all() + return Pagination(items, page, page_size, total_items) + + # return ( + # Job.query.filter(*query_filter) + # .order_by(Job.processing_started.desc(), Job.created_at.desc()) + # .paginate(page=page, per_page=page_size) + # ) def dao_get_scheduled_job_stats( From 0182affc893cc56ccfef000732c4134a15db3b93 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Wed, 16 Oct 2024 11:49:30 -0700 Subject: [PATCH 38/49] down to line 179 --- app/dao/jobs_dao.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 563eba68f..f0c777081 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -81,12 +81,6 @@ def dao_get_jobs_by_service_id( items = db.session.execute(stmt).scalars().all() return Pagination(items, page, page_size, total_items) - # return ( - # Job.query.filter(*query_filter) - # .order_by(Job.processing_started.desc(), Job.created_at.desc()) - # .paginate(page=page, per_page=page_size) - # ) - def dao_get_scheduled_job_stats( service_id, @@ -121,15 +115,15 @@ def dao_set_scheduled_jobs_to_pending(): the transaction so that if the task is run more than once concurrently, one task will block the other select from completing until it commits. """ - jobs = ( - Job.query.filter( + stmt = ( + select( Job.job_status == JobStatus.SCHEDULED, Job.scheduled_for < utc_now(), ) .order_by(asc(Job.scheduled_for)) .with_for_update() - .all() ) + jobs = db.session.execute(stmt).all() for job in jobs: job.job_status = JobStatus.PENDING @@ -141,12 +135,13 @@ def dao_set_scheduled_jobs_to_pending(): def dao_get_future_scheduled_job_by_id_and_service_id(job_id, service_id): - return Job.query.filter( + stmt = select(Job).filter( Job.service_id == service_id, Job.id == job_id, Job.job_status == JobStatus.SCHEDULED, Job.scheduled_for > utc_now(), - ).one() + ) + return db.session.execute(stmt).scalars().one() def dao_create_job(job): From e8efde314d0ad7e9557b53789aa227b2c22e25ff Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Wed, 16 Oct 2024 12:05:56 -0700 Subject: [PATCH 39/49] down to line 179 --- app/dao/jobs_dao.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index f0c777081..30b2b3b07 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -119,11 +119,11 @@ def dao_set_scheduled_jobs_to_pending(): select( Job.job_status == JobStatus.SCHEDULED, Job.scheduled_for < utc_now(), - ) + ).select_from(Job) .order_by(asc(Job.scheduled_for)) .with_for_update() ) - jobs = db.session.execute(stmt).all() + jobs = db.session.execute(stmt).scalars().all() for job in jobs: job.job_status = JobStatus.PENDING From 8c7aa30a3ec1b155d68583bab5d32f76f7ee793a Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Wed, 16 Oct 2024 12:22:00 -0700 Subject: [PATCH 40/49] down to line 179 --- app/dao/jobs_dao.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 30b2b3b07..ea64162b2 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -116,10 +116,11 @@ def dao_set_scheduled_jobs_to_pending(): from completing until it commits. """ stmt = ( - select( + select(Job) + .filter( Job.job_status == JobStatus.SCHEDULED, Job.scheduled_for < utc_now(), - ).select_from(Job) + ) .order_by(asc(Job.scheduled_for)) .with_for_update() ) From 5409c2a183dbe1e729e2cdce1098c31cb3686f01 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Wed, 16 Oct 2024 12:32:23 -0700 Subject: [PATCH 41/49] down to line 179 --- tests/app/job/test_rest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/app/job/test_rest.py b/tests/app/job/test_rest.py index 6d4112058..8d40a045a 100644 --- a/tests/app/job/test_rest.py +++ b/tests/app/job/test_rest.py @@ -837,7 +837,7 @@ def test_get_jobs_should_paginate(admin_request, sample_template): assert resp_json["page_size"] == 2 assert resp_json["total"] == 10 assert "links" in resp_json - assert set(resp_json["links"].keys()) == {"next", "last"} + assert set(resp_json["links"].keys()) == {"next", "last", "prev"} def test_get_jobs_accepts_page_parameter(admin_request, sample_template): From 965c5c9b847eef7c4a2876de36e816e362d95409 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 17 Oct 2024 07:36:24 -0700 Subject: [PATCH 42/49] everything except extend --- app/dao/jobs_dao.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index ea64162b2..f3106a821 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -226,7 +226,7 @@ def find_jobs_with_missing_rows(): ten_minutes_ago = utc_now() - timedelta(minutes=20) yesterday = utc_now() - timedelta(days=1) jobs_with_rows_missing = ( - db.session.query(Job) + select(Job) .filter( Job.job_status == JobStatus.FINISHED, Job.processing_finished < ten_minutes_ago, @@ -237,16 +237,16 @@ def find_jobs_with_missing_rows(): .having(func.count(Notification.id) != Job.notification_count) ) - return jobs_with_rows_missing.all() + return db.session.execute(jobs_with_rows_missing).all() def find_missing_row_for_job(job_id, job_size): - expected_row_numbers = db.session.query( + expected_row_numbers = select( func.generate_series(0, job_size - 1).label("row") ).subquery() query = ( - db.session.query( + select( Notification.job_row_number, expected_row_numbers.c.row.label("missing_row") ) .outerjoin( @@ -258,4 +258,4 @@ def find_missing_row_for_job(job_id, job_size): ) .filter(Notification.job_row_number == None) # noqa ) - return query.all() + return db.session.execute(query).all() From f1ecfd5e094e5584e6e538f6ef3ac3f4db3a8094 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 17 Oct 2024 08:06:26 -0700 Subject: [PATCH 43/49] try scalars --- app/dao/jobs_dao.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index f3106a821..bbf8606c5 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -237,7 +237,7 @@ def find_jobs_with_missing_rows(): .having(func.count(Notification.id) != Job.notification_count) ) - return db.session.execute(jobs_with_rows_missing).all() + return db.session.execute(jobs_with_rows_missing).scalar().all() def find_missing_row_for_job(job_id, job_size): @@ -258,4 +258,4 @@ def find_missing_row_for_job(job_id, job_size): ) .filter(Notification.job_row_number == None) # noqa ) - return db.session.execute(query).all() + return db.session.execute(query).scalars().all() From ead2127cca7987552e4f9d7a3665a69929e583ce Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 17 Oct 2024 08:22:34 -0700 Subject: [PATCH 44/49] try scalars --- app/dao/jobs_dao.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index bbf8606c5..f44624736 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -237,7 +237,7 @@ def find_jobs_with_missing_rows(): .having(func.count(Notification.id) != Job.notification_count) ) - return db.session.execute(jobs_with_rows_missing).scalar().all() + return db.session.execute(jobs_with_rows_missing).scalars().all() def find_missing_row_for_job(job_id, job_size): From f13fbf81d6f0f22a4bfaad2e7ae8fc0827712986 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 17 Oct 2024 08:44:37 -0700 Subject: [PATCH 45/49] revert scalrs --- app/dao/jobs_dao.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index f44624736..92b6aa77c 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -258,4 +258,4 @@ def find_missing_row_for_job(job_id, job_size): ) .filter(Notification.job_row_number == None) # noqa ) - return db.session.execute(query).scalars().all() + return db.session.execute(query).all() From 6ea003effde6b9af9c0f0e9b15380de8a9e638a9 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 17 Oct 2024 08:56:15 -0700 Subject: [PATCH 46/49] finish jobs_dao? --- app/dao/jobs_dao.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index 92b6aa77c..e7d79a8f8 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -177,16 +177,17 @@ def dao_update_job(job): def dao_get_jobs_older_than_data_retention(notification_types): - flexible_data_retention = ServiceDataRetention.query.filter( + stmt = select(ServiceDataRetention).filter( ServiceDataRetention.notification_type.in_(notification_types) - ).all() + ) + flexible_data_retention = db.session.execute(stmt).all() jobs = [] today = utc_now().date() for f in flexible_data_retention: end_date = today - timedelta(days=f.days_of_retention) - - jobs.extend( - Job.query.join(Template) + stmt = ( + select(Job) + .join(Template) .filter( func.coalesce(Job.scheduled_for, Job.created_at) < end_date, Job.archived == False, # noqa @@ -194,8 +195,8 @@ def dao_get_jobs_older_than_data_retention(notification_types): Job.service_id == f.service_id, ) .order_by(desc(Job.created_at)) - .all() ) + jobs.extend(db.session.execute(stmt).all()) # notify-api-1287, make default data retention 7 days, 23 hours end_date = today - timedelta(days=7, hours=23) @@ -205,8 +206,9 @@ def dao_get_jobs_older_than_data_retention(notification_types): for x in flexible_data_retention if x.notification_type == notification_type ] - jobs.extend( - Job.query.join(Template) + stmt = ( + select(Job) + .join(Template) .filter( func.coalesce(Job.scheduled_for, Job.created_at) < end_date, Job.archived == False, # noqa @@ -214,8 +216,8 @@ def dao_get_jobs_older_than_data_retention(notification_types): Job.service_id.notin_(services_with_data_retention), ) .order_by(desc(Job.created_at)) - .all() ) + jobs.extend(db.session.execute(stmt).all()) return jobs From a0db2b4610cd3a946bf6fc1ea1c3bb49efc3f392 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 17 Oct 2024 09:05:33 -0700 Subject: [PATCH 47/49] use saclars() for extend --- app/dao/jobs_dao.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index e7d79a8f8..b885b29d0 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -196,7 +196,7 @@ def dao_get_jobs_older_than_data_retention(notification_types): ) .order_by(desc(Job.created_at)) ) - jobs.extend(db.session.execute(stmt).all()) + jobs.extend(db.session.execute(stmt).scalars().all()) # notify-api-1287, make default data retention 7 days, 23 hours end_date = today - timedelta(days=7, hours=23) @@ -217,7 +217,7 @@ def dao_get_jobs_older_than_data_retention(notification_types): ) .order_by(desc(Job.created_at)) ) - jobs.extend(db.session.execute(stmt).all()) + jobs.extend(db.session.execute(stmt).scalars().all()) return jobs From 6c26db6b0334156e46fda2474a66e241a59b957a Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 17 Oct 2024 09:15:21 -0700 Subject: [PATCH 48/49] use saclars() for extend --- app/dao/jobs_dao.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index b885b29d0..ddec26956 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -180,7 +180,7 @@ def dao_get_jobs_older_than_data_retention(notification_types): stmt = select(ServiceDataRetention).filter( ServiceDataRetention.notification_type.in_(notification_types) ) - flexible_data_retention = db.session.execute(stmt).all() + flexible_data_retention = db.session.execute(stmt).scalars().all() jobs = [] today = utc_now().date() for f in flexible_data_retention: From f8f4e46f482760889a9f64757b88c4013c429087 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 17 Oct 2024 09:34:29 -0700 Subject: [PATCH 49/49] fix test_jobs_dao --- tests/app/dao/test_jobs_dao.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/app/dao/test_jobs_dao.py b/tests/app/dao/test_jobs_dao.py index ca98257e5..b499faefa 100644 --- a/tests/app/dao/test_jobs_dao.py +++ b/tests/app/dao/test_jobs_dao.py @@ -4,8 +4,10 @@ import pytest from freezegun import freeze_time +from sqlalchemy import func, select from sqlalchemy.exc import IntegrityError +from app import db from app.dao.jobs_dao import ( dao_create_job, dao_get_future_scheduled_job_by_id_and_service_id, @@ -108,7 +110,8 @@ def test_should_return_notifications_only_for_this_service( def test_create_sample_job(sample_template): - assert Job.query.count() == 0 + stmt = select(func.count()).select_from(Job) + assert db.session.execute(stmt).scalar() == 0 job_id = uuid.uuid4() data = { @@ -123,9 +126,9 @@ def test_create_sample_job(sample_template): job = Job(**data) dao_create_job(job) - - assert Job.query.count() == 1 - job_from_db = Job.query.get(job_id) + stmt = select(func.count()).select_from(Job) + assert db.session.execute(stmt).scalar() == 1 + job_from_db = db.session.get(Job, job_id) assert job == job_from_db assert job_from_db.notifications_delivered == 0 assert job_from_db.notifications_failed == 0 @@ -221,7 +224,7 @@ def test_update_job(sample_job): dao_update_job(sample_job) - job_from_db = Job.query.get(sample_job.id) + job_from_db = db.session.get(Job, sample_job.id) assert job_from_db.job_status == JobStatus.IN_PROGRESS