-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
643 lines (578 loc) · 20.2 KB
/
app.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
from os import getenv
import collections
import argparse
import itertools
import os
import json
import numpy as np
import subprocess
import ppygis
import psycopg2
import psycopg2.extras
import gpxpy
import gpxpy.gpx
import datetime
import life_source
import threading
import multiprocessing
from multiprocessing import Process
from threading import Thread
from life_source import Life
from life_source import Day
from flask import Response, Flask, request, abort, render_template
from flask import jsonify
from flask_restful import Resource, Api
from json import dumps
app = Flask(__name__)
api = Api(app)
############################################################################
################ LIFE and tracks manipulation ####################
############################################################################
life = Life("MyTracks.life")
# We use the RDP-simplified tracks to show information on the
# hexbins and the tracks themselves more quickly.
# It lowers the quantity of data sent through the endpoints,
# allowing more speed and responsiveness.
# We will lose little to no quality of the visual information.
files_directory = 'MyTracks/ProcessedTracks/'
def moreTimeSpent (day_number, hour_number):
monday_list = []
tuesday_list = []
wednesday_list = []
thursday_list = []
friday_list = []
saturday_list = []
sunday_list = []
day_list = []
for day in life.days:
date_object = datetime.datetime.strptime(day.date, '%Y_%m_%d')
day_list.append(date_object)
for date in day_list:
# For the datetime.weekday() function, Monday is 0 and Sunday is 6
# We need to convert to a date object in order to use the weekday()
# Then it is converted again to the LIFE format
if (date.weekday() == 0):
monday_list.append(date.strftime('%Y_%m_%d'))
elif (date.weekday() == 1):
tuesday_list.append(date.strftime('%Y_%m_%d'))
elif (date.weekday() == 2):
wednesday_list.append(date.strftime('%Y_%m_%d'))
elif (date.weekday() == 3):
thursday_list.append(date.strftime('%Y_%m_%d'))
elif (date.weekday() == 4):
friday_list.append(date.strftime('%Y_%m_%d'))
elif (date.weekday() == 5):
saturday_list.append(date.strftime('%Y_%m_%d'))
elif (date.weekday() == 6):
sunday_list.append(date.strftime('%Y_%m_%d'))
# Convert the hour number to military format
if (hour_number == 1):
hour_number = "0000"
elif (hour_number == 2):
hour_number = "0100"
elif (hour_number == 3):
hour_number = "0200"
elif (hour_number == 4):
hour_number = "0300"
elif (hour_number == 5):
hour_number = "0400"
elif (hour_number == 6):
hour_number = "0500"
elif (hour_number == 7):
hour_number = "0600"
elif (hour_number == 8):
hour_number = "0700"
elif (hour_number == 9):
hour_number = "0800"
elif (hour_number == 10):
hour_number = "0900"
elif (hour_number == 11):
hour_number = "1000"
elif (hour_number == 12):
hour_number = "1100"
elif (hour_number == 13):
hour_number = "1200"
elif (hour_number == 14):
hour_number = "1300"
elif (hour_number == 15):
hour_number = "1400"
elif (hour_number == 16):
hour_number = "1500"
elif (hour_number == 17):
hour_number = "1600"
elif (hour_number == 18):
hour_number = "1700"
elif (hour_number == 19):
hour_number = "1800"
elif (hour_number == 20):
hour_number = "1900"
elif (hour_number == 21):
hour_number = "2000"
elif (hour_number == 22):
hour_number = "2100"
elif (hour_number == 23):
hour_number = "2200"
elif (hour_number == 24):
hour_number = "2300"
# Operate the day and hour numbers
if (day_number == 1):
final = []
label_array = []
time_spent_array = []
for day in sunday_list:
# We ll calculate in the half hour, as a rough estimate
plus_half = '%04d' % (int(hour_number) + 30)
half_hour_later = life.where_when(day, plus_half)
if (not isinstance(half_hour_later, basestring)):
pass
elif (half_hour_later is not None):
label_array.append(half_hour_later)
# Appended the label of the stay associated with that day/hour tuple. We have only
# identified it. Now we need the duration of that exact stay, in that exact day/hour.
# For that, we need to find the spans that contain the stays for each label, and then
# match the days of the spans to the days in our day_of_the_week_list.
# When a match is found, it means we got the span corresponding to that label, in
# that exact day. We also need to make sure that the span is in the correct hour.
# If it is, we finally have the desired span, and only need to get its length.
else:
pass
for datum in label_array:
tmp = life.when_at(datum)
for span in tmp:
if (life_source.minutes_to_military(span.start) <= plus_half <= life_source.minutes_to_military(span.end)):
if (span.day == day):
time_spent_array.append(span.length())
# final step: convert the arrays to comma-separated or hyphen-separated strings and return them on an array
# in which the first position has the label string and the second position has the time spent string
# concatenated_labels = ", ".join(label_array)
# concatenated_times = ','.join(map(str, time_spent_array))
seen = set()
uniq = []
for x in label_array:
if x not in seen:
uniq.append(x)
seen.add(x)
final.append(uniq)
final.append(sum(time_spent_array))
return final
elif (day_number == 2):
final = []
label_array = []
time_spent_array = []
for day in monday_list:
# We ll calculate in the half hour, as a rough estimate
plus_half = '%04d' % (int(hour_number) + 30)
half_hour_later = life.where_when(day, plus_half)
if (not isinstance(half_hour_later, basestring)):
pass
elif (half_hour_later is not None):
label_array.append(half_hour_later)
else:
pass
for datum in label_array:
tmp = life.when_at(datum)
for span in tmp:
if (life_source.minutes_to_military(span.start) <= plus_half <= life_source.minutes_to_military(span.end)):
if (span.day == day):
time_spent_array.append(span.length())
# concatenated_labels = ", ".join(label_array)
# concatenated_times = ','.join(map(str, time_spent_array))
seen = set()
uniq = []
for x in label_array:
if x not in seen:
uniq.append(x)
seen.add(x)
final.append(uniq)
final.append(sum(time_spent_array))
return final
elif (day_number == 3):
final = []
label_array = []
time_spent_array = []
for day in tuesday_list:
# We ll calculate in the half hour, as a rough estimate
plus_half = '%04d' % (int(hour_number) + 30)
half_hour_later = life.where_when(day, plus_half)
if (not isinstance(half_hour_later, basestring)):
pass
elif (half_hour_later is not None):
label_array.append(half_hour_later)
else:
pass
for datum in label_array:
tmp = life.when_at(datum)
for span in tmp:
if (life_source.minutes_to_military(span.start) <= plus_half <= life_source.minutes_to_military(span.end)):
if (span.day == day):
time_spent_array.append(span.length())
# concatenated_labels = ", ".join(label_array)
# concatenated_times = ','.join(map(str, time_spent_array))
seen = set()
uniq = []
for x in label_array:
if x not in seen:
uniq.append(x)
seen.add(x)
final.append(uniq)
final.append(sum(time_spent_array))
return final
elif (day_number == 4):
final = []
label_array = []
time_spent_array = []
for day in wednesday_list:
# We ll calculate in the half hour, as a rough estimate
plus_half = '%04d' % (int(hour_number) + 30)
half_hour_later = life.where_when(day, plus_half)
if (not isinstance(half_hour_later, basestring)):
pass
elif (half_hour_later is not None):
label_array.append(half_hour_later)
else:
pass
for datum in label_array:
tmp = life.when_at(datum)
for span in tmp:
if (life_source.minutes_to_military(span.start) <= plus_half <= life_source.minutes_to_military(span.end)):
if (span.day == day):
time_spent_array.append(span.length())
# concatenated_labels = ", ".join(label_array)
# concatenated_times = ','.join(map(str, time_spent_array))
seen = set()
uniq = []
for x in label_array:
if x not in seen:
uniq.append(x)
seen.add(x)
final.append(uniq)
final.append(sum(time_spent_array))
return final
elif (day_number == 5):
final = []
label_array = []
time_spent_array = []
for day in thursday_list:
# We ll calculate in the half hour, as a rough estimate
plus_half = '%04d' % (int(hour_number) + 30)
half_hour_later = life.where_when(day, plus_half)
if (not isinstance(half_hour_later, basestring)):
pass
elif (half_hour_later is not None):
label_array.append(half_hour_later)
else:
pass
for datum in label_array:
tmp = life.when_at(datum)
for span in tmp:
if (life_source.minutes_to_military(span.start) <= plus_half <= life_source.minutes_to_military(span.end)):
if (span.day == day):
time_spent_array.append(span.length())
# concatenated_labels = ", ".join(label_array)
# concatenated_times = ','.join(map(str, time_spent_array))
seen = set()
uniq = []
for x in label_array:
if x not in seen:
uniq.append(x)
seen.add(x)
final.append(uniq)
final.append(sum(time_spent_array))
return final
elif (day_number == 6):
final = []
label_array = []
time_spent_array = []
for day in friday_list:
# We ll calculate in the half hour, as a rough estimate
plus_half = '%04d' % (int(hour_number) + 30)
half_hour_later = life.where_when(day, plus_half)
if (not isinstance(half_hour_later, basestring)):
pass
elif (half_hour_later is not None):
label_array.append(half_hour_later)
else:
pass
for datum in label_array:
tmp = life.when_at(datum)
for span in tmp:
if (life_source.minutes_to_military(span.start) <= plus_half <= life_source.minutes_to_military(span.end)):
if (span.day == day):
time_spent_array.append(span.length())
# concatenated_labels = ", ".join(label_array)
# concatenated_times = ','.join(map(str, time_spent_array))
seen = set()
uniq = []
for x in label_array:
if x not in seen:
uniq.append(x)
seen.add(x)
final.append(uniq)
final.append(sum(time_spent_array))
return final
elif (day_number == 7):
final = []
label_array = []
time_spent_array = []
for day in saturday_list:
# We ll calculate in the half hour, as a rough estimate
plus_half = '%04d' % (int(hour_number) + 30)
half_hour_later = life.where_when(day, plus_half)
if (not isinstance(half_hour_later, basestring)):
pass
elif (half_hour_later is not None):
label_array.append(half_hour_later)
else:
pass
for datum in label_array:
tmp = life.when_at(datum)
for span in tmp:
if (life_source.minutes_to_military(span.start) <= plus_half <= life_source.minutes_to_military(span.end)):
if (span.day == day):
time_spent_array.append(span.length())
# concatenated_labels = ", ".join(label_array)
# concatenated_times = ','.join(map(str, time_spent_array))
seen = set()
uniq = []
for x in label_array:
if x not in seen:
uniq.append(x)
seen.add(x)
final.append(uniq)
final.append(sum(time_spent_array))
return final
def loadLatLon(gpx, vector):
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
print 'Point at ({0},{1}) -> {2} {3}'.format(point.latitude, point.longitude, point.elevation, point.time)
# Hexbin library works with (lon, lat) instead of (lat, lon)
vector.append([point.longitude, point.latitude, point.time.strftime("%Y-%m-%d")])
time_spent_result = []
result = []
def gpxParse():
files =[]
for f in os.listdir(files_directory):
files.append(f)
files.sort()
for f in files:
if f.endswith(".gpx"):
filename = os.path.join(files_directory, f)
print filename
gpx_file = open(filename, 'r')
tracks = gpxpy.parse(gpx_file)
loadLatLon(tracks, result)
def staysGraphProcessing():
for day in range(1, 8):
for hour in range (1, 25):
time_label = moreTimeSpent(day, hour)
d = {
'day': day,
'hour': hour,
'time_spent': time_label[1],
'label': time_label[0]
}
print "append"
time_spent_result.append(d)
############################################################################
################ Database connection and operations ########################
############################################################################
def connectDB():
connectionString = 'dbname=tracemysteps user=PedroFrancisco host=localhost'
#print connectionString
try:
return psycopg2.connect(connectionString)
except:
print("Can't connect to database")
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
@app.route("/")
def Home():
return render_template("index.html")
class Hexbin_Places_Data(Resource):
def get(self):
#Connect to databse
conn = connectDB()
cur = conn.cursor()
#Perform query and return JSON data
try:
#Centroids cannot be null, we need them to show the coordinates
cur.execute("SELECT ST_X(centroid::geometry), ST_Y(centroid::geometry), visit_frequency, label FROM locations WHERE centroid NOTNULL")
except:
print("Error executing select")
response = cur.fetchall()
array = []
for datum in response:
for _ in range(datum[2]):
# Hexbin library works with (lon, lat) instead of (lat, lon)
array.append([datum[1], datum[0], datum[3], datum[2]])
return array
class Calendar_Data(Resource):
def get(self):
result = []
# Times are converted to seconds
for day in life.days:
details_array = []
for span in day.spans:
if type(span.place) is str:
real_date = datetime.datetime.strptime("%s %s" % (day.date, life_source.minutes_to_military(span.start)), "%Y_%m_%d %H%M")
details = {
'name': span.place,
'date': str(real_date),
'value': (span.length() * 60),
}
details_array.append(details)
data = {
'date': datetime.datetime.strptime(day.date, '%Y_%m_%d').strftime('%Y-%m-%d'),
'total': (day.somewhere() * 60),
'details': details_array
}
result.append(data)
return result
class Area_Gradient_Data(Resource):
def get(self):
result = []
for day in life.days:
d = {
'date' : datetime.datetime.strptime(day.date, '%Y_%m_%d').strftime('%Y-%m-%d'),
'price': day.moving()
}
result.append(d)
return result
class GPS_Tracks(Resource):
def get(self):
files =[]
for f in os.listdir(files_directory):
if f.endswith(".gpx"):
files.append(f)
files.sort()
return files
class BarChart_Frequency_Data(Resource):
def get(self):
#Connect to databse
conn = connectDB()
cur = conn.cursor()
#Perform query and return JSON data
try:
cur.execute("select json_build_object('label', label, 'value', visit_frequency) from locations")
except:
print("Error executing select")
FrequencyData = list (i[0] for i in cur.fetchall())
return FrequencyData
class BarChart_TimeSpent_Data(Resource):
def get(self):
#Connect to databse
conn = connectDB()
cur = conn.cursor()
#Perform query and return JSON data
try:
cur.execute("select json_build_object('label', location_label, 'value', time_spent, 'date', ddmmyy) from stays")
except:
print("Error executing select")
TimeData = list (i[0] for i in cur.fetchall())
return TimeData
class Chord_Data(Resource):
def get(self):
#Connect to databse
conn = connectDB()
cur = conn.cursor()
#Perform query and return JSON data
try:
cur.execute("select json_build_object('from', start_location, 'to', end_location, 'start', start_of_trip) from trips")
except:
print("Error executing select")
ChordList = list (i[0] for i in cur.fetchall())
return ChordList
class Arc_Edges_Data(Resource):
def get(self):
#Connect to databse
conn = connectDB()
cur = conn.cursor()
#Perform query and return JSON data
try:
cur.execute("select json_build_object('source', start_location, 'target', end_location, 'frequency', 1) from trips")
except:
print("Error executing select")
ArcList = list (i[0] for i in cur.fetchall())
counter = collections.Counter()
for datum in ArcList:
if (datum['source'] == datum['target']):
# Ignore trips with the same source and target
pass
else:
counter[(datum['source'], datum['target'])] += datum['frequency']
processed_ArcList = [{
'source': k[0],
'target': k[1],
'frequency': v,
} for k, v in counter.items()]
return processed_ArcList
class Arc_Nodes_Data(Resource):
def get(self):
#Connect to databse
conn = connectDB()
cur = conn.cursor()
#Perform query and return JSON data
try:
cur.execute("SELECT start_location FROM trips UNION SELECT end_location FROM trips")
except:
print("Error executing select")
nodes = [{'id': i[0]} for i in cur.fetchall()]
return nodes
class Stays_Graph(Resource):
def get(self):
return time_spent_result
class Hexbin_Tracks_Data(Resource):
def get(self):
response = []
for datum in result:
d = {
'lon': datum[0],
'lat': datum[1],
'date': datum[2],
}
response.append(d)
return response
class Slider_Min_Date(Resource):
def get(self):
result = []
for day in life.days:
d = day.date
result.append(d)
return result[0]
class Slider_Max_Date(Resource):
def get(self):
result = []
for day in life.days:
d = day.date
result.append(d)
return result[-1]
############################################################################
######################### Endpoints and run ################################
############################################################################
api.add_resource(Hexbin_Places_Data, '/hexbinPlaces')
api.add_resource(Hexbin_Tracks_Data, '/hexbinTracks')
api.add_resource(Calendar_Data, '/calendar')
api.add_resource(Area_Gradient_Data, '/areagradient')
api.add_resource(GPS_Tracks, '/gpstracklist')
api.add_resource(BarChart_Frequency_Data, '/barchartFrequency')
api.add_resource(BarChart_TimeSpent_Data, '/barchartTime')
api.add_resource(Chord_Data, '/chord')
api.add_resource(Arc_Edges_Data, '/arcedges')
api.add_resource(Arc_Nodes_Data, '/arcnodes')
api.add_resource(Stays_Graph, '/staysgraph')
api.add_resource(Slider_Min_Date, '/slidermin')
api.add_resource(Slider_Max_Date, '/slidermax')
if __name__ == '__main__':
#Temporary
print datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
start_date = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
Thread(target = staysGraphProcessing).start()
Thread(target = gpxParse).start()
#Temporary
end_date = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
app.run(debug=True, host='0.0.0.0')