|
| 1 | +import datetime |
| 2 | + |
| 3 | +import plotly |
| 4 | +import plotly.graph_objs as go |
| 5 | +import pygal |
| 6 | +from flask import session, render_template |
| 7 | + |
| 8 | +from dashboard import blueprint, config |
| 9 | +from dashboard.database.endpoint import get_last_accessed_times, get_num_requests |
| 10 | +from dashboard.database.function_calls import get_times, get_reqs_endpoint_day, get_versions, get_data_per_version, \ |
| 11 | + get_endpoints, get_data_per_endpoint |
| 12 | +from dashboard.security import secure |
| 13 | + |
| 14 | + |
| 15 | +@blueprint.route('/measurements/<int:index>') |
| 16 | +@secure |
| 17 | +def measurements(index): |
| 18 | + """ |
| 19 | + Returns a page with one of the 4 graphs below: |
| 20 | + TODO: rewrite graph at index==1 to plotly, such that function can be simplified |
| 21 | + :param index: |
| 22 | + :return: |
| 23 | + """ |
| 24 | + |
| 25 | + # returns a page with the number of requests per endpoint |
| 26 | + if index == 1: |
| 27 | + page = 'measurements/measurements_pygal.html' |
| 28 | + graph = get_stacked_bar() |
| 29 | + |
| 30 | + # returns a page with the execution times per version |
| 31 | + elif index == 2: |
| 32 | + page = 'measurements/measurements_plotly.html' |
| 33 | + graph = get_boxplot_per_version() |
| 34 | + |
| 35 | + # returns a page with the execution time per endpoint |
| 36 | + elif index == 3: |
| 37 | + page = 'measurements/measurements_plotly.html' |
| 38 | + graph = get_boxplot_per_endpoint() |
| 39 | + |
| 40 | + # default: return a page with a heatmap of number of requests |
| 41 | + else: |
| 42 | + index = 0 |
| 43 | + page = 'measurements/measurements_plotly.html' |
| 44 | + graph = get_heatmap(end=None) |
| 45 | + |
| 46 | + return render_template(page, link=config.link, curr=2, times=get_times(), |
| 47 | + access=get_last_accessed_times(), session=session, index=index, graph=graph) |
| 48 | + |
| 49 | + |
| 50 | +def get_stacked_bar(): |
| 51 | + data = get_reqs_endpoint_day() |
| 52 | + graph = pygal.graph.horizontalstackedbar.HorizontalStackedBar(height=100 + len(data) * 7) |
| 53 | + graph.title = 'Number of requests per endpoint per day' |
| 54 | + graph.x_labels = [] |
| 55 | + endpoints = [] |
| 56 | + for d in data: |
| 57 | + if d.newTime not in graph.x_labels: |
| 58 | + graph.x_labels.append(d.newTime) |
| 59 | + if d.endpoint not in endpoints: |
| 60 | + endpoints.append(d.endpoint) |
| 61 | + |
| 62 | + graph.x_labels.sort(reverse=True) |
| 63 | + for e in endpoints: |
| 64 | + lst = [] |
| 65 | + for t in graph.x_labels: |
| 66 | + found = False |
| 67 | + for d in data: |
| 68 | + if e == d.endpoint and t == d.newTime: |
| 69 | + found = True |
| 70 | + lst.append(d.cnt) |
| 71 | + if not found: |
| 72 | + lst.append(0) |
| 73 | + graph.add(e, lst) |
| 74 | + |
| 75 | + return graph.render_data_uri() |
| 76 | + |
| 77 | + |
| 78 | +def get_boxplot_per_version(): |
| 79 | + """ |
| 80 | + Creates a graph with the execution times per version |
| 81 | + :return: |
| 82 | + """ |
| 83 | + versions = get_versions() |
| 84 | + |
| 85 | + data = [] |
| 86 | + for v in versions: |
| 87 | + values = [c.execution_time for c in get_data_per_version(v.version)] |
| 88 | + data.append(go.Box(x=values, name="{0} {1}".format(v.version, v.startedUsingOn.strftime("%b %d %H:%M")))) |
| 89 | + |
| 90 | + layout = go.Layout( |
| 91 | + autosize=False, |
| 92 | + width=900, |
| 93 | + height=350 + 40 * len(versions), |
| 94 | + plot_bgcolor='rgba(249,249,249,1)', |
| 95 | + showlegend=False, |
| 96 | + title='Execution time for every version', |
| 97 | + xaxis=dict(title='Execution time (ms)'), |
| 98 | + yaxis=dict(tickangle=-50, autorange='reversed') |
| 99 | + ) |
| 100 | + return plotly.offline.plot(go.Figure(data=data, layout=layout), output_type='div', show_link=False) |
| 101 | + |
| 102 | + |
| 103 | +def get_boxplot_per_endpoint(): |
| 104 | + """ |
| 105 | + Creates a graph with the execution times per endpoint |
| 106 | + :return: |
| 107 | + """ |
| 108 | + endpoints = [str(e.endpoint) for e in get_endpoints()] |
| 109 | + |
| 110 | + data = [] |
| 111 | + for e in endpoints: |
| 112 | + values = [c.execution_time for c in get_data_per_endpoint(e)] |
| 113 | + if len(e) > 16: |
| 114 | + e = '...' + e[-14:] |
| 115 | + data.append(go.Box(x=values, name=e)) |
| 116 | + |
| 117 | + layout = go.Layout( |
| 118 | + autosize=False, |
| 119 | + width=900, |
| 120 | + height=350 + 40 * len(endpoints), |
| 121 | + plot_bgcolor='rgba(249,249,249,1)', |
| 122 | + showlegend=False, |
| 123 | + title='Execution time for every endpoint', |
| 124 | + xaxis=dict(title='Execution time (ms)'), |
| 125 | + yaxis=dict(tickangle=-45) |
| 126 | + ) |
| 127 | + return plotly.offline.plot(go.Figure(data=data, layout=layout), output_type='div', show_link=False) |
| 128 | + |
| 129 | + |
| 130 | +def get_heatmap(end): |
| 131 | + # list of hours: 1:00 - 23:00 |
| 132 | + hours = ['0' + str(hour) + ':00' for hour in range(0, 10)] + \ |
| 133 | + [str(hour) + ':00' for hour in range(10, 24)] |
| 134 | + |
| 135 | + data = get_num_requests(end) |
| 136 | + # list of days (format: year-month-day) |
| 137 | + days = [str(d.newTime[:10]) for d in data] |
| 138 | + # remove duplicates and sort the result |
| 139 | + days = sorted(list(set(days))) |
| 140 | + if len(days) > 0: |
| 141 | + first_day = max(datetime.datetime.strptime(days[0], '%Y-%m-%d'), |
| 142 | + datetime.datetime.now() - datetime.timedelta(days=30)) |
| 143 | + last_day = datetime.datetime.strptime(days[len(days)-1], '%Y-%m-%d') |
| 144 | + else: |
| 145 | + first_day = datetime.datetime.now() - datetime.timedelta(days=30) |
| 146 | + last_day = datetime.datetime.now() |
| 147 | + |
| 148 | + # create empty 2D-dictionary with the keys: [hour][day] |
| 149 | + requests = {} |
| 150 | + for hour in hours: |
| 151 | + requests_day = {} |
| 152 | + for day in days: |
| 153 | + requests_day[day] = 0 |
| 154 | + requests[hour] = requests_day |
| 155 | + |
| 156 | + # add data to the dictionary |
| 157 | + for d in data: |
| 158 | + day = str(d.newTime[:10]) |
| 159 | + hour = str(d.newTime[11:16]) |
| 160 | + requests[hour][day] = d.count |
| 161 | + |
| 162 | + # create a 2D-list out of the dictionary |
| 163 | + requests_list = [] |
| 164 | + for hour in hours: |
| 165 | + day_list = [] |
| 166 | + for day in days: |
| 167 | + day_list.append(requests[hour][day]) |
| 168 | + requests_list.append(day_list) |
| 169 | + |
| 170 | + layout = go.Layout( |
| 171 | + autosize=False, |
| 172 | + width=900, |
| 173 | + height=800, |
| 174 | + plot_bgcolor='rgba(249,249,249,1)', |
| 175 | + showlegend=False, |
| 176 | + title='Heatmap of number of requests', |
| 177 | + xaxis=go.XAxis(range=[first_day, last_day], |
| 178 | + title='Date'), |
| 179 | + yaxis=dict(title='Time', autorange='reversed') |
| 180 | + ) |
| 181 | + |
| 182 | + trace = go.Heatmap(z=requests_list, x=days, y=hours) |
| 183 | + return plotly.offline.plot(go.Figure(data=[trace], layout=layout), output_type='div', show_link=False) |
0 commit comments