Statistics page added to magneticow.
This commit is contained in:
parent
8b837be714
commit
a3adf88b45
@ -17,6 +17,7 @@ import functools
|
||||
from datetime import datetime
|
||||
import logging
|
||||
import sqlite3
|
||||
import time
|
||||
import os
|
||||
|
||||
import appdirs
|
||||
@ -206,6 +207,29 @@ def torrent(**kwargs):
|
||||
return flask.render_template("torrent.html", **context)
|
||||
|
||||
|
||||
@app.route("/statistics")
|
||||
@requires_auth
|
||||
def statistics():
|
||||
with magneticod_db:
|
||||
now = int(time.time())
|
||||
# Retrieve all the torrents discovered in the past 30 days (30 days * 24 hours * 60 minutes * 60 seconds...)
|
||||
# Also, see http://www.sqlite.org/lang_datefunc.html for details of `date()`.
|
||||
# Function Equivalent strftime()
|
||||
# date(...) strftime('%Y-%m-%d', ...)
|
||||
cur = magneticod_db.execute(
|
||||
"SELECT date(discovered_on, 'unixepoch') AS day, count() FROM torrents WHERE discovered_on >= ? "
|
||||
"GROUP BY day;",
|
||||
(now - 30 * 24 * 60 * 60, )
|
||||
)
|
||||
results = cur.fetchall() # for instance, [('2017-04-01', 17428), ('2017-04-02', 28342)]
|
||||
|
||||
return flask.render_template("statistics.html", **{
|
||||
# We directly substitute them in the JavaScript code.
|
||||
"dates": str([t[0] for t in results]),
|
||||
"amounts": str([t[1] for t in results])
|
||||
})
|
||||
|
||||
|
||||
def initialize_magneticod_db() -> None:
|
||||
global magneticod_db
|
||||
|
||||
@ -216,7 +240,6 @@ def initialize_magneticod_db() -> None:
|
||||
|
||||
with magneticod_db:
|
||||
magneticod_db.execute("PRAGMA journal_mode=WAL;")
|
||||
magneticod_db.execute("PRAGMA temp_store=2;")
|
||||
|
||||
magneticod_db.execute("CREATE VIRTUAL TABLE temp.fts_torrents USING fts4(name);")
|
||||
magneticod_db.execute("INSERT INTO fts_torrents (docid, name) SELECT id, name FROM torrents;")
|
||||
|
75
magneticow/magneticow/static/scripts/plotly-v1.26.1.min.js
vendored
Normal file
75
magneticow/magneticow/static/scripts/plotly-v1.26.1.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
15
magneticow/magneticow/static/scripts/statistics.js
Normal file
15
magneticow/magneticow/static/scripts/statistics.js
Normal file
@ -0,0 +1,15 @@
|
||||
"use strict";
|
||||
|
||||
|
||||
window.onload = function() {
|
||||
Plotly.newPlot("discoveryRateGraph", discoveryRateData, {
|
||||
title: "New Discovered Torrents Per Day in the Past 30 Days",
|
||||
xaxis: {
|
||||
title: "Days",
|
||||
tickformat: "%d %B"
|
||||
},
|
||||
yaxis: {
|
||||
title: "Amount of Torrents Discovered"
|
||||
}
|
||||
});
|
||||
};
|
@ -24,7 +24,4 @@ main > div {
|
||||
|
||||
footer {
|
||||
margin-top: 0.833em;
|
||||
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
21
magneticow/magneticow/static/styles/statistics.css
Normal file
21
magneticow/magneticow/static/styles/statistics.css
Normal file
@ -0,0 +1,21 @@
|
||||
header {
|
||||
padding-bottom: 0.833em;
|
||||
border-bottom: 1px solid;
|
||||
margin-bottom: 0.833em;
|
||||
}
|
||||
|
||||
|
||||
header a {
|
||||
text-decoration: none;
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
|
||||
#discoveryRateGraph {
|
||||
width: calc(540px * 1.618);
|
||||
height: 540px;
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
border-style: solid;
|
||||
border-width: 1px;
|
||||
}
|
@ -17,7 +17,7 @@
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
Now with {{ "{:,}".format(n_torrents) }} torrents available !
|
||||
~{{ "{:,}".format(n_torrents) }} torrents available (see the <a href="/statistics">statistics</a>).
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
|
29
magneticow/magneticow/templates/statistics.html
Normal file
29
magneticow/magneticow/templates/statistics.html
Normal file
@ -0,0 +1,29 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Statistics - magneticow</title>
|
||||
<link rel="stylesheet" href=" {{ url_for('static', filename='styles/reset.css') }} ">
|
||||
<link rel="stylesheet" href=" {{ url_for('static', filename='styles/essential.css') }} ">
|
||||
<link rel="stylesheet" href=" {{ url_for('static', filename='styles/statistics.css') }} ">
|
||||
<script defer src=" {{ url_for('static', filename='scripts/plotly-v1.26.1.min.js') }} "></script>
|
||||
<script defer src=" {{ url_for('static', filename='scripts/statistics.js') }} "></script>
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<script>
|
||||
var discoveryRateData = [{
|
||||
x: {{ dates | safe }},
|
||||
y: {{ amounts | safe }},
|
||||
mode: "lines+markers"
|
||||
}];
|
||||
</script>
|
||||
|
||||
<div><a href="/"><b>magnetico<sup>w</sup></b></a>​<sub>(pre-alpha)</sub></div>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<div id="discoveryRateGraph"></div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user