Skip to content

Commit c52a8e2

Browse files
committed
throttling moved into tweets service
1 parent 0327a25 commit c52a8e2

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

app/assets/javascripts/controllers.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
angular.module('birdwatch.controllers', ['birdwatch.services', 'charts.barchart', 'charts.wordcloud', 'ui.bootstrap']).
55
controller('BirdWatchCtrl',function ($scope, $location, utils, barchart, wordcloud, $timeout, wordCount, cf, tweets) {
66
$scope.prevSizeOpts = ['100', '500', '1000', '2000', '5000', '10000', '20000'];
7-
$scope.prevSize = $scope.prevSizeOpts[3];
7+
$scope.prevSize = $scope.prevSizeOpts[4];
88
$scope.pageSizeOpts = [5, 10, 25, 50, 100];
99
$scope.pageSize = $scope.pageSizeOpts[1];
1010
$scope.live = true;
@@ -31,16 +31,10 @@ angular.module('birdwatch.controllers', ['birdwatch.services', 'charts.barchart'
3131
var onTimeout = function () { updateTimeout = $timeout(onTimeout, 10000); };
3232
var updateTimeout = onTimeout();
3333

34-
var insertionCache = [];
35-
// callback to perform when new tweets available
34+
/** actions to perform when new tweets are available through the streaming connection */
3635
tweets.registerCallback(function (t) {
37-
insertionCache = insertionCache.concat(t); // every received item is appended to insertionCache.
38-
_.throttle(function() { // throttle because every insertion triggers expensive
39-
$scope.wordCount.insert(insertionCache); // $scope.apply(), insert cache once every 2 seconds,
40-
insertionCache = []; // then empty cache.
41-
$scope.words = $scope.wordCount.getWords();
42-
}, 2000)();
43-
36+
$scope.wordCount.insert(t);
37+
$scope.words = $scope.wordCount.getWords();
4438
cf.add(t);
4539
});
4640

app/assets/javascripts/services/tweets.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
/** tweets service, load previous tweets and receives subsequent live tweets for given query */
44
angular.module('birdwatch.services').factory('tweets', function ($http, utils, $location) {
55
var tweetFeed;
6+
var tweetsCache = [];
67

78
/** callback function to perform when new tweet(s) arrive */
89
var onNewTweets = function (t) {};
910
var registerCallback = function (callback) { onNewTweets = callback; };
1011

11-
/** handle incoming tweets: add to tweets array */
12-
var addTweet = function (msg) { onNewTweets([utils.formatTweet(JSON.parse(msg.data))]); };
13-
1412
/** Load previous Tweets, paginated. Recursive function, calls itself with the next chunk to load until
1513
* eventually n, the remaining tweets to load, is not larger than 0 any longer. guarantees at least n hits
1614
* if available, potentially more if (n % chunkSize != 0) */
@@ -20,8 +18,7 @@ angular.module('birdwatch.services').factory('tweets', function ($http, utils, $
2018
.success(function (data) {
2119
onNewTweets(data.hits.hits.reverse().map(function (t) { return t._source; }).map(utils.formatTweet));
2220
loadPrev(searchString, n - chunkSize, chunkSize, offset + chunkSize);
23-
}).error(function (data, status, headers, config) { }
24-
);
21+
}).error(function (data, status, headers, config) { });
2522
}
2623
};
2724

@@ -36,10 +33,19 @@ angular.module('birdwatch.services').factory('tweets', function ($http, utils, $
3633
}
3734
else $location.path("");
3835

36+
/** handle incoming tweets: add to tweetsCache array, run callback at most every second */
37+
var cachedCallback = function(msg) {
38+
tweetsCache = tweetsCache.concat(utils.formatTweet(JSON.parse(msg.data)));
39+
_.throttle(function() { // throttle because insertion too expensive on high traffic searches
40+
onNewTweets(tweetsCache); // run callback with all items in cache
41+
tweetsCache = []; // then empty cache.
42+
}, 1000)();
43+
};
44+
3945
tweetFeed = new EventSource("/tweetFeed?q=" + searchString);
40-
tweetFeed.addEventListener("message", addTweet, false);
46+
tweetFeed.addEventListener("message", cachedCallback, false);
4147

42-
loadPrev(searchString, prevSize, 100, 0);
48+
loadPrev(searchString, prevSize, 500, 0); // load previous tweets in chunks of size 500
4349
};
4450

4551
return { search: search, registerCallback: registerCallback};

0 commit comments

Comments
 (0)