chg: dev: tag statistic

This commit is contained in:
rachmadaniHaryono 2018-04-21 10:20:25 +08:00
parent c9191e7d83
commit 28e382bb84
2 changed files with 138 additions and 24 deletions

View File

@ -435,31 +435,61 @@ def view_statistic():
if not statistic_data or request.method == 'POST':
all_bookmarks = bukudb.get_rec_all()
netloc = [urlparse(x[1]).netloc for x in all_bookmarks]
tag_set = [x[3] for x in all_bookmarks]
tag_items = []
for tags in tag_set:
tag_items.extend([x.strip() for x in tags.split(',') if x.strip()])
tag_counter = Counter(tag_items)
statistic_datetime = arrow.now()
STATISTIC_DATA = {
'datetime': statistic_datetime,
'netloc': netloc,
'tag_counter': tag_counter,
}
else:
netloc = statistic_data['netloc']
statistic_datetime = statistic_data['datetime']
tag_counter = statistic_data['tag_counter']
netloc_counter = Counter(netloc)
unique_netloc_len = len(set(netloc))
colors = [
"#F7464A", "#46BFBD", "#FDB45C", "#FEDCBA",
"#ABCDEF", "#DDDDDD", "#ABCABC", "#4169E1",
"#C71585", "#FF4500", "#FEDCBA", "#46BFBD"]
show_netloc_table = False
if unique_netloc_len > len(colors):
max_netloc_item = len(colors)
netloc_colors = colors
show_netloc_table = True
else:
colors = colors[:unique_netloc_len]
netloc_colors = colors[:unique_netloc_len]
max_netloc_item = unique_netloc_len
most_common_netlocs = netloc_counter.most_common(max_netloc_item)
most_common_netlocs = [[val[0], val[1], colors[idx]] for idx, val in enumerate(most_common_netlocs)]
most_common_netlocs = [
[val[0], val[1], netloc_colors[idx]] for idx, val in enumerate(most_common_netlocs)]
unique_tag_len = len(tag_counter)
show_tag_rank_table = False
if unique_tag_len > len(colors):
max_tag_item = len(colors)
tag_colors = colors
show_tag_rank_table = True
else:
tag_colors = colors[:unique_tag_len]
max_tag_item = unique_tag_len
most_common_tags = tag_counter.most_common(max_tag_item)
most_common_tags = [
[val[0], val[1], tag_colors[idx]] for idx, val in enumerate(most_common_tags)]
return render_template(
'bukuserver/statistic.html',
most_common_netlocs=most_common_netlocs,
netloc_counter=netloc_counter,
show_netloc_table=show_netloc_table,
most_common_tags=most_common_tags,
tag_counter=tag_counter,
show_tag_rank_table=show_tag_rank_table,
datetime=statistic_datetime,
datetime_text=statistic_datetime.humanize(arrow.now(), granularity='second'),
)

View File

@ -8,14 +8,18 @@
<span rel="tooltip" title="{{datetime}}">{{datetime_text}}</span>
<button type="submit" class="btn btn-default btn-sm">refresh</button>
</form>
<h3>Most common netloc</h3>
<h3>Netloc</h3>
<div class="col-md-6">
<canvas id="mostCommonChart" width="500" height="500"></canvas>
</div>
<div class="col-md-6">
{% if show_netloc_table %}
<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#netlocModal">
View all
</button>
{% endif %}
<table class="table">
<tr>
<th>Rank</th>
@ -36,6 +40,8 @@
{% endfor %}
</table>
</div>
{% if show_netloc_table %}
<div class="modal fade" id="netlocModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
@ -73,29 +79,107 @@
</div>
</div>
</div>
<script>
var ctx = document.getElementById("mostCommonChart").getContext('2d');
var netlocChart = new Chart(ctx, {
type: 'pie',
data: {
datasets: [{
data: [
{% for val in most_common_netlocs %} {{val.1}}, {% endfor %}
],
backgroundColor: [
{% for val in most_common_netlocs %} "{{val.2}}", {% endfor %}
],
}],
// These labels appear in the legend and in the tooltips when hovering different arcs
labels: [
{% for val in most_common_netlocs %} "{{val.0}}", {% endfor %}
]
},
});
{% endif %}
netlocChart.canvas.parentNode.style.height = '128px';
<h3>Tag</h3>
</script>
<div class="col-md-6">
<canvas id="mostCommonTagChart" width="500" height="500"></canvas>
</div>
<div class="col-md-6">
{% if show_tag_rank_table %}
<button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#tagRankModal">
View all
</button>
{% endif %}
<table class="table">
<tr>
<th>Rank</th>
<th>Tag</th>
<th>Number</th>
</tr>
{% for item, number, _ in most_common_tags %}
<tr>
<td>{{loop.index}}</td>
<td>
<a href="{{url_for('bookmarks-html', tag=item)}}">{{item}}</a>
</td>
<td>{{number}}</td>
</tr>
{% endfor %}
</table>
</div>
{% if show_tag_rank_table %}
<div class="modal fade" id="tagRankModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Netloc ranking</h4>
</div>
<div class="modal-body">
<table class="table table-condensed">
<tr>
<th>Rank</th>
<th>Tag</th>
<th>Number</th>
</tr>
{% for item, number in tag_counter.most_common() %}
<tr>
<td>{{loop.index}}</td>
<td> <a href="{{url_for('bookmarks-html', tag=item)}}">{{item}}</a> </td>
<td>{{number}}</td>
</tr>
{% endfor %}
</table>
</div>
</div>
</div>
</div>
{% endif %}
<script>
var ctx = document.getElementById("mostCommonChart").getContext('2d');
var netlocChart = new Chart(ctx, {
type: 'pie',
data: {
datasets: [{
data: [
{% for val in most_common_netlocs %} {{val.1}}, {% endfor %}
],
backgroundColor: [
{% for val in most_common_netlocs %} "{{val.2}}", {% endfor %}
],
}],
// These labels appear in the legend and in the tooltips when hovering different arcs
labels: [
{% for val in most_common_netlocs %} "{{val.0}}", {% endfor %}
]
},
});
var tagRankCtx = document.getElementById("mostCommonTagChart").getContext('2d');
var tagRankChart = new Chart(tagRankCtx, {
type: 'pie',
data: {
datasets: [{
data: [
{% for val in most_common_tags %} {{val.1}}, {% endfor %}
],
backgroundColor: [
{% for val in most_common_tags %} "{{val.2}}", {% endfor %}
],
}],
// These labels appear in the legend and in the tooltips when hovering different arcs
labels: [
{% for val in most_common_tags %} "{{val.0}}", {% endfor %}
]
},
});
netlocChart.canvas.parentNode.style.height = '128px';
</script>
</div>
{% endblock %}