Merge branch 'master' of https://github.com/alphagov/notifications-api into vb-free-sms-limit-history

This commit is contained in:
venusbb
2017-10-26 17:21:41 +01:00
5 changed files with 113 additions and 38 deletions

View File

@@ -100,10 +100,10 @@ def _transform_billing_for_month(billing_for_month):
@billing_blueprint.route('/free-sms-fragment-limit/current-year', methods=["GET"])
def get_free_sms_fragment_limit(service_id):
financial_year_start = request.args.get('financial_year_start')
if request.path.split('/')[-1] == 'current-year':
financial_year_start = get_current_financial_year_start_year()
else:
financial_year_start = request.args.get('financial_year_start')
if financial_year_start is None:
results = dao_get_all_free_sms_fragment_limit(service_id)
@@ -122,7 +122,12 @@ def get_free_sms_fragment_limit(service_id):
@billing_blueprint.route('/free-sms-fragment-limit', methods=["POST"])
def create_or_update_free_sms_fragment_limit(service_id):
form = validate(request.get_json(), create_or_update_free_sms_fragment_limit_schema)
dict_arg = request.get_json()
if 'financial_year_start' not in dict_arg:
dict_arg['financial_year_start'] = get_current_financial_year_start_year()
form = validate(dict_arg, create_or_update_free_sms_fragment_limit_schema)
financial_year_start = form.get('financial_year_start')
free_sms_fragment_limit = form.get('free_sms_fragment_limit')

View File

@@ -376,9 +376,9 @@ def dao_fetch_monthly_historical_stats_for_service(service_id, year):
@statsd(namespace='dao')
def dao_fetch_todays_stats_for_all_services(include_from_test_key=True):
def dao_fetch_todays_stats_for_all_services(include_from_test_key=True, only_active=True):
query = db.session.query(
subquery = db.session.query(
Notification.notification_type,
Notification.status,
Notification.service_id,
@@ -389,26 +389,43 @@ def dao_fetch_todays_stats_for_all_services(include_from_test_key=True):
Notification.notification_type,
Notification.status,
Notification.service_id
).order_by(
Notification.service_id
)
if not include_from_test_key:
query = query.filter(Notification.key_type != KEY_TYPE_TEST)
subquery = subquery.filter(Notification.key_type != KEY_TYPE_TEST)
subquery = subquery.subquery()
query = db.session.query(
Service.id.label('service_id'),
Service.name,
Service.restricted,
Service.research_mode,
Service.active,
Service.created_at,
subquery.c.notification_type,
subquery.c.status,
subquery.c.count
).outerjoin(
subquery,
subquery.c.service_id == Service.id
).order_by(Service.id)
if only_active:
query = query.filter(Service.active)
return query.all()
@statsd(namespace='dao')
def fetch_stats_by_date_range_for_all_services(start_date, end_date, include_from_test_key=True):
def fetch_stats_by_date_range_for_all_services(start_date, end_date, include_from_test_key=True, only_active=True):
start_date = get_london_midnight_in_utc(start_date)
end_date = get_london_midnight_in_utc(end_date + timedelta(days=1))
table = NotificationHistory
if start_date >= datetime.utcnow() - timedelta(days=7):
table = Notification
query = db.session.query(
subquery = db.session.query(
table.notification_type,
table.status,
table.service_id,
@@ -420,12 +437,27 @@ def fetch_stats_by_date_range_for_all_services(start_date, end_date, include_fro
table.notification_type,
table.status,
table.service_id
).order_by(
table.service_id
)
if not include_from_test_key:
query = query.filter(table.key_type != KEY_TYPE_TEST)
subquery = subquery.filter(table.key_type != KEY_TYPE_TEST)
subquery = subquery.subquery()
query = db.session.query(
Service.id.label('service_id'),
Service.name,
Service.restricted,
Service.research_mode,
Service.active,
Service.created_at,
subquery.c.notification_type,
subquery.c.status,
subquery.c.count
).outerjoin(
subquery,
subquery.c.service_id == Service.id
).order_by(Service.id)
if only_active:
query = query.filter(Service.active)
return query.all()

View File

@@ -408,23 +408,33 @@ def get_detailed_service(service_id, today_only=False):
def get_detailed_services(start_date, end_date, only_active=False, include_from_test_key=True):
services = {service.id: service for service in dao_fetch_all_services(only_active)}
if start_date == datetime.utcnow().date():
stats = dao_fetch_todays_stats_for_all_services(include_from_test_key=include_from_test_key)
stats = dao_fetch_todays_stats_for_all_services(include_from_test_key=include_from_test_key,
only_active=only_active)
else:
stats = fetch_stats_by_date_range_for_all_services(start_date=start_date,
end_date=end_date,
include_from_test_key=include_from_test_key)
include_from_test_key=include_from_test_key,
only_active=only_active)
results = []
for service_id, rows in itertools.groupby(stats, lambda x: x.service_id):
services[service_id].statistics = statistics.format_statistics(rows)
# if service has not sent anything, query will not have set statistics correctly
for service in services.values():
if not hasattr(service, 'statistics'):
service.statistics = statistics.create_zeroed_stats_dicts()
return detailed_service_schema.dump(services.values(), many=True).data
rows = list(rows)
if rows[0].count is None:
s = statistics.create_zeroed_stats_dicts()
else:
s = statistics.format_statistics(rows)
results.append({
'id': str(rows[0].service_id),
'name': rows[0].name,
'notification_type': rows[0].notification_type,
'research_mode': rows[0].research_mode,
'restricted': rows[0].restricted,
'active': rows[0].active,
'created_at': rows[0].created_at,
'statistics': s
})
return results
@service_blueprint.route('/<uuid:service_id>/whitelist', methods=['GET'])