updating pending count

This commit is contained in:
Beverly Nguyen
2025-01-10 10:28:28 -08:00
parent 8656c44757
commit b6337ad807
2 changed files with 28 additions and 23 deletions

View File

@@ -2,7 +2,7 @@ import uuid
from datetime import timedelta
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.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 = (
select(
Job.id,
Job.notification_count,
cast(Job.notification_count, Integer).label("notification_count"), # <-- i added cast here
NotificationAllTimeView.notification_type,
NotificationAllTimeView.status,
func.date_trunc("day", NotificationAllTimeView.created_at).label("day"),
func.count(NotificationAllTimeView.id).label("count"),
)
.join_from(
NotificationAllTimeView,
Job,
cast(func.count(NotificationAllTimeView.id), Integer).label("count"), # <-- i added cast here
)
.join_from(NotificationAllTimeView, Job, NotificationAllTimeView.job_id == Job.id) # <-- i changed this to NotificationAllTimeView from notifications
.where(
NotificationAllTimeView.service_id == service_id,
NotificationAllTimeView.key_type != KeyType.TEST,
@@ -486,21 +483,22 @@ def dao_fetch_stats_for_service_from_days(service_id, start_date, end_date):
stmt = (
select(
func.sum(sub_stmt.notification_count).label("total_notifications"),
sub_stmt.notification_type,
sub_stmt.status,
sub_stmt.day,
func.sum(sub_stmt.count).label("count"),
cast(func.sum(sub_stmt.c.notification_count), Integer).label("total_notifications"), # <-- i added cast here
sub_stmt.c.notification_type,
sub_stmt.c.status,
sub_stmt.c.day,
cast(func.sum(sub_stmt.c.count), Integer).label("count"), # <-- i added cast here
)
.group_by(
sub_stmt.notification_type,
sub_stmt.status,
sub_stmt.day,
.group_by( # <-- i added this group here
sub_stmt.c.notification_type,
sub_stmt.c.status,
sub_stmt.c.day
)
)
return db.session.execute(stmt).all()
def dao_fetch_stats_for_service_from_days_for_user(
service_id, start_date, end_date, user_id
):

View File

@@ -83,11 +83,21 @@ def create_zeroed_stats_dicts():
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:
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):
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.TECHNICAL_FAILURE,
NotificationStatus.TEMPORARY_FAILURE,
@@ -96,13 +106,10 @@ def _update_statuses_from_row(update_dict, row):
NotificationStatus.VIRUS_SCAN_FAILED,
):
update_dict[StatisticsType.FAILURE] += row.count
elif row.status in (
NotificationStatus.PENDING,
NotificationStatus.CREATED,
NotificationStatus.SENDING,
):
update_dict[StatisticsType.PENDING] += row.count
pending_count -= row.count # Subtract from pending_count
# Update pending count directly
update_dict[StatisticsType.PENDING] = pending_count
def create_empty_monthly_notification_status_stats_dict(year):
utc_month_starts = get_months_for_financial_year(year)