added fetch job which updates the data every n minutes (set by config value)
This commit is contained in:
@@ -8,6 +8,9 @@ CALDAV_PASS=
|
||||
# timezone. for example the default: Europe/Berlin
|
||||
TIMEZONE=
|
||||
|
||||
# the intervall in which the calendar sessions will be re-read in minutes
|
||||
REFETCH_INTERVALL_MINUTES=
|
||||
|
||||
# an comma seperated list of semester starts.
|
||||
# Studytime will only count sessions up to the last date in the past
|
||||
# format: MM-DD,MM-DD,...
|
||||
|
||||
+36
-10
@@ -1,40 +1,66 @@
|
||||
from flask import Flask, jsonify, render_template
|
||||
from src.fetch_sessions import *
|
||||
import os, sys, threading, time
|
||||
from src.parser import *
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
sessions = get_this_semesters_sessions()
|
||||
try:
|
||||
sessions = get_this_semesters_sessions()
|
||||
except Exception:
|
||||
print("Failed to fetch from calDAV server.")
|
||||
sys.exit(1)
|
||||
|
||||
# web ui
|
||||
@app.route("/")
|
||||
def index():
|
||||
labels = [s["subject"] for s in sessions]
|
||||
values = [s["minutes"] for s in sessions]
|
||||
current = list(sessions)
|
||||
labels = [s["subject"] for s in current]
|
||||
values = [s["minutes"] for s in current]
|
||||
return render_template(
|
||||
"index.html",
|
||||
labels=labels,
|
||||
values=values,
|
||||
sessions=sessions
|
||||
sessions=current
|
||||
)
|
||||
|
||||
# internal api
|
||||
|
||||
@app.route("/api/dashboard")
|
||||
def api_dashboard():
|
||||
totals = calculate_totals(sessions)
|
||||
current = list(sessions)
|
||||
totals = calculate_totals(current)
|
||||
return jsonify({
|
||||
"sessions": sessions,
|
||||
"sessions": current,
|
||||
"subjectTotals": totals,
|
||||
"dailyMinutes": get_last_n_days(50, sessions),
|
||||
"stats": get_stats(sessions, totals),
|
||||
"dailyMinutes": get_last_n_days(50, current),
|
||||
"stats": get_stats(current, totals),
|
||||
})
|
||||
|
||||
# external api
|
||||
|
||||
@app.route("/api/sessions")
|
||||
def api_sessions():
|
||||
return jsonify(sessions)
|
||||
current = list(sessions)
|
||||
return jsonify(current)
|
||||
|
||||
|
||||
def fetch_job():
|
||||
global sessions
|
||||
while True:
|
||||
time.sleep(20) # 5 minutes
|
||||
try:
|
||||
sessions = get_this_semesters_sessions()
|
||||
print("Successfully fetched data from calDAV")
|
||||
except Exception:
|
||||
print("Failed to refresh from calDAV server, keeping old data.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
if os.environ.get("WERKZEUG_RUN_MAIN") == "true" or not app.debug:
|
||||
# we need this guard because flask runs __main__ twice in debug mode
|
||||
if not os.environ["REFETCH_INTERVALL_MINUTES"].isdigit():
|
||||
print("Invalid refetch interval")
|
||||
sys.exit(1)
|
||||
t = threading.Thread(target=fetch_job, daemon=True)
|
||||
t.start()
|
||||
|
||||
app.run(host="0.0.0.0", port=5000, debug=True)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from dotenv import load_dotenv
|
||||
from datetime import datetime, timedelta, date, time
|
||||
from datetime import datetime, timedelta, date, time as dtime
|
||||
|
||||
from zoneinfo import ZoneInfo
|
||||
import caldav, os, random
|
||||
@@ -27,7 +27,7 @@ calendar = client.calendar(url=CALDAV_URL)
|
||||
# dont know when id even need an all day event but better be safe so why not
|
||||
def to_datetime(value):
|
||||
if isinstance(value, date) and not isinstance(value, datetime):
|
||||
value = datetime.combine(value, time.min, tzinfo=TIME_ZONE) # just set time to 00:00
|
||||
value = datetime.combine(value, dtime.min, tzinfo=TIME_ZONE) # just set time to 00:00
|
||||
return value
|
||||
|
||||
# grab ALL sessions and return as array of dicts
|
||||
Reference in New Issue
Block a user