Skip to content

Commit

Permalink
Merge pull request #169 from flask-dashboard/sunburst
Browse files Browse the repository at this point in the history
Sunburst
  • Loading branch information
bogdanp05 committed Jun 15, 2018
2 parents be59777 + 6875a11 commit 12b5c47
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 7 deletions.
2 changes: 1 addition & 1 deletion flask_monitoringdashboard/core/profiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def start_thread_last_requested(endpoint):
Starts a thread that updates the last_requested time in the database.
:param endpoint: Endpoint object
"""
BaseProfiler(endpoint.id).start()
BaseProfiler(endpoint).start()


def start_performance_thread(endpoint, duration):
Expand Down
15 changes: 10 additions & 5 deletions flask_monitoringdashboard/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ def create_app():
dashboard.config.version = '3.1'
dashboard.bind(app)

def f(count=10):
if count == 0:
time.sleep(1)
def f(i=5):
if i == 0:
time.sleep(1)
else:
f(count-1)
f(i-1)

def g():
time.sleep(1)

@app.route('/endpoint')
def endpoint():
f()
if random.randint(0, 1):
f()
else:
g()
return 'Ok'

return app
Expand Down
3 changes: 3 additions & 0 deletions flask_monitoringdashboard/templates/fmd_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

<link href="{{ url_for('dashboard.static', filename='css/custom.css') }}" rel="stylesheet">
<link rel="shortcut icon" href="{{ url_for('dashboard.static', filename='img/favicon.ico') }}" />

{% block head %}
{% endblock %}
</head>

{% macro active_if_is(name) -%}
Expand Down
2 changes: 2 additions & 0 deletions flask_monitoringdashboard/templates/fmd_dashboard/graph.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ <h4>{{ title }}</h4>
</div>
</div>
</div>
{% block graph2 %}
{% endblock %}
{% block summary %}
{% endblock %}
{% endblock %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{% extends "fmd_dashboard/profiler.html" %}

{% block head %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.12.0/d3.min.js"></script>
<script src="https://unpkg.com/sunburst-chart"></script>
{% endblock %}

{% block graph_content %}

Expand Down Expand Up @@ -49,4 +53,28 @@
{% endfor %}
</tbody>
</table>
{% endblock %}

{% block graph2 %}
<div class="card mb-3" style="margin-top: 10px;">
<div class="card-header"><h4>Sunburst</h4></div>
<div class="card-body">
<div id="sunburst" style="margin-left: -200px;"></div>
</div>
</div>
{% endblock %}

{% block script %}
{{ super() }}
<script>
const color = d3.scaleOrdinal(d3.schemeCategory20);

Sunburst()
.data({{ sunburst|safe }})
.label('name')
.size('size')
.color((d, parent) => color(parent ? parent.data.name : null))
.tooltipContent((d, node) => `Size: <i>${node.value}</i>`)
(document.getElementById('sunburst'));
</script>
{% endblock %}
10 changes: 10 additions & 0 deletions flask_monitoringdashboard/test/core/profiler/test_profiler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import threading
import time
import unittest

from flask_monitoringdashboard.core.profiler import start_thread_last_requested, start_performance_thread, \
Expand All @@ -15,10 +16,16 @@ def setUp(self):
add_fake_data()
self.app = get_test_app()

@staticmethod
def wait_until_threads_finished(num_threads):
while threading.active_count() > num_threads:
time.sleep(0.001)

def test_start_thread_last_requested(self):
num_threads = threading.active_count()
start_thread_last_requested(Endpoint(id=1, name=NAME))
self.assertEqual(threading.active_count(), num_threads + 1)
self.wait_until_threads_finished(num_threads)

def test_start_performance_thread(self):
with self.app.test_request_context():
Expand All @@ -27,6 +34,7 @@ def test_start_performance_thread(self):
num_threads = threading.active_count()
start_performance_thread(Endpoint(id=1, name=NAME), 1234)
self.assertEqual(threading.active_count(), num_threads + 1)
self.wait_until_threads_finished(num_threads)

def test_start_profiler_thread(self):
with self.app.test_request_context():
Expand All @@ -36,6 +44,7 @@ def test_start_profiler_thread(self):
thread = start_profiler_thread(Endpoint(id=1, name=NAME))
self.assertEqual(threading.active_count(), num_threads + 1)
thread.stop(1)
self.wait_until_threads_finished(num_threads)

def test_start_profiler_and_outlier_thread(self):
with self.app.test_request_context():
Expand All @@ -45,3 +54,4 @@ def test_start_profiler_and_outlier_thread(self):
thread = start_profiler_and_outlier_thread(Endpoint(id=1, name=NAME))
self.assertEqual(threading.active_count(), num_threads + 2)
thread.stop(1)
self.wait_until_threads_finished(num_threads)
31 changes: 30 additions & 1 deletion flask_monitoringdashboard/views/details/grouped_profiler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import defaultdict

from flask import render_template
import json

from flask_monitoringdashboard import blueprint
from flask_monitoringdashboard.core.auth import secure
Expand Down Expand Up @@ -34,5 +35,33 @@ def grouped_profiler(endpoint_id):
for index, item in enumerate(table):
table[index].compute_body(index, table)

return render_template('fmd_dashboard/profiler_grouped.html', details=details, table=table,
sunburst = json.dumps(table_to_json(table))
return render_template('fmd_dashboard/profiler_grouped.html', details=details, table=table, sunburst=sunburst,
title='Grouped Profiler results for {}'.format(details['endpoint']))


def table_to_json(table, parent=None):
if not parent:
root_list = [row for row in table if row.indent == 0]
if root_list:
root = root_list[len(root_list)-1]
return {
'name': root.code,
'children': table_to_json(table, parent=root)
}
else:
return {}

children = []
for child in [table[index] for index in parent.body if table[index].indent == parent.indent + 1]:
if child.body:
children.append({
'name': child.code,
'children': table_to_json(table, child)
})
else: # child is leaf
children.append({
'name': child.code,
'size': child.sum
})
return children

0 comments on commit 12b5c47

Please sign in to comment.