|
1 | 1 | import itertools |
2 | 2 | from datetime import datetime, timedelta |
| 3 | +from zoneinfo import ZoneInfo |
3 | 4 |
|
4 | 5 | from flask import Blueprint, current_app, jsonify, request |
5 | 6 | from sqlalchemy import select |
6 | 7 | from sqlalchemy.exc import IntegrityError |
7 | 8 | from sqlalchemy.orm.exc import NoResultFound |
8 | 9 | from werkzeug.datastructures import MultiDict |
9 | 10 |
|
10 | | -from app import db, redis_store |
| 11 | +from app import db |
11 | 12 | from app.aws.s3 import get_personalisation_from_s3, get_phone_number_from_s3 |
12 | 13 | from app.config import QueueNames |
13 | 14 | from app.dao import fact_notification_status_dao, notifications_dao |
|
28 | 29 | fetch_stats_for_all_services_by_date_range, |
29 | 30 | ) |
30 | 31 | from app.dao.inbound_numbers_dao import dao_allocate_number_for_service |
31 | | -from app.dao.notifications_dao import dao_get_notification_count_for_service |
| 32 | +from app.dao.notifications_dao import ( |
| 33 | + dao_get_notification_count_for_service, |
| 34 | + dao_get_notification_count_for_service_message_ratio, |
| 35 | +) |
32 | 36 | from app.dao.organization_dao import dao_get_organization_by_service_id |
33 | 37 | from app.dao.service_data_retention_dao import ( |
34 | 38 | fetch_service_data_retention, |
|
109 | 113 | from app.service.utils import get_guest_list_objects |
110 | 114 | from app.user.users_schema import post_set_permissions_schema |
111 | 115 | from app.utils import get_prev_next_pagination_links, utc_now |
112 | | -from notifications_utils.clients.redis import total_limit_cache_key |
113 | 116 |
|
114 | 117 | service_blueprint = Blueprint("service", __name__) |
115 | 118 |
|
@@ -1145,20 +1148,24 @@ def modify_service_data_retention(service_id, data_retention_id): |
1145 | 1148 | def get_service_message_ratio(): |
1146 | 1149 | service_id = request.args.get("service_id") |
1147 | 1150 |
|
| 1151 | + current_year = datetime.now(tz=ZoneInfo("UTC")).year |
1148 | 1152 | my_service = dao_fetch_service_by_id(service_id) |
1149 | | - |
1150 | | - cache_key = total_limit_cache_key(service_id) |
1151 | | - messages_sent = redis_store.get(cache_key) |
1152 | | - if messages_sent is None: |
1153 | | - messages_sent = 0 |
1154 | | - current_app.logger.warning( |
1155 | | - f"Messages sent was not being tracked for service {service_id}" |
| 1153 | + messages_sent = dao_get_notification_count_for_service_message_ratio( |
| 1154 | + service_id, current_year |
| 1155 | + ) |
| 1156 | + messages_remaining = my_service.total_message_limit - messages_sent |
| 1157 | + |
| 1158 | + if my_service.total_message_limit - messages_sent < 0: |
| 1159 | + raise Exception( |
| 1160 | + f"Math error get_service_message_ratio(), \ |
| 1161 | + total {my_service.total_message_limit} \ |
| 1162 | + messages_sent {messages_sent} remaining {messages_remaining} \ |
| 1163 | + service_id {service_id} current_year {current_year}" |
1156 | 1164 | ) |
1157 | | - else: |
1158 | | - messages_sent = int(messages_sent) |
1159 | 1165 |
|
1160 | 1166 | return { |
1161 | 1167 | "messages_sent": messages_sent, |
| 1168 | + "messages_remaining": messages_remaining, |
1162 | 1169 | "total_message_limit": my_service.total_message_limit, |
1163 | 1170 | }, 200 |
1164 | 1171 |
|
|
0 commit comments