Skip to content

Commit 68e4dd3

Browse files
committed
Create packet aggregate signatures graph
Creates /stats/packet/<packet_id>, which displays a graph of signatures over time for the given packet
1 parent ce7d037 commit 68e4dd3

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

packet/routes/upperclassmen.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Routes available to CSH users only
33
"""
4+
import json
45

56
from itertools import chain
67
from operator import itemgetter
@@ -10,6 +11,7 @@
1011
from packet.models import Packet, MiscSignature
1112
from packet.utils import before_request, packet_auth
1213
from packet.log_utils import log_cache, log_time
14+
from packet.stats import packet_stats
1315

1416

1517
@app.route('/')
@@ -61,3 +63,44 @@ def upperclassmen_total(info=None):
6163

6264
return render_template('upperclassmen_totals.html', info=info, num_open_packets=len(open_packets),
6365
upperclassmen=sorted(upperclassmen.items(), key=itemgetter(1), reverse=True))
66+
67+
68+
@app.route('/stats/packet/<packet_id>')
69+
@packet_auth
70+
@before_request
71+
def packet_graphs(packet_id, info=None):
72+
stats = packet_stats(packet_id)
73+
fresh = []
74+
misc = []
75+
upper = []
76+
77+
78+
# Make a rolling sum of signatures over time
79+
agg = lambda l, attr, date: l.append((l[-1] if l else 0) + len(stats['dates'][date][attr]))
80+
dates = list(stats['dates'].keys())
81+
for date in dates:
82+
agg(fresh, 'fresh', date)
83+
agg(misc, 'misc', date)
84+
agg(upper, 'upper', date)
85+
86+
# Stack misc and upper on top of fresh for a nice stacked line graph
87+
for i in range(len(dates)):
88+
misc[i] = misc[i] + fresh[i]
89+
upper[i] = upper[i] + misc[i]
90+
91+
return render_template('packet_stats.html',
92+
info=info,
93+
data=json.dumps({
94+
'dates':dates,
95+
'accum': {
96+
'fresh':fresh,
97+
'misc':misc,
98+
'upper':upper,
99+
},
100+
'daily': {
101+
102+
}
103+
}),
104+
fresh=stats['freshman'],
105+
packet=Packet.by_id(packet_id),
106+
)

packet/templates/packet_stats.html

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{% extends "extend/base.html" %}
2+
3+
{% block head %}
4+
{{ super() }}
5+
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
6+
{% endblock %}
7+
8+
{% block body %}
9+
<div class="container main">
10+
<div class="card">
11+
<h5 class="card-header bg-primary text-white">Cumulative Signatures Over Time for {{ fresh['name'] }} ({{ fresh['rit_username'] }})</h5>
12+
<div class="card-body">
13+
<canvas id="myChart" width="400" height="400"></canvas>
14+
<script>
15+
var data = {{ data|safe }};
16+
// Stack the lines
17+
var ctx = document.getElementById('myChart');
18+
var myChart = new Chart(ctx, {
19+
type: 'line',
20+
data: {
21+
labels: data.dates,
22+
datasets: [
23+
{
24+
fill: 'origin',
25+
label: 'Fresh Sigs',
26+
data: data.accum.fresh,
27+
backgroundColor: '#b0197e80',
28+
borderColor: '#b0197e',
29+
borderWidth: 1,
30+
lineTension: 0
31+
},
32+
{
33+
fill: '-1',
34+
label: 'Misc Sigs',
35+
data: data.accum.misc,
36+
backgroundColor: '#0000ff80',
37+
borderColor: 'blue',
38+
borderWidth: 1,
39+
lineTension: 0
40+
},
41+
{
42+
fill: '-1',
43+
label: 'Upper Sigs',
44+
data: data.accum.upper,
45+
backgroundColor: '#00ff0080',
46+
borderColor: 'green',
47+
borderWidth: 1,
48+
lineTension: 0
49+
}
50+
]
51+
},
52+
options: {
53+
scales: {
54+
xAxes: [{
55+
type: 'time',
56+
time: {
57+
unit: 'day',
58+
},
59+
}],
60+
yAxes: [{
61+
ticks: {
62+
beginAtZero: true
63+
}
64+
}]
65+
}
66+
}
67+
});
68+
</script>
69+
</div>
70+
</div>
71+
</div>
72+
{% endblock %}

0 commit comments

Comments
 (0)