-
Notifications
You must be signed in to change notification settings - Fork 0
/
safeentry_db.py
162 lines (113 loc) · 4.82 KB
/
safeentry_db.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# from asyncio.windows_events import NULL
import json
from datetime import timedelta, datetime
class Database():
'''Read and open json file first for optimised performance'''
def __init__(self):
self.data_file = json.load(open("datas/datas.json", "r"))
self.location_file = json.load(open("datas/location.json", "r"))
'''Function to add new SafeEntry data into datas.json
Args: user's name, nric, visiting location and check in datetime'''
def addData(self, name, nric, location, dateTime):
if nric in self.data_file:
print('NRIC already exist')
cur = self.data_file[nric]
#temporary store key and values
new_location = {
"location": location,
"checkInDateTime": dateTime,
"checkOutDateTime": ""
}
cur.append(new_location)
else:
datas = {
nric: [
{
"name": name,
"location": location,
"checkInDateTime": dateTime,
"checkOutDateTime": ""
}
]
}
self.data_file.update(datas)
json_obj = json.dumps(self.data_file, indent=4)
with open("datas/datas.json", "w") as out:
out.write(json_obj)
'''Function to update existing SafeEntry entry with check out datetime
Args: user's nric and check out datetime'''
def updateData(self, nric, dateTime):
selected_user = self.data_file[nric]
selected_user[-1]["checkOutDateTime"] = dateTime
print(selected_user[-1]["checkOutDateTime"])
json_obj = json.dumps(self.data_file, indent=4)
with open("datas/datas.json", "w") as out:
out.write(json_obj)
def addLocation(self, location, dateTime):
#dateTime input comes in only with date
dateTime += ", 00:00:00"
location = {
location:
{
"Date": dateTime
}
}
self.location_file.update(location)
json_obj = json.dumps(self.location_file, indent=4)
with open("datas/location.json", "w") as out:
out.write(json_obj)
'''Function to get list of locations visited by a Covid case within past 14 days
Returns list of locations'''
def getLocation(self):
locationList = {}
## 2022/6/4
now = datetime.now()
cur = now - timedelta(days=14)
for i in self.location_file:
locationDate = self.location_file[i]["Date"]
locationDateParsed = datetime.strptime(locationDate, '%d/%m/%Y, %H:%M:%S')
#If date visited by Covid case is within 14 days before current date
if (locationDateParsed > cur):
locationList[i] = locationDate
print("Infected locations:", locationList)
return locationList
'''Function to get list of locations visited by user
Returns list of locations'''
def getVisited(self, nric):
locationList = []
if not self.nricExists(nric):
return locationList
for i in self.data_file[nric]:
locationList.append(i["location"])
print(locationList)
return locationList
'''Function to check if user has visited an infected location within past 14 days
Args: nric of user and list of infected locations
Returns dict of location and vist datetime pairs, in the form of a string
Couldn't successfully send dicts over gRPC'''
def getCases(self, nric, infectedLocation: list) -> str:
locationDict = ""
if not self.nricExists(nric):
return locationDict
now = datetime.now()
cur = now - timedelta(days=14)
visitedLocation = self.data_file[nric]
# print(visitedLocation)
for j in visitedLocation:
locations = j["location"]
locationDateTime = j["checkInDateTime"]
locationDateTimeParsed = datetime.strptime(locationDateTime, '%d/%m/%Y, %H:%M:%S')
# If user visited before infection, shouldn't notify
if locationDateTimeParsed > cur and locations in infectedLocation:
infectedDateTimeParsed = datetime.strptime(infectedLocation[locations], '%d/%m/%Y, %H:%M:%S')
if locationDateTimeParsed > infectedDateTimeParsed:
locationDict += f"{locations}|{locationDateTime};"
print(f"Infected locations visited by {nric}: '{locationDict}'")
return locationDict
'''Reusable function to check if NRIC exists in datas.json
Returns true if so'''
def nricExists(self, nric):
if nric in self.data_file:
return True
else:
return False