mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-16 20:48:37 -04:00
merge from main
This commit is contained in:
@@ -60,7 +60,7 @@ def get_current_calendar_year_start_year():
|
||||
|
||||
|
||||
def get_calendar_year_for_datetime(start_date):
|
||||
if type(start_date) == date:
|
||||
if isinstance(start_date, date):
|
||||
start_date = datetime.combine(start_date, time.min)
|
||||
|
||||
year = int(start_date.strftime("%Y"))
|
||||
|
||||
@@ -197,6 +197,11 @@ def dao_get_notification_count_for_job_id(*, job_id):
|
||||
return Notification.query.filter_by(job_id=job_id).count()
|
||||
|
||||
|
||||
def dao_get_notification_count_for_service(*, service_id):
|
||||
notification_count = Notification.query.filter_by(service_id=service_id).count()
|
||||
return notification_count
|
||||
|
||||
|
||||
def get_notification_with_personalisation(service_id, notification_id, key_type):
|
||||
filter_dict = {"service_id": service_id, "id": notification_id}
|
||||
if key_type:
|
||||
|
||||
@@ -2,7 +2,6 @@ import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from flask import current_app
|
||||
from notifications_utils.clients import redis
|
||||
from notifications_utils.recipients import (
|
||||
format_email_address,
|
||||
get_international_phone_info,
|
||||
@@ -10,7 +9,6 @@ from notifications_utils.recipients import (
|
||||
)
|
||||
from notifications_utils.template import PlainTextEmailTemplate, SMSMessageTemplate
|
||||
|
||||
from app import redis_store
|
||||
from app.celery import provider_tasks
|
||||
from app.config import QueueNames
|
||||
from app.dao.notifications_dao import (
|
||||
@@ -141,17 +139,7 @@ def persist_notification(
|
||||
service.id
|
||||
)
|
||||
)
|
||||
total_key = redis.daily_total_cache_key()
|
||||
if redis_store.get(total_key) is None:
|
||||
current_app.logger.info("Redis daily total cache key does not exist")
|
||||
redis_store.set(total_key, 1, ex=86400)
|
||||
current_app.logger.info("Set redis daily total cache key to 1")
|
||||
else:
|
||||
current_app.logger.info("Redis total limit cache key does exist")
|
||||
redis_store.incr(total_key)
|
||||
current_app.logger.info(
|
||||
f"Redis total limit cache key has been incremented to {redis_store.get(total_key)}"
|
||||
)
|
||||
|
||||
current_app.logger.info(
|
||||
"{} {} created at {}".format(
|
||||
notification_type, notification_id, notification_created_at
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from flask import current_app
|
||||
from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
||||
from notifications_utils.clients.redis import (
|
||||
daily_total_cache_key,
|
||||
rate_limit_cache_key,
|
||||
total_limit_cache_key,
|
||||
)
|
||||
@@ -13,6 +12,7 @@ from notifications_utils.recipients import (
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app import redis_store
|
||||
from app.dao.notifications_dao import dao_get_notification_count_for_service
|
||||
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
|
||||
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
|
||||
from app.models import (
|
||||
@@ -69,15 +69,10 @@ def check_service_over_total_message_limit(key_type, service):
|
||||
def check_application_over_retention_limit(key_type, service):
|
||||
if key_type == KEY_TYPE_TEST or not current_app.config["REDIS_ENABLED"]:
|
||||
return 0
|
||||
total_stats = dao_get_notification_count_for_service(service_id=service.id)
|
||||
|
||||
cache_key = daily_total_cache_key()
|
||||
daily_message_limit = current_app.config["DAILY_MESSAGE_LIMIT"]
|
||||
total_stats = redis_store.get(cache_key)
|
||||
if total_stats is None:
|
||||
# first message of the day, set the cache to 0 and the expiry to 24 hours
|
||||
total_stats = 0
|
||||
redis_store.set(cache_key, total_stats, ex=86400)
|
||||
return total_stats
|
||||
|
||||
if int(total_stats) >= daily_message_limit:
|
||||
current_app.logger.info(
|
||||
"while sending for service {}, daily message limit of {} reached".format(
|
||||
|
||||
@@ -25,6 +25,7 @@ from app.dao.fact_notification_status_dao import (
|
||||
fetch_stats_for_all_services_by_date_range,
|
||||
)
|
||||
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.organization_dao import dao_get_organization_by_service_id
|
||||
from app.dao.service_data_retention_dao import (
|
||||
fetch_service_data_retention,
|
||||
@@ -1037,3 +1038,9 @@ def check_if_reply_to_address_already_in_use(service_id, email_address):
|
||||
),
|
||||
status_code=409,
|
||||
)
|
||||
|
||||
|
||||
@service_blueprint.route("/<uuid:service_id>/notification-count", methods=["GET"])
|
||||
def get_notification_count_for_service_id(service_id):
|
||||
count = dao_get_notification_count_for_service(service_id=service_id)
|
||||
return jsonify(count=count), 200
|
||||
|
||||
Reference in New Issue
Block a user