def test_fetch_notification_status_for_service_by_month(notify_db_session): service_1 = create_service(service_name='service_1') service_2 = create_service(service_name='service_2') create_ft_notification_status(date(2018, 1, 1), 'sms', service_1, count=4) create_ft_notification_status(date(2018, 1, 2), 'sms', service_1, count=10) create_ft_notification_status(date(2018, 1, 2), 'sms', service_1, notification_status='created') create_ft_notification_status(date(2018, 1, 3), 'email', service_1) create_ft_notification_status(date(2018, 2, 2), 'sms', service_1) # not included - too early create_ft_notification_status(date(2017, 12, 31), 'sms', service_1) # not included - too late create_ft_notification_status(date(2017, 3, 1), 'sms', service_1) # not included - wrong service create_ft_notification_status(date(2018, 1, 3), 'sms', service_2) # not included - test keys create_ft_notification_status(date(2018, 1, 3), 'sms', service_1, key_type=KEY_TYPE_TEST) results = sorted(fetch_notification_status_for_service_by_month( date(2018, 1, 1), date(2018, 2, 28), service_1.id), key=lambda x: (x.month, x.notification_type, x.notification_status)) assert len(results) == 4 assert results[0].month.date() == date(2018, 1, 1) assert results[0].notification_type == 'email' assert results[0].notification_status == 'delivered' assert results[0].count == 1 assert results[1].month.date() == date(2018, 1, 1) assert results[1].notification_type == 'sms' assert results[1].notification_status == 'created' assert results[1].count == 1 assert results[2].month.date() == date(2018, 1, 1) assert results[2].notification_type == 'sms' assert results[2].notification_status == 'delivered' assert results[2].count == 14 assert results[3].month.date() == date(2018, 2, 1) assert results[3].notification_type == 'sms' assert results[3].notification_status == 'delivered' assert results[3].count == 1
def get_monthly_notification_stats(service_id): # check service_id validity dao_fetch_service_by_id(service_id) try: year = int(request.args.get('year', 'NaN')) except ValueError: raise InvalidRequest('Year must be a number', status_code=400) start_date, end_date = get_financial_year(year) data = statistics.create_empty_monthly_notification_status_stats_dict(year) stats = fetch_notification_status_for_service_by_month(start_date, end_date, service_id) statistics.add_monthly_notification_status_stats(data, stats) now = datetime.utcnow() if end_date > now: todays_deltas = fetch_notification_status_for_service_for_day(convert_utc_to_bst(now), service_id=service_id) statistics.add_monthly_notification_status_stats(data, todays_deltas) return jsonify(data=data)