diff --git a/server/djrandom/frontend/templates/about.html b/server/djrandom/frontend/templates/about.html index ec50e01e6c74fc8d468ae8ca108b25c97853a8b2..713be5580cb1abf6588fbbe4e561121d5f97f78f 100644 --- a/server/djrandom/frontend/templates/about.html +++ b/server/djrandom/frontend/templates/about.html @@ -5,16 +5,27 @@ <h3>{{ config['SITE_NAME'] }}</h3> <p> - <b>{{ num_users }}</b> users, + <b>{{ data['num_users'] }}</b> users, </p> <p> - <b>{{ num_songs }}</b> songs - (<b>{{ used_gb }}</b> Gb). + <b>{{ data['num_songs'] }}</b> songs + (<b>{{ data['used_gb'] }}</b> Gb). </p> - {% if dup_songs %} + {% if data['dup_songs'] %} <p> - <b>{{ dup_songs }}</b> duplicates found. + <b>{{ data['dup_songs'] }}</b> duplicates found. </p> {% endif %} + <hr/> + + <p> + <small>Uploads (30 days):</small><br> + <img src="{{ data['incoming_graph'] }}"> + </p> + <p> + <small>Played songs (30 days):</small><br> + <img src="{{ data['play_graph'] }}"> + </p> + {% endblock %} diff --git a/server/djrandom/frontend/views.py b/server/djrandom/frontend/views.py index 24e6efd1f3b0d7a5d2b9524ec91e92dbfc73b4f8..519355327bb906d467f05df0cdafbad300026d1c 100644 --- a/server/djrandom/frontend/views.py +++ b/server/djrandom/frontend/views.py @@ -1,5 +1,6 @@ import os import logging +import pygooglechart from datetime import datetime from flask import Flask, request, Response, abort, jsonify, render_template, g from djrandom import utils @@ -47,16 +48,32 @@ def songs_fragment(): return render_template('songs_fragment.html', songs=mp3s) +def _makegraph(values): + max_v = max(values) * 1.2 + chart = pygooglechart.SimpleLineChart(220, 125, y_range=[0, max_v]) + chart.add_data(values) + chart.set_colours(['0000FF']) + return chart.get_url() + + @app.route('/about') @require_auth def about(): - num_songs = MP3.query.filter_by(state=MP3.READY).count() - dup_songs = MP3.query.filter_by(state=MP3.DUPLICATE).count() - used_gb = int(Session.query(func.sum(MP3.size)).first()[0] / (2 << 29)) - num_users = User.query.filter_by(active=True).count() - return render_template('about.html', num_users=num_users, - num_songs=num_songs, used_gb=used_gb, - dup_songs=dup_songs, config=app.config) + cache_key = 'about' + data = svcs['cache'].get(cache_key) + if not data: + data = { + 'num_songs': MP3.query.filter_by(state=MP3.READY).count(), + 'dup_songs': MP3.query.filter_by(state=MP3.DUPLICATE).count(), + 'used_gb': int(Session.query(func.sum( + MP3.size)).first()[0] / (2 << 29)), + 'num_users': User.query.filter_by(active=True).count(), + 'incoming_graph': _makegraph(MP3.uploads_by_day(30)), + 'play_graph': _makegraph(PlayLog.plays_by_day(30)), + } + svcs['cache'].set(cache_key, data, timeout=9600) + + return render_template('about.html', data=data, config=app.config) @app.route('/') diff --git a/server/djrandom/model/mp3.py b/server/djrandom/model/mp3.py index ecaeabbdf0ea795c489318ccdf5d27bd7876f72f..2e50cd4b5f56d8273557974d7476634d9cd6f019 100644 --- a/server/djrandom/model/mp3.py +++ b/server/djrandom/model/mp3.py @@ -154,6 +154,7 @@ class MP3(Base): def uploads_by_day(cls, days=30): # select to_days(uploaded_at) as d, count(*) from mp3 group by d order by d asc; result = [] + date_limit = datetime.now() - timedelta(days) for row in Session.query(func.to_days(cls.uploaded_at).label('day'), func.count('*').label('count')).filter( cls.uploaded_at > date_limit).group_by( @@ -206,6 +207,19 @@ class PlayLog(Base): & (cls.stamp > date_limit) ).group_by(cls.sha1).order_by('count desc').limit(n) + @classmethod + def plays_by_day(cls, days=30): + # select to_days(uploaded_at) as d, count(*) from mp3 group by d order by d asc; + result = [] + date_limit = datetime.now() - timedelta(days) + for row in Session.query(func.to_days(cls.stamp).label('day'), + func.count('*').label('count')).filter( + cls.stamp > date_limit).group_by( + 'day').order_by('day asc'): + result.append(row.count) + return result + + class SearchLog(Base):