mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
remove unused notification-statistics endpoints
This commit is contained in:
@@ -66,7 +66,6 @@ def create_app(app_name=None):
|
|||||||
from app.notifications.rest import notifications as notifications_blueprint
|
from app.notifications.rest import notifications as notifications_blueprint
|
||||||
from app.invite.rest import invite as invite_blueprint
|
from app.invite.rest import invite as invite_blueprint
|
||||||
from app.accept_invite.rest import accept_invite
|
from app.accept_invite.rest import accept_invite
|
||||||
from app.notifications_statistics.rest import notifications_statistics as notifications_statistics_blueprint
|
|
||||||
from app.template_statistics.rest import template_statistics as template_statistics_blueprint
|
from app.template_statistics.rest import template_statistics as template_statistics_blueprint
|
||||||
from app.events.rest import events as events_blueprint
|
from app.events.rest import events as events_blueprint
|
||||||
from app.provider_details.rest import provider_details as provider_details_blueprint
|
from app.provider_details.rest import provider_details as provider_details_blueprint
|
||||||
@@ -81,7 +80,7 @@ def create_app(app_name=None):
|
|||||||
application.register_blueprint(job_blueprint)
|
application.register_blueprint(job_blueprint)
|
||||||
application.register_blueprint(invite_blueprint)
|
application.register_blueprint(invite_blueprint)
|
||||||
application.register_blueprint(accept_invite, url_prefix='/invite')
|
application.register_blueprint(accept_invite, url_prefix='/invite')
|
||||||
application.register_blueprint(notifications_statistics_blueprint)
|
|
||||||
application.register_blueprint(template_statistics_blueprint)
|
application.register_blueprint(template_statistics_blueprint)
|
||||||
application.register_blueprint(events_blueprint)
|
application.register_blueprint(events_blueprint)
|
||||||
application.register_blueprint(provider_details_blueprint, url_prefix='/provider-details')
|
application.register_blueprint(provider_details_blueprint, url_prefix='/provider-details')
|
||||||
|
|||||||
@@ -1,105 +0,0 @@
|
|||||||
from datetime import (
|
|
||||||
date,
|
|
||||||
timedelta,
|
|
||||||
datetime
|
|
||||||
)
|
|
||||||
|
|
||||||
from flask import (
|
|
||||||
Blueprint,
|
|
||||||
jsonify,
|
|
||||||
request,
|
|
||||||
current_app
|
|
||||||
)
|
|
||||||
|
|
||||||
from app import DATE_FORMAT
|
|
||||||
|
|
||||||
from app.dao.notifications_dao import (
|
|
||||||
dao_get_notification_statistics_for_service,
|
|
||||||
dao_get_7_day_agg_notification_statistics_for_service,
|
|
||||||
dao_get_notification_statistics_for_service_and_day
|
|
||||||
)
|
|
||||||
from app.schemas import (
|
|
||||||
notifications_statistics_schema,
|
|
||||||
week_aggregate_notification_statistics_schema
|
|
||||||
)
|
|
||||||
|
|
||||||
notifications_statistics = Blueprint(
|
|
||||||
'notifications-statistics',
|
|
||||||
__name__, url_prefix='/service/<service_id>/notifications-statistics'
|
|
||||||
)
|
|
||||||
|
|
||||||
from app.errors import (
|
|
||||||
register_errors,
|
|
||||||
InvalidRequest
|
|
||||||
)
|
|
||||||
|
|
||||||
register_errors(notifications_statistics)
|
|
||||||
|
|
||||||
|
|
||||||
@notifications_statistics.route('', methods=['GET'])
|
|
||||||
def get_all_notification_statistics_for_service(service_id):
|
|
||||||
|
|
||||||
if request.args.get('limit_days'):
|
|
||||||
try:
|
|
||||||
statistics = dao_get_notification_statistics_for_service(
|
|
||||||
service_id=service_id,
|
|
||||||
limit_days=int(request.args['limit_days'])
|
|
||||||
)
|
|
||||||
except ValueError as e:
|
|
||||||
message = '{} is not an integer'.format(request.args['limit_days'])
|
|
||||||
errors = {'limit_days': [message]}
|
|
||||||
raise InvalidRequest(errors, status_code=400)
|
|
||||||
else:
|
|
||||||
statistics = dao_get_notification_statistics_for_service(service_id=service_id)
|
|
||||||
|
|
||||||
data, errors = notifications_statistics_schema.dump(statistics, many=True)
|
|
||||||
return jsonify(data=data)
|
|
||||||
|
|
||||||
|
|
||||||
@notifications_statistics.route('/day/<day>', methods=['GET'])
|
|
||||||
def get_notification_statistics_for_service_for_day(service_id, day):
|
|
||||||
|
|
||||||
try:
|
|
||||||
datetime.strptime(day, DATE_FORMAT)
|
|
||||||
except ValueError:
|
|
||||||
raise InvalidRequest('Invalid date {}'.format(day), status_code=400)
|
|
||||||
|
|
||||||
service_stats = dao_get_notification_statistics_for_service_and_day(
|
|
||||||
service_id,
|
|
||||||
day
|
|
||||||
)
|
|
||||||
|
|
||||||
if not service_stats:
|
|
||||||
message = 'No statistics found for service id: {} on day: {} '.format(service_id, day)
|
|
||||||
errors = {'not found': [message]}
|
|
||||||
raise InvalidRequest(errors, status_code=404)
|
|
||||||
|
|
||||||
data = notifications_statistics_schema.dump(service_stats).data
|
|
||||||
return jsonify(data=data)
|
|
||||||
|
|
||||||
|
|
||||||
@notifications_statistics.route('/seven_day_aggregate')
|
|
||||||
def get_notification_statistics_for_service_seven_day_aggregate(service_id):
|
|
||||||
data = week_aggregate_notification_statistics_schema.load(request.args).data
|
|
||||||
date_from = data['date_from'] if 'date_from' in data else date(date.today().year, 4, 1)
|
|
||||||
week_count = data['week_count'] if 'week_count' in data else 52
|
|
||||||
stats = dao_get_7_day_agg_notification_statistics_for_service(
|
|
||||||
service_id,
|
|
||||||
date_from,
|
|
||||||
week_count).all()
|
|
||||||
json_stats = []
|
|
||||||
for x in range(week_count - 1, -1, -1):
|
|
||||||
week_stats = stats.pop(0) if len(stats) > 0 and stats[0][0] == x else [x, 0, 0, 0, 0, 0, 0]
|
|
||||||
week_start = (date_from + timedelta(days=week_stats[0] * 7))
|
|
||||||
if week_start <= date.today():
|
|
||||||
json_stats.append({
|
|
||||||
'week_start': week_start.strftime('%Y-%m-%d'),
|
|
||||||
'week_end': (date_from + timedelta(days=(week_stats[0] * 7) + 6)).strftime('%Y-%m-%d'),
|
|
||||||
'emails_requested': week_stats[1],
|
|
||||||
'emails_delivered': week_stats[2],
|
|
||||||
'emails_failed': week_stats[3],
|
|
||||||
'sms_requested': week_stats[4],
|
|
||||||
'sms_delivered': week_stats[5],
|
|
||||||
'sms_failed': week_stats[6]
|
|
||||||
})
|
|
||||||
return jsonify(data=json_stats)
|
|
||||||
@@ -1,207 +0,0 @@
|
|||||||
import json
|
|
||||||
from datetime import (
|
|
||||||
date,
|
|
||||||
timedelta
|
|
||||||
)
|
|
||||||
|
|
||||||
from flask import url_for
|
|
||||||
from tests import create_authorization_header
|
|
||||||
from tests.app.conftest import sample_notification_statistics as create_sample_notification_statistics
|
|
||||||
|
|
||||||
from freezegun import freeze_time
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_notification_statistics_returns_empty_list_if_no_stats(notify_api,
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
sample_template,
|
|
||||||
sample_email_template):
|
|
||||||
with notify_api.test_request_context():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
|
|
||||||
path = '/service/{}/notifications-statistics'.format(sample_email_template.service.id)
|
|
||||||
|
|
||||||
auth_header = create_authorization_header(
|
|
||||||
service_id=sample_email_template.service_id)
|
|
||||||
|
|
||||||
response = client.get(path, headers=[auth_header])
|
|
||||||
assert response.status_code == 200
|
|
||||||
|
|
||||||
stats = json.loads(response.get_data(as_text=True))
|
|
||||||
assert stats['data'] == []
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_week_aggregate_statistics(notify_api,
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
sample_service):
|
|
||||||
with notify_api.test_request_context():
|
|
||||||
sample_notification_statistics = create_sample_notification_statistics(
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
day=date(date.today().year, 4, 1))
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
endpoint = url_for(
|
|
||||||
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
|
|
||||||
service_id=sample_service.id)
|
|
||||||
auth_header = create_authorization_header(
|
|
||||||
service_id=sample_service.id)
|
|
||||||
|
|
||||||
resp = client.get(endpoint, headers=[auth_header])
|
|
||||||
assert resp.status_code == 200
|
|
||||||
json_resp = json.loads(resp.get_data(as_text=True))
|
|
||||||
week_len_index = len(json_resp['data']) - 1
|
|
||||||
assert json_resp['data'][week_len_index]['emails_requested'] == 2
|
|
||||||
assert json_resp['data'][week_len_index]['sms_requested'] == 2
|
|
||||||
assert json_resp['data'][week_len_index]['week_start'] == date(date.today().year, 4, 1).strftime('%Y-%m-%d')
|
|
||||||
assert json_resp['data'][week_len_index]['week_end'] == date(date.today().year, 4, 7).strftime('%Y-%m-%d')
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_week_aggregate_statistics_date_from(notify_api,
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
sample_service):
|
|
||||||
with notify_api.test_request_context():
|
|
||||||
sample_notification_statistics = create_sample_notification_statistics(
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
day=date(date.today().year, 4, 1))
|
|
||||||
date_from_str = date(date.today().year, 4, 1).strftime('%Y-%m-%d')
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
endpoint = url_for(
|
|
||||||
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
|
|
||||||
service_id=sample_service.id,
|
|
||||||
date_from=date_from_str)
|
|
||||||
auth_header = create_authorization_header(
|
|
||||||
service_id=sample_service.id)
|
|
||||||
|
|
||||||
resp = client.get(endpoint, headers=[auth_header])
|
|
||||||
assert resp.status_code == 200
|
|
||||||
json_resp = json.loads(resp.get_data(as_text=True))
|
|
||||||
week_len_index = len(json_resp['data']) - 1
|
|
||||||
assert json_resp['data'][week_len_index]['emails_requested'] == 2
|
|
||||||
assert json_resp['data'][week_len_index]['sms_requested'] == 2
|
|
||||||
assert json_resp['data'][week_len_index]['week_start'] == date_from_str
|
|
||||||
assert json_resp['data'][week_len_index]['week_end'] == date(date.today().year, 4, 7).strftime('%Y-%m-%d')
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_week_aggregate_statistics_date_in_future(notify_api,
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
sample_service):
|
|
||||||
with notify_api.test_request_context():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
endpoint = url_for(
|
|
||||||
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
|
|
||||||
service_id=sample_service.id,
|
|
||||||
date_from=(date.today() + timedelta(days=1)).strftime('%Y-%m-%d'))
|
|
||||||
auth_header = create_authorization_header(
|
|
||||||
service_id=sample_service.id)
|
|
||||||
|
|
||||||
resp = client.get(endpoint, headers=[auth_header])
|
|
||||||
assert resp.status_code == 400
|
|
||||||
json_resp = json.loads(resp.get_data(as_text=True))
|
|
||||||
assert json_resp['result'] == 'error'
|
|
||||||
assert json_resp['message']['date_from'][0] == 'Date cannot be in the future'
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_week_aggregate_statistics_invalid_week_count(notify_api,
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
sample_service):
|
|
||||||
with notify_api.test_request_context():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
endpoint = url_for(
|
|
||||||
'notifications-statistics.get_notification_statistics_for_service_seven_day_aggregate',
|
|
||||||
service_id=sample_service.id,
|
|
||||||
week_count=-1)
|
|
||||||
auth_header = create_authorization_header(
|
|
||||||
service_id=sample_service.id)
|
|
||||||
|
|
||||||
resp = client.get(endpoint, headers=[auth_header])
|
|
||||||
assert resp.status_code == 400
|
|
||||||
json_resp = json.loads(resp.get_data(as_text=True))
|
|
||||||
assert json_resp['result'] == 'error'
|
|
||||||
assert json_resp['message']['week_count'][0] == 'Not a positive integer'
|
|
||||||
|
|
||||||
|
|
||||||
@freeze_time('2016-01-01')
|
|
||||||
def test_get_notification_statistics_for_specific_day(notify_api,
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
sample_template):
|
|
||||||
the_day = date.today()
|
|
||||||
|
|
||||||
sample_notification_statistics = create_sample_notification_statistics(
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
day=the_day)
|
|
||||||
|
|
||||||
with notify_api.test_request_context():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
path = '/service/{}/notifications-statistics/day/{}'.format(sample_template.service_id, the_day)
|
|
||||||
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
|
||||||
response = client.get(path, headers=[auth_header])
|
|
||||||
assert response.status_code == 200
|
|
||||||
stats = json.loads(response.get_data(as_text=True))
|
|
||||||
|
|
||||||
assert stats['data']['id'] == str(sample_notification_statistics.id)
|
|
||||||
assert stats['data']['day'] == the_day.strftime('%Y-%m-%d')
|
|
||||||
|
|
||||||
another_day = the_day - timedelta(days=1)
|
|
||||||
path = '/service/{}/notifications-statistics/day/{}'.format(sample_template.service_id, another_day)
|
|
||||||
|
|
||||||
response = client.get(path, headers=[auth_header])
|
|
||||||
assert response.status_code == 404
|
|
||||||
|
|
||||||
|
|
||||||
@freeze_time('2016-01-01')
|
|
||||||
def test_get_notification_statistics_for_specific_day_returns_404_if_no_stats(notify_api,
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
sample_template):
|
|
||||||
the_day = date.today()
|
|
||||||
|
|
||||||
with notify_api.test_request_context():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
path = '/service/{}/notifications-statistics/day/{}'.format(sample_template.service_id, the_day)
|
|
||||||
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
|
||||||
response = client.get(path, headers=[auth_header])
|
|
||||||
assert response.status_code == 404
|
|
||||||
|
|
||||||
|
|
||||||
@freeze_time('2016-01-01')
|
|
||||||
def test_get_notification_statistics_for_specific_day_returns_400_for_incorrect_date(notify_api,
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
sample_template):
|
|
||||||
the_day = date.today()
|
|
||||||
incorrect_date_format = the_day.strftime('%d-%m-%Y')
|
|
||||||
|
|
||||||
create_sample_notification_statistics(
|
|
||||||
notify_db,
|
|
||||||
notify_db_session,
|
|
||||||
day=the_day)
|
|
||||||
|
|
||||||
with notify_api.test_request_context():
|
|
||||||
with notify_api.test_client() as client:
|
|
||||||
path = '/service/{}/notifications-statistics/day/{}'.format(
|
|
||||||
sample_template.service_id,
|
|
||||||
incorrect_date_format)
|
|
||||||
auth_header = create_authorization_header(service_id=sample_template.service_id)
|
|
||||||
response = client.get(path, headers=[auth_header])
|
|
||||||
assert response.status_code == 400
|
|
||||||
resp_json = json.loads(response.get_data(as_text=True))
|
|
||||||
assert resp_json['result'] == 'error'
|
|
||||||
assert resp_json['message'] == 'Invalid date 01-01-2016'
|
|
||||||
|
|
||||||
another_dodgy_date = 'fish'
|
|
||||||
path = '/service/{}/notifications-statistics/day/{}'.format(
|
|
||||||
sample_template.service_id,
|
|
||||||
another_dodgy_date)
|
|
||||||
|
|
||||||
response = client.get(path, headers=[auth_header])
|
|
||||||
assert response.status_code == 400
|
|
||||||
resp_json = json.loads(response.get_data(as_text=True))
|
|
||||||
assert resp_json['result'] == 'error'
|
|
||||||
assert resp_json['message'] == 'Invalid date fish'
|
|
||||||
Reference in New Issue
Block a user