-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsonUtility.py
103 lines (73 loc) · 3.07 KB
/
jsonUtility.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import json
import datetime
import csv
import os
from logger_config import *
from models import *
datetimeFormat = "%Y-%m-%dT%H:%M:%S"
timeCheck = 300 # Check every 300 seconds (in production)
JSON_FILE_SERVICES = "services.json"
services = Services.load_from_json_file(JSON_FILE_SERVICES)
def timeUpdate():
with open("services.json", "r") as f:
j = json.load(f)
for k in j.keys():
j[k]["Last access time"] = datetime.datetime.utcnow().strftime(datetimeFormat)
with open("services.json", "w") as out:
json.dump(j, out, indent=4, sort_keys=True, default=str)
def listServices():
return services.names()
def statusUpdate():
for service in listServices():
service.refresh_status()
def deltaTime():
logger.info("[LOG]: Deprecated")
currentDate = datetime.datetime.utcnow()
for k in listServices():
# pastDate = datetime.datetime.strptime(j[k]["Last access time"], datetimeFormat)
print((currentDate - pastDate).total_seconds())
def deltaTimeService(services, service):
if service not in services:
logger.info("[LOG]: requested service is not tracked")
return 0
currentDate = datetime.datetime.utcnow()
pastDate = datetime.datetime.strptime(services[service]["Last access time"], datetimeFormat)
return (currentDate - pastDate).total_seconds() > timeCheck
def updateStatus(services, service, newStatus):
services[service]["Last access time"] = datetime.datetime.utcnow().strftime(datetimeFormat)
services[service]["Last status"] = newStatus
with open("services.json", "w") as out:
json.dump(services, out, indent=4, sort_keys=True, default=str)
def addNewService(service, url):
services.add_service(service, url)
def addBlankCSVService(service: Service):
service = service.name
filepath = "data/"
cols = "date,UP\n"
try:
os.mkdir(filepath + service)
except FileExistsError:
pass
except:
raise ValueError(f"[LOG]: Something went wrong with creating the folder {service}")
with open(filepath + service + "/log.csv", "w") as log:
log.write(cols)
def acceptRequest():
with open("data\\request\\log.csv", "r") as f:
read = csv.reader(f, delimiter=",")
next(read, None)
for row in read:
addNewService(row[1], row[2])
addBlankCSVService(row[1])
with open("data\\request\\log.csv", "w") as f:
f.write("time,service,url,reason\n")
if __name__ == "__main__":
#acceptRequest()
"""
name = [ "Inginious","UDS", "UCLSport","LEPL1104","LEPL1201"]
url = ["https://inginious.info.ucl.ac.be/","https://uds.siws.ucl.ac.be/","https://sites.uclouvain.be/uclsport/",
"https://perso.uclouvain.be/vincent.legat/zouLab/epl1104.php","https://perso.uclouvain.be/vincent.legat/zouLab/epl1201.php" ]
for i in range(len(name)):
services.add_service(name[i], url[i])
"""
#print(deltaTimeService(j, "UCLouvain"))