mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-09 06:32:11 -05:00
fix total message limit so it works
This commit is contained in:
@@ -14,6 +14,7 @@ from app.dao.notifications_dao import update_notification_status_by_id
|
|||||||
from app.delivery import send_to_providers
|
from app.delivery import send_to_providers
|
||||||
from app.enums import NotificationStatus
|
from app.enums import NotificationStatus
|
||||||
from app.exceptions import NotificationTechnicalFailureException
|
from app.exceptions import NotificationTechnicalFailureException
|
||||||
|
from notifications_utils.clients.redis import total_limit_cache_key
|
||||||
|
|
||||||
|
|
||||||
@notify_celery.task(
|
@notify_celery.task(
|
||||||
@@ -41,6 +42,9 @@ def deliver_sms(self, notification_id):
|
|||||||
# Code branches off to send_to_providers.py
|
# Code branches off to send_to_providers.py
|
||||||
send_to_providers.send_sms_to_provider(notification)
|
send_to_providers.send_sms_to_provider(notification)
|
||||||
|
|
||||||
|
cache_key = total_limit_cache_key(notification.service_id)
|
||||||
|
redis_store.incr(cache_key)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
update_notification_status_by_id(
|
update_notification_status_by_id(
|
||||||
notification_id,
|
notification_id,
|
||||||
|
|||||||
@@ -166,7 +166,7 @@ def process_row(row, template, job, service, sender_id=None):
|
|||||||
# Assuming the limit is annual, is it calendar year, fiscal year, MOU year?
|
# Assuming the limit is annual, is it calendar year, fiscal year, MOU year?
|
||||||
# Do we need a command to run to clear the redis value, or should it happen automatically?
|
# Do we need a command to run to clear the redis value, or should it happen automatically?
|
||||||
def __total_sending_limits_for_job_exceeded(service, job, job_id):
|
def __total_sending_limits_for_job_exceeded(service, job, job_id):
|
||||||
|
print(hilite("ENTER __total_sending_limits_for_job_exceeded"))
|
||||||
try:
|
try:
|
||||||
total_sent = check_service_over_total_message_limit(KeyType.NORMAL, service)
|
total_sent = check_service_over_total_message_limit(KeyType.NORMAL, service)
|
||||||
if total_sent + job.notification_count > service.total_message_limit:
|
if total_sent + job.notification_count > service.total_message_limit:
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ from app.enums import BrandType, KeyType, NotificationStatus, NotificationType
|
|||||||
from app.exceptions import NotificationTechnicalFailureException
|
from app.exceptions import NotificationTechnicalFailureException
|
||||||
from app.serialised_models import SerialisedService, SerialisedTemplate
|
from app.serialised_models import SerialisedService, SerialisedTemplate
|
||||||
from app.utils import hilite, utc_now
|
from app.utils import hilite, utc_now
|
||||||
|
from notifications_utils.clients.redis import total_limit_cache_key
|
||||||
from notifications_utils.template import (
|
from notifications_utils.template import (
|
||||||
HTMLEmailTemplate,
|
HTMLEmailTemplate,
|
||||||
PlainTextEmailTemplate,
|
PlainTextEmailTemplate,
|
||||||
@@ -118,8 +119,10 @@ def send_sms_to_provider(notification):
|
|||||||
}
|
}
|
||||||
db.session.close() # no commit needed as no changes to objects have been made above
|
db.session.close() # no commit needed as no changes to objects have been made above
|
||||||
|
|
||||||
|
|
||||||
message_id = provider.send_sms(**send_sms_kwargs)
|
message_id = provider.send_sms(**send_sms_kwargs)
|
||||||
current_app.logger.info(f"got message_id {message_id}")
|
|
||||||
|
|
||||||
update_notification_message_id(notification.id, message_id)
|
update_notification_message_id(notification.id, message_id)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
n = notification
|
n = notification
|
||||||
@@ -132,10 +135,14 @@ def send_sms_to_provider(notification):
|
|||||||
else:
|
else:
|
||||||
# Here we map the job_id and row number to the aws message_id
|
# Here we map the job_id and row number to the aws message_id
|
||||||
n = notification
|
n = notification
|
||||||
msg = f"Send to aws for job_id {n.job_id} row_number {n.job_row_number} message_id {message_id}"
|
msg = f"Send to AWS!!! for job_id {n.job_id} row_number {n.job_row_number} message_id {message_id}"
|
||||||
current_app.logger.info(hilite(msg))
|
current_app.logger.info(hilite(msg))
|
||||||
notification.billable_units = template.fragment_count
|
notification.billable_units = template.fragment_count
|
||||||
|
current_app.logger.info("GOING TO UPDATE NOTI TO SENDING")
|
||||||
update_notification_to_sending(notification, provider)
|
update_notification_to_sending(notification, provider)
|
||||||
|
|
||||||
|
cache_key = total_limit_cache_key(service.id)
|
||||||
|
redis_store.incr(cache_key)
|
||||||
return message_id
|
return message_id
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from app.models import ServicePermission
|
|||||||
from app.notifications.process_notifications import create_content_for_notification
|
from app.notifications.process_notifications import create_content_for_notification
|
||||||
from app.serialised_models import SerialisedTemplate
|
from app.serialised_models import SerialisedTemplate
|
||||||
from app.service.utils import service_allowed_to_send_to
|
from app.service.utils import service_allowed_to_send_to
|
||||||
from app.utils import get_public_notify_type_text
|
from app.utils import get_public_notify_type_text, hilite
|
||||||
from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
||||||
from notifications_utils.clients.redis import (
|
from notifications_utils.clients.redis import (
|
||||||
rate_limit_cache_key,
|
rate_limit_cache_key,
|
||||||
@@ -24,26 +24,13 @@ from notifications_utils.recipients import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def check_service_over_api_rate_limit(service, api_key):
|
|
||||||
if (
|
|
||||||
current_app.config["API_RATE_LIMIT_ENABLED"]
|
|
||||||
and current_app.config["REDIS_ENABLED"]
|
|
||||||
):
|
|
||||||
cache_key = rate_limit_cache_key(service.id, api_key.key_type)
|
|
||||||
rate_limit = service.rate_limit
|
|
||||||
interval = 60
|
|
||||||
if redis_store.exceeded_rate_limit(cache_key, rate_limit, interval):
|
|
||||||
current_app.logger.info(
|
|
||||||
"service {} has been rate limited for throughput".format(service.id)
|
|
||||||
)
|
|
||||||
raise RateLimitError(rate_limit, interval, api_key.key_type)
|
|
||||||
|
|
||||||
|
|
||||||
def check_service_over_total_message_limit(key_type, service):
|
def check_service_over_total_message_limit(key_type, service):
|
||||||
|
print(hilite("ENTER check_service_over_total_message_limit"))
|
||||||
if key_type == KeyType.TEST or not current_app.config["REDIS_ENABLED"]:
|
if key_type == KeyType.TEST or not current_app.config["REDIS_ENABLED"]:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
cache_key = total_limit_cache_key(service.id)
|
cache_key = total_limit_cache_key(service.id)
|
||||||
|
print(hilite(f"CACHE_KEY = {cache_key}"))
|
||||||
service_stats = redis_store.get(cache_key)
|
service_stats = redis_store.get(cache_key)
|
||||||
|
|
||||||
# Originally this was a daily limit check. It is now a free-tier limit check.
|
# Originally this was a daily limit check. It is now a free-tier limit check.
|
||||||
@@ -53,17 +40,23 @@ def check_service_over_total_message_limit(key_type, service):
|
|||||||
# TODO
|
# TODO
|
||||||
# setting expiration to one year for now on the assume that the free tier
|
# setting expiration to one year for now on the assume that the free tier
|
||||||
# limit resets annually.
|
# limit resets annually.
|
||||||
|
|
||||||
|
# add column for actual charges to notifications and notifification_history table
|
||||||
|
# add service api to return total_message_limit and actual number of messages for service
|
||||||
if service_stats is None:
|
if service_stats is None:
|
||||||
service_stats = 0
|
service_stats = 0
|
||||||
redis_store.set(cache_key, service_stats, ex=365*24*60*60)
|
redis_store.set(cache_key, service_stats, ex=365*24*60*60)
|
||||||
return service_stats
|
return service_stats
|
||||||
if int(service_stats) >= service.total_message_limit:
|
if int(service_stats) >= 5:
|
||||||
|
#if int(service_stats) >= service.total_message_limit:
|
||||||
current_app.logger.warning(
|
current_app.logger.warning(
|
||||||
"service {} has been rate limited for total use sent {} limit {}".format(
|
"service {} has been rate limited for total use sent {} limit {}".format(
|
||||||
service.id, int(service_stats), service.total_message_limit
|
service.id, int(service_stats), service.total_message_limit
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
raise TotalRequestsError(service.total_message_limit)
|
raise TotalRequestsError(service.total_message_limit)
|
||||||
|
else:
|
||||||
|
print(hilite(f"TOTAL MESSAGE LIMIT {service.total_message_limit} CURRENT {service_stats}"))
|
||||||
return int(service_stats)
|
return int(service_stats)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,12 +14,10 @@ revision = "0414_change_total_message_limit"
|
|||||||
|
|
||||||
|
|
||||||
def upgrade():
|
def upgrade():
|
||||||
"""
|
# TODO This needs updating when the agreement model is ready. We only want free tier at 100k
|
||||||
This limit is only used
|
op.execute("UPDATE services set total_message_limit=100000 where total_message_limit=250000")
|
||||||
"""
|
|
||||||
op.execute("UPDATE services set total_message_limit=100000")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
op.execute("UPDATE services set total_message_limit=250000")
|
op.execute("UPDATE services set total_message_limit=250000 where total_message_limit=100000")
|
||||||
|
|||||||
Reference in New Issue
Block a user