def get_dashboard_partials(service_id):
    # all but scheduled and cancelled
    statuses_to_display = job_api_client.JOB_STATUSES - {
        'scheduled', 'cancelled'
    }

    template_statistics = aggregate_usage(
        template_statistics_client.get_template_statistics_for_service(
            service_id, limit_days=7))

    scheduled_jobs = sorted(job_api_client.get_jobs(service_id,
                                                    statuses=['scheduled'
                                                              ])['data'],
                            key=lambda job: job['scheduled_for'])
    immediate_jobs = [
        add_rate_to_job(job) for job in job_api_client.get_jobs(
            service_id, limit_days=7, statuses=statuses_to_display)['data']
    ]
    service = service_api_client.get_detailed_service(service_id)

    return {
        'upcoming':
        render_template('views/dashboard/_upcoming.html',
                        scheduled_jobs=scheduled_jobs),
        'totals':
        render_template('views/dashboard/_totals.html',
                        service_id=service_id,
                        statistics=get_dashboard_totals(
                            service['data']['statistics'])),
        'template-statistics':
        render_template(
            'views/dashboard/template-statistics.html',
            template_statistics=template_statistics,
            most_used_template_count=max(
                [row['count'] for row in template_statistics] or [0]),
        ),
        'has_template_statistics':
        bool(template_statistics),
        'jobs':
        render_template('views/dashboard/_jobs.html', jobs=immediate_jobs),
        'has_jobs':
        bool(immediate_jobs),
        'usage':
        render_template(
            'views/dashboard/_usage.html',
            **calculate_usage(
                service_api_client.get_service_usage(service_id)['data'])),
    }
def get_dashboard_partials(service_id):
    # all but scheduled and cancelled
    statuses_to_display = job_api_client.JOB_STATUSES - {'scheduled', 'cancelled'}

    template_statistics = aggregate_usage(
        template_statistics_client.get_template_statistics_for_service(service_id, limit_days=7)
    )

    scheduled_jobs = sorted(
        job_api_client.get_jobs(service_id, statuses=['scheduled'])['data'],
        key=lambda job: job['scheduled_for']
    )
    immediate_jobs = [
        add_rate_to_job(job)
        for job in job_api_client.get_jobs(service_id, limit_days=7, statuses=statuses_to_display)['data']
    ]
    service = service_api_client.get_detailed_service(service_id)

    return {
        'upcoming': render_template(
            'views/dashboard/_upcoming.html',
            scheduled_jobs=scheduled_jobs
        ),
        'totals': render_template(
            'views/dashboard/_totals.html',
            service_id=service_id,
            statistics=get_dashboard_totals(service['data']['statistics'])
        ),
        'template-statistics': render_template(
            'views/dashboard/template-statistics.html',
            template_statistics=template_statistics,
            most_used_template_count=max(
                [row['count'] for row in template_statistics] or [0]
            ),
        ),
        'has_template_statistics': bool(template_statistics),
        'jobs': render_template(
            'views/dashboard/_jobs.html',
            jobs=immediate_jobs
        ),
        'has_jobs': bool(immediate_jobs),
        'usage': render_template(
            'views/dashboard/_usage.html',
            **calculate_usage(service_api_client.get_service_usage(service_id)['data'])
        ),
    }
Example #3
0
def get_dashboard_partials(service_id):

    template_statistics = aggregate_usage(
        template_statistics_client.get_template_statistics_for_service(service_id, limit_days=7)
    )

    jobs = add_rate_to_jobs(filter(
        lambda job: job['original_file_name'] != current_app.config['TEST_MESSAGE_FILENAME'],
        job_api_client.get_job(service_id, limit_days=7)['data']
    ))
    service = service_api_client.get_detailed_service(service_id)

    return {
        'totals': render_template(
            'views/dashboard/_totals.html',
            service_id=service_id,
            statistics=get_dashboard_totals(service['data']['statistics'])
        ),
        'template-statistics': render_template(
            'views/dashboard/template-statistics.html',
            template_statistics=template_statistics,
            most_used_template_count=max(
                [row['usage_count'] for row in template_statistics] or [0]
            ),
        ),
        'has_template_statistics': bool(template_statistics),
        'jobs': render_template(
            'views/dashboard/_jobs.html',
            jobs=jobs
        ),
        'has_jobs': bool(jobs),
        'usage': render_template(
            'views/dashboard/_usage.html',
            **calculate_usage(service_api_client.get_service_usage(service_id)['data'])
        ),
    }
