mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 18:31:13 -05:00
fix message ratio endpoint
This commit is contained in:
@@ -279,6 +279,44 @@ def dao_get_notification_count_for_service(*, service_id):
|
|||||||
return db.session.execute(stmt).scalar()
|
return db.session.execute(stmt).scalar()
|
||||||
|
|
||||||
|
|
||||||
|
def dao_get_notification_count_for_service_message_ratio(service_id, current_year):
|
||||||
|
start_date = datetime(current_year, 1, 1)
|
||||||
|
end_date = datetime(current_year + 1, 1, 1)
|
||||||
|
stmt1 = (
|
||||||
|
select(func.count())
|
||||||
|
.select_from(Notification)
|
||||||
|
.where(
|
||||||
|
Notification.service_id == service_id,
|
||||||
|
Notification.status
|
||||||
|
not in [
|
||||||
|
NotificationStatus.CANCELLED,
|
||||||
|
NotificationStatus.CREATED,
|
||||||
|
NotificationStatus.SENDING,
|
||||||
|
],
|
||||||
|
Notification.created_at >= start_date,
|
||||||
|
Notification.created_at < end_date,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
stmt2 = (
|
||||||
|
select(func.count())
|
||||||
|
.select_from(NotificationHistory)
|
||||||
|
.where(
|
||||||
|
NotificationHistory.service_id == service_id,
|
||||||
|
NotificationHistory.status
|
||||||
|
not in [
|
||||||
|
NotificationStatus.CANCELLED,
|
||||||
|
NotificationStatus.CREATED,
|
||||||
|
NotificationStatus.SENDING,
|
||||||
|
],
|
||||||
|
NotificationHistory.created_at >= start_date,
|
||||||
|
NotificationHistory.created_at < end_date,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
recent_count = db.session.execute(stmt1).scalar_one()
|
||||||
|
old_count = db.session.execute(stmt2).scalar_one()
|
||||||
|
return recent_count + old_count
|
||||||
|
|
||||||
|
|
||||||
def dao_get_failed_notification_count():
|
def dao_get_failed_notification_count():
|
||||||
stmt = select(func.count(Notification.id)).where(
|
stmt = select(func.count(Notification.id)).where(
|
||||||
Notification.status == NotificationStatus.FAILED
|
Notification.status == NotificationStatus.FAILED
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import itertools
|
import itertools
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
from flask import Blueprint, current_app, jsonify, request
|
from flask import Blueprint, current_app, jsonify, request
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
@@ -7,7 +8,7 @@ from sqlalchemy.exc import IntegrityError
|
|||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
from werkzeug.datastructures import MultiDict
|
from werkzeug.datastructures import MultiDict
|
||||||
|
|
||||||
from app import db, redis_store
|
from app import db
|
||||||
from app.aws.s3 import get_personalisation_from_s3, get_phone_number_from_s3
|
from app.aws.s3 import get_personalisation_from_s3, get_phone_number_from_s3
|
||||||
from app.config import QueueNames
|
from app.config import QueueNames
|
||||||
from app.dao import fact_notification_status_dao, notifications_dao
|
from app.dao import fact_notification_status_dao, notifications_dao
|
||||||
@@ -28,7 +29,10 @@ from app.dao.fact_notification_status_dao import (
|
|||||||
fetch_stats_for_all_services_by_date_range,
|
fetch_stats_for_all_services_by_date_range,
|
||||||
)
|
)
|
||||||
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
|
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
|
||||||
from app.dao.notifications_dao import dao_get_notification_count_for_service
|
from app.dao.notifications_dao import (
|
||||||
|
dao_get_notification_count_for_service,
|
||||||
|
dao_get_notification_count_for_service_message_ratio,
|
||||||
|
)
|
||||||
from app.dao.organization_dao import dao_get_organization_by_service_id
|
from app.dao.organization_dao import dao_get_organization_by_service_id
|
||||||
from app.dao.service_data_retention_dao import (
|
from app.dao.service_data_retention_dao import (
|
||||||
fetch_service_data_retention,
|
fetch_service_data_retention,
|
||||||
@@ -109,7 +113,6 @@ from app.service.service_senders_schema import (
|
|||||||
from app.service.utils import get_guest_list_objects
|
from app.service.utils import get_guest_list_objects
|
||||||
from app.user.users_schema import post_set_permissions_schema
|
from app.user.users_schema import post_set_permissions_schema
|
||||||
from app.utils import get_prev_next_pagination_links, utc_now
|
from app.utils import get_prev_next_pagination_links, utc_now
|
||||||
from notifications_utils.clients.redis import total_limit_cache_key
|
|
||||||
|
|
||||||
service_blueprint = Blueprint("service", __name__)
|
service_blueprint = Blueprint("service", __name__)
|
||||||
|
|
||||||
@@ -1145,17 +1148,11 @@ def modify_service_data_retention(service_id, data_retention_id):
|
|||||||
def get_service_message_ratio():
|
def get_service_message_ratio():
|
||||||
service_id = request.args.get("service_id")
|
service_id = request.args.get("service_id")
|
||||||
|
|
||||||
|
current_year = datetime.datetime.now(tzinfo=ZoneInfo("UTC")).year
|
||||||
my_service = dao_fetch_service_by_id(service_id)
|
my_service = dao_fetch_service_by_id(service_id)
|
||||||
|
messages_sent = dao_get_notification_count_for_service_message_ratio(
|
||||||
cache_key = total_limit_cache_key(service_id)
|
service_id, current_year
|
||||||
messages_sent = redis_store.get(cache_key)
|
|
||||||
if messages_sent is None:
|
|
||||||
messages_sent = 0
|
|
||||||
current_app.logger.warning(
|
|
||||||
f"Messages sent was not being tracked for service {service_id}"
|
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
messages_sent = int(messages_sent)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"messages_sent": messages_sent,
|
"messages_sent": messages_sent,
|
||||||
|
|||||||
Reference in New Issue
Block a user