Exemplo n.º 1
0
def get_endpoint_overview(db_session):
    """
    :param db_session: session for the database
    :return: A list of properties for each endpoint that is found in the database
    """
    week_ago = datetime.datetime.utcnow() - datetime.timedelta(days=7)
    now_local = to_local_datetime(datetime.datetime.utcnow())
    today_local = now_local.replace(hour=0, minute=0, second=0, microsecond=0)
    today_utc = to_utc_datetime(today_local)

    hits_today = count_requests_group(db_session, Request.time_requested > today_utc)
    hits_week = count_requests_group(db_session, Request.time_requested > week_ago)
    hits = count_requests_group(db_session)

    median_today = get_endpoint_data_grouped(db_session, median, Request.time_requested > today_utc)
    median_week = get_endpoint_data_grouped(db_session, median, Request.time_requested > week_ago)
    median_overall = get_endpoint_data_grouped(db_session, median)
    access_times = get_last_requested(db_session)

    return [{
        'id': endpoint.id,
        'name': endpoint.name,
        'monitor': endpoint.monitor_level,
        'color': get_color(endpoint.name),
        'hits-today': get_value(hits_today, endpoint.id),
        'hits-week': get_value(hits_week, endpoint.id),
        'hits-overall': get_value(hits, endpoint.id),
        'median-today': get_value(median_today, endpoint.id),
        'median-week': get_value(median_week, endpoint.id),
        'median-overall': get_value(median_overall, endpoint.id),
        'last-accessed': get_value(access_times, endpoint.name, default=None)
    } for endpoint in get_endpoints(db_session)]
def overview():
    week_ago = datetime.datetime.utcnow() - datetime.timedelta(days=7)
    now_local = to_local_datetime(datetime.datetime.utcnow())
    today_local = now_local.replace(hour=0, minute=0, second=0, microsecond=0)
    today_utc = to_utc_datetime(today_local)

    result = []
    with session_scope() as db_session:
        from numpy import median

        hits_today = count_requests_group(db_session,
                                          Request.time_requested > today_utc)
        hits_week = count_requests_group(db_session,
                                         Request.time_requested > week_ago)
        hits = count_requests_group(db_session)

        median_today = get_endpoint_data_grouped(
            db_session, median, Request.time_requested > today_utc)
        median_week = get_endpoint_data_grouped(
            db_session, median, Request.time_requested > week_ago)
        median = get_endpoint_data_grouped(db_session, median)
        access_times = get_last_requested(db_session)

        for endpoint in get_endpoints(db_session):
            result.append({
                'id':
                endpoint.id,
                'name':
                endpoint.name,
                'color':
                get_color(endpoint.name),
                'hits-today':
                get_value(hits_today, endpoint.id),
                'hits-week':
                get_value(hits_week, endpoint.id),
                'hits-overall':
                get_value(hits, endpoint.id),
                'median-today':
                get_value(median_today, endpoint.id),
                'median-week':
                get_value(median_week, endpoint.id),
                'median-overall':
                get_value(median, endpoint.id),
                'last-accessed':
                get_value(access_times, endpoint.name, default=None)
            })
        version = get_details(db_session)['dashboard-version']
    return render_template('fmd_dashboard/overview.html',
                           result=result,
                           is_admin=is_admin(),
                           title='Dashboard Overview',
                           version=version)
