js/playscreen.js
branchold-fetch-method
changeset 8 ba3652cdd44b
parent 7 a2d5b669f663
equal deleted inserted replaced
7:a2d5b669f663 8:ba3652cdd44b
   425     		var mmso = data.segments[localMmsoDelta + i];
   425     		var mmso = data.segments[localMmsoDelta + i];
   426     		var mmsostruct = tweetstructure[mmso.id];
   426     		var mmsostruct = tweetstructure[mmso.id];
   427     		if (mmsostruct) {
   427     		if (mmsostruct) {
   428 	    		for (var j = 0; j < topics.length; j++) {
   428 	    		for (var j = 0; j < topics.length; j++) {
   429 	    			var topicid = topics[j];
   429 	    			var topicid = topics[j];
   430 					if (mmsostruct.tweetids) {
   430 					if (mmsostruct[topicid]) {
   431 						var tweetids = mmsostruct.tweetids[topicid];
   431 						var tweetids = mmsostruct[topicid].tweetids;
   432 						for (var k = 0; k < tweetids.length; k++) {
   432 						for (var k = 0; k < tweetids.length; k++) {
   433 							toshow.push(tweetids[k]);
   433 							toshow.push(tweetids[k]);
   434 						}
   434 						}
   435 					}
   435 					}
   436 	    		}
   436 	    		}
   444     		return ((tw.status == 2) && (tw.timestamp > (localpos - localduration / 2)) && (tw.timestamp < (localpos + localduration / 2)));
   444     		return ((tw.status == 2) && (tw.timestamp > (localpos - localduration / 2)) && (tw.timestamp < (localpos + localduration / 2)));
   445 		});
   445 		});
   446 		
   446 		
   447 		tweetstoshow.forEach(function(tw) {
   447 		tweetstoshow.forEach(function(tw) {
   448 			tw.topic = tw.topics.filter(function(t) {
   448 			tw.topic = tw.topics.filter(function(t) {
   449 				return topics.indexOf(t.topic.toString()) !== -1;
   449 				return topics.indexOf(t.topic) !== -1;
   450 			}).sort(function(a,b) {
   450 			}).sort(function(a,b) {
   451 				return b.weight - a.weight
   451 				return b.weight - a.weight
   452 			})[0];
   452 			})[0];
   453 		});
   453 		});
   454 		
   454 		
   477 		showTopicViz();
   477 		showTopicViz();
   478     	
   478     	
   479     }
   479     }
   480     
   480     
   481     function getMmsoTweetIds(mmstruct) {
   481     function getMmsoTweetIds(mmstruct) {
   482     	TopicsBean.bestSocialInteractionsIdsMatching(mmstruct.mmsoid, 0, _NTWEETS, {
   482     	console.log("TopicsBean.bestSocialInteractionsIdsMatching('" + mmstruct.mmso + "', " + mmstruct.topic + ", 0, " + _NTWEETS + ")");
   483 			callback: function(res) {
   483     	TopicsBean.bestSocialInteractionsIdsMatching(mmstruct.mmso, mmstruct.topic, 0, _NTWEETS, {
       
   484 			callback: function(tw) {
   484 				mmstruct.status = 2;
   485 				mmstruct.status = 2;
   485 				mmstruct.tweetids = res;
   486 				mmstruct.tweetids = tw;
   486 				for (var j = 0; j < res.length; j++) {
   487 				for (var k = 0; k < tw.length; k++) {
   487 					var tweetids = res[j],
   488 					var tweetid = tw[k],
   488 						ntw = tweetids.length,
   489 						relevance = mmstruct.weight * (_NTWEETS - k) / _NTWEETS;
   489 						topicweight = data.topics[j].weights[mmstruct.mmsoindex];
   490 					if (!requestedtweets[tweetid]) {
   490 					for (var k = 0; k < tweetids.length; k++) {
   491 						requestedtweets[tweetid] = {
   491 						var relevance = topicweight * (ntw - k) / ntw,
   492 							id: tweetid,
   492 							tweetid = tweetids[k];
   493 							status: 0,
   493 						if (!requestedtweets[tweetid]) {
   494 							topics: []
   494 							requestedtweets[tweetid] = {
       
   495 								id: tweetid,
       
   496 								status: 0,
       
   497 								topics: []
       
   498 							}
       
   499 						}
   495 						}
   500 						requestedtweets[tweetid].topics.push({
   496 						requestedtweets[tweetid].topics.push({
   501 							topic: j,
   497 							topic: j,
   502 							weight: relevance
   498 							weight: relevance
   503 						});
   499 						});
   504 					}
   500 					}
       
   501 					requestedtweets[tweetid].topics.push({
       
   502 						topic: mmstruct.topic,
       
   503 						weight: relevance
       
   504 					});
   505 				}
   505 				}
   506 				debouncedGetTweetData();
   506 				debouncedGetTweetData();
   507 			},
   507 			},
   508 			errorHandler: function(err,info) {
   508 			errorHandler: function(err,info) {
   509 				mmstruct.status = 0;
   509 				mmstruct.status = 0;
   510 				console.error(err,info);
   510 				console.error(err,info);
   511 				throttledGetTweetIds();
   511 				debouncedGetTweetIds();
   512 			}
   512 			}
   513 		});
   513 		});
   514     }
   514     }
   515     
   515     
   516     var _MAX_BATCH = 20;
   516     var _MAX_BATCH = 20;
   519     	
   519     	
   520     	console.log("getTweetIds");
   520     	console.log("getTweetIds");
   521     	
   521     	
   522     	var toload = [];
   522     	var toload = [];
   523     	
   523     	
   524     	_(tweetstructure).each(function(w) {
   524     	_(tweetstructure).each(function(v) {
   525 			if (!w.status) {
   525     		_(v).each(function(w) {
   526 				w.status = 1;
   526     			if (!w.status) {
   527 				toload.push(w);
   527     				w.status = 1;
   528 			}
   528     				toload.push(w);
       
   529     			}
       
   530     		});
   529     	});
   531     	});
   530     	
   532     	
   531     	if (toload.length) {
   533     	if (toload.length) {
   532     		
   534     		
   533     		if (toload.length > _MAX_BATCH) {
   535     		if (toload.length > _MAX_BATCH) {
   534     			toload = _(toload).shuffle().slice(0,_MAX_BATCH);
   536     			toload = _(toload).shuffle().slice(0,_MAX_BATCH);
   535     			window.setInterval(throttledGetTweetIds,0);
   537     			window.setInterval(throttledGetTweetIds,0);
   536     		}
   538     		}
   537     		
   539     		console.log("Begin Batch");
   538     		dwr.engine.beginBatch();
   540     		dwr.engine.beginBatch();
   539     		toload.forEach(getMmsoTweetIds);
   541     		toload.forEach(getMmsoTweetIds);
   540     		dwr.engine.endBatch();
   542     		dwr.engine.endBatch();
       
   543     		console.log("End Batch");
   541     	}
   544     	}
   542     }
   545     }
   543     
   546     
   544     function getTweetData() {
   547     function getTweetData() {
   545     	
   548     	
   580     
   583     
   581     debouncedGetTweetData = _(getTweetData).debounce(125);
   584     debouncedGetTweetData = _(getTweetData).debounce(125);
   582     
   585     
   583     throttledGetTweetIds = _(getTweetIds).throttle(10000);
   586     throttledGetTweetIds = _(getTweetIds).throttle(10000);
   584     
   587     
       
   588     debouncedGetTweetIds = _(throttledGetTweetIds).debounce(125);
       
   589     
   585     throttledShowTweets = _(showTweets).throttle(200);
   590     throttledShowTweets = _(showTweets).throttle(200);
   586     
   591     
   587     function getLocalTweets() {
   592     function getLocalTweets() {
   588     	
   593     	
   589     	console.log("getLocalTweets");
   594     	console.log("getLocalTweets");
   591     	var topics = Array.prototype.join.call($(".topic.selected").map(function(){return $(this).attr("data-topic-id")})).split(",");
   596     	var topics = Array.prototype.join.call($(".topic.selected").map(function(){return $(this).attr("data-topic-id")})).split(",");
   592     	
   597     	
   593     	for (var i = 0; i < localMmsos.length; i++) {
   598     	for (var i = 0; i < localMmsos.length; i++) {
   594     		var mmso = data.segments[localMmsoDelta + i];
   599     		var mmso = data.segments[localMmsoDelta + i];
   595     		if (!tweetstructure[mmso.id]) {
   600     		if (!tweetstructure[mmso.id]) {
   596     			tweetstructure[mmso.id] = {
   601     			tweetstructure[mmso.id] = {}
   597     				mmsoid: mmso.id,
   602     		}
   598     				mmsoindex: localMmsoDelta + i,
   603     		var mmsostruct = tweetstructure[mmso.id];
   599     				status: 0
   604     		for (var j = 0; j < topics.length; j++) {
   600     			}
   605 				var topicid = topics[j],
       
   606 					weight = data.topics[topics[j]].weights[localMmsoDelta + i];
       
   607 				if (weight > .1 && !mmsostruct[topicid]) {
       
   608 					mmsostruct[topicid] = {
       
   609 						topic: topicid,
       
   610 						mmso: mmso.id,
       
   611 						status: 0,
       
   612 						weight: weight,
       
   613 						tweetids: []
       
   614 					};
       
   615 				}
   601     		}
   616     		}
   602     	}
   617     	}
   603     	
   618     	
   604     	throttledGetTweetIds();
   619     	throttledGetTweetIds();
   605     	throttledShowTweets();
   620     	throttledShowTweets();
   694 
   709 
   695 var data = { duration: 10200, topics: [] },
   710 var data = { duration: 10200, topics: [] },
   696     colorset = ["#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#A65628", "#F781BF"];
   711     colorset = ["#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#A65628", "#F781BF"];
   697 
   712 
   698 $(function() {
   713 $(function() {
   699 	
       
   700 	dwr.engine.setErrorHandler(function(a, b) { console.error("DWR", b); });
   714 	dwr.engine.setErrorHandler(function(a, b) { console.error("DWR", b); });
   701     
   715     
   702     $(".topics-block").draggable({axis:"x"});
   716     $(".topics-block").draggable({axis:"x"});
   703     
   717     
   704     var loadedSteps = 0,
   718     var loadedSteps = 0,