mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
more
This commit is contained in:
@@ -191,7 +191,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(
|
|||||||
all_stats_alias = aliased(all_stats_union, name="all_stats")
|
all_stats_alias = aliased(all_stats_union, name="all_stats")
|
||||||
|
|
||||||
# Final query with optional template joins
|
# Final query with optional template joins
|
||||||
query = select(
|
querie = select(
|
||||||
*(
|
*(
|
||||||
[
|
[
|
||||||
TemplateFolder.name.label("folder"),
|
TemplateFolder.name.label("folder"),
|
||||||
@@ -214,8 +214,8 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if by_template:
|
if by_template:
|
||||||
query = (
|
querie = (
|
||||||
query.join(Template, all_stats_alias.c.template_id == Template.id)
|
querie.join(Template, all_stats_alias.c.template_id == Template.id)
|
||||||
.join(User, Template.created_by_id == User.id)
|
.join(User, Template.created_by_id == User.id)
|
||||||
.outerjoin(
|
.outerjoin(
|
||||||
template_folder_map, Template.id == template_folder_map.c.template_id
|
template_folder_map, Template.id == template_folder_map.c.template_id
|
||||||
@@ -227,7 +227,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Group by all necessary fields except date_used
|
# Group by all necessary fields except date_used
|
||||||
query = query.group_by(
|
querie = querie.group_by(
|
||||||
*(
|
*(
|
||||||
[
|
[
|
||||||
TemplateFolder.name,
|
TemplateFolder.name,
|
||||||
@@ -245,7 +245,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Execute the query using Flask-SQLAlchemy's session
|
# Execute the query using Flask-SQLAlchemy's session
|
||||||
result = db.session.execute(query)
|
result = db.session.execute(querie)
|
||||||
return result.mappings().all()
|
return result.mappings().all()
|
||||||
|
|
||||||
|
|
||||||
@@ -361,7 +361,7 @@ def fetch_stats_for_all_services_by_date_range(
|
|||||||
|
|
||||||
if start_date <= utc_now().date() <= end_date:
|
if start_date <= utc_now().date() <= end_date:
|
||||||
today = get_midnight_in_utc(utc_now())
|
today = get_midnight_in_utc(utc_now())
|
||||||
subquery = (
|
subquerie = (
|
||||||
select(
|
select(
|
||||||
Notification.notification_type.label("notification_type"),
|
Notification.notification_type.label("notification_type"),
|
||||||
Notification.status.label("status"),
|
Notification.status.label("status"),
|
||||||
@@ -377,8 +377,8 @@ def fetch_stats_for_all_services_by_date_range(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
if not include_from_test_key:
|
if not include_from_test_key:
|
||||||
subquery = subquery.filter(Notification.key_type != KeyType.TEST)
|
subquerie = subquerie.filter(Notification.key_type != KeyType.TEST)
|
||||||
subquery = subquery.subquery()
|
subquerie = subquerie.subquery()
|
||||||
|
|
||||||
stats_for_today = select(
|
stats_for_today = select(
|
||||||
Service.id.label("service_id"),
|
Service.id.label("service_id"),
|
||||||
@@ -386,10 +386,10 @@ def fetch_stats_for_all_services_by_date_range(
|
|||||||
Service.restricted.label("restricted"),
|
Service.restricted.label("restricted"),
|
||||||
Service.active.label("active"),
|
Service.active.label("active"),
|
||||||
Service.created_at.label("created_at"),
|
Service.created_at.label("created_at"),
|
||||||
subquery.c.notification_type.cast(db.Text).label("notification_type"),
|
subquerie.c.notification_type.cast(db.Text).label("notification_type"),
|
||||||
subquery.c.status.cast(db.Text).label("status"),
|
subquerie.c.status.cast(db.Text).label("status"),
|
||||||
subquery.c.count.label("count"),
|
subquerie.c.count.label("count"),
|
||||||
).outerjoin(subquery, subquery.c.service_id == Service.id)
|
).outerjoin(subquerie, subquerie.c.service_id == Service.id)
|
||||||
|
|
||||||
all_stats_table = stats.union_all(stats_for_today).subquery()
|
all_stats_table = stats.union_all(stats_for_today).subquery()
|
||||||
query = (
|
query = (
|
||||||
@@ -515,7 +515,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
|
|||||||
|
|
||||||
|
|
||||||
def get_total_notifications_for_date_range(start_date, end_date):
|
def get_total_notifications_for_date_range(start_date, end_date):
|
||||||
query = (
|
querie = (
|
||||||
select(
|
select(
|
||||||
FactNotificationStatus.local_date.label("local_date"),
|
FactNotificationStatus.local_date.label("local_date"),
|
||||||
func.sum(
|
func.sum(
|
||||||
@@ -546,11 +546,11 @@ def get_total_notifications_for_date_range(start_date, end_date):
|
|||||||
.order_by(FactNotificationStatus.local_date)
|
.order_by(FactNotificationStatus.local_date)
|
||||||
)
|
)
|
||||||
if start_date and end_date:
|
if start_date and end_date:
|
||||||
query = query.filter(
|
querie = querie.filter(
|
||||||
FactNotificationStatus.local_date >= start_date,
|
FactNotificationStatus.local_date >= start_date,
|
||||||
FactNotificationStatus.local_date <= end_date,
|
FactNotificationStatus.local_date <= end_date,
|
||||||
)
|
)
|
||||||
return db.session.execute(query).all()
|
return db.session.execute(querie).all()
|
||||||
|
|
||||||
|
|
||||||
def fetch_monthly_notification_statuses_per_service(start_date, end_date):
|
def fetch_monthly_notification_statuses_per_service(start_date, end_date):
|
||||||
|
|||||||
@@ -514,7 +514,7 @@ def dao_fetch_todays_stats_for_all_services(
|
|||||||
start_date = get_midnight_in_utc(today)
|
start_date = get_midnight_in_utc(today)
|
||||||
end_date = get_midnight_in_utc(today + timedelta(days=1))
|
end_date = get_midnight_in_utc(today + timedelta(days=1))
|
||||||
|
|
||||||
subquery = (
|
subquerie = (
|
||||||
select(
|
select(
|
||||||
Notification.notification_type,
|
Notification.notification_type,
|
||||||
Notification.status,
|
Notification.status,
|
||||||
@@ -530,9 +530,9 @@ def dao_fetch_todays_stats_for_all_services(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if not include_from_test_key:
|
if not include_from_test_key:
|
||||||
subquery = subquery.filter(Notification.key_type != KeyType.TEST)
|
subquerie = subquerie.filter(Notification.key_type != KeyType.TEST)
|
||||||
|
|
||||||
subquery = subquery.subquery()
|
subquerie = subquerie.subquery()
|
||||||
|
|
||||||
stmt = (
|
stmt = (
|
||||||
select(
|
select(
|
||||||
@@ -541,11 +541,11 @@ def dao_fetch_todays_stats_for_all_services(
|
|||||||
Service.restricted,
|
Service.restricted,
|
||||||
Service.active,
|
Service.active,
|
||||||
Service.created_at,
|
Service.created_at,
|
||||||
subquery.c.notification_type,
|
subquerie.c.notification_type,
|
||||||
subquery.c.status,
|
subquerie.c.status,
|
||||||
subquery.c.count,
|
subquerie.c.count,
|
||||||
)
|
)
|
||||||
.outerjoin(subquery, subquery.c.service_id == Service.id)
|
.outerjoin(subquerie, subquerie.c.service_id == Service.id)
|
||||||
.order_by(Service.id)
|
.order_by(Service.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -617,7 +617,7 @@ def dao_find_services_sending_to_tv_numbers(start_date, end_date, threshold=500)
|
|||||||
|
|
||||||
|
|
||||||
def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10000):
|
def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10000):
|
||||||
subquery = (
|
subquerie = (
|
||||||
select(
|
select(
|
||||||
func.count(Notification.id).label("total_count"),
|
func.count(Notification.id).label("total_count"),
|
||||||
Notification.service_id.label("service_id"),
|
Notification.service_id.label("service_id"),
|
||||||
@@ -637,19 +637,19 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
|
|||||||
.having(func.count(Notification.id) >= threshold)
|
.having(func.count(Notification.id) >= threshold)
|
||||||
)
|
)
|
||||||
|
|
||||||
subquery = subquery.subquery()
|
subquerie = subquerie.subquery()
|
||||||
|
|
||||||
stmt = (
|
stmt = (
|
||||||
select(
|
select(
|
||||||
Notification.service_id.label("service_id"),
|
Notification.service_id.label("service_id"),
|
||||||
func.count(Notification.id).label("permanent_failure_count"),
|
func.count(Notification.id).label("permanent_failure_count"),
|
||||||
subquery.c.total_count.label("total_count"),
|
subquerie.c.total_count.label("total_count"),
|
||||||
(
|
(
|
||||||
cast(func.count(Notification.id), Float)
|
cast(func.count(Notification.id), Float)
|
||||||
/ cast(subquery.c.total_count, Float)
|
/ cast(subquerie.c.total_count, Float)
|
||||||
).label("permanent_failure_rate"),
|
).label("permanent_failure_rate"),
|
||||||
)
|
)
|
||||||
.join(subquery, subquery.c.service_id == Notification.service_id)
|
.join(subquerie, subquerie.c.service_id == Notification.service_id)
|
||||||
.filter(
|
.filter(
|
||||||
Notification.service_id == Service.id,
|
Notification.service_id == Service.id,
|
||||||
Notification.created_at >= start_date,
|
Notification.created_at >= start_date,
|
||||||
@@ -660,10 +660,10 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
|
|||||||
Service.restricted == False, # noqa
|
Service.restricted == False, # noqa
|
||||||
Service.active == True, # noqa
|
Service.active == True, # noqa
|
||||||
)
|
)
|
||||||
.group_by(Notification.service_id, subquery.c.total_count)
|
.group_by(Notification.service_id, subquerie.c.total_count)
|
||||||
.having(
|
.having(
|
||||||
cast(func.count(Notification.id), Float)
|
cast(func.count(Notification.id), Float)
|
||||||
/ cast(subquery.c.total_count, Float)
|
/ cast(subquerie.c.total_count, Float)
|
||||||
>= 0.25
|
>= 0.25
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ def dao_get_uploads_by_service_id(service_id, limit_days=None, page=1, page_size
|
|||||||
Notification.created_at >= midnight_n_days_ago(limit_days)
|
Notification.created_at >= midnight_n_days_ago(limit_days)
|
||||||
)
|
)
|
||||||
|
|
||||||
letters_subquery = (
|
letters_subquerie = (
|
||||||
db.session.query(
|
db.session.query(
|
||||||
func.count().label("notification_count"),
|
func.count().label("notification_count"),
|
||||||
_naive_gmt_to_utc(_get_printing_datetime(Notification.created_at)).label(
|
_naive_gmt_to_utc(_get_printing_datetime(Notification.created_at)).label(
|
||||||
@@ -117,18 +117,18 @@ def dao_get_uploads_by_service_id(service_id, limit_days=None, page=1, page_size
|
|||||||
letters_query = db.session.query(
|
letters_query = db.session.query(
|
||||||
literal(None).label("id"),
|
literal(None).label("id"),
|
||||||
literal("Uploaded letters").label("original_file_name"),
|
literal("Uploaded letters").label("original_file_name"),
|
||||||
letters_subquery.c.notification_count.label("notification_count"),
|
letters_subquerie.c.notification_count.label("notification_count"),
|
||||||
literal("letter").label("template_type"),
|
literal("letter").label("template_type"),
|
||||||
literal(None).label("days_of_retention"),
|
literal(None).label("days_of_retention"),
|
||||||
letters_subquery.c.printing_at.label("created_at"),
|
letters_subquerie.c.printing_at.label("created_at"),
|
||||||
literal(None).label("scheduled_for"),
|
literal(None).label("scheduled_for"),
|
||||||
letters_subquery.c.printing_at.label("processing_started"),
|
letters_subquerie.c.printing_at.label("processing_started"),
|
||||||
literal(None).label("status"),
|
literal(None).label("status"),
|
||||||
literal("letter_day").label("upload_type"),
|
literal("letter_day").label("upload_type"),
|
||||||
literal(None).label("recipient"),
|
literal(None).label("recipient"),
|
||||||
).group_by(
|
).group_by(
|
||||||
letters_subquery.c.notification_count,
|
letters_subquerie.c.notification_count,
|
||||||
letters_subquery.c.printing_at,
|
letters_subquerie.c.printing_at,
|
||||||
)
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user