def get_dashboard_partials(service_id):
    # all but scheduled and cancelled
    statuses_to_display = job_api_client.JOB_STATUSES - {
        'scheduled', 'cancelled'
    }

    template_statistics = aggregate_usage(
        template_statistics_client.get_template_statistics_for_service(
            service_id, limit_days=7))

    scheduled_jobs = sorted(job_api_client.get_jobs(service_id,
                                                    statuses=['scheduled'
                                                              ])['data'],
                            key=lambda job: job['scheduled_for'])
    immediate_jobs = [
        add_rate_to_job(job) for job in job_api_client.get_jobs(
            service_id, limit_days=7, statuses=statuses_to_display)['data']
    ]
    service = service_api_client.get_detailed_service(service_id)
    column_width, max_notifiction_count = get_column_properties(
        number_of_columns=(
            3 if 'letter' in current_service['permissions'] else 2))
    dashboard_totals = get_dashboard_totals(service['data']['statistics']),
    highest_notification_count = max(
        sum(value[key] for key in {'requested', 'failed', 'delivered'})
        for key, value in dashboard_totals[0].items())

    return {
        'upcoming':
        render_template('views/dashboard/_upcoming.html',
                        scheduled_jobs=scheduled_jobs),
        'inbox':
        render_template(
            'views/dashboard/_inbox.html',
            inbound_sms_summary=(
                service_api_client.get_inbound_sms_summary(service_id)
                if 'inbound_sms' in current_service['permissions'] else None),
        ),
        'totals':
        render_template(
            'views/dashboard/_totals.html',
            service_id=service_id,
            statistics=dashboard_totals[0],
            column_width=column_width,
            smaller_font_size=(highest_notification_count >
                               max_notifiction_count),
        ),
        'template-statistics':
        render_template(
            'views/dashboard/template-statistics.html',
            template_statistics=template_statistics,
            most_used_template_count=max(
                [row['count'] for row in template_statistics] or [0]),
        ),
        'has_template_statistics':
        bool(template_statistics),
        'jobs':
        render_template('views/dashboard/_jobs.html', jobs=immediate_jobs),
        'has_jobs':
        bool(immediate_jobs)
    }
Example #5
0
def get_notifications(service_id, message_type, status_override=None):
    # TODO get the api to return count of pages as well.
    page = get_page_from_request()
    if page is None:
        abort(
            404, "Invalid page argument ({}) reverting to page 1.".format(
                request.args['page'], None))
    if message_type not in ['email', 'sms', 'letter']:
        abort(404)
    filter_args = parse_filter_args(request.args)
    filter_args['status'] = set_status_filters(filter_args)
    if request.path.endswith('csv'):
        return Response(generate_notifications_csv(
            service_id=service_id,
            page=page,
            page_size=5000,
            template_type=[message_type],
            status=filter_args.get('status'),
            limit_days=current_app.config['ACTIVITY_STATS_LIMIT_DAYS']),
                        mimetype='text/csv',
                        headers={
                            'Content-Disposition':
                            'inline; filename="notifications.csv"'
                        })
    notifications = notification_api_client.get_notifications_for_service(
        service_id=service_id,
        page=page,
        template_type=[message_type],
        status=filter_args.get('status'),
        limit_days=current_app.config['ACTIVITY_STATS_LIMIT_DAYS'],
        to=request.form.get('to', ''),
    )
    url_args = {
        'message_type': message_type,
        'status': request.args.get('status')
    }
    prev_page = None

    if 'links' in notifications and notifications['links'].get('prev', None):
        prev_page = generate_previous_dict('main.view_notifications',
                                           service_id,
                                           page,
                                           url_args=url_args)
    next_page = None

    if 'links' in notifications and notifications['links'].get('next', None):
        next_page = generate_next_dict('main.view_notifications', service_id,
                                       page, url_args)

    return {
        'counts':
        render_template('views/activity/counts.html',
                        status=request.args.get('status'),
                        status_filters=get_status_filters(
                            current_service, message_type,
                            service_api_client.get_detailed_service(service_id)
                            ['data']['statistics'])),
        'notifications':
        render_template('views/activity/notifications.html',
                        notifications=list(
                            add_preview_of_content_to_notifications(
                                notifications['notifications'])),
                        page=page,
                        prev_page=prev_page,
                        next_page=next_page,
                        status=request.args.get('status'),
                        message_type=message_type,
                        download_link=url_for(
                            '.view_notifications_csv',
                            service_id=current_service['id'],
                            message_type=message_type,
                            status=request.args.get('status'))),
    }
