@app.route('/reports/', defaults={'env': app.config['DEFAULT_ENVIRONMENT'], 'page': 1})
@app.route('/<env>/reports/', defaults={'page': 1})
@app.route('/<env>/reports/page/<int:page>')
def reports(env, page):
    """Displays a list of reports and status from all nodes, retreived using the
    reports endpoint, sorted by start_time.

    :param env: Search for all reports in this environment
    :type env: :obj:`string`
    :param page: Calculates the offset of the query based on the report count
        and this value
    :type page: :obj:`int`
    """
    envs = environments()
    check_env(env, envs)

    if env == '*':
        reports_query = None
        total_query = '["extract", [["function", "count"]], ["~", "certname", ""]]'
    else:
        reports_query = '["=", "environment", "{0}"]'.format(env)
        total_query = '["extract", [["function", "count"]],'\
            '["and", ["=", "environment", "{0}"]]]'.format(env)

    reports = get_or_abort(puppetdb.reports,
        query=reports_query,
        limit=app.config['REPORTS_COUNT'],
        offset=(page-1) * app.config['REPORTS_COUNT'],
        order_by='[{"field": "start_time", "order": "desc"}]')
    total = get_or_abort(puppetdb._query,
        'reports',
        query=total_query)
    total = total[0]['count']
    reports, reports_events = tee(reports)
    report_event_counts = {}

    if total == 0 and page != 1:
        abort(404)

    for report in reports_events:
        if env == '*':
            event_count_query = '["and",' \
                '["=", "certname", "{0}"],' \
                '["=", "report", "{1}"]]'.format(
                    report.node,
                    report.hash_)
        else:
            event_count_query = '["and",' \
                '["=", "environment", "{0}"],' \
                '["=", "certname", "{1}"],' \
                '["=", "report", "{2}"]]'.format(
                    env,
                    report.node,
                    report.hash_)
        counts = get_or_abort(puppetdb.event_counts,
            query=event_count_query,
            summarize_by="certname")
        try:
            report_event_counts[report.hash_] = counts[0]
        except __HOLE__:
            report_event_counts[report.hash_] = {}
    return Response(stream_with_context(stream_template(
        'reports.html',
        reports=yield_or_stop(reports),
        reports_count=app.config['REPORTS_COUNT'],
        report_event_counts=report_event_counts,
        pagination=Pagination(page, app.config['REPORTS_COUNT'], total),
        envs=envs,
        current_env=env)))
dataset/ETHPy150Open voxpupuli/puppetboard/puppetboard/app.py/reports