Statistics

This commit is contained in:
Kekskurse 2019-07-09 12:15:53 +02:00
parent d8801fd30e
commit ab3a95fd76
3 changed files with 70 additions and 45 deletions

View file

@ -192,50 +192,6 @@ def function_updateAccountsAtFunk():
else:
print("Cant update plan!!! ERROR")
@app.route("/updateAll")
@requires_auth
def function_updateAccountsAtFunkOLD():
cur = get_db().cursor()
today = datetime.datetime.now()
done = cur.execute("SELECT count(*) FROM run_actions WHERE year = %s AND month = %s AND day = %s" % (today.year, today.month, today.day)).fetchone()
if done[0] == 0:
numbers = cur.execute("SELECT number FROM accounts").fetchall()
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
for number in numbers:
print("Update Account: "+str(number[0]))
#function_updateAccount(number[0])
account = function_getAccount(number[0], True)
print(account)
nextPlan = function_getPlanForDay(number[0], tomorrow.year, tomorrow.month, tomorrow.day)
if app.debug == False:
if account["currentPlan"] == nextPlan:
print("Plan match, nothing to do")
else:
api = FunkAPI(account["mail"], account["password"])
if nextPlan == "1 GB":
api.order1GBPlan()
elif nextPlan == "unlimited":
api.orderUnlimitedPlan()
elif nextPlan == "Break":
api.startPause()
else:
print("SOMETHING GO WRONG")
cur.execute("INSERT INTO run_actions VALUES (%s, %s, %s) " % (today.year, today.month, today.day))
get_db().commit()
return "OK";
else:
return "Done for today"
#Routes
@app.route("/")
@requires_auth
@ -267,6 +223,31 @@ def gui_account(number):
return render_template("account.html", debug=app.debug, account=account, plans=plans, special_days=special_days, currentTime=int(time.time()))
@app.route("/account/<number>/statistics")
@requires_auth
def gui_statistic(number):
cur = get_db().cursor()
ende = datetime.datetime.now()
start = ende - relativedelta(months=1)
delta = ende - start
dayList = []
for i in range(delta.days + 1):
day = {}
date = start + datetime.timedelta(days=i)
dayStart = datetime.datetime(date.year, date.month, date.day, 0, 0, 0)
dayEnde = datetime.datetime(date.year, date.month, date.day, 23, 59, 59)
day["date"] = str(date.day)+"."+str(date.month)+"."+str(date.year)
lastUpdate = cur.execute("SELECT dataUsed, updateTime FROM `updates` WHERE `number` = %s AND `updateTime` > %s AND `updateTime` < %s ORDER BY `updateTime` DESC" % (number, int(dayStart.timestamp()), int(dayEnde.timestamp()))).fetchone()
if lastUpdate == None:
day["usage"] = 0
else:
day["usage"] = round(lastUpdate[0], 4);
dayList.append(day)
return render_template("statistics.html", debug=app.debug, dayList=dayList)
@app.route("/account/<number>/special", methods=['POST'])
@requires_auth
def special_days(number):

View file

@ -31,7 +31,7 @@
</tr>
<tr>
<th>Data Usage</th>
<td>{{ account["dataUsed"]|round(2) }}% <a href="#" class="btn btn-sm btn-success">See Statistics</a></td>
<td>{{ account["dataUsed"]|round(2) }}% <a href="/account/{{ account["number"] }}/statistics" class="btn btn-sm btn-success">See Statistics</a></td>
</tr>
</table>
<a href="#" class="btn btn-warning btn-sm updateNow">Update now</a>
@ -177,6 +177,7 @@
<option value="{{ plan["number"] }}" >{{ plan["name"] }}</option>
{% endif %}
{% endfor %}
<option value="Default">Default (remove other Special Days)</option>
</select><br>
<input type="submit" class="btn btn-success" value="Save">

43
templates/statistics.html Normal file
View file

@ -0,0 +1,43 @@
{% extends "base.html" %}
{% block content %}
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
<h3>Statistics</h3>
<canvas id="myChart"></canvas>
<script language="javascript">
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',
// The data for our dataset
data: {
labels: [
{% for d in dayList %}
'{{ d["date"] }}',
{% endfor %}
],
datasets: [{
label: 'Mobile Data Usage in %',
//backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
fill: false,
data: [
{% for d in dayList %}
'{{ d["usage"] }}',
{% endfor %}
]
}],
},
options: {
scales: {
yAxes: [{
ticks: {
suggestedMin: 00,
suggestedMax: 100
}
}]
}
}
});
</script>
{% endblock %}