mirror of
https://github.com/house-of-vanity/libopenanal.git
synced 2025-07-06 21:24:08 +00:00
Add charts on user page.
This commit is contained in:
23
database.py
23
database.py
@ -153,6 +153,29 @@ class DataBase:
|
||||
}
|
||||
return conf_info
|
||||
|
||||
def get_user_word_count_per_day(self, user_id):
|
||||
sql = """
|
||||
SELECT count(*),
|
||||
date(date, 'unixepoch') as dt
|
||||
FROM relations r
|
||||
WHERE r.user_id = '%s'
|
||||
GROUP BY dt order by dt
|
||||
""" % user_id
|
||||
return self.execute(sql)
|
||||
|
||||
def get_user_message_count_per_day(self, user_id):
|
||||
sql = """
|
||||
SELECT count(dt), dt
|
||||
FROM(
|
||||
SELECT count(date),
|
||||
date(date, 'unixepoch') as dt
|
||||
FROM relations WHERE
|
||||
user_id='%s' GROUP BY date
|
||||
)
|
||||
GROUP BY dt ORDER BY dt
|
||||
""" % user_id
|
||||
return self.execute(sql)
|
||||
|
||||
def get_user_info(self, user_id):
|
||||
if not user_id.isdigit():
|
||||
return False
|
||||
|
44
index.py
44
index.py
@ -4,8 +4,7 @@ import os
|
||||
import requests
|
||||
import time
|
||||
|
||||
from flask import Flask, request, send_from_directory
|
||||
from flask import render_template
|
||||
from flask import Flask, request, send_from_directory, jsonify, render_template
|
||||
|
||||
from database import DataBase
|
||||
|
||||
@ -140,6 +139,47 @@ def stat():
|
||||
now=datetime.datetime.now().strftime('%s'),
|
||||
)
|
||||
|
||||
@app.route('/data', methods=['GET'])
|
||||
def data():
|
||||
action = request.args.get('action', default=None, type=str)
|
||||
if action == 'user_word_count':
|
||||
user_id = request.args.get('user', default=None, type=str)
|
||||
if user_id == None:
|
||||
return jsonify([])
|
||||
raw = db.get_user_word_count_per_day(user_id)
|
||||
js = [
|
||||
{
|
||||
'type': 'scatter',
|
||||
'mode': 'lines+markers',
|
||||
'name': 'Words',
|
||||
'x': [],
|
||||
'y': []
|
||||
}
|
||||
]
|
||||
for day in raw:
|
||||
js[0]['x'].append(day[1])
|
||||
js[0]['y'].append(day[0])
|
||||
elif action == 'user_message_count':
|
||||
user_id = request.args.get('user', default=None, type=str)
|
||||
if user_id == None:
|
||||
return jsonify([])
|
||||
raw = db.get_user_message_count_per_day(user_id)
|
||||
js = [
|
||||
{
|
||||
'type': 'scatter',
|
||||
'mode': 'lines+markers',
|
||||
'name': 'Messages',
|
||||
'x': [],
|
||||
'y': []
|
||||
}
|
||||
]
|
||||
for day in raw:
|
||||
js[0]['x'].append(day[1])
|
||||
js[0]['y'].append(day[0])
|
||||
else:
|
||||
js = []
|
||||
return jsonify(js)
|
||||
|
||||
|
||||
def main():
|
||||
app.run(host=flask_host, port=flask_port)
|
||||
|
@ -8,7 +8,9 @@
|
||||
<!-- Bootstrap CSS -->
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
|
||||
crossorigin="anonymous">
|
||||
|
||||
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
|
||||
|
||||
<link rel="stylesheet" type="text/css" href="../static/css/style.css">
|
||||
<title>libOpenAnal appliance</title>
|
||||
{% endblock %}
|
||||
@ -24,7 +26,8 @@
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
|
||||
crossorigin="anonymous"></script>
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
ultradesu 2018
|
||||
</body>
|
||||
|
||||
</html>
|
@ -113,5 +113,53 @@
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
{% endblock %}
|
||||
<div class="card text-center">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Charts</h5>
|
||||
<div id="words_chart">
|
||||
</div>
|
||||
<div id="messages_chart">
|
||||
</div>
|
||||
<script>
|
||||
var user_id = {{ user_info.id }};
|
||||
var plot_data = [];
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: '/data',
|
||||
data: { action: 'user_word_count', user: user_id },
|
||||
dataType: 'json',
|
||||
success: function (data) {
|
||||
plot_data[0] = data[0];
|
||||
write_plot()
|
||||
}
|
||||
});
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: '/data',
|
||||
data: { action: 'user_message_count', user: user_id },
|
||||
dataType: 'json',
|
||||
success: function (data) {
|
||||
plot_data[1] = data[0];
|
||||
write_plot()
|
||||
}
|
||||
});
|
||||
function write_plot() {
|
||||
var layout = {
|
||||
title: 'Words per day',
|
||||
showlegend: false,
|
||||
//autosize: false,
|
||||
//width: 500,
|
||||
//height: 500,
|
||||
margin: {
|
||||
t: 30,
|
||||
pad: 20
|
||||
},
|
||||
};
|
||||
Plotly.newPlot('words_chart', plot_data, layout, { displayModeBar: false });
|
||||
}
|
||||
</script>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
Reference in New Issue
Block a user