Commit 820a0c92 authored by Julian Rother's avatar Julian Rother
Browse files

Implemented 'viewsperday' statistics

parent 697df400
......@@ -22,3 +22,47 @@ def stats_data(dataname, parameter=None):
else:
data = query(statsqueries[dataname])
return Response(json.dumps(data, default=date_json_handler), mimetype='application/json')
@app.route('/stats/viewsperday')
def stats_viewsperday():
start = None
end = None
courseid = request.args.get('course', None)
lectureid = request.args.get('lecture', None)
videoid = request.args.get('video', None)
print(start, end, courseid, lectureid, videoid)
rows = query('''SELECT log.id AS id, log.time AS time, formats.description AS fmt
FROM log
JOIN videos ON (videos.id = log.obj_id)
JOIN lectures ON (lectures.id = videos.lecture_id)
JOIN courses ON (courses.id = lectures.course_id)
JOIN formats ON (formats.id = videos.video_format)
WHERE (? OR courses.id = ?) AND (? OR lectures.id = ?) AND (? OR videos.id = ?)''',
courseid == None, courseid, lectureid == None, lectureid, videoid == None, videoid)
data = {}
if not end:
end = date.today()
first = None
fmts = ['total']
for e in rows:
key = e['time'].date()
if not first or key < first:
first = key
if key not in data:
data[key] = {'total': set()}
if e['fmt'] not in data[key]:
data[key][e['fmt']] = set()
data[key]['total'].add(e['id'])
data[key][e['fmt']].add(e['id'])
if e['fmt'] not in fmts:
fmts.append(e['fmt'])
if not start:
start = first
res = {'times': [], 'views': [{'name': name, 'vals': []} for name in fmts]}
while start and start <= end:
res['times'].append(start.isoformat())
for d in res['views']:
d['vals'].append(len(data.get(start, {}).get(d['name'], set())))
start += timedelta(days=1)
return Response(json.dumps(res, default=date_json_handler), mimetype='application/json')
......@@ -27,6 +27,7 @@
<script src="{{url_for('static', filename='js.cookie.js')}}"></script>
{%if ismod() %}
<script src="{{url_for('static', filename='moderator.js')}}"></script>
<script src="{{url_for('static', filename='plotly.min.js')}}"></script>
{% endif %}
<script src="{{url_for('static', filename='videojs/video.js')}}"></script>
<script src="{{url_for('static', filename='videojs/ie8/videojs-ie8.js')}}"></script>
......
......@@ -4,6 +4,7 @@
{% from 'macros.html' import moderator_checkbox %}
{% from 'macros.html' import preview %}
{% from 'macros.html' import moderator_permissioneditor %}
{% from 'macros.html' import stats_viewsperday %}
{% extends "base.html" %}
{% block title %}- {{course.title}}{% endblock %}
......@@ -46,6 +47,8 @@
</tbody>
</table>
</div>
<div id="statview" class="col-xs-11" style="height:600px"></div>
{{stats_viewsperday("statview", "Abrufe pro Tag", course=course.id)}}
{% endif %}
</div>
</div>
......
......@@ -5,6 +5,7 @@
{% from 'macros.html' import moderator_editor %}
{% from 'macros.html' import moderator_delete %}
{% from 'macros.html' import moderator_checkbox %}
{% from 'macros.html' import stats_viewsperday %}
{% set page_border = 1 -%}
{% extends "base.html" %}
......@@ -58,6 +59,8 @@
{% endfor %}
</table>
</div>
<div id="statview" class="col-xs-12" style="height:600px"></div>
{{stats_viewsperday("statview", "Abrufe pro Tag", lecture=lecture.id)}}
{% endif %}
</div>
</div>
......
......@@ -245,3 +245,28 @@ $('#embedcodebtn').popover(
{% endmacro %}
{% macro vtttime(time) %}{{ '%02d:%02d:%02d.000'|format( time//3600, (time//60)%60, time%60) }}{% endmacro %}
{% macro stats_viewsperday(id, title, type="scatter", course=None, lecture=None, video=None) %}
<script>
$.ajax({
method: "GET",
url: "{{url_for('stats_viewsperday', course=course, lecture=lecture, video=video)}}",
dataType: "json",
error: moderator.api.handleapierror,
success: function (data) {
var traces = [];
var i = 0;
if (data.views.length == 2)
i = 1;
for (; i < data.views.length; i++) {
traces.push({"x": data.times, "y": data.views[i].vals, "type": "{{type}}", "name": data.views[i].name});
}
var layout = {
"title": "{{title}}",
"showlegend": (traces.length != 1)
};
Plotly.newPlot("{{id}}", traces, layout, { "modeBarButtonsToRemove": ['sendDataToCloud','hoverCompareCartesian'], "displaylogo": false});
}
});
</script>
{% endmacro %}
{% from 'macros.html' import preview %}
{% from 'macros.html' import stats_viewsperday %}
{% extends "base.html" %}
{% block header %}
......@@ -60,6 +60,8 @@
}
});
</script>
<div id="statview" class="col-xs-12" style="height:600px"></div>
{{stats_viewsperday("statview", "Abrufe pro Tag")}}
</div>
i </div>
</div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment