mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-09 04:33:13 -04:00
updating pending count
This commit is contained in:
@@ -2,7 +2,7 @@ import uuid
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from sqlalchemy import Float, cast, delete, select
|
from sqlalchemy import Float, cast, delete, select, Integer
|
||||||
from sqlalchemy.orm import joinedload
|
from sqlalchemy.orm import joinedload
|
||||||
from sqlalchemy.sql.expression import and_, asc, case, func
|
from sqlalchemy.sql.expression import and_, asc, case, func
|
||||||
|
|
||||||
@@ -458,16 +458,13 @@ def dao_fetch_stats_for_service_from_days(service_id, start_date, end_date):
|
|||||||
sub_stmt = (
|
sub_stmt = (
|
||||||
select(
|
select(
|
||||||
Job.id,
|
Job.id,
|
||||||
Job.notification_count,
|
cast(Job.notification_count, Integer).label("notification_count"), # <-- i added cast here
|
||||||
NotificationAllTimeView.notification_type,
|
NotificationAllTimeView.notification_type,
|
||||||
NotificationAllTimeView.status,
|
NotificationAllTimeView.status,
|
||||||
func.date_trunc("day", NotificationAllTimeView.created_at).label("day"),
|
func.date_trunc("day", NotificationAllTimeView.created_at).label("day"),
|
||||||
func.count(NotificationAllTimeView.id).label("count"),
|
cast(func.count(NotificationAllTimeView.id), Integer).label("count"), # <-- i added cast here
|
||||||
)
|
|
||||||
.join_from(
|
|
||||||
NotificationAllTimeView,
|
|
||||||
Job,
|
|
||||||
)
|
)
|
||||||
|
.join_from(NotificationAllTimeView, Job, NotificationAllTimeView.job_id == Job.id) # <-- i changed this to NotificationAllTimeView from notifications
|
||||||
.where(
|
.where(
|
||||||
NotificationAllTimeView.service_id == service_id,
|
NotificationAllTimeView.service_id == service_id,
|
||||||
NotificationAllTimeView.key_type != KeyType.TEST,
|
NotificationAllTimeView.key_type != KeyType.TEST,
|
||||||
@@ -486,21 +483,22 @@ def dao_fetch_stats_for_service_from_days(service_id, start_date, end_date):
|
|||||||
|
|
||||||
stmt = (
|
stmt = (
|
||||||
select(
|
select(
|
||||||
func.sum(sub_stmt.notification_count).label("total_notifications"),
|
cast(func.sum(sub_stmt.c.notification_count), Integer).label("total_notifications"), # <-- i added cast here
|
||||||
sub_stmt.notification_type,
|
sub_stmt.c.notification_type,
|
||||||
sub_stmt.status,
|
sub_stmt.c.status,
|
||||||
sub_stmt.day,
|
sub_stmt.c.day,
|
||||||
func.sum(sub_stmt.count).label("count"),
|
cast(func.sum(sub_stmt.c.count), Integer).label("count"), # <-- i added cast here
|
||||||
)
|
)
|
||||||
.group_by(
|
.group_by( # <-- i added this group here
|
||||||
sub_stmt.notification_type,
|
sub_stmt.c.notification_type,
|
||||||
sub_stmt.status,
|
sub_stmt.c.status,
|
||||||
sub_stmt.day,
|
sub_stmt.c.day
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return db.session.execute(stmt).all()
|
return db.session.execute(stmt).all()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def dao_fetch_stats_for_service_from_days_for_user(
|
def dao_fetch_stats_for_service_from_days_for_user(
|
||||||
service_id, start_date, end_date, user_id
|
service_id, start_date, end_date, user_id
|
||||||
):
|
):
|
||||||
|
|||||||
@@ -83,11 +83,21 @@ def create_zeroed_stats_dicts():
|
|||||||
|
|
||||||
|
|
||||||
def _update_statuses_from_row(update_dict, row):
|
def _update_statuses_from_row(update_dict, row):
|
||||||
|
# Initialize pending_count to total_notifications
|
||||||
|
pending_count = row.total_notifications
|
||||||
|
|
||||||
|
# Update requested count
|
||||||
if row.status != NotificationStatus.CANCELLED:
|
if row.status != NotificationStatus.CANCELLED:
|
||||||
update_dict[StatisticsType.REQUESTED] += row.count
|
update_dict[StatisticsType.REQUESTED] += row.count
|
||||||
|
pending_count -= row.count # Subtract from pending_count
|
||||||
|
|
||||||
|
# Update delivered count
|
||||||
if row.status in (NotificationStatus.DELIVERED, NotificationStatus.SENT):
|
if row.status in (NotificationStatus.DELIVERED, NotificationStatus.SENT):
|
||||||
update_dict[StatisticsType.DELIVERED] += row.count
|
update_dict[StatisticsType.DELIVERED] += row.count
|
||||||
elif row.status in (
|
pending_count -= row.count # Subtract from pending_count
|
||||||
|
|
||||||
|
# Update failure count
|
||||||
|
if row.status in (
|
||||||
NotificationStatus.FAILED,
|
NotificationStatus.FAILED,
|
||||||
NotificationStatus.TECHNICAL_FAILURE,
|
NotificationStatus.TECHNICAL_FAILURE,
|
||||||
NotificationStatus.TEMPORARY_FAILURE,
|
NotificationStatus.TEMPORARY_FAILURE,
|
||||||
@@ -96,13 +106,10 @@ def _update_statuses_from_row(update_dict, row):
|
|||||||
NotificationStatus.VIRUS_SCAN_FAILED,
|
NotificationStatus.VIRUS_SCAN_FAILED,
|
||||||
):
|
):
|
||||||
update_dict[StatisticsType.FAILURE] += row.count
|
update_dict[StatisticsType.FAILURE] += row.count
|
||||||
elif row.status in (
|
pending_count -= row.count # Subtract from pending_count
|
||||||
NotificationStatus.PENDING,
|
|
||||||
NotificationStatus.CREATED,
|
|
||||||
NotificationStatus.SENDING,
|
|
||||||
):
|
|
||||||
update_dict[StatisticsType.PENDING] += row.count
|
|
||||||
|
|
||||||
|
# Update pending count directly
|
||||||
|
update_dict[StatisticsType.PENDING] = pending_count
|
||||||
|
|
||||||
def create_empty_monthly_notification_status_stats_dict(year):
|
def create_empty_monthly_notification_status_stats_dict(year):
|
||||||
utc_month_starts = get_months_for_financial_year(year)
|
utc_month_starts = get_months_for_financial_year(year)
|
||||||
|
|||||||
Reference in New Issue
Block a user