forked from PKU-Dragon-Team/back-mobile-data-visualization
/
app.py
626 lines (512 loc) · 21.1 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
#!/usr/bin/env python
# encoding: utf-8
import pymysql
pymysql.install_as_MySQLdb()
from flask import Flask, make_response
from json import dumps
from flask.ext.cors import CORS
import MySQLdb
import MySQLdb.converters
from config import HOST, USER, PASSWD, DATABASE
from get_stop import get_stop, get_delta, get_stop_by_day, get_delta_by_day
from get_stop import date2str
from periodic_probability_matrix import generate_matrix
from get_most_proba_locations import get_most_proba_locations, pretty_print_most_proba_locations
from apriori import freq_seq_mining
from get_move import get_moves_by_day, get_moves
from get_transient_entropy import transient_entropy, entropy
import pandas as pd
import datetime
from merge_locations import merge_locations, merge_locations_by_date, raw_merge_locations_by_date, check_error_points
from move_stop_probability_matrix import generate_status_matrix
from move_stop_probability_matrix import get_status
from app_site_matrix import active_matrix
from tag_config import clean_tags
app = Flask(__name__)
cors = CORS(app)
conv = MySQLdb.converters.conversions.copy()
# convert decimals to int
conv[246] = int
db = MySQLdb.connect(HOST, USER, PASSWD, DATABASE, conv=conv, charset='utf8')
holidays = ['01', '07', '08', '14', '15', '21', '22', '28', '29']
@app.route("/usercount")
def usercount():
db.ping(True)
cursor = db.cursor()
prepare_sql = """
select count(1) from users where high >= '7'"""
cursor.execute(prepare_sql)
row = cursor.fetchone()
return make_response(dumps(row[0]))
@app.route('/users/<uid>')
def user(uid):
cols = ['uid', 'gender', 'age', 'brand_chn', 'call_fee', 'gprs_fee', 'dept_name']
db.ping(True)
cursor = db.cursor()
prepare_sql = """
select uid, gender, age, brand_chn, call_fee, gprs_fee, dept_name
from users where uid = %s"""
cursor.execute(prepare_sql, (uid,))
row = cursor.fetchone()
result = dict(list(zip(cols, row)))
return make_response(dumps(result))
@app.route("/users/<int:offset>/<int:limit>")
def users(offset, limit):
cols = ['uid', 'gender', 'age', 'brand_chn', 'call_fee', 'gprs_fee', 'dept_name']
db.ping(True)
cursor = db.cursor()
prepare_sql = """
select uid, gender, age, brand_chn, call_fee, gprs_fee, dept_name
from users where high >= '7' limit %s offset %s"""
cursor.execute(prepare_sql, (limit, offset))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
return make_response(dumps(results))
@app.route("/gprs_count_by_hour/<uid>")
def gprs_count_by_hour(uid):
cols = ['uid', 'day', 'hour', 'count']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select uid, day, substring(minute, 3, 2) as hour, count(distinct minute) as count
from app_domain_logs where uid = %s group by day, hour"""
cursor.execute(prepare_sql, (uid,))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
return make_response(dumps(results))
@app.route("/call_count_by_hour/<uid>")
def call_count_by_hour(uid):
cols = ['uid', 'day', 'hour', 'count']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select uid,
day(start_time) as day,
hour(start_time) as hour,
count(1) as count
from calls where uid = %s group by day, hour"""
cursor.execute(prepare_sql, (uid,))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
return make_response(dumps(results))
@app.route("/gprs_count_by_day/<uid>")
def gprs_count_by_day(uid):
cols = ['uid', 'day', 'count']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select uid, day, sum(count) as count from gprs_hour_counts
where uid = %s group by day"""
cursor.execute(prepare_sql, (uid,))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
return make_response(dumps(results))
@app.route("/call_count_by_day/<uid>")
def call_count_by_day(uid):
cols = ['uid', 'day', 'count']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select uid, day(start_time) as day, count(1) as count from calls
where uid = %s group by day"""
cursor.execute(prepare_sql, (uid,))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
return make_response(dumps(results))
@app.route("/location/daycount/<uid>")
def location_daycount_by_uid(uid):
db.ping(True)
cursor = db.cursor()
prepare_sql = """select distinct(log_date) as day
from location_logs_with_date
where uid = %s"""
cursor.execute(prepare_sql, (uid,))
rows = cursor.fetchall()
results = [row[0] for row in rows]
return make_response(dumps(results))
def fetch_uid_location_data(uid):
cols = ['day', 'start_time', 'location']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select log_date as day, start_time, location
from location_logs_with_date
where uid = %s order by day, start_time"""
cursor.execute(prepare_sql, (uid,))
rows = cursor.fetchall()
return [dict(list(zip(cols, row))) for row in rows]
def fetch_uid_semantic_data(uid):
cols = ['day', 'start_time', 'location', 'district', 'business']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select a.log_date as day, a.start_time, a.location,
b.district, b.business
from location_logs_with_date a
left join semantic4 b
on a.location = b.location
where a.uid = %s
order by a.start_time"""
cursor.execute(prepare_sql, (uid,))
rows = cursor.fetchall()
return [dict(list(zip(cols, row))) for row in rows]
def fetch_uid_business_data(uid):
rows = fetch_uid_semantic_data(uid)
for row in rows:
row['location'] = row['business'] and row['business'] or row['district']
return rows
def fetch_uid_district_data(uid):
rows = fetch_uid_semantic_data(uid)
for row in rows:
row['location'] = row['district']
return rows
@app.route("/location_by_uid/<uid>")
def location_by_uid(uid):
results = fetch_uid_location_data(uid)
return make_response(dumps(merge_locations(results)))
def _location_by_uid_stop(uid):
results = fetch_uid_location_data(uid)
locations = merge_locations(results)
get_delta(locations)
locations = get_stop(locations, 30)
return locations
def area_by_uid_stop(uid, area_func=fetch_uid_business_data):
results = area_func(uid)
invalids = check_error_points(raw_merge_locations_by_date(results))
results = [x for x in results if (x['location'], x['start_time']) not in invalids]
locations = merge_locations(results)
get_delta(locations)
locations = get_stop(locations, 30)
return locations
def _location_by_uid_stop_holiday(uid):
locations = _location_by_uid_stop(uid)
return [data for data in locations if data['date'] in holidays]
def _location_by_uid_stop_workday(uid):
locations = _location_by_uid_stop(uid)
return [data for data in locations if data['date'] not in holidays]
@app.route("/location_by_uid_stop/<uid>")
def location_by_uid_stop(uid):
return make_response(dumps(_location_by_uid_stop(uid)))
@app.route("/raw_location_by_uid_day/<uid>/<day>")
def raw_location_by_uid_day(uid, day):
day = '201312' + day
cols = ['start_time', 'location']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select start_time, location
from location_logs_with_date
where uid = %s and log_date = %s order by start_time"""
cursor.execute(prepare_sql, (uid, day))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
invalids = check_error_points(raw_merge_locations_by_date(results))
results = [x for x in results if (x['location'], x['start_time']) not in invalids]
return make_response(dumps(results))
@app.route("/entropy_by_uid_day/<uid>/<day>")
def entropy_by_uid_day(uid, day):
day = '201312' + day
cols = ['start_time', 'location']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select start_time, location
from location_logs_with_date
where uid = %s and log_date = %s order by start_time"""
cursor.execute(prepare_sql, (uid, day))
rows = cursor.fetchall()
results = merge_locations_by_date([dict(list(zip(cols, row))) for row in rows])
get_delta_by_day(results)
moves = get_moves_by_day(results)
result = []
for move in moves:
for location in move:
result.append({
'entropy': transient_entropy(location, move),
'time': location['start_time']
})
return make_response(dumps(result))
def get_speed_by_day(all_rows, day):
timestamps = pd.date_range(start=day + '001500',
end=day + '235959',
freq='30Min')
cols = ['start_time', 'location']
speeds = []
if len(all_rows) == 0:
return speeds
delta_t = 60
for i in range(len(timestamps)):
start_time = timestamps[i].to_datetime() - datetime.timedelta(minutes=delta_t / 2)
end_time = timestamps[i].to_datetime() + datetime.timedelta(minutes=delta_t / 2)
rows = [x for x in all_rows if date2str(start_time) <= x[0] <= date2str(end_time)]
if len(rows) == 0:
speeds.append({
'time': date2str(timestamps[i]),
'speed': 0
})
continue
rows = merge_locations_by_date([dict(list(zip(cols, row))) for row in rows])
get_delta_by_day(rows)
speed = entropy(rows, delta_t, [start_time, end_time])
speeds.append({
'time': date2str(timestamps[i]),
'speed': speed
})
return speeds
@app.route("/speed_by_uid_day/<uid>/<day>")
def speed_by_uid_day(uid, day):
day = '201312' + day
speeds = []
db.ping(True)
cursor = db.cursor()
prepare_sql = """select start_time, location
from location_logs_with_date
where uid = %s and log_date = %s order by start_time"""
cursor.execute(prepare_sql, (uid, day))
all_rows = cursor.fetchall()
speeds = get_speed_by_day(all_rows, day)
return make_response(dumps(speeds))
@app.route("/speed_by_uid/<uid>")
def speed_by_uid(uid):
db.ping(True)
cursor = db.cursor()
prepare_sql = """select start_time, location, log_date
from location_logs_with_date
where uid = %s order by start_time"""
cursor.execute(prepare_sql, (uid,))
all_rows = cursor.fetchall()
result = []
for day in range(1, 32):
day = '201312%02d' % day
rows_by_day = [x for x in all_rows if x[2] == day]
rows_by_day = [x[:2] for x in rows_by_day]
speeds = get_speed_by_day(rows_by_day, day)
result.append(speeds)
return make_response(dumps(result))
@app.route("/location_by_uid_day/<uid>/<day>")
def location_by_uid_day(uid, day):
day = '201312' + day
cols = ['start_time', 'location']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select start_time, location
from location_logs_with_date
where uid = %s and log_date = %s order by start_time"""
cursor.execute(prepare_sql, (uid, day))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
return make_response(dumps(merge_locations_by_date(results)))
@app.route("/location_by_uid_day_stop/<uid>/<day>")
def location_by_uid_day_stop(uid, day):
day = '201312' + day
cols = ['start_time', 'location']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select start_time, location
from location_logs_with_date
where uid = %s and log_date = %s order by start_time"""
cursor.execute(prepare_sql, (uid, day))
rows = cursor.fetchall()
results = merge_locations_by_date([dict(list(zip(cols, row))) for row in rows])
get_delta_by_day(results)
results = get_stop_by_day(results)
return make_response(dumps(results))
@app.route("/app_log_by_uid_day/<uid>/<day>")
def app_log_by_uid_day(uid, day):
cols = ['minute', 'busi_name', 'app_name',
'site_name', 'site_channel_name', 'domain', 'count']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select minute, busi_name, app_name, site_name, site_channel_name, domain, count
from app_domain_logs
where uid = %s and day = %s order by minute"""
cursor.execute(prepare_sql, (uid, day))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
return make_response(dumps(results))
@app.route("/semantic_proba_matrix/<uid>")
def semantic_proba_matrix(uid):
locations = area_by_uid_stop(uid, area_func=fetch_uid_business_data)
return make_response(dumps(generate_matrix(locations)))
@app.route("/district_proba_matrix/<uid>")
def district_proba_matrix(uid):
locations = area_by_uid_stop(uid, area_func=fetch_uid_district_data)
return make_response(dumps(generate_matrix(locations)))
@app.route("/proba_matrix/<uid>")
def proba_matrix(uid):
locations = _location_by_uid_stop(uid)
return make_response(dumps(generate_matrix(locations)))
@app.route("/tag_proba_matrix/<uid>")
def tag_proba_matrix(uid):
locations = _location_by_uid_stop(uid)
matrix = generate_matrix(locations)
semantic_data = fetch_semantic_data(list(matrix.keys()))
semantic_dict = {}
for row in semantic_data:
semantic_dict[row['location']] = clean_tags(row['tags'], 5)
tag_matrix = {}
for location, proba in list(matrix.items()):
tag_dict = semantic_dict[location]
tag_weight = sum(v for v in list(tag_dict.values()))
if tag_weight == 0:
continue
for tag, cnt in list(tag_dict.items()):
tag_matrix.setdefault(tag, [0] * 48)
for i in range(48):
tag_matrix[tag][i] += (proba[i] * cnt + 0.001) / (tag_weight + 0.001)
return make_response(dumps(tag_matrix))
@app.route("/proba_matrix_holiday/<uid>")
def proba_matrix_holiday(uid):
locations = _location_by_uid_stop_holiday(uid)
return make_response(dumps(generate_matrix(locations)))
@app.route("/proba_matrix_workday/<uid>")
def proba_matrix_workday(uid):
locations = _location_by_uid_stop_workday(uid)
return make_response(dumps(generate_matrix(locations)))
@app.route("/most_proba_locations/<uid>")
def most_proba_locations(uid):
locations = _location_by_uid_stop(uid)
matrix = generate_matrix(locations)
most_proba_locations = pretty_print_most_proba_locations(get_most_proba_locations(matrix))
return make_response(dumps(most_proba_locations))
@app.route("/most_proba_locations_workday/<uid>")
def most_proba_locations_workday(uid):
locations = _location_by_uid_stop_workday(uid)
matrix = generate_matrix(locations)
most_proba_locations = pretty_print_most_proba_locations(get_most_proba_locations(matrix))
return make_response(dumps(most_proba_locations))
@app.route("/most_proba_locations_holiday/<uid>")
def most_proba_locations_holiday(uid):
locations = _location_by_uid_stop_holiday(uid)
matrix = generate_matrix(locations)
most_proba_locations = pretty_print_most_proba_locations(get_most_proba_locations(matrix))
return make_response(dumps(most_proba_locations))
def _stop_to_seq(locations):
seqs = []
for day in locations:
seq = [stop['location'] for stop in day['locations']]
if len(seq) > 0:
seqs.append(seq)
return seqs
@app.route("/freq_seq/<uid>")
def freq_seq(uid):
locations = _location_by_uid_stop(uid)
dataset = _stop_to_seq(locations)
L, supportData = freq_seq_mining(dataset, 5)
flattenL = []
for ck in L:
flattenL += ck
flattenL = [seq for seq in flattenL if len(seq) > 1]
return make_response(dumps(flattenL))
@app.route("/web_req_histgram/<uid>")
def web_req_histgram(uid):
cols = ['hour', 'count']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select cast(substring(minute, 3, 2) as SIGNED) as hour, count(distinct minute) as count
from app_domain_logs where uid = %s and dirty is NULL group by hour"""
cursor.execute(prepare_sql, (uid,))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
return make_response(dumps(results))
@app.route("/site_count/<uid>")
def site_count(uid):
cols = ['site_name', 'count']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select site_name, count(1) as count from app_domain_logs
where uid = %s group by site_name
order by count desc limit 10"""
cursor.execute(prepare_sql, (uid,))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
return make_response(dumps(results))
@app.route("/app_count/<uid>")
def app_count(uid):
cols = ['app_name', 'count']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select app_name, count(1) as count from app_domain_logs
where uid = %s group by app_name
order by count desc limit 10"""
cursor.execute(prepare_sql, (uid,))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
return make_response(dumps(results))
@app.route("/call_histgram/<uid>")
def call_histgram(uid):
cols = ['hour', 'count']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select hour(start_time) as hour, count(1) as count
from calls where uid = %s group by hour"""
cursor.execute(prepare_sql, (uid,))
rows = cursor.fetchall()
results = [dict(list(zip(cols, row))) for row in rows]
return make_response(dumps(results))
def fetch_semantic_data(locations):
cols = ['location', 'station_desc', 'tags', 'addr', 'business']
cursor = db.cursor()
prepare_sql = """select location, station_desc, tags, addr, business from semantic4 where location in (%s)""" % \
','.join(["'" + x + "'" for x in locations])
cursor.execute(prepare_sql)
rows = cursor.fetchall()
return [dict(list(zip(cols, row))) for row in rows]
@app.route("/semantic_data/<uid>")
def semantic_data(uid):
locations = _location_by_uid_stop(uid)
locationlist = set()
for day in locations:
for item in day['locations']:
locationlist.add(item['location'])
results = fetch_semantic_data(locationlist)
return make_response(dumps(list(results)))
@app.route("/user_status_proba/<uid>")
def user_status_proba(uid):
logs = fetch_uid_location_data(uid)
results = merge_locations(logs)
get_delta(results)
moves = get_moves(results)
stops = get_stop(results)
return make_response(dumps(generate_status_matrix(moves, stops)))
@app.route("/user_status/<uid>")
def user_status(uid):
logs = fetch_uid_location_data(uid)
results = merge_locations(logs)
get_delta(results)
moves = get_moves(results)
stops = get_stop(results)
return make_response(dumps(get_status(moves, stops)))
def fetch_uid_app_data(uid):
cols = ['day', 'minute', 'entity']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select day, concat('201312', minute) as start_time,
app_name
from app_domain_logs
where uid = %s and app_name != '其他' and
site_channel_name not like %s
and dirty is NULL
order by day, minute"""
cursor.execute(prepare_sql, (uid, '被动%'))
rows = cursor.fetchall()
return [dict(list(zip(cols, row))) for row in rows]
def fetch_uid_app_data_with_condition(uid, condition=''):
cols = ['day', 'minute', 'entity']
db.ping(True)
cursor = db.cursor()
prepare_sql = """select day, concat('201312', minute) as start_time,
app_name
from app_domain_logs
where uid = %s and app_name != '其他' and
site_channel_name not like %s and
app_name != '微信' and
app_name != '手机腾讯网' and
app_name != 'QQ' and
dirty is NULL
order by day, minute"""
cursor.execute(prepare_sql, (uid, '被动%'))
rows = cursor.fetchall()
return [dict(list(zip(cols, row))) for row in rows]
@app.route("/app_by_uid/<uid>")
def app_by_uid(uid):
results = fetch_uid_app_data(uid)
return make_response(dumps(active_matrix(results)))
@app.route("/app_by_uid_with_condition/<uid>")
def app_by_uid_condition(uid):
results = fetch_uid_app_data_with_condition(uid)
return make_response(dumps(active_matrix(results)))
if __name__ == "__main__":
app.run(debug=True)