mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-12 17:34:48 -04:00
Delete unused aggregate_statistics endpoint
`@service_blueprint.route('/<uuid:service_id>/fragment/aggregate_statistics')`
is not being used anywhere, so has been removed. The `provider_statistics_dao`
can also be removed, since the function this contained was only used for
the endpoint.
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from app import db
|
||||
from app.dao.date_util import get_financial_year
|
||||
from app.models import (
|
||||
NotificationHistory,
|
||||
SMS_TYPE,
|
||||
EMAIL_TYPE,
|
||||
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
||||
KEY_TYPE_TEST
|
||||
)
|
||||
|
||||
|
||||
def get_fragment_count(service_id, year=None):
|
||||
shared_filters = [
|
||||
NotificationHistory.service_id == service_id,
|
||||
NotificationHistory.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE),
|
||||
NotificationHistory.key_type != KEY_TYPE_TEST
|
||||
]
|
||||
|
||||
if year:
|
||||
shared_filters.append(NotificationHistory.created_at.between(
|
||||
*get_financial_year(year)
|
||||
))
|
||||
|
||||
sms_count = db.session.query(
|
||||
func.sum(NotificationHistory.billable_units)
|
||||
).filter(
|
||||
NotificationHistory.notification_type == SMS_TYPE,
|
||||
*shared_filters
|
||||
)
|
||||
|
||||
email_count = db.session.query(
|
||||
func.count(NotificationHistory.id)
|
||||
).filter(
|
||||
NotificationHistory.notification_type == EMAIL_TYPE,
|
||||
*shared_filters
|
||||
)
|
||||
return {
|
||||
'sms_count': int(sms_count.scalar() or 0),
|
||||
'email_count': email_count.scalar() or 0
|
||||
}
|
||||
@@ -65,7 +65,6 @@ from app.dao.service_letter_contact_dao import (
|
||||
add_letter_contact_for_service,
|
||||
update_letter_contact
|
||||
)
|
||||
from app.dao.provider_statistics_dao import get_fragment_count
|
||||
from app.dao.users_dao import get_user_by_id
|
||||
from app.errors import (
|
||||
InvalidRequest,
|
||||
@@ -299,17 +298,6 @@ def remove_user_from_service(service_id, user_id):
|
||||
return jsonify({}), 204
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/fragment/aggregate_statistics')
|
||||
def get_service_provider_aggregate_statistics(service_id):
|
||||
year = request.args.get('year')
|
||||
if year is not None:
|
||||
try:
|
||||
year = int(year)
|
||||
except ValueError:
|
||||
raise InvalidRequest('Year must be a number', status_code=400)
|
||||
return jsonify(data=get_fragment_count(service_id, year=year))
|
||||
|
||||
|
||||
# This is placeholder get method until more thought
|
||||
# goes into how we want to fetch and view various items in history
|
||||
# tables. This is so product owner can pass stories as done
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from freezegun import freeze_time
|
||||
|
||||
from app.models import (
|
||||
NotificationHistory, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST,
|
||||
NOTIFICATION_STATUS_TYPES_BILLABLE
|
||||
)
|
||||
from app.dao.provider_statistics_dao import get_fragment_count
|
||||
|
||||
|
||||
def test_get_fragment_count_with_no_data(sample_template):
|
||||
assert get_fragment_count(sample_template.service_id)['sms_count'] == 0
|
||||
assert get_fragment_count(sample_template.service_id)['email_count'] == 0
|
||||
|
||||
|
||||
def test_get_fragment_count_separates_sms_and_email(notify_db, sample_template, sample_email_template):
|
||||
noti_hist(notify_db, sample_template)
|
||||
noti_hist(notify_db, sample_template)
|
||||
noti_hist(notify_db, sample_email_template)
|
||||
assert get_fragment_count(sample_template.service_id) == {
|
||||
'sms_count': 2,
|
||||
'email_count': 1
|
||||
}
|
||||
|
||||
|
||||
def test_get_fragment_count_filters_on_status(notify_db, sample_template):
|
||||
for status in NOTIFICATION_STATUS_TYPES_BILLABLE:
|
||||
noti_hist(notify_db, sample_template, status=status)
|
||||
# sending, sent, delivered, failed, temporary-failure, permanent-failure
|
||||
assert get_fragment_count(sample_template.service_id)['sms_count'] == 6
|
||||
|
||||
|
||||
def test_get_fragment_count_filters_on_service_id(notify_db, sample_template, service_factory):
|
||||
service_2 = service_factory.get('service 2', email_from='service.2')
|
||||
noti_hist(notify_db, sample_template)
|
||||
assert get_fragment_count(service_2.id)['sms_count'] == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize('creation_time, expected_count', [
|
||||
('2000-03-31 22:59:59', 0), # before the start of the year
|
||||
('2000-04-01 00:00:00', 1), # after the start of the year
|
||||
('2001-03-31 22:59:59', 1), # before the end of the year
|
||||
('2001-04-01 00:00:00', 0), # after the end of the year
|
||||
])
|
||||
def test_get_fragment_count_filters_on_year(
|
||||
notify_db, sample_template, creation_time, expected_count
|
||||
):
|
||||
with freeze_time(creation_time):
|
||||
noti_hist(notify_db, sample_template)
|
||||
assert get_fragment_count(sample_template.service_id, year=2000)['sms_count'] == expected_count
|
||||
|
||||
|
||||
def test_get_fragment_count_sums_billable_units_for_sms(notify_db, sample_template):
|
||||
noti_hist(notify_db, sample_template, billable_units=1)
|
||||
noti_hist(notify_db, sample_template, billable_units=2)
|
||||
assert get_fragment_count(sample_template.service_id)['sms_count'] == 3
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key_type,sms_count', [
|
||||
(KEY_TYPE_NORMAL, 1),
|
||||
(KEY_TYPE_TEAM, 1),
|
||||
(KEY_TYPE_TEST, 0),
|
||||
])
|
||||
def test_get_fragment_count_ignores_test_api_keys(notify_db, sample_template, key_type, sms_count):
|
||||
noti_hist(notify_db, sample_template, key_type=key_type)
|
||||
assert get_fragment_count(sample_template.service_id)['sms_count'] == sms_count
|
||||
|
||||
|
||||
def noti_hist(notify_db, template, status='delivered', billable_units=None, key_type=KEY_TYPE_NORMAL):
|
||||
if not billable_units and template.template_type == 'sms':
|
||||
billable_units = 1
|
||||
|
||||
notification_history = NotificationHistory(
|
||||
id=uuid.uuid4(),
|
||||
service=template.service,
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
status=status,
|
||||
created_at=datetime.utcnow(),
|
||||
billable_units=billable_units,
|
||||
notification_type=template.template_type,
|
||||
key_type=key_type
|
||||
)
|
||||
notify_db.session.add(notification_history)
|
||||
notify_db.session.commit()
|
||||
|
||||
return notification_history
|
||||
@@ -1678,26 +1678,6 @@ def test_get_detailed_services_for_date_range(notify_db, notify_db_session, set_
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('query_string, expected_status, expected_json', [
|
||||
('', 200, {'data': {'email_count': 0, 'sms_count': 0}}),
|
||||
('?year=2000', 200, {'data': {'email_count': 0, 'sms_count': 0}}),
|
||||
('?year=abcd', 400, {'message': 'Year must be a number', 'result': 'error'}),
|
||||
])
|
||||
def test_get_service_provider_aggregate_statistics(
|
||||
client,
|
||||
sample_service,
|
||||
query_string,
|
||||
expected_status,
|
||||
expected_json,
|
||||
):
|
||||
response = client.get(
|
||||
'/service/{}/fragment/aggregate_statistics{}'.format(sample_service.id, query_string),
|
||||
headers=[create_authorization_header()]
|
||||
)
|
||||
assert response.status_code == expected_status
|
||||
assert json.loads(response.get_data(as_text=True)) == expected_json
|
||||
|
||||
|
||||
@freeze_time('2017-11-11 02:00')
|
||||
def test_get_template_usage_by_month_returns_correct_data(
|
||||
notify_db,
|
||||
|
||||
Reference in New Issue
Block a user