Skip to content
Snippets Groups Projects
Commit d924d550 authored by ale's avatar ale
Browse files

add pretty graphs of uploads/plays to the about page

parent e618e850
Branches
Tags
No related merge requests found
......@@ -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 %}
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('/')
......
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment