Skip to content
Snippets Groups Projects
Commit 263def9c authored by Julian Rother's avatar Julian Rother
Browse files

Simplified log schema

parent d1959b89
No related branches found
No related tags found
No related merge requests found
...@@ -125,14 +125,11 @@ CREATE TABLE IF NOT EXISTS `site_texts` ( ...@@ -125,14 +125,11 @@ CREATE TABLE IF NOT EXISTS `site_texts` (
`modified_by` text NOT NULL `modified_by` text NOT NULL
); );
CREATE TABLE IF NOT EXISTS `log` ( CREATE TABLE IF NOT EXISTS `log` (
`ip` varchar(64), `id` INTEGER,
`id` varchar(64),
`time` datetime NOT NULL, `time` datetime NOT NULL,
`date` datetime NOT NULL, `date` datetime NOT NULL,
`source` varchar(8), `source` INTEGER,
`object` varchar(10), `video` INTEGER
`obj_id` INTEGER,
`path` varchar(255) NOT NULL
); );
CREATE TABLE IF NOT EXISTS `logcache` ( CREATE TABLE IF NOT EXISTS `logcache` (
`req` varchar(64), `req` varchar(64),
......
...@@ -527,11 +527,10 @@ def auth(): # For use with nginx auth_request ...@@ -527,11 +527,10 @@ def auth(): # For use with nginx auth_request
return 'Internal Server Error', 500 return 'Internal Server Error', 500
url = request.headers['X-Original-Uri'].lstrip(config['VIDEOPREFIX']) url = request.headers['X-Original-Uri'].lstrip(config['VIDEOPREFIX'])
ip = request.headers.get('X-Real-IP', '') ip = request.headers.get('X-Real-IP', '')
if 'tracking' in request.cookies: if request.cookies.get('tracking', '') and request.cookies['tracking'].isdigit():
cookie = request.cookies['tracking'] cookie = int(request.cookies['tracking'])
else: else:
cookie = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(64)) cookie = random.getrandbits(8*8-1)
# r.set_cookie('tracking', request.cookies.get('tracking', ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(64))), max_age=2147483647)
if url.endswith('jpg') or ismod(): if url.endswith('jpg') or ismod():
return "OK", 200 return "OK", 200
perms = query('''SELECT videos.path, videos.id AS vid, perm.* perms = query('''SELECT videos.path, videos.id AS vid, perm.*
...@@ -552,11 +551,11 @@ def auth(): # For use with nginx auth_request ...@@ -552,11 +551,11 @@ def auth(): # For use with nginx auth_request
password = auth.password password = auth.password
if checkperm(perms, username=username, password=password): if checkperm(perms, username=username, password=password):
try: try:
modify('INSERT INTO log (ip, id, `time`, `date`, object, obj_id, path) VALUES (?, ?, ?, "video", ?, ?)', ip, cookie, datetime.now(), datetime.combine(date.today(), time()), perms[0]['vid'], url) modify('INSERT INTO log (id, `time`, `date`, video, source) VALUES (?, ?, ?, ?, 1)', cookie, datetime.now(), datetime.combine(date.today(), time()), perms[0]['vid'])
except: except:
pass pass
r = make_response('OK', 200) r = make_response('OK', 200)
r.set_cookie('tracking', cookie, max_age=2147483647) # Many many years r.set_cookie('tracking', str(cookie), max_age=2147483647) # Many many years
return r return r
password_auth = False password_auth = False
for perm in perms: for perm in perms:
......
...@@ -10,13 +10,13 @@ def stats(): ...@@ -10,13 +10,13 @@ def stats():
return render_template('stats.html') return render_template('stats.html')
statsqueries = {} statsqueries = {}
statsqueries['formats_views'] = "SELECT formats.description AS x, count(DISTINCT log.id) AS y FROM log JOIN videos ON (videos.id = log.obj_id) JOIN formats ON (formats.id = videos.video_format) GROUP BY formats.id" statsqueries['formats_views'] = "SELECT formats.description AS x, count(DISTINCT log.id) AS y FROM log JOIN videos ON (videos.id = log.video) JOIN formats ON (formats.id = videos.video_format) GROUP BY formats.id"
statsqueries['course_count'] = "SELECT semester AS x, count(id) AS y FROM courses GROUP BY semester" statsqueries['course_count'] = "SELECT semester AS x, count(id) AS y FROM courses GROUP BY semester"
statsqueries['lectures_count'] = "SELECT semester AS x, count(lectures.id) AS y FROM lectures JOIN courses ON (courses.id = lectures.course_id) GROUP BY semester" statsqueries['lectures_count'] = "SELECT semester AS x, count(lectures.id) AS y FROM lectures JOIN courses ON (courses.id = lectures.course_id) GROUP BY semester"
statsqueries['categories_courses'] = "SELECT courses.subject AS x, count(courses.id) AS y FROM courses GROUP BY courses.subject ORDER BY y DESC LIMIT 100" statsqueries['categories_courses'] = "SELECT courses.subject AS x, count(courses.id) AS y FROM courses GROUP BY courses.subject ORDER BY y DESC LIMIT 100"
statsqueries['organizer_courses'] = "SELECT courses.organizer AS x, count(courses.id) AS y FROM courses GROUP BY courses.organizer ORDER BY y DESC LIMIT 100" statsqueries['organizer_courses'] = "SELECT courses.organizer AS x, count(courses.id) AS y FROM courses GROUP BY courses.organizer ORDER BY y DESC LIMIT 100"
statsqueries['categories_lectures'] = "SELECT courses.subject AS x, count(lectures.id) AS y FROM lectures JOIN courses ON (courses.id = lectures.course_id) WHERE lectures.visible GROUP BY courses.subject ORDER BY y DESC LIMIT 100" statsqueries['categories_lectures'] = "SELECT courses.subject AS x, count(lectures.id) AS y FROM lectures JOIN courses ON (courses.id = lectures.course_id) WHERE lectures.visible GROUP BY courses.subject ORDER BY y DESC LIMIT 100"
statsqueries['lecture_views'] = "SELECT lectures.time AS x, count(DISTINCT log.id) AS y FROM log JOIN videos ON (videos.id = log.obj_id) JOIN lectures ON (lectures.id = videos.lecture_id) WHERE (lectures.course_id = ?) GROUP BY lectures.id ORDER BY lectures.time" statsqueries['lecture_views'] = "SELECT lectures.time AS x, count(DISTINCT log.id) AS y FROM log JOIN videos ON (videos.id = log.video) JOIN lectures ON (lectures.id = videos.lecture_id) WHERE (lectures.course_id = ?) GROUP BY lectures.id ORDER BY lectures.time"
def plotly_date_handler(obj): def plotly_date_handler(obj):
return obj.strftime("%Y-%m-%d %H:%M:%S") return obj.strftime("%Y-%m-%d %H:%M:%S")
...@@ -39,15 +39,16 @@ def stats_generic(req, param=None): ...@@ -39,15 +39,16 @@ def stats_generic(req, param=None):
@app.route('/stats/viewsperday/<req>') @app.route('/stats/viewsperday/<req>')
@app.route('/stats/viewsperday/<req>/<param>') @app.route('/stats/viewsperday/<req>/<param>')
@mod_required
def stats_viewsperday(req, param=""): def stats_viewsperday(req, param=""):
update_expr = 'INSERT INTO logcache (req, param, trace, date, value) (SELECT "%s", ?, trace, date, y FROM (%s) AS cachetmp WHERE date < ?)' update_expr = 'INSERT INTO logcache (req, param, trace, date, value) (SELECT "%s", ?, trace, date, y FROM (%s) AS cachetmp WHERE date < ?)'
query_expr = '(SELECT date, trace, value AS y FROM logcache WHERE req = "%s" AND param = ?) UNION (SELECT * FROM (%s) AS cachetmp)' query_expr = '(SELECT date, trace, value AS y FROM logcache WHERE req = "%s" AND param = ?) UNION (SELECT * FROM (%s) AS cachetmp)'
date_subexpr = '(SELECT CASE WHEN MAX(date) IS NULL THEN "2000-00-00" ELSE MAX(date) END FROM `logcache` WHERE req = "%s" AND param = ?)' date_subexpr = '(SELECT CASE WHEN MAX(date) IS NULL THEN "2000-00-00" ELSE MAX(date) END FROM `logcache` WHERE req = "%s" AND param = ?)'
queries = { queries = {
'lecture': '(SELECT log.date AS date, formats.description AS trace, COUNT(DISTINCT log.id) AS y FROM log JOIN videos ON videos.id = log.obj_id JOIN formats ON formats.id = videos.video_format WHERE log.date > %T AND videos.lecture_id = ? GROUP BY log.date, videos.video_format) UNION (SELECT log.date AS date, "total" AS trace, COUNT(DISTINCT log.id) AS y FROM log JOIN videos ON videos.id = log.obj_id WHERE log.date > %T AND videos.lecture_id = ? GROUP BY log.date)', 'lecture': '(SELECT log.date AS date, formats.description AS trace, COUNT(DISTINCT log.id) AS y FROM log JOIN videos ON videos.id = log.video JOIN formats ON formats.id = videos.video_format WHERE log.date > %T AND videos.lecture_id = ? GROUP BY log.date, videos.video_format) UNION (SELECT log.date AS date, "total" AS trace, COUNT(DISTINCT log.id) AS y FROM log JOIN videos ON videos.id = log.video WHERE log.date > %T AND videos.lecture_id = ? GROUP BY log.date)',
'course': '(SELECT log.date AS date, formats.description AS trace, COUNT(DISTINCT log.id) AS y FROM log JOIN videos ON videos.id = log.obj_id JOIN lectures ON lectures.id = videos.lecture_id JOIN formats ON formats.id = videos.video_format WHERE log.date > %T AND lectures.course_id = ? GROUP BY log.date, videos.video_format) UNION (SELECT log.date AS date, "total" AS trace, COUNT(DISTINCT log.id) AS y FROM log JOIN videos ON videos.id = log.obj_id JOIN lectures ON lectures.id = videos.lecture_id WHERE log.date > %T AND lectures.course_id = ? GROUP BY log.date)', 'course': '(SELECT log.date AS date, formats.description AS trace, COUNT(DISTINCT log.id) AS y FROM log JOIN videos ON videos.id = log.video JOIN lectures ON lectures.id = videos.lecture_id JOIN formats ON formats.id = videos.video_format WHERE log.date > %T AND lectures.course_id = ? GROUP BY log.date, videos.video_format) UNION (SELECT log.date AS date, "total" AS trace, COUNT(DISTINCT log.id) AS y FROM log JOIN videos ON videos.id = log.video JOIN lectures ON lectures.id = videos.lecture_id WHERE log.date > %T AND lectures.course_id = ? GROUP BY log.date)',
'global': '(SELECT log.date AS date, formats.description AS trace, COUNT(DISTINCT log.id) AS y FROM log JOIN videos ON videos.id = log.obj_id JOIN formats ON formats.id = videos.video_format WHERE log.date > %T GROUP BY log.date, videos.video_format) UNION (SELECT log.date AS date, "total" AS trace, COUNT(DISTINCT log.id) AS y FROM log WHERE log.date > %T GROUP BY log.date)', 'global': '(SELECT log.date AS date, formats.description AS trace, COUNT(DISTINCT log.id) AS y FROM log JOIN videos ON videos.id = log.video JOIN formats ON formats.id = videos.video_format WHERE log.date > %T GROUP BY log.date, videos.video_format) UNION (SELECT log.date AS date, "total" AS trace, COUNT(DISTINCT log.id) AS y FROM log WHERE log.date > %T GROUP BY log.date)',
'courses': 'SELECT log.date AS date, courses.handle AS trace, COUNT(DISTINCT log.id) AS y 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 WHERE log.date > %T GROUP BY log.date, courses.id' 'courses': 'SELECT log.date AS date, courses.handle AS trace, COUNT(DISTINCT log.id) AS y FROM log JOIN videos ON videos.id = log.video JOIN lectures ON lectures.id = videos.lecture_id JOIN courses ON courses.id = lectures.course_id WHERE log.date > %T GROUP BY log.date, courses.id'
} }
expr = queries[req] expr = queries[req]
expr = expr.replace('%T', date_subexpr%('viewsperday.'+req)) expr = expr.replace('%T', date_subexpr%('viewsperday.'+req))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment