Removed/replaced retention redis count with notification count from db call

This commit is contained in:
Andrew Shumway
2023-09-21 13:26:27 -06:00
committed by Carlo Costino
parent 246e23f193
commit a346a734fc
7 changed files with 134 additions and 141 deletions

View File

@@ -190,6 +190,10 @@ 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):
return Notification.query.filter_by(service_id=service.id).count()
def get_notification_with_personalisation(service_id, notification_id, key_type):
filter_dict = {"service_id": service_id, "id": notification_id}
if key_type:

View File

@@ -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

View File

@@ -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=service)
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(

View File

@@ -1,8 +1,7 @@
from collections import defaultdict
from datetime import datetime
from app.dao.date_util import get_months_for_financial_year
from app.models import NOTIFICATION_STATUS_TYPES, NOTIFICATION_TYPES
from app.models import NOTIFICATION_TYPES
def format_statistics(statistics):
@@ -55,26 +54,6 @@ def create_stats_dict():
return stats_dict
def format_monthly_template_notification_stats(year, rows):
stats = {
datetime.strftime(date, "%Y-%m"): {}
for date in [datetime(year, month, 1) for month in range(4, 13)]
+ [datetime(year + 1, month, 1) for month in range(1, 4)]
}
for row in rows:
formatted_month = row.month.strftime("%Y-%m")
if str(row.template_id) not in stats[formatted_month]:
stats[formatted_month][str(row.template_id)] = {
"name": row.name,
"type": row.template_type,
"counts": dict.fromkeys(NOTIFICATION_STATUS_TYPES, 0),
}
stats[formatted_month][str(row.template_id)]["counts"][row.status] += row.count
return stats
def create_zeroed_stats_dicts():
return {
template_type: {status: 0 for status in ("requested", "delivered", "failed")}