Add statistics for pages in documents, refactoring a bit to share more code

- Legacy-Id: 12639
This commit is contained in:
Ole Laursen 2017-01-10 18:27:15 +00:00
parent 13f3b4ed1a
commit 656ed8c89d
7 changed files with 107 additions and 24 deletions

View file

@ -1,5 +1,17 @@
$(document).ready(function () {
if (window.chartConf) {
window.chartConf.credits = {
enabled: false
};
window.chartConf.exporting = {
fallbackToExportServer: false
};
if (!window.chartConf.legend)
window.chartConf.legend = {
enabled: false
};
var chart = Highcharts.chart('chart', window.chartConf);
}

View file

@ -31,7 +31,7 @@ class StatisticsTests(TestCase):
self.assertTrue(authors_all_url in r["Location"])
# check various stats types
for stats_type in ["authors"]:
for stats_type in ["authors", "pages"]:
for document_type in ["all", "rfc", "draft"]:
url = urlreverse(ietf.stats.views.document_stats, kwargs={ "stats_type": stats_type, "document_type": document_type })
r = self.client.get(url)

View file

@ -61,7 +61,7 @@ def document_stats(request, stats_type=None, document_type=None):
# statistics type - one of the tables or the chart
possible_stats_types = [
("authors", "Number of authors"),
# ("pages", "Pages"),
("pages", "Pages"),
# ("format", "Format"),
# ("spectech", "Specification techniques"),
]
@ -106,7 +106,6 @@ def document_stats(request, stats_type=None, document_type=None):
stats_title = ""
if stats_type == "authors":
stats_title = "Number of authors for each {}".format(doc_label)
groups = defaultdict(list)
@ -118,15 +117,38 @@ def document_stats(request, stats_type=None, document_type=None):
series_data = []
for author_count, names in sorted(groups.iteritems(), key=lambda t: t[0]):
series_data.append((author_count, len(names) * 100.0 / total_docs))
table_data.append((author_count, names))
percentage = len(names) * 100.0 / total_docs
series_data.append((author_count, percentage))
table_data.append((author_count, percentage, names))
chart_data.append({
"data": series_data,
"name": "Percentage of {}s".format(doc_label),
"animation": False,
})
elif stats_type == "pages":
stats_title = "Number of pages for each {}".format(doc_label)
groups = defaultdict(list)
for name, pages in doc_qs.values_list("name", "pages"):
groups[pages].append(name)
total_docs = sum(len(names) for pages, names in groups.iteritems())
series_data = []
for pages, names in sorted(groups.iteritems(), key=lambda t: t[0]):
percentage = len(names) * 100.0 / total_docs
if pages is not None:
series_data.append((pages, len(names)))
table_data.append((pages, percentage, names))
chart_data.append({
"data": series_data,
"animation": False,
})
return render(request, "stats/document_stats.html", {
"chart_data": mark_safe(json.dumps(chart_data)),

View file

@ -37,6 +37,8 @@
{% if stats_type == "authors" %}
{% include "stats/document_stats_authors.html" %}
{% elif stats_type == "pages" %}
{% include "stats/document_stats_pages.html" %}
{% endif %}
{% endblock %}

View file

@ -7,12 +7,6 @@
chart: {
type: 'column'
},
credits: {
enabled: false,
},
exporting: {
fallbackToExportServer: false,
},
title: {
text: '{{ stats_title|escapejs }}'
},
@ -32,16 +26,12 @@
}
}
},
legend: {
enabled: false,
},
tooltip: {
formatter: function () {
var s = '<b>' + this.x + ' ' + (this.x == 1 ? "author" : 'authors') + '</b>';
$.each(this.points, function () {
s += '<br/>' + this.series.name + ': ' +
this.y.toFixed(1) + '%';
s += '<br/>' + chartConf.yAxis.title.text + ': ' + this.y.toFixed(1) + '%';
});
return s;
@ -58,18 +48,16 @@
<thead>
<tr>
<th>Authors</th>
<th>Documents</th>
<th>Percentage of {{ doc_label }}s</th>
<th>{{ doc_label }}s</th>
</tr>
</thead>
<tbody>
{% for author_count, names in table_data %}
{% for author_count, percentage, names in table_data %}
<tr>
<td>{{ author_count }}</td>
<td><a class="popover-docnames"
href=""
data-docnames="{% for n in names|slice:":20" %}{{ n }}{% if not forloop.last %} {% endif %}{% endfor %}"
data-sliced="{% if names|length > 20 %}1{% endif %}"
>{{ names|length }}</a></td>
<td>{{ percentage|floatformat:2 }}%</td>
<td>{% include "stats/includes/docnames_cell.html" %}</td>
</tr>
{% endfor %}
</tbody>

View file

@ -0,0 +1,58 @@
<h3>{{ stats_title }}</h3>
<div id="chart"></div>
<script>
var chartConf = {
chart: {
type: 'line'
},
title: {
text: '{{ stats_title|escapejs }}'
},
xAxis: {
title: {
text: 'Number of pages'
}
},
yAxis: {
title: {
text: 'Number of {{ doc_label }}s'
}
},
tooltip: {
formatter: function () {
var s = '<b>' + this.x + ' ' + (this.x == 1 ? "page" : 'pages') + '</b>';
$.each(this.points, function () {
s += '<br/>' + chartConf.yAxis.title.text + ': ' + this.y;
});
return s;
},
shared: true
},
series: {{ chart_data }}
};
</script>
<h3>Data</h3>
<table class="table table-condensed stats-data">
<thead>
<tr>
<th>Authors</th>
<th>Percentage of {{ doc_label }}s</th>
<th>{{ doc_label }}s</th>
</tr>
</thead>
<tbody>
{% for pages, percentage, names in table_data %}
<tr>
<td>{{ pages }}</td>
<td>{{ percentage|floatformat:2 }}%</td>
<td>{% include "stats/includes/docnames_cell.html" %}</td>
</tr>
{% endfor %}
</tbody>
</table>

View file

@ -0,0 +1 @@
<a class="popover-docnames" href="" data-docnames="{% for n in names|slice:":20" %}{{ n }}{% if not forloop.last %} {% endif %}{% endfor %}" data-sliced="{% if names|length > 20 %}1{% endif %}">{{ names|length }}</a>