-
Notifications
You must be signed in to change notification settings - Fork 314
/
step2_pushToMongo.py
executable file
·355 lines (327 loc) · 10.5 KB
/
step2_pushToMongo.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
import pymongo
import json
from helpers.config import *
import os
def pushBuilding(database):
path = getCollectionsPath() + "/buildings.json"
count = 0
try:
file = open(path, encoding="utf8")
except:
print("Path was not correct.")
return
count = 0
print("Pushing Buildings..")
col = database["spaces"]
while True:
line = file.readline()
if not line:
break
try:
col.insert_one(json.loads(line))
count += 1
except:
print("Failed validation: ", json.loads(line))
if count == 0:
print("File is empty.")
else:
print(count, "Buildings were pushed.")
return
def pushCampuses(database):
path = getCollectionsPath() + "/campuses.json"
count = 0
try:
file = open(path, encoding="utf8")
except:
print("Path was not correct.")
return
count = 0
print("Pushing Campuses..")
col = database["campuses"]
while True:
line = file.readline()
if not line:
break
count += 1
col.insert_one(json.loads(line))
if count == 0:
print("File is empty.")
else:
print(count, "Campuses were pushed.")
return
def pushEdges(database):
path = getCollectionsPath() + "/edges.json"
count = 0
try:
file = open(path, encoding="utf8")
except:
print("Path was not correct.")
return
count = 0
print("Pushing Edges..")
col = database["edges"]
while True:
line = file.readline()
if not line:
break
count += 1
col.insert_one(json.loads(line))
if count == 0:
print("File is empty.")
else:
print(count, "Edges were pushed.")
return
# "D:\JsonFiles\\fingerprintswifi.json", collection: FingerprintWifi
def pushFingerprintsWifi(database):
path = getCollectionsPath() + "/fingerprintsWifi"
dupes = 0
countAll = 0
filesPaths = os.listdir(path)
print("Pushing FingerprintsWifi..")
for fileP in filesPaths:
try:
file = open(path + "/" + fileP, encoding="utf8")
except:
print("Path was not correct.")
return
count = 0
col = database["fingerprintsWifi"]
query = {"buid": fileP}
doc = col.find_one(query)
if doc is None:
print("There are no fingerprints in database with buid: " + fileP)
print("Adding fingerprints of building: " + fileP)
else:
# print("Found fingreprints for building: " + fileP)
continue
visited = []
while True:
line = file.readline() # read new obj
if not line:
break
count += 1
found = False
obj = json.loads(line)
newFingerprint = createNewFingerprint(obj) # create new fingerprint with "measurements"
if len(visited) == 0: # if its the first obj then add it to visited fingerprints
visited.insert(0, newFingerprint) # mark as visited
else:
for jsonFingeprint in visited: # for every visited json
if obj["timestamp"] == jsonFingeprint["timestamp"] and obj["buid"] == jsonFingeprint["buid"] \
and obj["floor"] == jsonFingeprint["floor"] and obj["x"] == jsonFingeprint["x"] and obj["y"] == jsonFingeprint["y"] \
and obj["heading"] == jsonFingeprint["heading"]: # locate visited
# dupes += 1
found = True
updateExistingFingerprint(jsonFingeprint, obj) # update visited
if not found:
visited.insert(0, newFingerprint) # if first seen, add it
if count == 0:
print("File is empty.")
else:
print(len(visited), "FingerprintsWifi were pushed for building" + fileP +"\n")
for j in visited:
try:
col.insert_one(addCountSum(j))
countAll += 1
except:
print("Failed validation: ", json.loads(line))
print(countAll, "FingerprintsWifi were pushed in total.")
# print("visited length = ", len(visited))
# print("dupes = ", dupes)
return
def createNewFingerprint(obj):
newFin = obj
newFin["measurements"] = [[obj["MAC"], obj["rss"]]]
del newFin["MAC"]
del newFin["rss"]
return newFin
def updateExistingFingerprint(old, new):
updFin = old
newMeasurement = new["measurements"]
updFin["measurements"] += newMeasurement
def addCountSum(obj):
obj["count"] = len(obj["measurements"])
total = 0
for measurement in obj["measurements"]:
total -= int(measurement[1]) * -1
obj["sum"] = total
return obj
def pushFingerprintsBle(database):
path = getCollectionsPath() + "/fingerprintsBle.json"
count = 0
print("Pushing FingerprintsBle..")
print(count, "json files were pushed.")
return
def pushFloorplans(database):
path = getCollectionsPath() + "/floorplans.json"
count = 0
try:
file = open(path, encoding="utf8")
except:
print("Path was not correct.")
return
count = 0
print("Pushing Floorplans..")
col = database["floorplans"]
while True:
line = file.readline()
if not line:
break
count += 1
col.insert_one(json.loads(line))
if count == 0:
print("File is empty.")
else:
print(count, "Floorplans were pushed.")
return
def pushPois(database):
path = getCollectionsPath() + "/pois.json"
count = 0
try:
file = open(path, encoding="utf8")
except:
print("Path was not correct.")
return
count = 0
print("Pushing Pois..")
col = database["pois"]
while True:
line = file.readline()
if not line:
break
count += 1
col.insert_one(json.loads(line))
if count == 0:
print("File is empty.")
else:
print(count, "Pois were pushed.")
return
def pushUsers(database):
path = getCollectionsPath() + "/users.json"
try:
file = open(path, encoding="utf8")
except:
print("Path was not correct.")
return
count = 0
print("Pushing Users..")
col = database["users"]
while True:
line = file.readline()
if not line:
break
count += 1
col.insert_one(json.loads(line))
if count == 0:
print("File is empty.")
else:
print(count, "Users were pushed.")
return
def dropAllCollections(database, colls):
print("Dropping all collections..")
for x in colls:
mycol = database[x]
print("Dropping ", x)
mycol.drop()
def createCollections(database):
print("\nCreating collections..")
temp = {"created": True}
colList = ["spaces", "campuses", "edges", "fingerprintsBle", "fingerprintsWifi", "floorplans", "pois", "users"]
for x in colList:
coll = database[x]
coll.insert_one(temp)
coll.delete_one(temp)
print("Created ", x)
# Main
uri = 'mongodb://' + MDB_USER + ':' + MDB_PASSWORD + '@' + MDB_DOMAIN_NAME + ':' + MDB_PORT + '/'
client = pymongo.MongoClient(uri)
db = client[MDB_DATABASE]
collections = db.collection_names()
countCollections = len(collections)
print("Current number of collections: ", countCollections)
# dropAllCollections(db, collections); exit()
if countCollections >= 11:
print("Checking collections..")
for x in collections: # are those the correct colletions?
if x != "spaces" and x != "campuses" and x != "edges" and x != "fingerprintsBle" and x != "fingerprintsWifi" \
and x != "floorplans" and x != "pois" and x != "users" and x != "accessPointsWifi" and x != "fingerprintsHeatmap" \
and x != "heatmapWifi1" and x != "heatmapWifi2" and x != "heatmapWifi3" \
and x != "heatmapWifiTimestamp1" and x != "heatmapWifiTimestamp2" and x != "heatmapWifiTimestamp3":
print("Collection ", x, "has wrong name.")
print("Exiting..")
exit()
print("All collections were found.")
print("Drop all collections? [y/n]")
value = input()
if value == "y" or value == "Y" or value == "yes" or value == "YES":
dropAllCollections(db, collections)
else:
print("Do you want to check fingerprintsWifi? [Y/N]. (Recommended if the script crashed before adding all fingerprints)")
value = input()
if value == "y" or value == "Y" or value == "yes" or value == "YES":
pushFingerprintsWifi(db)
else:
print("Exiting..")
exit()
elif countCollections < 9:
buildings = False
campuses = False
edges = False
fingerprintsBle = False
fingerprintsWifi = False
floorplans = False
pois = False
users = False
if "spaces" in collections:
buildings = True
if "campuses" in collections:
campuses = True
if "edges" in collections:
edges = True
if "fingerprintsBle" in collections:
fingerprintsBle = True
if "fingerprintsWifi" in collections:
fingerprintsWifi = True
if "floorplans" in collections:
floorplans = True
if "pois" in collections:
pois = True
if "users" in collections:
users = True
if buildings == False:
pushBuilding(db)
else:
print("Buildings already exists.")
if campuses == False:
pushCampuses(db)
else:
print("Campuses already exists.")
if edges == False:
pushEdges(db)
else:
print("Edges already exists.")
if fingerprintsWifi == False:
pushFingerprintsWifi(db)
else:
print("Fingerprints WiFi already exists.")
print("Do you want to check them anyway? [Y/N]. (Recommended if the script crashed before adding all fingerprins)")
value = input()
if value == "y" or value == "Y" or value == "yes" or value == "YES":
pushFingerprintsWifi(db)
if fingerprintsBle == False:
pushFingerprintsBle(db) # creating empty collection for now
else:
print("Fingerprints BLE already exists.")
if floorplans == False:
pushFloorplans(db)
else:
print("Floorplans already exists.")
if pois == False:
pushPois(db)
else:
print("Pois already exists.")
if users == False:
pushUsers(db)
else:
print("Users already exists.")