Skip to content

Commit

Permalink
updated setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingBird95 committed Mar 29, 2018
2 parents 90e58ae + e1b2607 commit 108c64b
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Unreleased
----------
Changed

- Added new graph: Version usage

- Added column (Hits in past 7 days) in Measurements Overview

- Fixed bug with configuration

- Changed rows and column in outlier-table
Expand Down
38 changes: 37 additions & 1 deletion flask_monitoringdashboard/database/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ def get_times():
return result


def get_hits(date_from=None):
""" Return all entries of measurements with the number of execution times which are called after 'date_from'
:param date_from: A datetime-object
"""
with session_scope() as db_session:
result = db_session.query(FunctionCall.endpoint,
func.count(FunctionCall.execution_time).label('count')). \
filter(FunctionCall.time > date_from).group_by(FunctionCall.endpoint).all()
db_session.expunge_all()
return result


def get_average(date_from=None):
""" Return the average of execution times which are called after 'date_from' grouped per endpoint
:param date_from: A datetime-object
"""
with session_scope() as db_session:
result = db_session.query(FunctionCall.endpoint,
func.avg(FunctionCall.execution_time).label('average')). \
filter(FunctionCall.time > date_from).group_by(FunctionCall.endpoint).all()
db_session.expunge_all()
return result


def get_data_between(time_from, time_to):
"""
Returns all data in the FunctionCall table, for the export data option.
Expand All @@ -67,14 +91,26 @@ def get_data():


def get_data_per_version(version):
""" Returns all data in the FuctionCall table, grouped by their version. """
""" Returns all data in the FunctionCall table, grouped by their version. """
with session_scope() as db_session:
result = db_session.query(FunctionCall.execution_time, FunctionCall.version). \
filter(FunctionCall.version == version).all()
db_session.expunge_all()
return result


def get_hits_per_version(version):
""" Returns the hits per endpoint per version """
with session_scope() as db_session:
result = db_session.query(FunctionCall.endpoint,
FunctionCall.version,
func.count(FunctionCall.endpoint).label('count')). \
filter(FunctionCall.version == version). \
group_by(FunctionCall.endpoint).all()
db_session.expunge_all()
return result


def get_versions(end=None):
with session_scope() as db_session:
result = db_session.query(FunctionCall.version,
Expand Down
3 changes: 1 addition & 2 deletions flask_monitoringdashboard/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def get_session_id():
# implement here your own custom function
return '12345'

dashboard.config.outlier_detection_constant = 0

dashboard.config.version = 'test-version'
dashboard.bind(app=app)

@app.route('/')
Expand Down
87 changes: 82 additions & 5 deletions flask_monitoringdashboard/routings/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from flask_monitoringdashboard.colors import get_color
from flask_monitoringdashboard.database.endpoint import get_last_accessed_times, get_num_requests
from flask_monitoringdashboard.database.function_calls import get_times, get_reqs_endpoint_day, get_versions, \
get_data_per_version, get_endpoints, get_data_per_endpoint
get_data_per_version, get_endpoints, get_data_per_endpoint, get_hits_per_version, get_hits, get_average
from flask_monitoringdashboard.security import secure, is_admin


Expand All @@ -18,9 +18,14 @@ def overview():
colors = {}
for result in get_times():
colors[result.endpoint] = get_color(result.endpoint)

week_ago = datetime.datetime.now() - datetime.timedelta(days=7)
today = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)

return render_template('dashboard/measurement-overview.html', link=config.link, curr=2, times=get_times(),
colors=colors, access=get_last_accessed_times(), session=session, index=0,
is_admin=is_admin())
is_admin=is_admin(), hits_last_days=get_hits(week_ago), hits_today=get_hits(today),
average_last_days=get_average(week_ago), average_today=get_average(today))


@blueprint.route('/measurements/heatmap')
Expand All @@ -33,13 +38,20 @@ def heatmap():
graph=get_heatmap(end=None))


@blueprint.route('/measurements/version_usage')
@secure
def version_usage():
return render_template('dashboard/measurement.html', link=config.link, curr=2, session=session, index=2,
graph=get_version_usage())


@blueprint.route('/measurements/requests')
@secure
def page_number_of_requests_per_endpoint():
colors = {}
for result in get_times():
colors[result.endpoint] = get_color(result.endpoint)
return render_template('dashboard/measurement.html', link=config.link, curr=2, session=session, index=2,
return render_template('dashboard/measurement.html', link=config.link, curr=2, session=session, index=3,
graph=get_stacked_bar())


Expand All @@ -49,7 +61,7 @@ def page_boxplot_per_version():
colors = {}
for result in get_times():
colors[result.endpoint] = get_color(result.endpoint)
return render_template('dashboard/measurement.html', link=config.link, curr=2, session=session, index=3,
return render_template('dashboard/measurement.html', link=config.link, curr=2, session=session, index=4,
graph=get_boxplot_per_version())


Expand All @@ -59,7 +71,7 @@ def page_boxplot_per_endpoint():
colors = {}
for result in get_times():
colors[result.endpoint] = get_color(result.endpoint)
return render_template('dashboard/measurement.html', link=config.link, curr=2, session=session, index=4,
return render_template('dashboard/measurement.html', link=config.link, curr=2, session=session, index=5,
graph=get_boxplot_per_endpoint())


Expand Down Expand Up @@ -194,6 +206,71 @@ def get_boxplot_per_endpoint():
return plotly.offline.plot(go.Figure(data=data, layout=layout), output_type='div', show_link=False)


def get_version_usage():
"""
Used for getting a Heatmap with an overview of which endpoints are used in which versions
:return:
"""
all_endpoints = []
versions = [v.version for v in get_versions()]

hits_version = {}
for version in versions:
hits_version[version] = get_hits_per_version(version)
for record in hits_version[version]:
if record.endpoint not in all_endpoints:
all_endpoints.append(record.endpoint)

all_endpoints = sorted(all_endpoints)
data = {}
data_list = []
for endpoint in all_endpoints:
data[endpoint] = {}
for version in versions:
data[endpoint][version] = 0

for version in versions:
total_hits = sum([record.count for record in hits_version[version]])
if total_hits == 0:
total_hits = 1 # avoid division by zero

for record in hits_version[version]:
data[record.endpoint][version] = record.count / total_hits

for i in range(len(all_endpoints)):
data_list.append([])
for j in range(len(versions)):
data_list[i].append(data[all_endpoints[i]][versions[j]])

layout = go.Layout(
autosize=True,
height=800,
plot_bgcolor='rgba(249,249,249,1)',
showlegend=False,
title='Heatmap of hits per endpoint per version',
xaxis=go.XAxis(title='Versions', type='category'),
yaxis=dict(type='category', autorange='reversed'),
margin=go.Margin(
l=200
)
)

trace = go.Heatmap(
z=data_list,
x=versions,
y=all_endpoints,
colorscale=[[0, 'rgb(254, 254, 254)'], [1, 'rgb(1, 1, 254)']],
colorbar=dict(
titleside='top',
tickmode='array',
tickvals=[1, 0],
ticktext=['100%', '0%'],
# ticks='outside'
)
)
return plotly.offline.plot(go.Figure(data=[trace], layout=layout), output_type='div', show_link=False)


def get_heatmap(end):
# list of hours: 1:00 - 23:00
hours = ['0' + str(hour) + ':00' for hour in range(0, 10)] + \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
href="{{ url_for('dashboard.overview') }}">Overview</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 1 }}
href="{{ url_for('dashboard.heatmap') }}">Heatmap of number of requests</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 2 }}
<li><a {{'class="active-menu" style="color: white;"'|safe if index == 2 }}
href="{{ url_for('dashboard.version_usage') }}">Version usage</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 3 }}
href="{{ url_for('dashboard.page_number_of_requests_per_endpoint') }}">Requests per
endpoint</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 3 }}
href="{{ url_for('dashboard.page_boxplot_per_version') }}">Time per version</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 4 }}
href="{{ url_for('dashboard.page_boxplot_per_version') }}">Time per version</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 5 }}
href="{{ url_for('dashboard.page_boxplot_per_endpoint') }}">Time per endpoint</a></li>
</ul>

Expand All @@ -36,26 +38,58 @@
<table class="table table-striped table-bordered table-hover sortable">
<thead>
<tr>
<th>Color</th>
<th class="clickable">Endpoint</th>
<th class="clickable">Hits</th>
<th class="clickable">Last accessed</th>
<th class="clickable">Average execution time (in ms)</th>
<th></th>
<th rowspan="2">Color</th>
<th class="clickable" rowspan="2">Endpoint</th>
<th colspan="3" style="text-align: center;">Number of hits</th>
<th colspan="3" style="text-align: center;">Average execution time (ms)</th>
<th class="clickable" rowspan="2">Last accessed</th>
<th rowspan="2">Details</th>
</tr>
<tr>
<th class="clickable">Today</th>
<th class="clickable">Last 7 days</th>
<th class="clickable">Overall</th>

<th class="clickable">Today</th>
<th class="clickable">Last 7 days</th>
<th class="clickable">Overall</th>
</tr>
</thead>
<tbody>
{% for record in times %}
<tr>
<td style="background-color: {{ colors[record.endpoint] }}"></td>
<td style="max-width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;">{{ record.endpoint }}</td>
<td style="text-align: right;">
{% for hits in hits_today %}
{{ hits.count if hits.endpoint == record.endpoint }}
{% endfor %}
</td>
<td style="text-align: right;">
{% for hits in hits_last_days %}
{{ hits.count if hits.endpoint == record.endpoint }}
{% endfor %}
</td>
<td style="text-align: right;">{{ record.count }}</td>

<td style="text-align: right;">
{% for average in average_today %}
{{ ((average.average*10)|round)/10 if average.endpoint == record.endpoint }}
{% endfor %}
</td>
<td style="text-align: right;">
{% for average in average_last_days %}
{{ ((average.average*10)|round)/10 if average.endpoint == record.endpoint }}
{% endfor %}
</td>
<td style="text-align: right;">{{ ((record.average*10)|round)/10 }}</td>

{% for a in access %}
{% if record.endpoint == a.endpoint %}
<td style="text-align: center;">{{ "{:%Y-%m-%d %H:%M:%S }".format(a.last_accessed) if a.last_accessed }}</td>
{% endif %}
{% endfor %}
<td style="text-align: right;">{{ ((record.average*10)|round)/10 }}</td>

<td>
<a href="{{ url_for('dashboard.result_heatmap', end=record.endpoint) }}"
class="btn btn-default btn-sm right-align-custom">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
href="{{ url_for('dashboard.overview') }}">Overview</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 1 }}
href="{{ url_for('dashboard.heatmap') }}">Heatmap of number of requests</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 2 }}
<li><a {{'class="active-menu" style="color: white;"'|safe if index == 2 }}
href="{{ url_for('dashboard.version_usage') }}">Version usage</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 3 }}
href="{{ url_for('dashboard.page_number_of_requests_per_endpoint') }}">Requests per
endpoint</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 3 }}
href="{{ url_for('dashboard.page_boxplot_per_version') }}">Time per version</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 4 }}
href="{{ url_for('dashboard.page_boxplot_per_version') }}">Time per version</a></li>
<li><a {{ 'class="active-menu" style="color: white;"'|safe if index == 5 }}
href="{{ url_for('dashboard.page_boxplot_per_endpoint') }}">Time per endpoint</a></li>
</ul>


<div id="loading">
<div class="col-md-12 col-sm-12">
<br>
Expand Down
8 changes: 8 additions & 0 deletions flask_monitoringdashboard/test/routings/test_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def test_overview(self):
login(self.app)
self.assertEqual(200, self.app.get('dashboard/measurements/overview').status_code)

def test_version_usage(self):
"""
Just retrieve the content and check if nothing breaks
"""
self.assertEqual(302, self.app.get('dashboard/measurements/version_usage').status_code)
login(self.app)
self.assertEqual(200, self.app.get('dashboard/measurements/version_usage').status_code)

def test_heatmap(self):
"""
Just retrieve the content and check if nothing breaks
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def desc():

setuptools.setup(
name="Flask-MonitoringDashboard",
version='1.10.9',
version='1.11.1',
packages=setuptools.find_packages(),
include_package_data=True,
platforms='Any',
Expand Down

0 comments on commit 108c64b

Please sign in to comment.