ensure stats returned for lifespan of service

even if they've never sent a notification for realsies
This commit is contained in:
Leo Hemsted
2016-07-28 15:24:21 +01:00
parent 8ad47481d7
commit e5b0d568fa
4 changed files with 46 additions and 9 deletions

View File

@@ -43,6 +43,7 @@ from app.errors import (
register_errors,
InvalidRequest
)
from app.service import statistics
service = Blueprint('service', __name__)
register_errors(service)
@@ -239,8 +240,9 @@ def get_all_notifications_for_service(service_id):
@service.route('/<uuid:service_id>/notifications/weekly', methods=['GET'])
def get_weekly_notification_stats(service_id):
service = dao_fetch_service_by_id(service_id)
statistics = dao_fetch_weekly_historical_stats_for_service(service_id, created_at, preceeding_monday)
return jsonify(data=statistics.format_weekly_notification_stats(statistics))
stats = dao_fetch_weekly_historical_stats_for_service(service_id)
stats = statistics.format_weekly_notification_stats(stats, service.created_at)
return jsonify(data={week.date().isoformat(): statistics for week, statistics in stats.items()})
def get_detailed_service(service_id, today_only=False):

View File

@@ -16,10 +16,12 @@ def format_statistics(statistics):
def format_weekly_notification_stats(statistics, service_created_at):
preceeding_monday = service_created_at - timedelta(days=service_created_at.weekday())
preceeding_monday = (service_created_at - timedelta(days=service_created_at.weekday()))
# turn a datetime into midnight that day http://stackoverflow.com/a/1937636
preceeding_monday_midnight = datetime.combine(preceeding_monday.date(), datetime.min.time())
week_dict = {
week: _create_zeroed_stats_dicts()
for week in _weeks_for_range(preceeding_monday, datetime.utcnow())
for week in _weeks_for_range(preceeding_monday_midnight, datetime.utcnow())
}
for row in statistics:
_update_statuses_from_row(week_dict[row.week_start][row.notification_type], row)