forked from harisundaram1/NetworkLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_msg.py
1670 lines (1488 loc) · 95 KB
/
generate_msg.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Copyright (c) 2015 Crowd Dynamics Labs
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
from script import find_images,is_int,ignore,relevance_score,find_weight,begin_localimg
import pyorient
import json
import collections
import operator
import time
#from collections import Counter
from pyorient.utils import *
import datetime
import random
import matplotlib.pyplot as plt
import base64
positive_messages = []
negative_messages = []
most_influential_num_pos = (0,0,0)
most_influential_msg_pos = ""
most_influential_num_neg = (0,0,0)
most_influential_msg_neg = ""
delta = datetime.timedelta(days=1)
def connect():
#print "in connect"
client = pyorient.OrientDB("localhost", 2424)
session_id = client.connect( "root", "rootlabs" )
client.db_open( "NetworkLabs", "root", "rootlabs")
return client
def get_friends(rid):
cl = connect()
# print "Retrieving rid's of friends"
friends = cl.command("select expand(both('friends_with')) from Person where @rid = "+str(rid))
return friends
def create_binary_data(path):
# print 'saving the graphs as nodes'
cl = connect()
file_names = []
file_names.append(path)
# print file_names
for fl in file_names:
print 'creating node for image '+fl
with open(fl,mode='rd') as img_file:
encoded_str = base64.b64encode(img_file.read())
res =cl.command('create vertex BinaryData set data="'+encoded_str+'"')
return res[0].rid
def plotbarchart(x,y,xlabels,xaxis,yaxis,graph_title,bar_color):
print "In plot bar chart"
fig = plt.figure()
#plt.bar(x,y,align='center',color=bar_color)
plt.bar(x,y,color=bar_color)
# plt.tight_layout()
plt.xticks(x,xlabels)
fig.autofmt_xdate()
fig.suptitle(graph_title, fontsize=15)
plt.xlabel(xaxis)
plt.ylabel(yaxis)
fig.savefig("barchart.jpg")
recid_img = create_binary_data("/home/chndrsh4/barchart.jpg")
# plt.tight_layout()
return recid_img
def check_health_index(rname):
cl = connect()
cmd_health_options = "select health_option from Restaurant where name =\""+str(rname)+"\""
# print cmd_health_options
rest_details = cl.command(cmd_health_options)
#print rest_details
#print rest_details[0].health_option
if (rest_details[0].health_option>30):
return 1
else:
return 0
def check_health_index_id(rid):
cl = connect()
cmd_health_options = "select health_option from Restaurant where res_id =\""+str(rid)+"\""
# print cmd_health_options
rest_details = cl.command(cmd_health_options)
#print rest_details
#print rest_details[0].health_option
if (rest_details[0].health_option>30):
return 1
else:
return 0
def find_healthy_visits(visit_counts):
healthy_eats = 0
unhealthy_eats = 0
for key,val in visit_counts.items():
# print key
# print val
if (check_health_index_id(key)):
healthy_eats = healthy_eats+val
else:
unhealthy_eats = unhealthy_eats+val
return(healthy_eats,unhealthy_eats)
def count_visits(visited_rest):
visited_count = {}
for rest in visited_rest:
if rest.res_id in visited_count:
visited_count[rest.res_id] = visited_count[rest.res_id]+1
else:
visited_count[rest.res_id] = 1
asorted_restaurant_visits = sorted(visited_count.items(), key=operator.itemgetter(1))
dsorted_restaurant_visits = sorted(visited_count.items(), key=operator.itemgetter(1), reverse=True)
return (visited_count,asorted_restaurant_visits,dsorted_restaurant_visits)
def insert_card(cmd_str,recordid,card_type=0):
card_record = cl.command(cmd_str)
# print card_record
rec_id = card_record[0].rid
print rec_id
cl.command('create edge can_view from '+str(recordid)+' to '+str(rec_id))
print "Successfully inserted"
if card_type ==1: # Indicates an image has to be inserted
return rec_id
def count_cuisines_eaten(all_restaurants_eaten):
cuisine_count = {}
asc_cuisine = []
desc_cuisine = []
all_cuisines = []
cl = connect()
flag = 0
for restaurants in all_restaurants_eaten:
if len(restaurants)>=1:
flag = 1
for rest in restaurants:
if(check_health_index_id(rest.res_id)):
# print rest
#Find the type of cuisine eaten
cmd_str = "select cuisines as cuisine from Restaurant where res_id=\""+str(rest.res_id)+"\""
# print cmd_str
rest_cuisine = cl.command(cmd_str)
# for cuisine in rest_cuisine:
# print cuisine
all_cuisines = rest_cuisine[0].cuisine[0].split(",")[1::2]
for i in all_cuisines:
if i in cuisine_count.keys():
cuisine_count[i] += 1
else:
cuisine_count[i] = 1
asc_cuisine = sorted(cuisine_count.items(), key=operator.itemgetter(1))
desc_cuisine = sorted(cuisine_count.items(), key=operator.itemgetter(1), reverse=True)
if flag==1:
return (cuisine_count,asc_cuisine,desc_cuisine)
else:
return ("","","")
def count_restaurants_visited(all_restaurants_eaten):
restaurant_visits = {}
asorted_restaurant_visits = []
dsorted_restaurant_visits = []
flag = 0
for restaurants in all_restaurants_eaten:
if len(restaurants)>=1:
flag =1
for rest in restaurants:
# print rest
# print rest.name
if(check_health_index_id(rest.res_id)):
if rest.res_id in restaurant_visits:
restaurant_visits[rest.res_id] += 1
else:
restaurant_visits[rest.res_id] = 1
asorted_restaurant_visits = sorted(restaurant_visits.items(), key=operator.itemgetter(1))
dsorted_restaurant_visits = sorted(restaurant_visits.items(), key=operator.itemgetter(1), reverse=True)
if flag==1:
return (restaurant_visits,asorted_restaurant_visits,dsorted_restaurant_visits)
else:
return ("","","")
def count_healthy_users(all_healthy_eaten):
visit_counts_user = {}
desc_visitcount = []
asc_visitcount = []
(healthy_users,unhealthy_users) = (0,0)
(healthy_visits,unhealthy_visits) = (0,0)
for visit in all_healthy_eaten:
if len(visit)>=1:
(visit_counts_user,desc_visitcount,asc_visitcount) = count_visits(visit)
(healthy_visits,unhealthy_visits) = find_healthy_visits(visit_counts_user)
if healthy_visits>=unhealthy_visits:
healthy_users+=1
else:
unhealthy_users+=1
return (healthy_users,unhealthy_users)
def get_restaurant_name(rid,code=0):
cl = connect()
cmd_str = "select name,@rid as rid from Restaurant where res_id='"+str(rid)+"'"
# print cmd_str
rest = cl.command(cmd_str)
# print rest
if code==1:
return (str(rest[0].name),rest.rid)
else:
return str(rest[0].name)
def compute_spatial_stats(all_restaurants_eaten):
rest_spatial_stats = {'North Urbana':0,'South Urbana':0,'North Champaign':0,'South Champaign':0}
flag = 0
max_index = 0
area_list = []
for restaurants in all_restaurants_eaten:
if len(restaurants)>=1:
flag = 1
area_list = [0,0,0,0] #index0: north urbana;index1:south urbana;index2: north champaign;index3:south champaign
for rest in restaurants:
cmd_str = "select latitude as lat, longitude as lon from Restaurant where res_id='"+str(rest.res_id)+"'"
# print cmd_str
rest_coord = cl.command(cmd_str)
if (rest_coord[0].lat > 40.11150 and rest_coord[0].lon>-88.22900 ):
area_list[0]+=1
elif (rest_coord[0].lat < 40.11150 and rest_coord[0].lon>-88.22900):
area_list[1] +=1
elif (rest_coord[0].lat > 40.11150 and rest_coord[0].lon<-88.22900 ):
area_list[2] +=1
#(rest.lat < 40.11150 and rest.lon<-88.22900 ):
else:
area_list[3] +=1
#max_index = area_list.index(max(area_list))
if area_list[0]>0:
rest_spatial_stats["North Urbana"] +=1
if area_list[1]>0:
rest_spatial_stats["South Urbana"] +=1
if area_list[2]>0:
rest_spatial_stats["North Champaign"] +=1
if area_list[3]>0:
rest_spatial_stats["South Champaign"] +=1
asorted_rest_spatial_stats = sorted(rest_spatial_stats.items(), key=operator.itemgetter(1))
dsorted_rest_spatial_stats = sorted(rest_spatial_stats.items(), key=operator.itemgetter(1), reverse=True)
if flag ==1:
return (rest_spatial_stats,asorted_rest_spatial_stats,dsorted_rest_spatial_stats)
else:
return ("","","")
def calc_stats(total_friends,dsorted_rest_visits):
if len(dsorted_rest_visits)>=1:
visited = dsorted_rest_visits[0][1]
notvisited = total_friends-visited
percent_visited = (float(visited)/total_friends)*100
percent_notvisited = (float(notvisited)/total_friends)*100
# print dsorted_rest_visits
restaurant_name = get_restaurant_name(dsorted_rest_visits[0][0])
return(visited,notvisited,percent_visited,percent_notvisited,restaurant_name)
else:
return ("","","","","")
def find_epoch_time():
current_epoch_time = int(time.time())
week_ago = current_epoch_time*1000 - 604800000
month_ago = current_epoch_time*1000 - 2629743000
# print "Epoch week is "+ str(week_ago)
# print "Epoch month is "+ str(month_ago)
return (week_ago,month_ago)
def walking_message(steps,msg_start,total_users):
# print "In walking messages"
# print steps
# print msg_start
mesg = ""
if steps in range(0,100000):
# print "In if 1"
more_steps = 100000- steps
avg_steps = more_steps/total_users
mesg = ''+str(msg_start)+" "+str(avg_steps)+" more steps each, you'll would have collectively walked a total of 100000 steps"
elif steps in range(100000,300000):
# print "in if 2"
more_steps = 280896-steps
avg_steps = more_steps/total_users
print more_steps
mesg = ''+str(msg_start)+' '+str(avg_steps)+" more steps each, you'll would have collectively walked all the way from Champiagn-Urbana to Chicago "
mesg = str(mesg)
print mesg
# print "End of if"
elif steps in range(300000,16739712):
more_steps = 16739712-steps
avg_steps = more_steps/total_users
mesg = ''+str(msg_start)+" "+str(avg_steps)+" more steps each, you'll would have collectively walked a full circle around the earth"
elif steps in range(16739712,504556800):
more_steps = 504556800-steps
avg_steps = more_steps/total_users
mesg = ''+str(msg_start)+" "+str(avg_steps)+" more steps each, you'll would have collectively walked from the earth to the moon"
else:
print "Matches no if's"
mesg = "Houston we have a problem"
# print "Matches no other if's"
return str(mesg)
def check_data_existence(all_stats):
count =0
for res in all_stats:
if not res:
count +=1
if count==len(all_stats):
return 0
else:
return 1
def categorize_messages(num,den,msg_tail,template_type):
global positive_messages
global negative_messages
global most_influential_num_pos
global most_influential_num_neg
global most_influential_msg_pos
global most_influential_msg_neg
percent = num/den*100
print num
print msg_tail
msg_type_stat = 0
if percent >60:
msg_type_stat = 1
if den>10 and percent>num:
msg_type = 0 #msg_type =0 indicates use of percentage
msg = ''+str(percent)+' % of'+str(msg_tail)
else:
msg_type = 1 #use the "out of" notation
msg = ''+str(num)+' out of '+str(den)+' '+str(msg_tail)
elif percent in range(45,60):
msg_type_stat = 0
if den>10:
msg_type = 0 #msg_type =0 indicates use of percentage
msg = ''+str(percent)+' % of'+str(msg_tail)
else:
msg_type = 1 #use the "out of" notation
msg = ''+str(num)+' out of '+str(den)+' '+str(msg_tail)
else:
msg_type_stat = -1
if den>10 and percent<num:
msg_type = 0 #msg_type =0 indicates use of percentage
msg = ''+str(percent)+' % of'+str(msg_tail)
else:
msg_type = 1 #use the "out of" notation
msg = ''+str(num)+' out of '+str(den)+' '+str(msg_tail)
if template_type==1 and msg_type_stat==1:
positive_messages.append(msg)
print most_influential_num_pos[0]
print percent
print most_influential_num_pos[1]
print num
if percent>most_influential_num_pos[0] or num>most_influential_num_pos[1]:
most_influential_num_pos = (percent,num,den)
most_influential_msg_pos = msg
elif template_type==-1 and msg_type_stat==-1:
print most_influential_num_pos[0]
print percent
print most_influential_num_pos[1]
print num
positive_messages.append(msg)
if percent>most_influential_num_pos[0] or num>most_influential_num_pos[1]:
most_influential_num_pos = (percent,num,den)
most_influential_msg_pos = msg
elif template_type==-1 and msg_type_stat==1:
negative_messages.append(msg)
if percent<most_influential_num_pos[0] or num<most_influential_num_pos[1]:
most_influential_num_neg = (percent,num,den)
most_influential_msg_neg = msg
else:
negative_messages.append(msg)
if percent<most_influential_num_pos[0] or num<most_influential_num_pos[1]:
most_influential_num_neg = (percent,num,den)
most_influential_msg_neg = msg
print msg
print most_influential_msg_pos
print most_influential_msg_neg
def rest_visited_by_friends(rid,msg_type,restaurant_name=""):
cl = connect()
steps_walked = 0
all_friends_rest_visits = []
all_friends_healthy_visits = []
all_friends_healthy_visits_weekly = []
all_friends_healthy_visits_monthly = []
all_friends_rest_visits_weekly = []
all_friends_rest_visits_monthly = []
all_friends_visits_spatial = []
all_friends_visits_spatial_weekly = []
all_friends_visits_spatial_monthly = []
(epoch_week,epoch_month) = find_epoch_time()
res1= get_friends(rid)
total_friends = len(res1)
for i in range(total_friends):
friendid = str(res1[i].rid)
steps_walked += res1[i].steps_walked
print friendid
rest_visit = cl.command("select distinct(name) as name, distinct(res_id) as res_id, distinct(cuisines) as cuisine from (select expand(both('eats_at')) from Person where @rid = "+friendid+")")
healthy_visits = cl.command("select name, res_id as res_id from (select expand(both('eats_at')) from Person where @rid = "+friendid+")")
healthy_visits_weekly = cl.command("select name, res_id as res_id from (select expand(in) from eats_at where created_at > "+str(epoch_week)+" and out="+friendid+")")
healthy_visits_monthly = cl.command("select name, res_id as res_id from (select expand(in) from eats_at where created_at > "+str(epoch_month)+" and out="+friendid+")")
rest_visit_weekly = cl.command("select distinct(name) as name, distinct(res_id) as res_id, distinct(cuisines) as cuisine from (select expand(in) from eats_at where created_at > "+str(epoch_week)+" and out="+friendid+")")
rest_visit_monthly = cl.command("select distinct(name) as name, distinct(res_id) as res_id, distinct(cuisines) as cuisine from (select expand(in) from eats_at where created_at > "+str(epoch_month)+" and out="+friendid+")")
all_friends_rest_visits.append(rest_visit)
all_friends_healthy_visits.append(healthy_visits)
all_friends_rest_visits_weekly.append(rest_visit_weekly)
all_friends_rest_visits_monthly.append(rest_visit_monthly)
all_friends_healthy_visits_weekly.append(healthy_visits_weekly)
all_friends_healthy_visits_monthly.append(healthy_visits_monthly)
all_friends_visits_spatial.append(rest_visit)
all_friends_visits_spatial_weekly.append(rest_visit_weekly)
all_friends_visits_spatial_monthly.append(rest_visit_monthly)
if (check_data_existence(all_friends_rest_visits)):
print "Counting visits over all time"
(restaurant_visits,asorted_restaurant_visits,dsorted_restaurant_visits) = count_restaurants_visited(all_friends_rest_visits)
print "Computing spatial stats"
(visits_spatial,asorted_visits_spatial,dsorted_visits_spatial) = compute_spatial_stats(all_friends_visits_spatial)
print "Counting healthy visits"
(healthy_friends,unhealthy_friends) = count_healthy_users(all_friends_healthy_visits)
print "Counting cuisines eaten over all time"
(cuisines_count,asc_cuisine,desc_cuisine) = count_cuisines_eaten(all_friends_rest_visits)
(visited_alltime,notvisited_alltime,percent_visited_alltime,percent_notvisited_alltime,rname_alltime) = calc_stats(total_friends,dsorted_restaurant_visits)
(index,w1,w2) = select_random_cuisine(desc_cuisine)
if dsorted_visits_spatial:
spatial_index = random.randrange(0,4)
if(msg_type==1):
msg = 'Your friends have walked a total of '+str(steps_walked)+' steps'
print msg
msg = walking_message(steps_walked,"If you and your friends on an average walk",total_friends)
print msg
msg_end = 'of your friends have eaten at '+str(rname_alltime)
categorize_messages(visited_alltime,total_friends,msg_end,1)
#msg = ''+str(int(percent_visited_alltime))+' % of your friends have eaten at '+str(rname_alltime)
#print msg
#msg = ''+str(visited_alltime)+' out of '+str(total_friends)+' of your friends have eaten at '+str(rname_alltime)
#print msg
if spatial_index:
msg_end = ' of your friends have eaten in '+str(dsorted_visits_spatial[spatial_index][0])
categorize_messages(dsorted_visits_spatial[spatial_index][1],total_friends,msg_end,1)
# msg = ''+str(dsorted_visits_spatial[spatial_index][1])+' out of '+str(total_friends)+' of your friends have eaten in '+str(dsorted_visits_spatial[spatial_index][0])
# print msg
msg_end = ' of your friends have eaten healthy'
categorize_messages(healthy_friends,total_friends,msg_end,1)
# msg = ''+str(healthy_friends)+' out of '+str(total_friends)+' of your friends have eaten healthy'
# print msg
if index:
msg_end = ' of your friends have eaten healthy '+str(desc_cuisine[index][0])
categorize_messages(desc_cuisine[index][1],total_friends,msg_end,1)
msg = ''+str(desc_cuisine[index][1])+' out of '+str(total_friends)+' of your friends have eaten healthy '+str(desc_cuisine[index][0])
print msg
tags = ["healthy","chinese"]
ret_val = begin_localimg(msg.replace("1 times","once"),"namedfriend_overlay_cuisine_monthly.jpg")
if ret_val == 1:
recid_img = create_binary_data("/Local/Users/dev/NetworkLabs/namedfriend_overlay_cuisine_monthly.jpg")
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int((creation_date-(datetime.timedelta(days=1))).strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",image="'+recid_img+'"'
card_rid = insert_card(cmd_str,rid,1)
id_rec = cl.command('create edge has_uploaded_image from '+str(card_rid)+' to '+str(recid_img))
print id_rec[0].rid
else:
msg_end = ' of your friends have not eaten at '+str(rname_alltime)
categorize_messages(notvisited_alltime,total_friends,msg_end,-1)
# msg = ''+str(int(percent_notvisited_alltime))+' % of your friends have not eaten at '+str(rname_alltime)
# print msg
msg = ''+str(notvisited_alltime)+' out of '+str(total_friends)+' of your friends have not eaten at '+str(rname_alltime)
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int((creation_date-(datetime.timedelta(days=5))).strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",html_content="'+msg+'"'
# print cmd_str
insert_card(cmd_str,rid)
# print msg
if spatial_index:
msg_end = ' of your friends have not eaten in '+str(dsorted_visits_spatial[spatial_index][0])
categorize_messages(total_friends-dsorted_visits_spatial[spatial_index][1],total_friends,msg_end,-1)
# msg = ''+str(total_friends-dsorted_visits_spatial[spatial_index][1])+' out of '+str(total_friends)+' of your friends have not eaten in '+str(dsorted_visits_spatial[spatial_index][0])
# print msg
msg_end = ' of your friends have not eaten healthy'
categorize_messages(unhealthy_friends,total_friends,msg_end,-1)
# msg = ''+str(unhealthy_friends)+' out of '+str(total_friends)+' of your friends have not eaten healthy'
# print msg
if index:
msg_end = ' of your friends have not eaten'+str(w1)+'healthy '+str(desc_cuisine[index][0])+str(w2)
categorize_messages(total_friends-desc_cuisine[index][1],total_friends,msg_end,-1)
# msg = ''+str(total_friends-desc_cuisine[index][1])+' out of '+str(total_friends)+' of your friends have not eaten'+str(w1)+'healthy '+str(desc_cuisine[index][0])+str(w2)
# print msg
if (check_data_existence(all_friends_rest_visits_weekly)):
print "Counting weekly visits"
(weekly_restaurant_visits,asorted_weekly_restaurant_visits,dsorted_weekly_restaurant_visits) = count_restaurants_visited(all_friends_rest_visits_weekly)
print "Computing spatial stats weekly"
(visits_spatial_weekly,asorted_visits_spatial_weekly,dsorted_visits_spatial_weekly) = compute_spatial_stats(all_friends_visits_spatial_weekly)
print "Counting healthy visits weekly"
(healthy_friends_weekly,unhealthy_friends_weekly) = count_healthy_users(all_friends_healthy_visits_weekly)
print "Cuisines eaten weekly"
(cuisines_count_weekly,asc_cuisine_weekly,desc_cuisine_weekly) = count_cuisines_eaten(all_friends_rest_visits_weekly)
(visited_weekly,notvisited_weekly,percent_visited_weekly,percent_notvisited_weekly,rname_weekly) = calc_stats(total_friends,dsorted_weekly_restaurant_visits)
if desc_cuisine_weekly:
(index_weekly,w1_w,w2_w) = select_random_cuisine(desc_cuisine_weekly)
if dsorted_visits_spatial_weekly:
spatial_index_weekly = random.randrange(0,4)
if (msg_type==1):
msg_end = 'of your friends have eaten at '+str(rname_weekly)+' over the past week'
categorize_messages(visited_weekly,total_friends,msg_end,1)
# msg = ''+str(int(percent_visited_weekly))+' % of your friends have eaten at '+str(rname_weekly)+' over the past week'
# print msg
# msg = ''+str(visited_weekly)+' out of '+str(total_friends)+' of your friends have eaten at '+str(rname_weekly)+' over the past week'
# print msg
msg_end = ' of your friends have eaten healthy over the past week'
categorize_messages(healthy_friends_weekly,total_friends,msg_end,1)
msg = '<b>'+str(healthy_friends_weekly)+'</b> out of <b>'+str(total_friends)+'</b> of your friends have eaten healthy <b>over the past week</b>'
cmd_str = 'create vertex Card set comment_count=0,display_type="info",comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int((creation_date-(datetime.timedelta(days=6))).strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",content_html="'+msg+'"'
print cmd_str
insert_card(cmd_str,rid)
print msg
if spatial_index_weekly:
msg_end = ' of your friends have eaten in '+str(dsorted_visits_spatial_weekly[spatial_index_weekly][0])+' over the past week'
categorize_messages(dsorted_visits_spatial_weekly[spatial_index_weekly][1],total_friends,msg_end,1)
# msg = '<b>'+str(dsorted_visits_spatial_weekly[spatial_index_weekly][1])+'</b> out of <b>'+str(total_friends)+'</b> of your friends have eaten in '+str(dsorted_visits_spatial_weekly[spatial_index_weekly][0])+' over the past week'
# print msg
if index_weekly:
msg_end = ' of your friends have eaten'+str(w1_w)+' healthy '+str(desc_cuisine_weekly[index_weekly][0])+str(w2_w)+' over the past week'
categorize_messages(desc_cuisine_weekly[index_weekly][1],total_friends,msg_end,1)
# msg = ''+str(desc_cuisine_weekly[index_weekly][1])+' out of '+str(total_friends)+' of your friends have eaten'+str(w1_w)+' healthy '+str(desc_cuisine_weekly[index_weekly][0])+str(w2_w)+' over the past week'
# print msg
else:
msg_end = 'of your friends have not eaten at '+str(rname_weekly)+' over the past week'
categorize_messages(notvisited_weekly,total_friends,msg_end,-1)
# msg = ''+str(int(percent_notvisited_weekly))+' % of your friends have not eaten at '+str(rname_weekly)+' over the past week'
# print msg
# msg = ''+str(notvisited_weekly)+' out of '+str(total_friends)+' of your friends have not eaten at '+str(rname_weekly)+' over the past week'
# print msg
if spatial_index_weekly:
msg_end = ' of your friends have not eaten in '+str(dsorted_visits_spatial_weekly[spatial_index_weekly][0])+' over the past week'
categorize_messages(total_friends-dsorted_visits_spatial_weekly[spatial_index_weekly][1],total_friends,msg_end,-1)
# msg = ''+str(total_friends-dsorted_visits_spatial_weekly[spatial_index_weekly][1])+' out of '+str(total_friends)+' of your friends have not eaten in '+str(dsorted_visits_spatial_weekly[spatial_index_weekly][0])+' over the past week'
# print msg
msg_end = ' of your friends have not eaten healthy over the past week'
categorize_messages(unhealthy_friends_weekly,total_friends,msg_end,-1)
# msg = ''+str(unhealthy_friends_weekly)+' out of '+str(total_friends)+' of your friends have not eaten healthy over the past week'
# print msg
if (check_data_existence(all_friends_rest_visits_monthly)):
print "Counting monthly visits"
(monthly_restaurant_visits,asorted_monthly_restaurant_visits,dsorted_monthly_restaurant_visits) = count_restaurants_visited(all_friends_rest_visits_monthly)
print "Computing spatial stats monthly"
(visits_spatial_monthly,asorted_visits_spatial_monthly,dsorted_visits_spatial_monthly) = compute_spatial_stats(all_friends_visits_spatial_monthly)
print "Counting healthy visits monthly"
(healthy_friends_monthly,unhealthy_friends_monthly) = count_healthy_users(all_friends_healthy_visits_monthly)
print "Cuisines eaten monthly"
(cuisines_count_monthly,asc_cuisine_monthly,desc_cuisine_monthly) = count_cuisines_eaten(all_friends_rest_visits_monthly)
(visited_monthly,notvisited_monthly,percent_visited_monthly,percent_notvisited_monthly,rname_monthly) = calc_stats(total_friends,dsorted_monthly_restaurant_visits)
if desc_cuisine_monthly:
(index_monthly,w1_m,w2_m) = select_random_cuisine(desc_cuisine_monthly)
if dsorted_visits_spatial_monthly:
spatial_index_monthly = random.randrange(0,4)
if msg_type==1:
msg_end = 'of your friends have eaten at '+str(rname_monthly)+' over the past month'
categorize_messages(visited_monthly,total_friends,msg_end,1)
# msg = ''+str(int(percent_visited_monthly))+' % of your friends have eaten at '+str(rname_monthly)+' over the past month'
# print msg
# msg = ''+str(visited_monthly)+' out of '+str(total_friends)+' of your friends have eaten at '+str(rname_monthly)+' over the past month'
msg_end = ' of your friends have eaten healthy over the past month'
categorize_messages(healthy_friends_monthly,total_friends,msg_end,1)
msg = '<b>'+str(healthy_friends_monthly)+'</b> out of <b>'+str(total_friends)+'</b> of your friends have eaten healthy over the <b>past month</b>'
print msg
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int((creation_date-delta).strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",html_content="'+msg+'"'
print cmd_str
#insert_card(cmd_str,rid)
if index_monthly:
msg_end = ' of your friends have eaten'+str(w1_m)+' healthy '+str(desc_cuisine_monthly[index_monthly][0])+str(w1_m)+' over the past month'
categorize_messages(desc_cuisine_monthly[index_monthly][1],total_friends,msg_end,1)
# msg = ''+str(desc_cuisine_monthly[index_monthly][1])+' out of '+str(total_friends)+' of your friends have eaten'+str(w1_m)+' healthy '+str(desc_cuisine_monthly[index_monthly][0])+str(w1_m)+' over the past month'
# print msg
if spatial_index_monthly:
msg_end = ' of your friends have eaten in '+str(dsorted_visits_spatial_monthly[spatial_index_monthly][0])+' over the past month'
categorize_messages(dsorted_visits_spatial_monthly[spatial_index_monthly][1],total_friends,msg_end,1)
msg = '<b>'+str(dsorted_visits_spatial_monthly[spatial_index_monthly][1])+'</b> out of <b> '+str(total_friends)+'</b> of your friends have eaten in <b>'+str(dsorted_visits_spatial_monthly[spatial_index_monthly][0])+'</b> over the past month'
print msg
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int((creation_date-delta).strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",html_content="'+msg+'"'
print cmd_str
#insert_card(cmd_str,rid)
else:
#Not eaten cuisine weekly and monthly
msg_end = 'of your friends have not eaten at '+str(rname_monthly)+' over the past month'
categorize_messages(notvisited_monthly,total_friends,msg_end,-1)
# msg = ''+str(int(percent_notvisited_monthly))+' % of your friends have not eaten at '+str(rname_monthly)+' over the past month'
# print msg
# print msg
# msg = ''+str(notvisited_monthly)+' out of '+str(total_friends)+' of your friends have not eaten at '+str(rname_monthly)+' over the past month'
# print msg
msg_end = ' of your friends have not eaten healthy over the past month'
categorize_messages(unhealthy_friends_monthly,total_friends,msg_end,-1)
# msg = ''+str(unhealthy_friends_monthly)+' out of '+str(total_friends)+' of your friends have not eaten healthy over the past month'
# print msg
if spatial_index_monthly:
msg_end = ' of your friends have not eaten in '+str(dsorted_visits_spatial_monthly[spatial_index_monthly][0])+' over the past month'
categorize_messages(total_friends-dsorted_visits_spatial_monthly[spatial_index_monthly][1],total_friends,msg_end,-1)
# msg = ''+str(total_friends-dsorted_visits_spatial_monthly[spatial_index_monthly][1])+' out of '+str(total_friends)+' of your friends have not eaten in '+str(dsorted_visits_spatial_monthly[spatial_index_monthly][0])+' over the past month'
# print msg
def rest_visited_by_network(rid,msg_type,restaurant_name=""):
cl = connect()
all_restaurants_visited = {}
all_network_healthy_visits = []
all_network_healthy_visits_weekly = []
all_network_healthy_visits_monthly = []
steps_walked = 0
(epoch_week,epoch_month) = find_epoch_time()
res1 = cl.command("select * from Person")
total_people = len(res1)
rest_visits = cl.command("select distinct(name) as name, distinct(res_id) as res_id, distinct(cuisines) as cuisine from (select expand(distinct(both('eats_at'))) from Person)")
rest_visits_weekly = cl.command("select distinct(name) as name, distinct(res_id) as res_id, distinct(cuisines) as cuisine from (select expand(in) from eats_at where created_at > "+str(epoch_week)+")")
rest_visits_monthly = cl.command("select distinct(name) as name, distinct(res_id) as res_id, distinct(cuisines) as cuisine from (select expand(in) from eats_at where created_at > "+str(epoch_month)+")")
#for healthy visits needs work
for i in range(total_people):
userid = str(res1[i].rid)
# print "Processing"+str(userid)
# steps_walked += res1[i].steps_walked
healthy_visits = cl.command("select name, res_id as res_id from (select expand(both('eats_at')) from Person where @rid = "+userid+")")
healthy_visits_weekly = cl.command("select name, res_id as res_id from (select expand(in) from eats_at where created_at > "+str(epoch_week)+" and out="+userid+")")
healthy_visits_monthly = cl.command("select name, res_id as res_id from (select expand(in) from eats_at where created_at > "+str(epoch_month)+" and out="+userid+")")
all_network_healthy_visits.append(healthy_visits)
all_network_healthy_visits_weekly.append(healthy_visits_weekly)
all_network_healthy_visits_monthly.append(healthy_visits_monthly)
if (check_data_existence([rest_visits])):
print "Counting restaurant visits over all time"
(all_restaurant_visits,asorted_all_restaurant_visits,dsorted_all_restaurant_visits) = count_visits(rest_visits)
print "Counting cuisines eaten- all time"
(cuisines_count,asc_cuisine,desc_cuisine) = count_cuisines_eaten([rest_visits])
print "Counting healthy friends- all time"
(healthy_users,unhealthy_users) = count_healthy_users(all_network_healthy_visits)
print "Computing spatial stats"
(visits_spatial,asorted_visits_spatial,dsorted_visits_spatial) = compute_spatial_stats([rest_visits])
(visited_alltime,notvisited_alltime,percent_visited_alltime,percent_notvisited_alltime,rname_alltime) = calc_stats(total_people,dsorted_all_restaurant_visits)
(index,w1,w2) = select_random_cuisine(desc_cuisine)
if dsorted_visits_spatial:
spatial_index = random.randrange(0,4)
if (msg_type==1):
msg = '<table><p><b>Top 3 restaurants visited</b></p><tr><td><b>Restaurant<b></td><td><b>Visits<b></td></tr><tr><td>'+get_restaurant_name(dsorted_all_restaurant_visits[0][0])+'</td><td>'+str(dsorted_all_restaurant_visits[0][1])+'</td></tr><tr><td>'+get_restaurant_name(dsorted_all_restaurant_visits[1][0])+'</td><td>'+str(dsorted_all_restaurant_visits[1][1])+'</td></tr><tr><td>'+get_restaurant_name(dsorted_all_restaurant_visits[2][0])+'</td><td>'+str(dsorted_all_restaurant_visits[2][1])+'</td></tr></table>'
#msg = ''+get_restaurant_name(dsorted_all_restaurant_visits[i][0])+'\tVisits:'+ str(dsorted_all_restaurant_visits[i][1])
print msg
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int((creation_date-(datetime.timedelta(days=3))).strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",html_content="'+msg+'"'
print cmd_str
insert_card(cmd_str,rid)
msg = ''+' Most people in the network have visited <b>'+get_restaurant_name(dsorted_all_restaurant_visits[0][0])+'</b>'
print msg
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int((creation_date-(datetime.timedelta(days=4))).strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",html_content="'+msg+'"'
print cmd_str
#insert_card(cmd_str,rid)
msg = '<b>'+str(int(percent_visited_alltime))+'%</b> of the people in the network have eaten at <b>'+str(rname_alltime)+'</b>'
print msg
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int((creation_date-(datetime.timedelta(days=5))).strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",html_content="'+msg+'"'
print cmd_str
#insert_card(cmd_str,rid)
msg = ''+str(visited_alltime)+' out of '+str(total_people)+' of the people in the network have eaten at '+str(rname_alltime)
print msg
msg = ''+str(healthy_users)+' out of '+str(total_people)+' of the people in the network have eaten healthy'
print msg
msg = ''+str(desc_cuisine[index][1])+' out of '+str(total_people)+' of the people in the network have eaten healthy '+str(desc_cuisine[index][0])
print msg
if spatial_index:
msg = '<b>'+str(dsorted_visits_spatial[spatial_index][1])+'</b> out of <b>'+str(total_people)+'</b> of the people in the network have eaten in <b>'+str(dsorted_visits_spatial[spatial_index][0])
print msg
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int(creation_date.strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",html_content="'+msg+'"'
print cmd_str
insert_card(cmd_str,rid)
msg = 'The people in the network have walked a total of '+str(steps_walked)+' steps'
print msg
msg = walking_message(steps_walked,"If you and the network on an average walk",total_people)
print msg
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int((creation_date-(datetime.timedelta(days=1))).strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",html_content="'+msg+'"'
print cmd_str
#insert_card(cmd_str,rid)
else:
msg = get_restaurant_name(asorted_all_restaurant_visits[0][0])+' was least popular among the people in the network '
print msg
msg = '<b>'+str(int(percent_notvisited_alltime))+'</b> % of the people in the network have <b>not</b> eaten at <b>'+str(rname_alltime)+'</b>'
print msg
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int((creation_date-(datetime.timedelta(days=4))).strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",html_content="'+msg+'"'
print cmd_str
#insert_card(cmd_str,rid)
msg = ''+str(notvisited_alltime)+' out of '+str(total_people)+' of the people in the network have not eaten at '+str(rname_alltime)
print msg
msg = ''+str(unhealthy_users)+' out of '+str(total_people)+' of the people in the network have not eaten healthy'
print msg
msg = ''+str(total_people-desc_cuisine[index][1])+' out of '+str(total_people)+' of the people in the network have not eaten healthy '+str(desc_cuisine[index][0])
print msg
if spatial_index:
msg = ''+str(total_people-dsorted_visits_spatial[spatial_index][1])+' out of '+str(total_people)+' of the people in the network have not eaten in '+str(dsorted_visits_spatial[spatial_index][0])
print msg
if (check_data_existence([rest_visits_weekly])):
print "Counting weekly visits"
(weekly_all_restaurant_visits,asorted_weekly_all_restaurant_visits,dsorted_weekly_all_restaurant_visits) = count_visits(rest_visits_weekly)
print "Cuisines eaten weekly"
(cuisines_count_weekly,asc_cuisine_weekly,desc_cuisine_weekly) = count_cuisines_eaten([rest_visits_weekly])
print "Counting healthy friends- weekly"
(healthy_users_weekly,unhealthy_users_weekly) = count_healthy_users(all_network_healthy_visits_weekly)
print "Computing spatial stats weekly"
(visits_spatial_weekly,asorted_visits_spatial_weekly,dsorted_visits_spatial_weekly) = compute_spatial_stats([rest_visits_weekly])
(visited_weekly,notvisited_weekly,percent_visited_weekly,percent_notvisited_weekly,rname_weekly) = calc_stats(total_people,dsorted_weekly_all_restaurant_visits)
(index_weekly,w1_w,w2_w) = select_random_cuisine(desc_cuisine_weekly)
if dsorted_visits_spatial_weekly:
spatial_index_weekly = random.randrange(0,4)
if (msg_type==1):
msg = ''+str(int(percent_visited_weekly))+' % of the people in the network have eaten at '+str(rname_weekly)+' over the past week'
print msg
msg = ''+str(visited_weekly)+' out of '+str(total_people)+' of the people in the network have eaten at '+str(rname_weekly)+' over the past week'
print msg
msg = '<b>'+str(healthy_users_weekly)+'</b> out of <b>'+str(total_people)+'</b> of the people in the network have eaten healthy over the past week'
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int((creation_date-(datetime.timedelta(days=9))).strftime('%s'))*1000-15000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",html_content="'+msg+'"'
print cmd_str
#insert_card(cmd_str,rid)
print msg
msg = ''+str(desc_cuisine_weekly[index_weekly][1])+' out of '+str(total_people)+' of the people in the network have eaten healthy '+str(desc_cuisine_weekly[index_weekly][0])+' over the past week'
print msg
tags = ["healthy","chinese"]
ret_val = begin_localimg(msg.replace("1 times","once"),"namedfriend_overlay_cuisine_monthly.jpg")
if ret_val == 1:
recid_img = create_binary_data("/Local/Users/dev/NetworkLabs/namedfriend_overlay_cuisine_monthly.jpg")
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int(creation_date.strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",image="'+recid_img+'"'
card_rid = insert_card(cmd_str,user_id,1)
id_rec = cl.command('create edge has_uploaded_image from '+str(card_rid)+' to '+str(recid_img))
print id_rec[0].rid
if spatial_index_weekly:
msg = ''+str(dsorted_visits_spatial_weekly[spatial_index_weekly][1])+' out of '+str(total_people)+' of the people in the network have eaten in '+str(dsorted_visits_spatial_weekly[spatial_index_weekly][0])
print msg
else:
msg = ''+str(int(percent_notvisited_weekly))+' % of the people in the network have not eaten at '+str(rname_weekly)+' over the past week'
print msg
msg = ''+str(notvisited_weekly)+' out of '+str(total_people)+' of the people in the network have not eaten at '+str(rname_weekly)+' over the past week'
print msg
msg = ''+str(unhealthy_users_weekly)+' out of '+str(total_people)+' of the people in the network have not eaten healthy over the past week'
print msg
if spatial_index_weekly:
msg = ''+str(total_people-dsorted_visits_spatial_weekly[spatial_index_weekly][1])+' out of '+str(total_people)+' of the people in the network have not eaten in '+str(dsorted_visits_spatial_weekly[spatial_index_weekly][0])+' over the past week'
print msg
if (check_data_existence([rest_visits_monthly])):
print "Counting monthly visits"
(monthly_all_restaurant_visits,asorted_monthly_all_restaurant_visits,dsorted_monthly_all_restaurant_visits) = count_visits(rest_visits_monthly)
print "Cuisines eaten monthly"
(cuisines_count_monthly,asc_cuisine_monthly,desc_cuisine_monthly) = count_cuisines_eaten([rest_visits_monthly])
print "Counting healthy friends- monthly"
(healthy_users_monthly,unhealthy_users_monthly) = count_healthy_users(all_network_healthy_visits_monthly)
print "Computing spatial stats monthly"
(visits_spatial_monthly,asorted_visits_spatial_monthly,dsorted_visits_spatial_monthly) = compute_spatial_stats([rest_visits_monthly])
(visited_monthly,notvisited_monthly,percent_visited_monthly,percent_notvisited_monthly,rname_monthly) = calc_stats(total_people,dsorted_monthly_all_restaurant_visits)
(index_monthly,w1_m,w2_m) = select_random_cuisine(desc_cuisine_monthly)
if dsorted_visits_spatial_monthly:
spatial_index_monthly = random.randrange(0,4)
if msg_type == 1:
msg = ''+str(int(percent_visited_monthly))+' % of the people in the network have eaten at '+str(rname_monthly)+' over the past month'
print msg
msg = ''+str(visited_monthly)+' out of '+str(total_people)+' of the people in the network have eaten at '+str(rname_monthly)+' over the past month'
print msg
msg = ''+str(healthy_users_monthly)+' out of '+str(total_people)+' of the people in the network have eaten healthy over the past month'
print msg
msg = '<b>'+str(desc_cuisine_monthly[index_monthly][1])+'</b> out of <b>'+str(total_people)+'</b> of the people in the network have eaten healthy '+str(desc_cuisine_monthly[index_monthly][0])+' over the <b>past month</b>'
print msg
if spatial_index_monthly:
msg = ''+str(dsorted_visits_spatial_monthly[spatial_index_monthly][1])+' out of '+str(total_people)+' of the people in the network have eaten in '+str(dsorted_visits_spatial_monthly[spatial_index_monthly][0])+' over the past month'
print msg
else:
msg = ''+str(int(percent_notvisited_monthly))+' % of the people in the network have not eaten at '+str(rname_monthly)+' over the past month'
print msg
msg = ''+str(notvisited_monthly)+' out of '+str(total_people)+' of the people in the network have not eaten at '+str(rname_monthly)+' over the past month'
print msg
msg = ''+str(unhealthy_users_monthly)+' out of '+str(total_people)+' of the people in the network have not eaten healthy over the past month'
print msg
if spatial_index_monthly:
msg = ''+str(total_people-dsorted_visits_spatial_monthly[spatial_index_monthly][1])+' out of '+str(total_people)+' of the people in the network have not eaten in '+str(dsorted_visits_spatial_monthly[spatial_index_monthly][0])+' over the past month'
print msg
def rest_visited_similar_pref(rid,rest_name=""):
# print "In rest visited by similar pref"
cl = connect()
all_restaurants_visited = {}
# print rid
res1 = cl.command("select is_vegetarian,major,year from Person where @rid="+str(rid))
vegetarian = res1[0].is_vegetarian
major = res1[0].major
year = res1[0].year
# print vegetarian
# print type(vegetarian)
# print major
# print year
# if vegetarian==True:
# rest_visited_by_vegetarians(rid,vegetarian,1,rest_name)
# rest_visited_by_vegetarians(rid,vegetarian,-1,rest_name)
rest_visited_by_majoryear(rid,major,year,rest_name,1)
rest_visited_by_majoryear(rid,major,year,rest_name,-1)
def queries(user_id):
(epoch_week,epoch_month) = find_epoch_time()
rest_visits = cl.command("select distinct(name) as name, distinct(res_id) as res_id, distinct(cuisine) as cuisine from (select expand(both('eats_at')) from Person where @rid = "+user_id+")")
rest_visits_weekly = cl.command("select distinct(name) as name, distinct(res_id) as res_id, distinct(cuisines) as cuisine from (select expand(in) from eats_at where created_at > "+str(epoch_week)+" and out="+user_id+")")
rest_visits_monthly = cl.command("select distinct(name) as name, distinct(res_id) as res_id, distinct(cuisines) as cuisine from (select expand(in) from eats_at where created_at > "+str(epoch_month)+" and out="+user_id+")")
healthy_visits = cl.command("select name, res_id as res_id from (select expand(both('eats_at')) from Person where @rid = "+user_id+")")
healthy_visits_weekly = cl.command("select name, res_id as res_id from (select expand(in) from eats_at where created_at > "+str(epoch_week)+" and out="+user_id+")")
healthy_visits_monthly = cl.command("select name, res_id as res_id from (select expand(in) from eats_at where created_at > "+str(epoch_month)+" and out="+user_id+")")
return (rest_visits,rest_visits_weekly,rest_visits_monthly,healthy_visits,healthy_visits_weekly,healthy_visits_monthly)
def process_query_alltime(all_visits,all_healthy_visits,all_visits_cuisine,all_visits_spatial):
print "Counting visits over all time"
(rest_visit,asc_rest_visit,desc_rest_visit) = count_restaurants_visited(all_visits)
print "Counting healthy visits"
(healthy_users,unhealthy_users) = count_healthy_users(all_healthy_visits)
print "Counting cuisines eaten over all time"
(cuisines_count,asc_cuisine,desc_cuisine) = count_cuisines_eaten(all_visits_cuisine)
print "Computing spatial stats"
(visits_spatial,asorted_visits_spatial,desc_visits_spatial) = compute_spatial_stats(all_visits_spatial)
return (rest_visit,desc_rest_visit,healthy_users,unhealthy_users,cuisines_count,desc_cuisine,visits_spatial,desc_visits_spatial)
def process_query_weekly(all_visits_weekly,all_healthy_visits_weekly,all_visits_weekly_cuisine,all_visits_weekly_spatial):
print "Counting weekly visits"
(week_rest_visit,week_asc_rest_visit,week_desc_rest_visit) = count_restaurants_visited(all_visits_weekly)
print "Counting healthy visits weekly"
(healthy_users_weekly,unhealthy_users_weekly) = count_healthy_users(all_healthy_visits_weekly)
print "Cuisines eaten weekly"
(cuisines_count_weekly,asc_cuisine_weekly,desc_cuisine_weekly) = count_cuisines_eaten(all_visits_weekly_cuisine)
print "Computing spatial stats weekly"
(visits_spatial_weekly,asorted_visits_spatial_weekly,desc_visits_spatial_weekly) = compute_spatial_stats(all_visits_weekly_spatial)
return (week_rest_visit,week_desc_rest_visit,healthy_users_weekly,unhealthy_users_weekly,cuisines_count_weekly,desc_cuisine_weekly,visits_spatial_weekly,desc_visits_spatial_weekly)
def process_query_monthly(all_visits_monthly,all_healthy_visits_monthly,all_visits_monthly_cuisine,all_visits_monthly_spatial):
print "Counting monthly visits"
(month_rest_visit,month_asc_rest_visit,month_desc_rest_visit) = count_restaurants_visited(all_visits_monthly)
print "Counting healthy visits monthly"
(healthy_users_monthly,unhealthy_users_monthly) = count_healthy_users(all_healthy_visits_monthly)
print "Cuisines eaten monthly"
(cuisines_count_monthly,asc_cuisine_monthly,desc_cuisine_monthly) = count_cuisines_eaten(all_visits_monthly_cuisine)
print "Computing spatial stats monthly"
(visits_spatial_monthly,asorted_visits_spatial_monthly,desc_visits_spatial_monthly) = compute_spatial_stats(all_visits_monthly_spatial)
return (month_rest_visit,month_desc_rest_visit,healthy_users_monthly,unhealthy_users_monthly,cuisines_count_monthly,desc_cuisine_monthly,visits_spatial_monthly,desc_visits_spatial_monthly)
def select_random_cuisine(cuisines_eaten):
len_visits = len(cuisines_eaten)
index_cuisine = random.randrange(0,len_visits)
cuisine_places = ['bars','streetvendors','Cheese Shops','delis','divebars','arcades','Wine & Spirits','poolhalls','ethnicmarkets','pubs','diners','lounges','sportsbars','bakeries','cafes','seafoodmarkets','Delis','restaurants','creperies']
cuisine_types = ['mexican','chinese','mediterranean','japanese','tradamerican','spanish','hotdog','newamerican','indpak','vietnamese','asianfusion','korean','tex-mex','cajun','latin','southern','vegan','gluten_free','mongolian','thai','malaysian','greek','szechuan','mideastern','argentine','vegetarian','irish','italian']
food = ['sushi','soup','Sandwiches','hotdog','chicken_wings','fishnchips','burgers','buffets','icecream','sandwiches','soulfood','bbq','pizza','cheesesteaks','chocolate','coffee','hotdogs','salad','gelato','breakfast_brunch','steak','seafood']
# print cuisines_eaten[index_cuisine][0]
if cuisines_eaten[index_cuisine][0] in cuisine_places:
word1 = ' at '
word2 = ' '
elif cuisines_eaten[index_cuisine][0] in cuisine_types:
word1 = ' '
word2 = ' food '
else:
word1 = ' '
word2 = ' '
return (index_cuisine,word1,word2)
def rest_visited_by_vegetarians(rid,veg,msg_type,restaurant_name):
restaurant_veg_visits ={}
msg = ""
steps_walked = 0
all_veg_rest_visits = []
all_veg_healthy_visits = []
all_veg_rest_visits_weekly = []
all_veg_rest_visits_monthly = []
all_veg_healthy_visits_weekly = []
all_veg_healthy_visits_monthly = []
all_veg_spatial_stats = []
all_veg_spatial_stats_weekly = []
all_veg_spatial_stats_monthly = []
cl = connect()
vegetarians = cl.command("select * from Person where is_vegetarian="+str(veg))
total_veg = len(vegetarians)
for i in xrange(total_veg):
vegid = str(vegetarians[i].rid)
steps_walked += vegetarians[i].steps_walked
(veg_rest_visits,veg_rest_visits_weekly,veg_rest_visits_monthly,veg_healthy_visits,veg_healthy_visits_weekly,veg_healthy_visits_monthly) = queries(vegid)
all_veg_rest_visits.append(veg_rest_visits)
all_veg_healthy_visits.append(veg_healthy_visits)
all_veg_rest_visits_weekly.append(veg_rest_visits_weekly)
all_veg_rest_visits_monthly.append(veg_rest_visits_monthly)
all_veg_healthy_visits_weekly.append(veg_healthy_visits_weekly)
all_veg_healthy_visits_monthly.append(veg_healthy_visits_monthly)
if (check_data_existence(all_veg_rest_visits)):
print "Finding stats vegetarians- all aggregates"
(veg_visits,veg_desc_visits,healthy_veg,unhealthy_veg,veg_cuisine_count,veg_desc_cuisine,veg_spatial,veg_desc_spatial) = process_query_alltime(all_veg_rest_visits,all_veg_healthy_visits,all_veg_rest_visits,all_veg_rest_visits)
(visited_alltime,notvisited_alltime,percent_visited_alltime,percent_notvisited_alltime,rname_alltime) = calc_stats(total_veg,veg_desc_visits)
(index,w1,w2) = select_random_cuisine(veg_desc_cuisine)
if veg_desc_spatial:
spatial_index = random.randrange(0,4)
if msg_type==1:
msg = '<table><p>Top 3 restaurants vegetarians visited</p><tr><td><b>Restaurant Name</b></td><td><b>Visits</b></td></tr><tr><td>'+get_restaurant_name(veg_desc_visits[0][0])+'</td><td>'+str(veg_desc_visits[0][1])+'</td></tr><tr><td>'+get_restaurant_name(veg_desc_visits[1][0])+'</td><td>'+str(veg_desc_visits[1][1])+'</td></tr><tr><td>'+get_restaurant_name(veg_desc_visits[2][0])+'</td><td>'+str(veg_desc_visits[2][1])+'</td></tr></table>'
print msg
cmd_str = 'create vertex Card set type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int(creation_date.strftime('%s'))*1000-20000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",html_content="'+msg+'"'
print cmd_str
#insert_card(cmd_str,rid)
msg = ''+str(int(percent_visited_alltime))+' % of the vegetarians have eaten at '+str(rname_alltime)
print msg
msg = ''+str(visited_alltime)+' out of '+str(total_veg)+' of the vegetarians have eaten at '+str(rname_alltime)
print msg
msg = ''+str(healthy_veg)+' out of '+str(total_veg)+' of the vegetarians have eaten healthy'
print msg
msg = ''+str(veg_desc_cuisine[index][1])+' out of '+str(total_veg)+' of the vegetarians have eaten'+str(w1)+'healthy '+str(veg_desc_cuisine[index][0])+str(w2)
print msg
tags = ["healthy","chinese"]
ret_val = begin_localimg(msg.replace("1 times","once"),"namedfriend_overlay_cuisine_monthly.jpg")
if ret_val == 1:
recid_img = create_binary_data("/Local/Users/dev/NetworkLabs/namedfriend_overlay_cuisine_monthly.jpg")
cmd_str = 'create vertex Card set display_type="info",comment_count=0,comment_list=[],like_count=0,like_list=[],people_involved=[],created_at="'+str(int(creation_date.strftime('%s'))*1000)+'",created_at_datetime="'+str(creation_date.strftime('%Y-%m-%d'))+'",image="'+recid_img+'"'
# card_rid = insert_card(cmd_str,user_id,1)
# id_rec = cl.command('create edge has_uploaded_image from '+str(card_rid)+' to '+str(recid_img))
# print id_rec[0].rid
if spatial_index:
msg = ''+str(veg_desc_spatial[spatial_index][1])+' out of '+str(total_veg)+' of the vegetarians have eaten in '+str(veg_desc_spatial[spatial_index][0])
print msg
msg = 'Your friends have walked a total of '+str(steps_walked)+' steps'
print msg
msg = walking_message(steps_walked,"If you and the other vegetarians in the network walk",total_veg)
print msg
else:
msg = ''+str(int(percent_notvisited_alltime))+' % of the vegetarians have not eaten at '+str(rname_alltime)
print msg
msg = ''+str(notvisited_alltime)+' out of '+str(total_veg)+' of the vegetarians have not eaten at '+str(rname_alltime)
print msg
msg = ''+str(unhealthy_veg)+' out of '+str(total_veg)+' of the vegetarians have not eaten healthy'
print msg
msg = ''+str(total_veg-veg_desc_cuisine[index][1])+' out of '+str(total_veg)+' of the vegetarians have not eaten healthy '+str(veg_desc_cuisine[index][0])
print msg
if spatial_index:
msg = ''+str(total_veg-veg_desc_spatial[spatial_index][1])+' out of '+str(total_veg)+' of the vegetarians have not eaten in '+str(veg_desc_spatial[spatial_index][0])
print msg