diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index fddb8ad44..5959c26cd 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -221,10 +221,16 @@ def get_notification_by_id(notification_id): return Notification.query.filter_by(id=notification_id).first() -def get_notifications_for_service(service_id, filter_dict=None, page=1, page_size=None): +def get_notifications_for_service(service_id, filter_dict=None, page=1, page_size=None, limit_days=None): if page_size is None: page_size = current_app.config['PAGE_SIZE'] - query = Notification.query.filter_by(service_id=service_id) + filters = [Notification.service_id == service_id] + + if limit_days is not None: + days_ago = date.today() - timedelta(days=limit_days) + filters.append(func.date(Notification.created_at) >= days_ago) + + query = Notification.query.filter(*filters) query = filter_query(query, filter_dict) pagination = query.order_by(desc(Notification.created_at)).paginate( page=page, diff --git a/app/notifications/rest.py b/app/notifications/rest.py index 713d928f0..4bfd85db2 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -191,12 +191,14 @@ def get_all_notifications(): page = data['page'] if 'page' in data else 1 page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE') + limit_days = data.get('limit_days') pagination = notifications_dao.get_notifications_for_service( api_user['client'], filter_dict=data, page=page, - page_size=page_size) + page_size=page_size, + limit_days=limit_days) return jsonify( notifications=notification_status_schema.dump(pagination.items, many=True).data, page_size=page_size, @@ -218,12 +220,14 @@ def get_all_notifications_for_service(service_id): page = data['page'] if 'page' in data else 1 page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE') + limit_days = data.get('limit_days') pagination = notifications_dao.get_notifications_for_service( service_id, filter_dict=data, page=page, - page_size=page_size) + page_size=page_size, + limit_days=limit_days) kwargs = request.args.to_dict() kwargs['service_id'] = service_id return jsonify( diff --git a/app/schemas.py b/app/schemas.py index 235cdc281..e5facc137 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -245,6 +245,7 @@ class NotificationsFilterSchema(ma.Schema): status = fields.Nested(NotificationModelSchema, only=['status'], many=True) page = fields.Int(required=False) page_size = fields.Int(required=False) + limit_days = fields.Int(required=False) @pre_load def handle_multidict(self, in_data): diff --git a/config.py b/config.py index 23687f4b5..a93592aee 100644 --- a/config.py +++ b/config.py @@ -52,12 +52,12 @@ class Config(object): }, 'delete-failed-notifications': { 'task': 'delete-failed-notifications', - 'schedule': crontab(minute=0, hour=0), + 'schedule': crontab(minute=0, hour='0,1,2'), 'options': {'queue': 'periodic'} }, 'delete-successful-notifications': { 'task': 'delete-successful-notifications', - 'schedule': crontab(minute=0, hour=0), + 'schedule': crontab(minute=0, hour='0,1,2'), 'options': {'queue': 'periodic'} } } diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index 0da5e8715..b4e8db8ce 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -29,7 +29,8 @@ from app.dao.notifications_dao import ( update_notification_status_by_reference, dao_get_template_statistics_for_service, get_character_count_of_content, - get_sms_message_count + get_sms_message_count, + get_notifications_for_service ) from tests.app.conftest import sample_job @@ -1067,6 +1068,27 @@ def test_get_template_stats_for_service_with_limit_if_no_records_returns_empty_l assert len(template_stats) == 0 +@freeze_time("2016-01-10") +def test_should_limit_notifications_return_by_day_limit_plus_one(notify_db, notify_db_session, sample_service): + + assert len(Notification.query.all()) == 0 + + # create one notification a day between 1st and 9th + for i in range(1, 11): + past_date = '2016-01-{0:02d}'.format(i) + with freeze_time(past_date): + sample_notification(notify_db, notify_db_session, created_at=datetime.utcnow(), status="failed") + + all_notifications = Notification.query.all() + assert len(all_notifications) == 10 + + all_notifications = get_notifications_for_service(sample_service.id, limit_days=10).items + assert len(all_notifications) == 10 + + all_notifications = get_notifications_for_service(sample_service.id, limit_days=1).items + assert len(all_notifications) == 2 + + @pytest.mark.parametrize( "content,encoding,expected_length", [