mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 07:21:13 -05:00
@@ -8,10 +8,8 @@ from sqlalchemy.sql.expression import case, literal
|
||||
from app import db
|
||||
from app.dao.date_util import get_calendar_year_dates, get_calendar_year_for_datetime
|
||||
from app.dao.organization_dao import dao_get_organization_live_services
|
||||
from app.enums import NotificationStatus, NotificationType
|
||||
from app.enums import NotificationStatus, NotificationType, KeyType
|
||||
from app.models import (
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEAM,
|
||||
AnnualBilling,
|
||||
FactBilling,
|
||||
NotificationAllTimeView,
|
||||
@@ -411,7 +409,7 @@ def _query_for_billing_data(notification_type, start_date, end_date, service):
|
||||
)
|
||||
.filter(
|
||||
NotificationAllTimeView.status.in_(NotificationStatus.sent_emails),
|
||||
NotificationAllTimeView.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
|
||||
NotificationAllTimeView.key_type.in_((KeyType.NORMAL, KeyType.TEAM)),
|
||||
NotificationAllTimeView.created_at >= start_date,
|
||||
NotificationAllTimeView.created_at < end_date,
|
||||
NotificationAllTimeView.notification_type == notification_type,
|
||||
@@ -443,7 +441,7 @@ def _query_for_billing_data(notification_type, start_date, end_date, service):
|
||||
)
|
||||
.filter(
|
||||
NotificationAllTimeView.status.in_(NotificationStatus.billable_sms),
|
||||
NotificationAllTimeView.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
|
||||
NotificationAllTimeView.key_type.in_((KeyType.NORMAL, KeyType.TEAM)),
|
||||
NotificationAllTimeView.created_at >= start_date,
|
||||
NotificationAllTimeView.created_at < end_date,
|
||||
NotificationAllTimeView.notification_type == notification_type,
|
||||
|
||||
@@ -7,11 +7,8 @@ from sqlalchemy.types import DateTime, Integer
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import autocommit
|
||||
from app.enums import NotificationType, NotificationStatus
|
||||
from app.enums import NotificationType, NotificationStatus, KeyType
|
||||
from app.models import (
|
||||
KEY_TYPE_NORMAL,
|
||||
KEY_TYPE_TEAM,
|
||||
KEY_TYPE_TEST,
|
||||
FactNotificationStatus,
|
||||
Notification,
|
||||
NotificationAllTimeView,
|
||||
@@ -55,7 +52,7 @@ def update_fact_notification_status(process_day, notification_type, service_id):
|
||||
NotificationAllTimeView.created_at < end_date,
|
||||
NotificationAllTimeView.notification_type == notification_type,
|
||||
NotificationAllTimeView.service_id == service_id,
|
||||
NotificationAllTimeView.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
|
||||
NotificationAllTimeView.key_type.in_((KeyType.NORMAL, KeyType.TEAM)),
|
||||
)
|
||||
.group_by(
|
||||
NotificationAllTimeView.template_id,
|
||||
@@ -95,7 +92,7 @@ def fetch_notification_status_for_service_by_month(start_date, end_date, service
|
||||
FactNotificationStatus.service_id == service_id,
|
||||
FactNotificationStatus.local_date >= start_date,
|
||||
FactNotificationStatus.local_date < end_date,
|
||||
FactNotificationStatus.key_type != KEY_TYPE_TEST,
|
||||
FactNotificationStatus.key_type != KeyType.TEST,
|
||||
)
|
||||
.group_by(
|
||||
func.date_trunc("month", FactNotificationStatus.local_date).label("month"),
|
||||
@@ -120,7 +117,7 @@ def fetch_notification_status_for_service_for_day(fetch_day, service_id):
|
||||
Notification.created_at
|
||||
< get_midnight_in_utc(fetch_day + timedelta(days=1)),
|
||||
Notification.service_id == service_id,
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Notification.key_type != KeyType.TEST,
|
||||
)
|
||||
.group_by(Notification.notification_type, Notification.status)
|
||||
.all()
|
||||
@@ -144,7 +141,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(
|
||||
).filter(
|
||||
FactNotificationStatus.service_id == service_id,
|
||||
FactNotificationStatus.local_date >= start_date,
|
||||
FactNotificationStatus.key_type != KEY_TYPE_TEST,
|
||||
FactNotificationStatus.key_type != KeyType.TEST,
|
||||
)
|
||||
|
||||
stats_for_today = (
|
||||
@@ -157,7 +154,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(
|
||||
.filter(
|
||||
Notification.created_at >= get_midnight_in_utc(now),
|
||||
Notification.service_id == service_id,
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Notification.key_type != KeyType.TEST,
|
||||
)
|
||||
.group_by(
|
||||
Notification.notification_type,
|
||||
@@ -294,7 +291,7 @@ def fetch_stats_for_all_services_by_date_range(
|
||||
)
|
||||
)
|
||||
if not include_from_test_key:
|
||||
stats = stats.filter(FactNotificationStatus.key_type != KEY_TYPE_TEST)
|
||||
stats = stats.filter(FactNotificationStatus.key_type != KeyType.TEST)
|
||||
|
||||
if start_date <= datetime.utcnow().date() <= end_date:
|
||||
today = get_midnight_in_utc(datetime.utcnow())
|
||||
@@ -313,7 +310,7 @@ def fetch_stats_for_all_services_by_date_range(
|
||||
)
|
||||
)
|
||||
if not include_from_test_key:
|
||||
subquery = subquery.filter(Notification.key_type != KEY_TYPE_TEST)
|
||||
subquery = subquery.filter(Notification.key_type != KeyType.TEST)
|
||||
subquery = subquery.subquery()
|
||||
|
||||
stats_for_today = db.session.query(
|
||||
@@ -375,7 +372,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
|
||||
FactNotificationStatus.service_id == service_id,
|
||||
FactNotificationStatus.local_date >= start_date,
|
||||
FactNotificationStatus.local_date <= end_date,
|
||||
FactNotificationStatus.key_type != KEY_TYPE_TEST,
|
||||
FactNotificationStatus.key_type != KeyType.TEST,
|
||||
FactNotificationStatus.notification_status != NotificationStatus.CANCELLED,
|
||||
)
|
||||
.group_by(
|
||||
@@ -412,7 +409,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
|
||||
.filter(
|
||||
Notification.created_at >= today,
|
||||
Notification.service_id == service_id,
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Notification.key_type != KeyType.TEST,
|
||||
Notification.status != NotificationStatus.CANCELLED,
|
||||
)
|
||||
.group_by(
|
||||
@@ -480,7 +477,7 @@ def get_total_notifications_for_date_range(start_date, end_date):
|
||||
).label("sms"),
|
||||
)
|
||||
.filter(
|
||||
FactNotificationStatus.key_type != KEY_TYPE_TEST,
|
||||
FactNotificationStatus.key_type != KeyType.TEST,
|
||||
)
|
||||
.group_by(FactNotificationStatus.local_date)
|
||||
.order_by(FactNotificationStatus.local_date)
|
||||
@@ -581,7 +578,7 @@ def fetch_monthly_notification_statuses_per_service(start_date, end_date):
|
||||
.filter(
|
||||
FactNotificationStatus.notification_status != NotificationStatus.CREATED,
|
||||
Service.active.is_(True),
|
||||
FactNotificationStatus.key_type != KEY_TYPE_TEST,
|
||||
FactNotificationStatus.key_type != KeyType.TEST,
|
||||
Service.restricted.is_(False),
|
||||
FactNotificationStatus.local_date >= start_date,
|
||||
FactNotificationStatus.local_date <= end_date,
|
||||
|
||||
@@ -16,9 +16,8 @@ from werkzeug.datastructures import MultiDict
|
||||
|
||||
from app import create_uuid, db
|
||||
from app.dao.dao_utils import autocommit
|
||||
from app.enums import NotificationType, NotificationStatus
|
||||
from app.enums import NotificationType, NotificationStatus, KeyType
|
||||
from app.models import (
|
||||
KEY_TYPE_TEST,
|
||||
FactNotificationStatus,
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
@@ -36,7 +35,7 @@ def dao_get_last_date_template_was_used(template_id, service_id):
|
||||
.filter(
|
||||
Notification.service_id == service_id,
|
||||
Notification.template_id == template_id,
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Notification.key_type != KeyType.TEST,
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
@@ -48,7 +47,7 @@ def dao_get_last_date_template_was_used(template_id, service_id):
|
||||
db.session.query(functions.max(FactNotificationStatus.local_date))
|
||||
.filter(
|
||||
FactNotificationStatus.template_id == template_id,
|
||||
FactNotificationStatus.key_type != KEY_TYPE_TEST,
|
||||
FactNotificationStatus.key_type != KeyType.TEST,
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
@@ -269,7 +268,7 @@ def get_notifications_for_service(
|
||||
if key_type is not None:
|
||||
filters.append(Notification.key_type == key_type)
|
||||
elif not include_from_test_key:
|
||||
filters.append(Notification.key_type != KEY_TYPE_TEST)
|
||||
filters.append(Notification.key_type != KeyType.TEST)
|
||||
|
||||
if client_reference is not None:
|
||||
filters.append(Notification.client_reference == client_reference)
|
||||
@@ -409,7 +408,7 @@ def move_notifications_to_notification_history(
|
||||
Notification.notification_type == notification_type,
|
||||
Notification.service_id == service_id,
|
||||
Notification.created_at < timestamp_to_delete_backwards_from,
|
||||
Notification.key_type == KEY_TYPE_TEST,
|
||||
Notification.key_type == KeyType.TEST,
|
||||
).delete(synchronize_session=False)
|
||||
db.session.commit()
|
||||
|
||||
@@ -513,7 +512,7 @@ def dao_get_notifications_by_recipient_or_reference(
|
||||
Notification.normalised_to.like("%{}%".format(normalised)),
|
||||
Notification.client_reference.ilike("%{}%".format(search_term)),
|
||||
),
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Notification.key_type != KeyType.TEST,
|
||||
]
|
||||
|
||||
if statuses:
|
||||
@@ -576,7 +575,7 @@ def dao_get_notifications_processing_time_stats(start_date, end_date):
|
||||
Notification.created_at >= start_date,
|
||||
Notification.created_at < end_date,
|
||||
Notification.api_key_id.isnot(None),
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Notification.key_type != KeyType.TEST,
|
||||
)
|
||||
.one()
|
||||
)
|
||||
|
||||
@@ -13,9 +13,8 @@ from app.dao.organization_dao import dao_get_organization_by_email_address
|
||||
from app.dao.service_sms_sender_dao import insert_service_sms_sender
|
||||
from app.dao.service_user_dao import dao_get_service_user
|
||||
from app.dao.template_folder_dao import dao_get_valid_template_folders_by_id
|
||||
from app.enums import NotificationStatus, NotificationType, ServicePermissionType
|
||||
from app.enums import NotificationStatus, NotificationType, ServicePermissionType, KeyType
|
||||
from app.models import (
|
||||
KEY_TYPE_TEST,
|
||||
AnnualBilling,
|
||||
ApiKey,
|
||||
FactBilling,
|
||||
@@ -405,7 +404,7 @@ def dao_fetch_todays_stats_for_service(service_id):
|
||||
)
|
||||
.filter(
|
||||
Notification.service_id == service_id,
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Notification.key_type != KeyType.TEST,
|
||||
Notification.created_at >= start_date,
|
||||
)
|
||||
.group_by(
|
||||
@@ -439,7 +438,7 @@ def dao_fetch_todays_stats_for_all_services(
|
||||
)
|
||||
|
||||
if not include_from_test_key:
|
||||
subquery = subquery.filter(Notification.key_type != KEY_TYPE_TEST)
|
||||
subquery = subquery.filter(Notification.key_type != KeyType.TEST)
|
||||
|
||||
subquery = subquery.subquery()
|
||||
|
||||
@@ -510,7 +509,7 @@ def dao_find_services_sending_to_tv_numbers(start_date, end_date, threshold=500)
|
||||
Notification.service_id == Service.id,
|
||||
Notification.created_at >= start_date,
|
||||
Notification.created_at <= end_date,
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Notification.key_type != KeyType.TEST,
|
||||
Notification.notification_type == NotificationType.SMS,
|
||||
func.substr(Notification.normalised_to, 3, 7) == "7700900",
|
||||
Service.restricted == False, # noqa
|
||||
@@ -534,7 +533,7 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
|
||||
Notification.service_id == Service.id,
|
||||
Notification.created_at >= start_date,
|
||||
Notification.created_at <= end_date,
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Notification.key_type != KeyType.TEST,
|
||||
Notification.notification_type == NotificationType.SMS,
|
||||
Service.restricted == False, # noqa
|
||||
Service.active == True, # noqa
|
||||
@@ -562,7 +561,7 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
|
||||
Notification.service_id == Service.id,
|
||||
Notification.created_at >= start_date,
|
||||
Notification.created_at <= end_date,
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Notification.key_type != KeyType.TEST,
|
||||
Notification.notification_type == NotificationType.SMS,
|
||||
Notification.status == NotificationStatus.PERMANENT_FAILURE,
|
||||
Service.restricted == False, # noqa
|
||||
|
||||
Reference in New Issue
Block a user