-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix/add-notification-size-validator
- Loading branch information
Showing
9 changed files
with
145 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Enlarge DB | ||
|
||
## Purpose | ||
|
||
The purpose of this script is add rows to the notification_history table. This is useful in estimating how long database-related infrastructure operations will take when performed on a database the same size as that in production. | ||
|
||
## How to use | ||
|
||
The script should be run in the same environment as api. Locally this can be in the api repo devcontainer, while in AWS the api kubernetes pod would be preferred. | ||
|
||
To add 2000 rows to the table with a client_reference of "test2000" run | ||
|
||
``` | ||
cd scripts/enlarge_db | ||
python enlarge_db.py -n 2000 -r test2000 | ||
``` | ||
|
||
The new notifications are added in batches to improve performance, with a default batch size of 10000. You may use a different batch with the `-c` parameter, for example | ||
|
||
``` | ||
python enlarge_db.py -n 2000 -c 101 -r test2000x101 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
import argparse | ||
import sys | ||
from datetime import datetime | ||
from typing import List | ||
|
||
from flask import Flask | ||
|
||
sys.path.append("../..") | ||
from app import create_app, create_uuid, db # noqa: E402 | ||
from app.config import Config # noqa: E402 | ||
from app.models import NotificationHistory # noqa: E402 | ||
|
||
DEFAULT_CHUNK_SIZE = 10000 | ||
|
||
|
||
def create_notifications(n: int, ref: str) -> List[NotificationHistory]: | ||
notifications = [ | ||
NotificationHistory( | ||
id=create_uuid(), | ||
created_at=datetime.utcnow(), | ||
template_id=Config.NEW_USER_EMAIL_VERIFICATION_TEMPLATE_ID, | ||
template_version=1, | ||
service_id=Config.NOTIFY_SERVICE_ID, | ||
notification_type="email", | ||
key_type='normal', | ||
client_reference=ref, | ||
) | ||
for _ in range(n) | ||
] | ||
return notifications | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-n", "--notifications", default=1, type=int, help="number of notifications to add to the notification_history table (default 1)") | ||
parser.add_argument("-r", "--reference", default="manually created", type=str, help="client reference to use for the notifications (default 'manually created')") | ||
parser.add_argument("-c", "--chunksize", default=DEFAULT_CHUNK_SIZE, type=int, help=f"chunk size for bulk_save_objects (default {DEFAULT_CHUNK_SIZE})") | ||
args = parser.parse_args() | ||
|
||
app = Flask("enlarge_db") | ||
create_app(app) | ||
|
||
for notifications_done in range(0, args.notifications, args.chunksize): | ||
notifications = create_notifications(min(args.chunksize, args.notifications - notifications_done), args.reference) | ||
print(f"Adding {len(notifications)} notifications to notification_history") | ||
with app.app_context(): | ||
try: | ||
db.session.bulk_save_objects(notifications) | ||
db.session.commit() | ||
except Exception as e: | ||
print(f"Error adding notifications: {e}") | ||
db.session.rollback() | ||
sys.exit(1) | ||
print(f"Done {notifications_done+len(notifications)} / {args.notifications}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
LETTER_TYPE, | ||
NORMAL, | ||
PRIORITY, | ||
ApiKey, | ||
Notification, | ||
NotificationHistory, | ||
ScheduledNotification, | ||
|
@@ -433,6 +434,10 @@ def test_persist_notifications_list(self, sample_job, sample_api_key, notify_db_ | |
assert persisted_notification[1].to == "[email protected]" | ||
assert persisted_notification[0].service == sample_job.service | ||
|
||
# Test that the api key last_used_timestamp got updated | ||
api_key = ApiKey.query.get(sample_api_key.id) | ||
assert api_key.last_used_timestamp is not None | ||
|
||
def test_persist_notifications_reply_to_text_is_original_value_if_sender_is_changed_later( | ||
self, sample_template, sample_api_key, mocker | ||
): | ||
|
@@ -913,6 +918,8 @@ def test_transform_email_notification_stores_normalised_email( | |
|
||
assert persisted_notification.to == recipient | ||
assert persisted_notification.normalised_to == expected_recipient_normalised | ||
api_key = ApiKey.query.get(sample_api_key.id) | ||
assert api_key.last_used_timestamp is not None | ||
|
||
|
||
class TestDBSaveAndSendNotification: | ||
|