Ejemplo n.º 1
0
def api_get_scanner_stats():
    verbose = True if request.args.get('verbose',
                                       '').lower() == 'true' else False

    # Get the scanner statistics from the backend database, defaulting to the quick stats only
    stats = database.select_scan_scanner_statistics(verbose)

    # If a grade isn't in the database, return it with quantity 0
    grade_distribution = {
        grade: stats['grade_distribution'].get(grade, 0)
        for grade in GRADES
    }

    # Get the number of grade improvements
    grade_improvements_all = stats[
        'scan_score_difference_distribution_summation']

    # Make sure we only list the ones that are improvements, with a maximum of 5 letter grades
    grade_improvements = {k: 0 for k in range(0, 6)}
    for k, v in grade_improvements_all.items():
        grade_improvements[min(5, max(0, int(k / 20)))] += v

    return jsonify({
        'gradeDistribution': grade_distribution,
        'gradeImprovements': grade_improvements,
        'misc': {
            'mostRecentScanDate':
            stats['most_recent_scan_datetime'],
            'numHoursWithoutScansInLast24Hours':
            24 - len(stats['recent_scans']) if verbose else -1,
            'numImprovedSites':
            sum([v for k, v in grade_improvements_all.items() if k > 0]),
            'numScans':
            stats['scan_count'],
            'numScansLast24Hours':
            sum(stats['recent_scans'].values()) if verbose else -1,
            'numSuccessfulScans':
            sum(grade_distribution.values()),
            'numUniqueSites':
            sum(grade_improvements_all.values())
        },
        'recent': {
            'scans': {
                'best': database.select_scan_recent_finished_scans(
                    13, 90, 1000),  # 13, as there are 13 grades
                'recent': database.select_scan_recent_finished_scans(
                    13, 0, 1000),  # 13, as there are 13 grades
                'worst': database.select_scan_recent_finished_scans(
                    13, 0, 20),  # 13, as there are 13 grades
                'numPerHourLast24Hours': stats['recent_scans'],
            },
        },
        'states': stats['states'],
    })
Ejemplo n.º 2
0
def api_get_recent_scans():
    try:
        # Get the min and max scores, if they're there
        min_score = int(request.args.get('min-score', 0))
        max_score = int(request.args.get('max-score', 1000))

        min_score = max(0, min_score)
        max_score = min(1000, max_score)
    except ValueError:
        return {'error': 'invalid-parameters'}

    return jsonify(database.select_scan_recent_finished_scans(min_score=min_score, max_score=max_score))
Ejemplo n.º 3
0
def api_get_recent_scans():
    try:
        # Get the min and max scores, if they're there
        min_score = int(request.args.get('min-score', 0))
        max_score = int(request.args.get('max-score', 1000))

        min_score = max(0, min_score)
        max_score = min(1000, max_score)
    except ValueError:
        return {'error': 'invalid-parameters'}

    return jsonify(
        database.select_scan_recent_finished_scans(min_score=min_score,
                                                   max_score=max_score))
def api_get_scanner_stats():
    pretty = True if request.args.get('pretty',
                                      '').lower() == 'true' else False
    verbose = True if request.args.get('verbose',
                                       '').lower() == 'true' else False

    # Get the scanner statistics from the backend database, defaulting to the quick stats only
    stats = database.select_scan_scanner_statistics(verbose)

    # If a grade isn't in the database, return it with quantity 0
    grade_distribution = {
        grade: stats['grade_distribution'].get(grade, 0)
        for grade in GRADES
    }
    grade_distribution_all_scans = {
        grade: stats['grade_distribution_all_scans'].get(grade, 0)
        for grade in GRADES
    }

    # Get the number of grade improvements
    grade_improvements_all = stats[
        'scan_score_difference_distribution_summation']

    # Make sure we only list the ones that are improvements, with a maximum of 5 letter grades
    grade_improvements = {k: 0 for k in range(0, 6)}
    for k, v in grade_improvements_all.items():
        grade_improvements[min(5, max(0, int(k / 20)))] += v

    # Convert all the datetimes to HTTP strings
    stats['most_recent_scan_datetime'] = http_date(
        stats['most_recent_scan_datetime'].utctimetuple())
    stats['recent_scans'] = {
        http_date(i.utctimetuple()): v
        for i, v in stats['recent_scans']
    }

    resp = make_response(
        json.dumps(
            {
                'gradeDistribution': {
                    'latest': grade_distribution,
                    'all': grade_distribution_all_scans,
                },
                'gradeImprovements': grade_improvements,
                'misc': {
                    'mostRecentScanDate':
                    stats['most_recent_scan_datetime'],
                    'numHoursWithoutScansInLast24Hours':
                    24 - len(stats['recent_scans']) if verbose else -1,
                    'numImprovedSites':
                    sum([
                        v for k, v in grade_improvements_all.items() if k > 0
                    ]),
                    'numScans':
                    stats['scan_count'],
                    'numScansLast24Hours':
                    sum(stats['recent_scans'].values()) if verbose else -1,
                    'numSuccessfulScans':
                    sum(grade_distribution_all_scans.values()),
                    'numUniqueSites':
                    sum(grade_improvements_all.values())
                },
                'recent': {
                    'scans': {
                        'best':
                        database.select_scan_recent_finished_scans(
                            13, 90, 1000),  # 13, as there are 13 grades
                        'recent':
                        database.select_scan_recent_finished_scans(
                            13, 0, 1000),  # 13, as there are 13 grades
                        'worst':
                        database.select_scan_recent_finished_scans(
                            13, 0, 20),  # 13, as there are 13 grades
                        'numPerHourLast24Hours':
                        stats['recent_scans'],
                    },
                },
                'states':
                {state: stats['states'].get(state, 0)
                 for state in STATES},
            },
            indent=4 if pretty else None,
            sort_keys=pretty,
            default=str))

    resp.mimetype = 'application/json'

    return resp