Skip to content

Commit

Permalink
add api for message limit, messages_sent
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Kehl committed Jan 28, 2025
1 parent 481f27d commit 6b318b8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
14 changes: 3 additions & 11 deletions app/notifications/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,20 @@


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"]:
return 0

cache_key = total_limit_cache_key(service.id)
print(hilite(f"CACHE_KEY = {cache_key}"))
service_stats = redis_store.get(cache_key)

# Originally this was a daily limit check. It is now a free-tier limit check.
# TODO is this annual or forever for each service?
# TODO do we need a way to clear this out? How do we determine if it is
# free-tier or paid? What are the limits for paid? Etc.
# TODO
# setting expiration to one year for now on the assume that the free tier
# 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
# For now we are using calendar year
# Switch to using service agreement dates when the Agreement model is ready
if service_stats is None:
service_stats = 0
redis_store.set(cache_key, service_stats, ex=365 * 24 * 60 * 60)
return service_stats
# TODO CHANGE THIS BACK TO SERVICE TOTAL MESSAGE LIMIT
if int(service_stats) >= 5:
# if int(service_stats) >= service.total_message_limit:
current_app.logger.warning(
Expand Down
25 changes: 24 additions & 1 deletion app/service/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.datastructures import MultiDict

from app import db
from app import db, redis_store
from app.aws.s3 import get_personalisation_from_s3, get_phone_number_from_s3
from app.config import QueueNames
from app.dao import fact_notification_status_dao, notifications_dao
Expand Down Expand Up @@ -109,6 +109,7 @@
from app.service.utils import get_guest_list_objects
from app.user.users_schema import post_set_permissions_schema
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__)

Expand Down Expand Up @@ -1120,6 +1121,28 @@ def modify_service_data_retention(service_id, data_retention_id):
return "", 204


@service_blueprint.route("/get-service-message-ratio")
def get_service_message_ratio():
service_id = request.args.get("service_id")

my_service = dao_fetch_service_by_id(service_id)

cache_key = total_limit_cache_key(service_id)
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 {
"messages_sent": messages_sent,
"total_message_limit": my_service.total_message_limit,
}


@service_blueprint.route("/monthly-data-by-service")
def get_monthly_notification_data_by_service():
start_date = request.args.get("start_date")
Expand Down

0 comments on commit 6b318b8

Please sign in to comment.