mirror of
https://github.com/PhoenixTwoFive/karaoqueue.git
synced 2025-05-19 02:51:48 +02:00
Statistik Import- und Export.
This commit is contained in:
parent
a1da421ffe
commit
6d2941cfca
@ -7,6 +7,7 @@ import os
|
|||||||
import json
|
import json
|
||||||
from flask_basicauth import BasicAuth
|
from flask_basicauth import BasicAuth
|
||||||
from helpers import nocache
|
from helpers import nocache
|
||||||
|
from werkzeug.utils import secure_filename
|
||||||
app = Flask(__name__, static_url_path='/static')
|
app = Flask(__name__, static_url_path='/static')
|
||||||
|
|
||||||
basic_auth = BasicAuth(app)
|
basic_auth = BasicAuth(app)
|
||||||
@ -186,6 +187,54 @@ def query_songs_with_details_suggest(input_string=""):
|
|||||||
return jsonify(result)
|
return jsonify(result)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/songs/stats")
|
||||||
|
@nocache
|
||||||
|
# Return the data from long_term_stats as json
|
||||||
|
def get_stats():
|
||||||
|
db_result = database.get_long_term_stats()
|
||||||
|
data = []
|
||||||
|
for row in db_result:
|
||||||
|
data.append(dict(zip(['id', 'count'], row)))
|
||||||
|
return jsonify(data)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/songs/stats.csv")
|
||||||
|
@nocache
|
||||||
|
# Return data from long_term_stats as csv
|
||||||
|
def get_stats_csv():
|
||||||
|
db_result = database.get_long_term_stats()
|
||||||
|
print(db_result)
|
||||||
|
csv = "Id,Playbacks\n"
|
||||||
|
for row in db_result:
|
||||||
|
csv += str(row[0]) + "," + str(row[1]) + "\n"
|
||||||
|
return Response(csv, mimetype='text/csv')
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/songs/stats.csv", methods=['POST'])
|
||||||
|
@nocache
|
||||||
|
@basic_auth.required
|
||||||
|
# Update long_term_stats from csv
|
||||||
|
def update_stats_csv():
|
||||||
|
if not request.files:
|
||||||
|
abort(400)
|
||||||
|
file = request.files['file']
|
||||||
|
if file.filename is None:
|
||||||
|
abort(400)
|
||||||
|
else:
|
||||||
|
filename = secure_filename(file.filename)
|
||||||
|
if filename == '':
|
||||||
|
abort(400)
|
||||||
|
if not filename.endswith('.csv'):
|
||||||
|
abort(400)
|
||||||
|
if file:
|
||||||
|
if database.import_stats(file):
|
||||||
|
return Response('{"status": "OK"}', mimetype='text/json')
|
||||||
|
else:
|
||||||
|
return Response('{"status": "FAIL"}', mimetype='text/json', status=400)
|
||||||
|
else:
|
||||||
|
abort(400)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/songs/details/<song_id>")
|
@app.route("/api/songs/details/<song_id>")
|
||||||
def get_song_details(song_id):
|
def get_song_details(song_id):
|
||||||
result = database.get_song_details(song_id)
|
result = database.get_song_details(song_id)
|
||||||
|
@ -40,6 +40,20 @@ def import_songs(song_csv):
|
|||||||
return ("Imported songs ({} in Database)".format(num_songs))
|
return ("Imported songs ({} in Database)".format(num_songs))
|
||||||
|
|
||||||
|
|
||||||
|
def import_stats(stats_csv):
|
||||||
|
print("Start importing Stats...")
|
||||||
|
df = pandas.read_csv(stats_csv, sep=',')
|
||||||
|
if (df.columns[0] != "Id" or df.columns[1] != "Playbacks"):
|
||||||
|
return False
|
||||||
|
with get_db_engine().connect() as conn:
|
||||||
|
for index, row in df.iterrows():
|
||||||
|
stmt = text(
|
||||||
|
"INSERT INTO long_term_stats (Id,Playbacks) VALUES (:par_id,:par_playbacks) ON DUPLICATE KEY UPDATE Playbacks=:par_playbacks")
|
||||||
|
conn.execute(stmt, {"par_id": row["Id"], "par_playbacks": row["Playbacks"]})
|
||||||
|
conn.commit()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def create_schema():
|
def create_schema():
|
||||||
create_song_table()
|
create_song_table()
|
||||||
create_entry_table()
|
create_entry_table()
|
||||||
@ -161,6 +175,16 @@ def get_song_suggestions(count: int):
|
|||||||
return cur.fetchall()
|
return cur.fetchall()
|
||||||
|
|
||||||
|
|
||||||
|
def get_long_term_stats():
|
||||||
|
with get_db_engine().connect() as conn:
|
||||||
|
stmt = text("""
|
||||||
|
SELECT lts.Id, lts.Playbacks
|
||||||
|
FROM long_term_stats lts
|
||||||
|
""")
|
||||||
|
cur = conn.execute(stmt)
|
||||||
|
return cur.fetchall()
|
||||||
|
|
||||||
|
|
||||||
def get_song_list():
|
def get_song_list():
|
||||||
with get_db_engine().connect() as conn:
|
with get_db_engine().connect() as conn:
|
||||||
stmt = text("SELECT Artist || \" - \" || Title AS Song, Id FROM songs;")
|
stmt = text("SELECT Artist || \" - \" || Title AS Song, Id FROM songs;")
|
||||||
|
@ -4,7 +4,8 @@
|
|||||||
<form method="post">
|
<form method="post">
|
||||||
<p>
|
<p>
|
||||||
<label for="entryquota">Maximale Anzahl an Einträgen pro Nutzer</label>
|
<label for="entryquota">Maximale Anzahl an Einträgen pro Nutzer</label>
|
||||||
<input type="number" class="form-control" id="entryquota" name="entryquota" min=1 value={{app.config['ENTRY_QUOTA']}}>
|
<input type="number" class="form-control" id="entryquota" name="entryquota" min=1
|
||||||
|
value={{app.config['ENTRY_QUOTA']}}>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<label for="maxqueue">Maximale Anzahl an Einträgen Insgesamt</label>
|
<label for="maxqueue">Maximale Anzahl an Einträgen Insgesamt</label>
|
||||||
@ -24,7 +25,8 @@
|
|||||||
</div>
|
</div>
|
||||||
<p>
|
<p>
|
||||||
<label for="username">Benutzername</label>
|
<label for="username">Benutzername</label>
|
||||||
<input type="text" class="form-control" id="username" name="username" value={{app.config['BASIC_AUTH_USERNAME']}}>
|
<input type="text" class="form-control" id="username" name="username"
|
||||||
|
value={{app.config['BASIC_AUTH_USERNAME']}}>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<label for="password">Passwort ändern</label>
|
<label for="password">Passwort ändern</label>
|
||||||
@ -32,10 +34,69 @@
|
|||||||
</p>
|
</p>
|
||||||
<input type="submit" class="btn btn-primary mr-1 mb-2" value="Einstellungen anwenden">
|
<input type="submit" class="btn btn-primary mr-1 mb-2" value="Einstellungen anwenden">
|
||||||
</form>
|
</form>
|
||||||
|
<form>
|
||||||
|
<p>
|
||||||
|
<label for="statsImport">Statistiken importieren/exportieren</label>
|
||||||
|
</p>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-sm-4">
|
||||||
|
<a class="btn btn-secondary" type="button" id="statsExport" href="/api/songs/stats.csv"><i class="fas fa-download mr-1"></i>Exportieren</a>
|
||||||
|
</div>
|
||||||
|
<div class="col input-group mb-3">
|
||||||
|
<div class="custom-file mr-1">
|
||||||
|
<input type="file" class="custom-file-input" id="statsImport" data-allowed-file-extensions='["csv"]'>
|
||||||
|
<label class="custom-file-label" for="statsImport">CSV-Datei auswählen</label>
|
||||||
|
</div>
|
||||||
|
<button class="btn btn-secondary" type="button" id="statsImportBtn"><i class="fas fa-upload mr-1"></i>Importieren</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
</p>
|
||||||
|
</form>
|
||||||
<details>
|
<details>
|
||||||
<summary>Current config:</summary>
|
<summary>Current config:</summary>
|
||||||
<pre>{% for key, val in config.items() %}{{key}}: {{val}}<br>{% endfor %}</pre>
|
<pre>{% for key, val in config.items() %}{{key}}: {{val}}<br>{% endfor %}</pre>
|
||||||
</details>
|
</details>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block extrajs %}
|
{% block extrajs %}
|
||||||
|
<script>
|
||||||
|
$(document).ready(function () {
|
||||||
|
$('#statsImport').on('change', function () {
|
||||||
|
var fileName = $(this).val().split('\\').pop();
|
||||||
|
$(this).next('.custom-file-label').html(fileName);
|
||||||
|
});
|
||||||
|
$('#statsImportBtn').on('click', function () {
|
||||||
|
var file_data = $('#statsImport').prop('files')[0];
|
||||||
|
var form_data = new FormData();
|
||||||
|
form_data.append('file', file_data);
|
||||||
|
$.ajax({
|
||||||
|
url: '/api/songs/stats.csv',
|
||||||
|
cache: false,
|
||||||
|
contentType: false,
|
||||||
|
processData: false,
|
||||||
|
data: form_data,
|
||||||
|
type: 'post',
|
||||||
|
success: function (response) {
|
||||||
|
toast = {
|
||||||
|
title: "Erfolgreich importiert",
|
||||||
|
message: "Die Statistiken wurden erfolgreich importiert.",
|
||||||
|
status: TOAST_STATUS.SUCCESS,
|
||||||
|
timeout: 5000
|
||||||
|
}
|
||||||
|
Toast.create(toast);
|
||||||
|
},
|
||||||
|
error: function (response) {
|
||||||
|
toast = {
|
||||||
|
title: "Fehler beim Importieren",
|
||||||
|
message: "Die Statistiken konnten nicht importiert werden.",
|
||||||
|
status: TOAST_STATUS.ERROR,
|
||||||
|
timeout: 5000
|
||||||
|
}
|
||||||
|
Toast.create(toast);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
Loading…
x
Reference in New Issue
Block a user