def endpoint_graph():
    """
    Creates a graph with the execution times per endpoint
    :return:
    """
    with session_scope() as db_session:
        data = get_endpoint_data_grouped(db_session, lambda x: simplify(x, 10))
        values = [
            boxplot(get_value(data, end.id, default=[]), name=end.name)
            for end in get_endpoints(db_session)
        ]

    layout = get_layout(height=350 + 40 * len(values),
                        xaxis={'title': 'Execution time (ms)'},
                        margin=get_margin())
    return get_figure(layout, values)
def version_ip_graph(db_session, endpoint_id, form):
    """
    :param db_session: session for the database
    :param endpoint_id: the endpoint to filter the data on
    :param form: form for reducing the size of the graph
    :return: an HTML bubble plot
    """
    users = get_ips(db_session, endpoint_id, form.get_slider_value(0))
    versions = get_versions(db_session, endpoint_id, form.get_slider_value(1))

    first_request = get_first_requests(db_session, endpoint_id)
    values = get_two_columns_grouped(db_session, Request.ip,
                                     Request.endpoint_id == endpoint_id)
    data = [[get_value(values, (user, v)) for v in versions] for user in users]

    average = get_average_bubble_size(data)
    trace = [
        scatter(x=[
            format_version(v, get_value(first_request, v)) for v in versions
        ],
                hovertext=[
                    'Time: {:,.1f}ms'.format(data[i][j])
                    for j in range(len(versions))
                ],
                y=[users[i]] * len(versions),
                name=users[i],
                mode='markers',
                marker={
                    'color': [get_color(users[i])] * len(versions),
                    'size': [math.sqrt(d) for d in data[i]],
                    'sizeref': average,
                    'sizemode': 'area'
                }) for i in range(len(users))
    ]

    layout = get_layout(height=350 + 40 * len(trace),
                        xaxis={
                            'title': 'Versions',
                            'type': 'category'
                        },
                        yaxis={
                            'title': 'IP-addresses',
                            'type': 'category',
                            'autorange': 'reversed'
                        },
                        margin=get_margin(b=200))
    return get_figure(layout, trace)
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])
def versions_graph(db_session, endpoint_id, form):
    times = get_version_data_grouped(db_session, lambda x: simplify(x, 10),
                                     Request.endpoint_id == endpoint_id)
    first_requests = get_first_requests(db_session, endpoint_id,
                                        form.get_slider_value())
    data = [
        boxplot(name=format_version(
            request.version_requested,
            get_value(first_requests, request.version_requested)),
                values=get_value(times, request.version_requested),
                marker={'color': get_color(request.version_requested)})
        for request in first_requests
    ]

    layout = get_layout(height=350 + 40 * len(first_requests),
                        xaxis={'title': 'Execution time (ms)'},
                        yaxis={
                            'type': 'category',
                            'title': 'Version',
                            'autorange': 'reversed'
                        },
                        margin=get_margin())
    return get_figure(layout=layout, data=data)