Example #6
0
def view_notifications(service_id, message_type):
    # TODO get the api to return count of pages as well.
    page = get_page_from_request()
    if page is None:
        abort(404, "Invalid page argument ({}) reverting to page 1.".format(request.args['page'], None))
    if message_type not in ['email', 'sms']:
        abort(404)

    filter_args = _parse_filter_args(request.args)
    filter_args['status'] = _set_status_filters(filter_args)

    notifications = notification_api_client.get_notifications_for_service(
        service_id=service_id,
        page=page,
        template_type=[message_type],
        status=filter_args.get('status'),
        limit_days=current_app.config['ACTIVITY_STATS_LIMIT_DAYS'])
    view_dict = dict(
        message_type=message_type,
        status=request.args.get('status')
    )
    prev_page = None
    if notifications['links'].get('prev', None):
        prev_page = generate_previous_next_dict(
            'main.view_notifications',
            service_id,
            view_dict,
            page - 1,
            'Previous page',
            'page {}'.format(page - 1))
    next_page = None
    if notifications['links'].get('next', None):
        next_page = generate_previous_next_dict(
            'main.view_notifications',
            service_id,
            view_dict,
            page + 1,
            'Next page',
            'page {}'.format(page + 1))
    if request.path.endswith('csv'):
        csv_content = generate_notifications_csv(
            notification_api_client.get_notifications_for_service(
                service_id=service_id,
                page=page,
                page_size=notifications['total'],
                template_type=[message_type],
                status=filter_args.get('status'),
                limit_days=current_app.config['ACTIVITY_STATS_LIMIT_DAYS'])['notifications'])
        return csv_content, 200, {
            'Content-Type': 'text/csv; charset=utf-8',
            'Content-Disposition': 'inline; filename="notifications.csv"'
        }
    return render_template(
        'views/notifications.html',
        notifications=notifications['notifications'],
        page=page,
        prev_page=prev_page,
        next_page=next_page,
        status=request.args.get('status'),
        message_type=message_type,
        download_link=url_for(
            '.view_notifications_csv',
            service_id=current_service['id'],
            message_type=message_type,
            status=request.args.get('status')
        ),
        status_filters=get_status_filters(
            current_service,
            message_type,
            service_api_client.get_detailed_service(service_id)['data']['statistics']
        )
    )
Example #7
0
def get_notifications(service_id, message_type, status_override=None):
    # TODO get the api to return count of pages as well.
    page = get_page_from_request()
    if page is None:
        abort(404, "Invalid page argument ({}) reverting to page 1.".format(request.args["page"], None))
    if message_type not in ["email", "sms"]:
        abort(404)

    filter_args = _parse_filter_args(request.args)
    filter_args["status"] = _set_status_filters(filter_args)

    notifications = notification_api_client.get_notifications_for_service(
        service_id=service_id,
        page=page,
        template_type=[message_type],
        status=filter_args.get("status"),
        limit_days=current_app.config["ACTIVITY_STATS_LIMIT_DAYS"],
    )

    url_args = {"message_type": message_type, "status": request.args.get("status")}
    prev_page = None
    if notifications["links"].get("prev", None):
        prev_page = generate_previous_dict("main.view_notifications", service_id, page, url_args=url_args)
    next_page = None
    if notifications["links"].get("next", None):
        next_page = generate_next_dict("main.view_notifications", service_id, page, url_args)

    if request.path.endswith("csv"):
        csv_content = generate_notifications_csv(
            notification_api_client.get_notifications_for_service(
                service_id=service_id,
                page=page,
                page_size=notifications["total"],
                template_type=[message_type],
                status=filter_args.get("status"),
                limit_days=current_app.config["ACTIVITY_STATS_LIMIT_DAYS"],
            )["notifications"]
        )
        return (
            csv_content,
            200,
            {"Content-Type": "text/csv; charset=utf-8", "Content-Disposition": 'inline; filename="notifications.csv"'},
        )
    return {
        "counts": render_template(
            "views/activity/counts.html",
            status=request.args.get("status"),
            status_filters=get_status_filters(
                current_service, message_type, service_api_client.get_detailed_service(service_id)["data"]["statistics"]
            ),
        ),
        "notifications": render_template(
            "views/activity/notifications.html",
            notifications=notifications["notifications"],
            page=page,
            prev_page=prev_page,
            next_page=next_page,
            status=request.args.get("status"),
            message_type=message_type,
            download_link=url_for(
                ".view_notifications_csv",
                service_id=current_service["id"],
                message_type=message_type,
                status=request.args.get("status"),
            ),
        ),
    }