-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase_fun.py
244 lines (186 loc) · 7.12 KB
/
database_fun.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import googlemaps
#-------User Management---------#
#Check user by User ID
def checkUserId(db, table, id):
data = db.child(table).get().val()
return id in data.keys()
#function to retrive subsessions (func 1)----Sachini
def retreivesubsessions(db, table, id, date, session):
summary = db.child(table).child(id).child(date).child(session).get()
result_dict = {}
for item in summary.each():
if item.key() == "alt":
break
result_dict[item.key()] = item.val()
#print(result_dict)
return result_dict
#function to retrive only time by given date(func 4)----Sachini
def retrieve_sessions_with_time(db, table, id, date):
summary = db.child(table).child(id).child(date).get().val()
result_dict = {}
for key, value in summary.items():
if key != "date":
result_dict[key] = value["strt"]
print(result_dict)
return result_dict
#function to retrive only date by given id(func 3)----Sachini
def retreivedate(db, table, id):
summary = db.child(table).child(id).get()
result_dict = {}
if summary.val() is None: # Check if the data is None
result_dict['error'] = 'Please input your data correctly'
else:
for item in summary.each():
result_dict[item.key()] = item.val().get("date")
return result_dict
#------------Sumarrlization function starts here----Sachini----#
#find the average of environment temperature
def temp(id, date, session):
data = db.child("history").child(id).child(date).child(session).get()
data_dict = data.val()
sum = 0
a = len(list(data_dict.values()))
for count in range(0, a - 3):
sum = sum + list(data_dict.values())[count]["envTemp"]
result = sum / (a - 3)
print(result)
db.child("history").child(id).child(date).child(session).update(
{"envtemp": result})
#find the average of heartrate
def heart_rate(db,id, date, session):
data = db.child("history").child(id).child(date).child(session).get()
data_dict = data.val()
sum = 0
a = len(list(data_dict.values()))
for count in range(0, a - 4):
sum = sum + list(data_dict.values())[count]["hrtRate"]
result = sum / (a - 4)
print(result)
db.child("history").child(id).child(date).child(session).update(
{"heart rate": result})
#find the average of speed
def speed(db,id, date, session):
data = db.child("history").child(id).child(date).child(session).get()
data_dict = data.val()
sum = 0
a = len(list(data_dict.values()))
for count in range(0, a - 5):
sum = sum + list(data_dict.values())[count]["speed"]
result = sum / (a - 5)
print(result)
db.child("history").child(id).child(date).child(session).update(
{"speed": result})
#find the average of humidity
def humidity(db,id, date, session):
data = db.child("history").child(id).child(date).child(session).get()
data_dict = data.val()
sum = 0
a = len(list(data_dict.values()))
for count in range(0, a - 6):
sum = sum + list(data_dict.values())[count]["humidity"]
result = sum / (a - 6)
print(result)
db.child("history").child(id).child(date).child(session).update(
{"humidity": result})
#find the average of body temperature
def body_temperature(db,id, date, session):
data = db.child("history").child(id).child(date).child(session).get()
data_dict = data.val()
sum = 0
a = len(list(data_dict.values()))
for count in range(0, a - 7):
sum = sum + list(data_dict.values())[count]["bdyTemp"]
result = sum / (a - 7)
print(result)
db.child("history").child(id).child(date).child(session).update(
{"body temp": result})
#function to retrive subpoints func 5
def retreiveonlysubsessions(db,table, id, date, session):
summary = db.child(table).child(id).child(date).child(session).get()
result_dict = {}
for item in summary.each():
if item.key() == "alt":
break
result_dict[item.key()] = item.val()
keys = result_dict.keys()
print(keys)
#print(result_dict)
return result_dict
#function to retrieve only summary func 6
def retrieve_only_summary(db,table, id, date, session):
summary = db.child(table).child(id).child(date).child(session).get()
result_dict = {}
found_alt = False
for item in summary.each():
if found_alt:
if item.key() == "stp":
break
result_dict[item.key()] = item.val()
elif item.key() == "alt":
result_dict[item.key()] = item.val()
found_alt = True
print(result_dict)
return result_dict
# Update user data in Firebase
def update_user_data(db,request,id):
table = 'users' # Firebase table name
#id = '715843108' # User ID from the form or authentication system
# Get the field values from the form
name = request.values.get('name')
email = request.values.get('email')
phonenum = request.values.get('phonenum')
dob=request.values.get('DOB')
timeinterval=request.values.get('timeinterval')
# Update user data in Firebase
if name:
db.child(table).child(id).update({'name': name})
if email:
db.child(table).child(id).update({'email': email})
if phonenum:
db.child(table).child(id).update({'phone': phonenum})
if dob:
db.child(table).child(id).update({'dob': dob})
if timeinterval:
db.child(table).child(id).update({'timeinterval': timeinterval})
return 'User data updated successfully'
# Update user data in Firebase
def delete(db,request):
table = 'history' # Firebase table name
id = '715843108' # User ID from the form or authentication system
# Get the field values from the form
field= request.values.get('delete')
# Update user data in Firebase
db.child("history").child(id).child(field).remove()
return 'deleted successfully'
#function for retrive previous summary
def get_pre_summary(db, id, date,session):
date_node = db.child("history").child(id).child(date).get()
session_keys = list(date_node.val().keys())
current_index = session_keys.index(session)
if current_index > 0:
previous_session_key = session_keys[current_index - 1]
# Access the previous session node
previous_session_node = db.child("history").child(id).child(date).child(
previous_session_key).get()
# Get the dictionary representation of the previous session node
previous_session_data = previous_session_node.val()
if "alt" in previous_session_data:
alt_index = list(previous_session_data.keys()).index("alt")
data_after_alt = {
k: v
for k, v in list(previous_session_data.items())[alt_index:]
if k not in ["strt", "stp", "strAd", "endAd"]
}
return data_after_alt
else:
return None
else:
return None
def summarizer(db,userid,day_id,ses_id):
#hrtRate=heart_rate(db,userid,day_id,ses_id)
#bdyTemp=body_temperature(db,userid,day_id,ses_id)
#envTemp=Etemp(db,userid,day_id,ses_id)
#spd=speed(db,userid,day_id,ses_id)
#hum=humidity(db,userid,day_id,ses_id)
#alt=alt(db,userid,day_id,ses_id)
return 0