Skip to content

Commit

Permalink
Now doing request async so no more slow loading on user startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tfloow committed Jan 23, 2024
1 parent 3d50015 commit f50fe9c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
43 changes: 30 additions & 13 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import json
import requests
import csv
from apscheduler.schedulers.background import BackgroundScheduler # To schedule the check
import datetime
import logging

# My modules
import jsonUtility
Expand All @@ -18,26 +21,40 @@ def urlOfService(services, service):
return services[service]["url"]
return "NaN"

def statusService(services, service):
def updateStatusService(services, service):
url = urlOfService(services, service)
if url == "NaN":
print("[LOG]: You passed a service that is not tracked")
return False

needToCheck = jsonUtility.deltaTimeService(services, service)
print(f"[LOG]: HTTP request for {services[service]["url"]}")
response = requests.get(url).status_code < 400 # Starting 400 codes are error for HTTP GET

if needToCheck:
print(f"[LOG]: HTTP request for {services[service]["url"]}")
response = requests.get(url).status_code < 400 # Starting 400 codes are error for HTTP GET

jsonUtility.updateStatus(services, service, response)

return response
else:
return services[service]["Last status"]
jsonUtility.updateStatus(services, service, response)

return response

def statusService(services, service):
url = urlOfService(services, service)
if url == "NaN":
print("[LOG]: You passed a service that is not tracked")
return False

return services[service]["Last status"]

def refreshServices(services):
print("[LOG]: Refreshing the services")
for service in services.keys():
updateStatusService(services, service)

# Setup Scheduler to periodically check the status of the website
scheduler = BackgroundScheduler()
scheduler.add_job(refreshServices, "interval" ,args=[services], minutes=jsonUtility.timeCheck, next_run_time=datetime.datetime.now() + datetime.timedelta(seconds=1))

# Start the scheduler
scheduler.start()

# Start the Flask app
# ------------------ Start the Flask app ------------------
app = Flask("UCLouvainDown")

@app.route("/")
Expand Down Expand Up @@ -135,7 +152,7 @@ def extractLog():
@app.route('/robots.txt')
@app.route('/sitemap.xml')
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])
return send_from_directory(app.static_folder, request.path[1:])

if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pytz>=2023.3.post1
Flask>=3.0.0
requests>=2.31.0
gunicorn>=21.2.0
matplotlib>=3.8.2
matplotlib>=3.8.2
APScheduler>=3.10.4
20 changes: 10 additions & 10 deletions services.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
{
"404-Test": {
"Last access time": "2024-01-23T17:42:05",
"Last access time": "2024-01-23T19:48:44",
"Last status": false,
"url": "https://www.google.com/404"
},
"ADE": {
"Last access time": "2024-01-23T17:42:05",
"Last access time": "2024-01-23T19:48:44",
"Last status": true,
"url": "https://horaire.uclouvain.be/direct/"
},
"ADE-Scheduler": {
"Last access time": "2024-01-23T17:42:05",
"Last access time": "2024-01-23T19:48:45",
"Last status": true,
"url": "https://ade-scheduler.info.ucl.ac.be/calendar/"
},
"Comproved": {
"Last access time": "2024-01-23T17:42:05",
"Last access time": "2024-01-23T19:48:46",
"Last status": true,
"url": "https://app.comproved.com/universite-de-louvain"
},
"Gradescope": {
"Last access time": "2024-01-23T17:42:06",
"Last access time": "2024-01-23T19:48:48",
"Last status": true,
"url": "https://www.gradescope.com/"
},
"Intranet": {
"Last access time": "2024-01-23T17:42:06",
"Last access time": "2024-01-23T19:48:49",
"Last status": true,
"url": "https://intranet.uclouvain.be"
},
"LEPL1104": {
"Last access time": "2024-01-23T17:42:06",
"Last access time": "2024-01-23T19:48:49",
"Last status": true,
"url": "https://perso.uclouvain.be/vincent.legat/zouLab/epl1104.php"
},
"LEPL1201": {
"Last access time": "2024-01-23T17:42:06",
"Last access time": "2024-01-23T19:48:50",
"Last status": true,
"url": "https://perso.uclouvain.be/vincent.legat/zouLab/epl1201.php"
},
"Moodle": {
"Last access time": "2024-01-23T17:42:06",
"Last access time": "2024-01-23T19:48:51",
"Last status": true,
"url": "https://moodle.uclouvain.be/"
},
"UCLouvain": {
"Last access time": "2024-01-23T17:42:07",
"Last access time": "2024-01-23T19:48:53",
"Last status": true,
"url": "https://www.uclouvain.be/"
}
Expand Down

0 comments on commit f50fe9c

Please sign in to comment.