Created a summary partial function view for metrics #23

This commit is contained in:
Sebastian Henschel 2017-05-21 21:55:11 +02:00
parent bb1287a289
commit 183c250dc9
6 changed files with 168 additions and 40 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,2 +1,27 @@
$(document).ready(function($) {
var barChart = new Chart($("#barchart"), {
type: 'horizontalBar',
data: {
labels: BarchartData['labels'],
datasets: [{
label: 'Count',
data: BarchartData['data'],
backgroundColor: BarchartData['backgroundColor'],
borderColor: BarchartData['borderColor'],
borderWidth: 1
}]
},
options: {
scales: {
xAxes: [{
ticks: {
beginAtZero: true
}
}]
},
legend: {
display: false
}
}
});
});

View File

@ -126,57 +126,71 @@
</div>
<button type="submit" class="btn btn-primary">Update</button>
<button type="submit" class="btn btn-primary">Update filter</button>
</form>
<div class="table-responsive">
<table class="table table-hover tablesorter" id="metrics-list">
<thead>
<tr>
<th>#</th>
<th>Verb</th>
<th>URL</th>
<th>Date</th>
<th>Details</th>
</tr>
</thead>
<tbody>
{% for metric in metrics %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ metric.verb }}</td>
<td>
{{ metric.url }}
</td>
<td>{{ metric.date|date:"Y-m-d H:m:s" }}</td>
<td>
<ul>
<li>User Name: {{ metric.user_name }}</li>
<li>User ID: {{ metric.user_id }}</li>
<li>Developer Email: {{ metric.developer_email }}</li>
<li>App Name: {{ metric.app_name }}</li>
<li>Consumer ID: {{ metric.consumer_id }}</li>
<li>Implemented by Partial Function: {{ metric.implemented_by_partial_function }}</li>
<li>Implemented In Version: {{ metric.implemented_in_version }}</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<ul class="nav nav-tabs">
{% block nav_tabs %}
<li class="active"><a href="{% url 'metrics-index' %}?{{ request.GET.urlencode }}">List</a></li>
<li><a href="{% url 'metrics-summary-partial-function' %}?{{ request.GET.urlencode }}">Summary by Partial Function</a></li>
{% endblock nav_tabs %}
</ul>
<div class="tab-content">
{% block tab_content %}
<div class="tab-pane active">
<div class="table-responsive">
<table class="table table-hover tablesorter" id="metrics-list">
<thead>
<tr>
<th>#</th>
<th>Verb</th>
<th>URL</th>
<th>Date</th>
<th>Details</th>
</tr>
</thead>
<tbody>
{% for metric in metrics %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ metric.verb }}</td>
<td>
{{ metric.url }}
</td>
<td>{{ metric.date|date:"Y-m-d H:m:s" }}</td>
<td>
<ul>
<li>User Name: {{ metric.user_name }}</li>
<li>User ID: {{ metric.user_id }}</li>
<li>Developer Email: {{ metric.developer_email }}</li>
<li>App Name: {{ metric.app_name }}</li>
<li>Consumer ID: {{ metric.consumer_id }}</li>
<li>Implemented by Partial Function: {{ metric.implemented_by_partial_function }}</li>
<li>Implemented In Version: {{ metric.implemented_in_version }}</li>
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock tab_content %}
</div>
</div>
{% endblock %}
{% block extrajs %}
{% comment %}
<script type="text/javascript" src="{% static 'metrics/js/Chart.min.js' %}"></script>
<script type="text/javascript" src="{% static 'metrics/js/metrics.js' %}"></script>
<script type="text/javascript">
const BarchartData = {{ barchart_data|safe }};
</script>
{% endcomment %}
{% endblock extrajs %}
{% block extracss %}
<link href="{% static 'metrics/css/metrics.css' %}" rel="stylesheet">
{% endblock extracss %}

View File

@ -0,0 +1,13 @@
{% extends 'metrics/index.html' %}
{% load static %}
{% block nav_tabs %}
<li><a href="{% url 'metrics-index' %}?{{ request.GET.urlencode }}">List</a></li>
<li class="active"><a href="{% url 'metrics-summary-partial-function' %}?{{ request.GET.urlencode }}">Summary by Partial Function</a></li>
{% endblock nav_tabs %}
{% block tab_content %}
<div class="tab-pane active">
<canvas id="barchart" width="1000" height="500"></canvas>
</div>
{% endblock tab_content %}

View File

@ -5,10 +5,14 @@ URLs for metrics app
from django.conf.urls import url
from .views import IndexView
from .views import IndexView, SummaryPartialFunctionView
urlpatterns = [
url(r'^$',
IndexView.as_view(),
name='metrics-index'),
url(r'^summary-partial-function$',
SummaryPartialFunctionView.as_view(),
name='metrics-summary-partial-function'),
]

View File

@ -3,6 +3,10 @@
Views of metrics app
"""
import json
import math
import random
from datetime import datetime
from django.conf import settings
@ -14,6 +18,43 @@ from base.api import api, APIError
def get_random_color():
r = int(math.floor(random.random() * 255))
b = int(math.floor(random.random() * 255))
g = int(math.floor(random.random() * 255))
return 'rgba({}, {}, {}, 0.2)'.format(r, g, b)
def get_barchart_data(metrics, fieldname):
"""
Gets bar chart data compatible with Chart.js from the field with given
fieldname in given metrics
"""
border_color = 'rgba(0, 0, 0, 1)'
data = {
'labels': [],
'data': [],
'backgroundColor': [],
'borderColor': [],
}
items = {}
for metric in metrics:
if not metric[fieldname]:
continue
if metric[fieldname] in items:
items[metric[fieldname]] += 1
else:
items[metric[fieldname]] = 1
for item in items:
data['labels'].append(item)
data['data'].append(items[item])
data['backgroundColor'].append(get_random_color())
data['borderColor'].append(border_color)
return data
class IndexView(LoginRequiredMixin, TemplateView):
"""Index view for metrics"""
template_name = "metrics/index.html"
@ -52,5 +93,22 @@ class IndexView(LoginRequiredMixin, TemplateView):
context.update({
'metrics': metrics,
'barchart_data': json.dumps({})
})
return context
class SummaryPartialFunctionView(IndexView):
template_name = "metrics/summary_partial_function.html"
def get_context_data(self, **kwargs):
context = super(SummaryPartialFunctionView, self).get_context_data(
**kwargs)
barchart_data = json.dumps(get_barchart_data(
context['metrics'], 'implemented_by_partial_function'))
context.update({
'barchart_data': barchart_data,
})
return context