Exemplo n.º 3
0
def get_endpoint_overview(session):
    """
    :param session: session for the database
    :return: A list of properties for each endpoint that is found in the database
    """
    week_ago = datetime.datetime.utcnow() - datetime.timedelta(days=7)
    now_local = to_local_datetime(datetime.datetime.utcnow())
    today_local = now_local.replace(hour=0, minute=0, second=0, microsecond=0)
    today_utc = to_utc_datetime(today_local)

    # First flush last requested info to db
    cache.flush_cache()
    error_hits_criterion = and_(Request.status_code >= 400, Request.status_code < 600)

    hits_today = count_requests_group(session, Request.time_requested > today_utc)
    hits_today_errors = count_requests_group(
        session, and_(Request.time_requested > today_utc, error_hits_criterion)
    )

    hits_week = count_requests_group(session, Request.time_requested > week_ago)
    hits_week_errors = count_requests_group(
        session, and_(Request.time_requested > week_ago, error_hits_criterion)
    )

    hits = count_requests_group(session)

    median_today = get_endpoint_data_grouped(session, median, Request.time_requested > today_utc)
    median_week = get_endpoint_data_grouped(session, median, Request.time_requested > week_ago)
    median_overall = get_endpoint_data_grouped(session, median)
    access_times = get_last_requested(session)

    return [
        {
            'id': endpoint.id,
            'name': endpoint.name,
            'monitor': endpoint.monitor_level,
            'color': get_color(endpoint.name),
            'hits-today': get_value(hits_today, endpoint.id),
            'hits-today-errors': get_value(hits_today_errors, endpoint.id),
            'hits-week': get_value(hits_week, endpoint.id),
            'hits-week-errors': get_value(hits_week_errors, endpoint.id),
            'hits-overall': get_value(hits, endpoint.id),
            'median-today': get_value(median_today, endpoint.id),
            'median-week': get_value(median_week, endpoint.id),
            'median-overall': get_value(median_overall, endpoint.id),
            'last-accessed': get_value(access_times, endpoint.name, default=None),
        }
        for endpoint in get_endpoints(session)
    ]
Exemplo n.º 4
0
def get_multi_version_data(session, endpoints, versions):
    """
    :param session: session for the database
    :param endpoints: a list of all endpoints for which the data must be
        collected (represented by their name)
    :param versions: a list of versions
    :return: a 2d list of data
    """
    endpoints = [get_endpoint_by_name(session, name) for name in endpoints]
    requests = [
        count_requests_group(session, Request.version_requested == v)
        for v in versions
    ]

    total_hits = numpy.zeros(len(versions))
    hits = numpy.zeros((len(endpoints), len(versions)))

    for i, _ in enumerate(versions):
        total_hits[i] = max(1, sum([value for key, value in requests[i]]))

    for j, _ in enumerate(endpoints):
        for i, _ in enumerate(versions):
            hits[j][i] = get_value(requests[i],
                                   endpoints[j].id) * 100 / total_hits[i]
    return hits.tolist()
def version_usage_graph(db_session, form):
    """
    Used for getting a Heatmap with an overview of which endpoints are used in which versions
    :param db_session: session for the database
    :param form: instance of SliderForm
    :return:
    """
    endpoints = get_endpoints(db_session)
    versions = get_versions(db_session, limit=form.get_slider_value())

    requests = [
        count_requests_group(db_session, Request.version_requested == v)
        for v in versions
    ]
    total_hits = []
    hits = [[]] * len(endpoints)

    for hits_version in requests:
        total_hits.append(max(1, sum([value for key, value in hits_version])))

    for j in range(len(endpoints)):
        hits[j] = [0] * len(versions)
        for i in range(len(versions)):
            hits[j][i] = get_value(requests[i],
                                   endpoints[j].id) * 100 / total_hits[i]

    layout = get_layout(xaxis={
        'title': 'Versions',
        'type': 'category'
    },
                        yaxis={
                            'type': 'category',
                            'autorange': 'reversed'
                        },
                        margin=get_margin())

    trace = heatmap(z=hits,
                    x=versions,
                    y=['{} '.format(e.name) for e in endpoints],
                    colorbar={
                        'titleside': 'top',
                        'tickmode': 'array',
                    })
    return get_figure(layout=layout, data=[trace])
Exemplo n.º 6
0
 def test_count_requests_group(self):
     with session_scope() as db_session:
         self.assertEqual(count_requests_group(db_session), [(1, 5)])
Exemplo n.º 7
0
def test_count_requests_group(session, request_1, endpoint):
    assert count_requests_group(
        session, Request.version_requested == request_1.version_requested) == [
            (endpoint.id, 1)
        ]