1
+
2
+ /*!
3
+ * Module dependencies.
4
+ */
5
+ var graph = require ( 'fbgraph' ) ;
6
+ var _ = require ( 'lodash' ) ;
7
+
8
+ exports . index = function ( req , res ) {
9
+ res . render ( 'index' , {
10
+ title : 'Node Express Mongoose Boilerplate'
11
+ } ) ;
12
+ } ;
13
+
14
+
15
+
16
+ exports . results = function ( req , res ) {
17
+
18
+ var nsp = req . io . of ( '/' + req . params . uid ) ;
19
+
20
+ var categories = { } ;
21
+ var totalPosts = 0 ;
22
+ var sharedStory = 0 ;
23
+
24
+ var handleResults = function ( err , res ) {
25
+ if ( err ) {
26
+ console . log ( err ) ;
27
+ return ;
28
+ }
29
+
30
+ if ( res . data ) {
31
+ totalPosts += res . data . length ;
32
+
33
+ // console.log('Got ' + res.data.length + ' results. ' + totalPosts + ' total posts fetched.');
34
+
35
+ _ . each ( res . data , function ( d ) {
36
+ var category = d . from . category || 'People' ;
37
+ categories [ category ] = ( categories [ category ] ) ? categories [ category ] + 1 : 1 ;
38
+
39
+ if ( d . status_type && d . status_type === 'shared_story' ) {
40
+ sharedStory ++ ;
41
+ }
42
+ } ) ;
43
+ }
44
+
45
+ if ( res . paging && res . paging . next ) {
46
+ graph . get ( res . paging . next , handleResults ) ;
47
+ }
48
+
49
+
50
+ // console.log('');
51
+ // console.log('Current Breakdown');
52
+ // console.log('-----------------');
53
+ // console.log(totalPosts + ' total posts');
54
+ // console.log((sharedStory / totalPosts * 100).toFixed(2) + '% are links');
55
+
56
+ // console.log('');
57
+ // _.each(_.sortBy(_.keys(categories), function(k) { return -categories[k]; }), function(key) {
58
+ // var val = categories[key];
59
+ // console.log((val / totalPosts * 100).toFixed(2) + '% from ' + key);
60
+ // });
61
+ // console.log('');
62
+
63
+ console . log ( 'emitting update' ) ;
64
+ nsp . emit ( 'update' , {
65
+ totalPosts : totalPosts ,
66
+ sharedStory : sharedStory ,
67
+ categories : categories
68
+ } ) ;
69
+
70
+ } ;
71
+
72
+ graph . get ( '/me/home' , { access_token : req . user . accessToken } , handleResults ) ;
73
+
74
+
75
+
76
+ res . render ( 'results' , {
77
+ title : 'Analysis Results'
78
+ } ) ;
79
+ }
0 commit comments