-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.py
508 lines (457 loc) · 19.3 KB
/
query.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
from pymongo import MongoClient
from datetime import datetime, timezone
import re
client = MongoClient('mongodb://localhost:27017/')
db = client['big_data_project_2024']
opt = None
err = False
while (opt == None):
print("\nWhat would you like to do:\n================================================================\n1. Find link with most cars within a time period\n2. Find link with greatest average speed within a time period\n3. Find longest route within a time period\n")
opt = input(f"{'Your option' if not err else 'Invalid option, try again'}: ")
#print("\nGreat!")
if not re.match(r"1|2|3", opt):
err=True
opt = None
print("\033[8A", end='')
print("\033[J", end='')
print("\nMake sure to insert UTC time in the format YYYY-MM-DD HH:MM:SS\n")
#Query parameters
#================================================================================
lower_str = input("Lower date: ")
if lower_str == "": #Default time, for quick demonstration. DO NOT USE
lower_str = "2024-06-21 21:09:09"
print("\033[1A", end='')
print("\033[15G", end='')
print("\b\b2024-06-21 21:09:09")
lower_time = datetime.strptime(lower_str, '%Y-%m-%d %H:%M:%S')
upper_str = input("Upper time: ")
if upper_str == "": #Default time, for quick demonstration. DO NOT USE
upper_str = "2024-06-21 21:10:29"
print("\033[1A", end='')
print("\033[15G", end='')
print("\b\b2024-06-21 21:10:29")
upper_time = datetime.strptime(upper_str, '%Y-%m-%d %H:%M:%S')
print()
#=======================================================================================================================================
if (opt == "1"):
query = [
{
"$match":
{
"time": {"$gte": lower_time, "$lte": upper_time}
}
},
{
"$group":
{
"_id": "$vcount",
"data": {"$push": "$$ROOT"}
}
},
{
"$sort": {"_id": 1}
},
{
"$limit": 1
}
]
#================================================================================
cursor = db["processed"].aggregate(query)
for doc in cursor:
num_vehicles = doc["_id"]
data = doc["data"]
for d in data:
print(f"Link {d['link']} with {num_vehicles} cars at {d['time']}")
#=======================================================================================================================================
elif (opt == "2"):
query = [
{
"$match":
{
"time": {"$gte": lower_time, "$lte": upper_time}
}
},
{
"$group":
{
"_id": "$vspeed",
"data": {"$push": "$$ROOT"}
}
},
{
"$sort": {"_id": -1}
},
{
"$limit": 1
}
]
#================================================================================
cursor = db["processed"].aggregate(query)
for doc in cursor:
avg_speed = doc["_id"]
data = doc["data"]
for d in data:
print(f"Link {d['link']} with average speed {avg_speed} km/h at {d['time']}")
#=======================================================================================================================================
elif (opt == "3"):
query = [
#Group by the vehicle
#Goal is find the distance each vehicle has driven within the time window, along with the actual path it traversed
#At the end, return the vehicle(s) and the paths with the greatest distance
#==========================================================================================================
{
"$group":
{
"_id": "$name",
"docs": {"$push": "$$ROOT"}
}
},
{
"$sort": {"time": 1}
},
#We have a SORTED array of all the entries, for each car
#Change array elements, so that they only contain:
#
# name: Name of the vehicle
# time: The timestamp
# link: The current link
# position: Position within the current link
# next: Information about the next document in the array (5 seconds from now)
# link: The next link
# position: Position within the next link
# dista_until_next_timestamp: The distance I'll drive between now and the next moment. THIS IS WHAT GETS SUMMED UP
#==========================================================================================================
{
"$project":
{
"mappedArray":
{
"$map":
{
"input": {"$range": [0, {"$size": "$docs"}]},
"as": "i",
"in":
{
#Current entry information
"name": {"$arrayElemAt": ["$docs.name", "$$i"]},
"time": {"$arrayElemAt": ["$docs.time", "$$i"]},
"link": {"$arrayElemAt": ["$docs.link", "$$i"]},
"position": {"$arrayElemAt": ["$docs.position", "$$i"]},
#Next entry information
"next":
{
"$cond":
{
#The last document
#The next entry will be about the same link (since I'm on my destination link)(Am I????)
#The position will be the end of the link (500)
"if": {"$eq": ["$$i", {"$subtract": [{"$size": "$docs"}, 1]}]},
"then":
{
"position": 500,
"link": {"$arrayElemAt": ["$docs.link", "$$i"]},
"dista_until_next_timestamp": {"$subtract": [500, {"$arrayElemAt": ["$docs.position", "$$i"]}]}
},
"else":
{
"$cond":
{
"if": #I stay within the same link between timestamps
{
"$eq":
[
{"$arrayElemAt": ["$docs.link", "$$i"] },
{"$arrayElemAt": ["$docs.link", {"$add": ["$$i", 1]} ] }
]
},
"then": #Just subtract the two positions
{
"position": {"$arrayElemAt": ["$docs.position", {"$add": ["$$i", 1]} ] },
"link": {"$arrayElemAt": ["$docs.link", {"$add": ["$$i", 1]} ] },
"dista_until_next_timestamp":
{
"$subtract":
[
{"$arrayElemAt": ["$docs.position", {"$add": ["$$i", 1]} ] },
{"$arrayElemAt": ["$docs.position", "$$i"]}
]
}
},
"else": #Add distance from current position to the end of the current link, plus the remaining distance
{
"position": {"$arrayElemAt": ["$docs.position", {"$add": ["$$i", 1]} ] },
"link": {"$arrayElemAt": ["$docs.link", {"$add": ["$$i", 1]} ] },
"dista_until_next_timestamp":
{
"$add":
[
{"$subtract": [500, {"$arrayElemAt": ["$docs.position", "$$i"]}]},
{"$arrayElemAt": ["$docs.position", {"$add": ["$$i", 1]}]}
]
}
}
}
}
}
}
}
}
}
}
},
#Limit time
#Why am I using "<" instead of "<="?
#Because from each array element, we will use the "distance until THE NEXT timestamp" for the sum
#During this next timestamp, we will be in our final position in the specified time window
#So we don't want the entry for that specific timestamp. We don't care about its next
#==========================================================================================================
{
"$project":
{
"filteredArray":
{
"$filter":
{
"input": "$mappedArray",
"as": "elem",
"cond":
{
"$and":
[
{"$gte": ["$$elem.time", lower_time]},
{"$lt": ["$$elem.time", upper_time]}
]
}
}
}
}
},
{
"$unwind": "$filteredArray"
},
{
"$replaceRoot": {"newRoot": "$filteredArray"}
},
#Calculate final dista for each vehicle
#==========================================================================================================
{
"$group":
{
"_id": "$name",
"docs": {"$push": "$$ROOT"},
"dista_travelled": {"$sum": "$next.dista_until_next_timestamp"}
}
},
#EVERYTHING AFTER THIS POINT IS ABOUT FORMATTING THE PATH
#For each vehicle, find its last link within the time window
#For each array element, mention its position using the constants "first", "last", "middle", "firstlast" (first & last)
#==========================================================================================================
{
"$project":
{
"dista_travelled": 1,
"last_link": {"$arrayElemAt": ["$docs.next.link", -1]},
"temp":
{
"$map":
{
"input": {"$range": [0, {"$size": "$docs"}]},
"as": "i",
"in":
{
"$mergeObjects": [{"$arrayElemAt": ["$docs", "$$i"]},
{
"$cond":
{
"if": {"$eq": ["$$i", 0]}, #Element is first
"then":
{
"$cond":
{
"if": {"$eq": ["$$i", {"$subtract": [{"$size": "$docs"}, 1]}]}, #Element is both first and last
"then": {"index": "firstlast"},
"else": {"index": "first"}
}
},
"else": #Element is not first
{
"$cond":
{
"if": {"$eq": ["$$i", {"$subtract": [{"$size": "$docs"}, 1]}]}, #Element is last
"then": {"index": "last"},
"else":{"index": "middle"}
}
}
}
}]
}
}
}
}
},
#==========================================================================================================
{
"$project":
{
"dista_travelled": 1,
"last_link": 1,
"path":
{
"$filter":
{
"input": "$temp",
"as": "elem",
"cond":
{
"$or":
[
{"$eq": ["$$elem.index", "first"]},
{"$eq": ["$$elem.index", "last"]},
{"$eq": ["$$elem.index", "firstlast"]},
{
"$and":
[
{"$ne": ["$$elem.link", "$$elem.next.link"]},
{"$ne": ["$$elem.index", "last"]}
]
}
]
}
}
}
}
},
#==========================================================================================================
{
"$project":
{
"dista_travelled": 1,
"last_link": 1,
"path":
{
"$map":
{
"input": "$path",
"as": "elem",
"in":
{
"$cond":
{
"if": {"$eq": ["$$elem.index", "first"]},
"then":
{
"$cond":
{
"if": {"$eq": ["$$elem.link", "$$elem.next.link"]},
"then":
{
"$concat": ["$$elem.link", "(", {"$toString": "$$elem.position"}, ")"]
},
"else":
{
"$concat": ["$$elem.link", "(", {"$toString": "$$elem.position"}, ")", " - ", "$$elem.next.link"]
}
}
},
"else":
{
"$cond":
{
"if": {"$eq": ["$$elem.index", "firstlast"]},
"then":
{
"$concat": ["$$elem.link", "(", {"$toString": "$$elem.position"}, ")", " - ", "$$elem.next.link", "(", {"$toString": "$$elem.next.position"}, ")"]
},
"else":
{
"$cond":
{
"if": {"$eq": ["$$elem.index", "last"]},
"then":
{
"$concat": ["$$elem.next.link", "(", {"$toString": "$$elem.next.position"}, ")"]
},
"else": "$$elem.next.link"
}
}
}
}
}
}
}
}
}
},
#==========================================================================================================
{
"$project":
{
"dista_travelled": 1,
"path":
{
"$filter":
{
"input": "$path",
"as": "elem",
"cond":
{
"$ne": ["$$elem", "$last_link"]
}
}
}
}
},
#==========================================================================================================
{
"$project":
{
"dista_travelled": 1,
"path":
{
"$reduce":
{
"input": "$path",
"initialValue": "",
"in":
{
"$concat":
[
{
"$cond":
{
"if": {"$ne": ["$$value", ""]},
"then": {"$concat": ["$$value", " - "]},
"else": ""
}
},
"$$this"
]
}
}
}
}
},
{
"$group":
{
"_id": "$dista_travelled",
"data": {"$push": "$$ROOT"}
}
},
{
"$sort":
{
"_id": -1
}
},
{
"$limit": 1
}
]
cursor = db["raw"].aggregate(query)
for doc in cursor:
dista = doc["_id"]
vehicles = doc["data"]
for vehicle in vehicles:
print(f"Vehicle {vehicle['_id']} drove for {dista} meters on path \"{vehicle['path']}\"")
print()