PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/database_queries/events.js

http://github.com/hpiwowar/alt-metrics_stats
JavaScript | 67 lines | 65 code | 2 blank | 0 comment | 2 complexity | b32b6265565f553242eec5f7ab92c942 MD5 | raw file
 1function(doc) {
 2
 3 pubEvent = function(eventType, doi, creator, date, value){
 4	articlePubDateObj = new Date(doc.pubDate.replace(' ', 'T'));
 5   eventPubDateObj = new Date(date.replace(' ', 'T'));
 6	
 7  this.eventType = eventType;
 8  this.doi       = doi;
 9  this.creator   = creator;
10  this.date      = date;
11  this.latency   = (eventPubDateObj.getTime() - articlePubDateObj.getTime()) / 1000; //in seconds, not milliseconds
12  if (typeof(value) == "string") {
13   this.value = '"' + value.replace(/"/g, '""').replace(/\n|\r|\c/g,"") + '"';
14  } 
15  else {
16   this.value = value.join('|'); // we're assuming it's an array
17  }
18 }
19 
20 
21 if (doc.pubDate) {
22  for (i in doc.backtweets.tweets){
23   var thisTweet = doc.backtweets.tweets[i];
24	var tweetDate = thisTweet.tweet_created_at.replace(' ', 'T');
25	var tweetText = thisTweet.tweet_text;
26	thisEvent = new pubEvent("backtweets", doc._id, thisTweet.tweet_from_user_id, tweetDate, tweetText );
27   emit(thisEvent.eventType, thisEvent);
28  }
29  
30  for (i in doc.delicious.bookmarks){
31   var thisDeliciousBookmark = doc.delicious.bookmarks[i];
32   thisEvent = new pubEvent("delicious", doc._id, thisDeliciousBookmark.a, thisDeliciousBookmark.dt, thisDeliciousBookmark.t );
33   emit(thisEvent.eventType, thisEvent);
34  }
35  
36  if (doc.plos_alm.current_stats){
37   for (i in doc.plos_alm.current_stats.citeulike) {
38	 thisCiteULikeBookmark = doc.plos_alm.current_stats.citeulike[i]
39    thisEvent = new pubEvent("citeulike", doc._id, thisCiteULikeBookmark.creator, thisCiteULikeBookmark.pubDate, thisCiteULikeBookmark.tags);
40    emit(thisEvent.eventType, thisEvent);
41	}
42	
43	for (i in doc.plos_alm.current_stats.blogs) {
44	 thisBlogPost = doc.plos_alm.current_stats.blogs[i];
45    thisEvent = new pubEvent(thisBlogPost.from, doc._id, thisBlogPost.blogName, thisBlogPost.pubDate, thisBlogPost.uri );
46    emit(thisEvent.eventType, thisEvent);
47	}
48	
49	for (yearMonth in doc.plos_alm.current_stats.views){
50	 for (viewType in doc.plos_alm.current_stats.views[yearMonth]){
51     thisYear = yearMonth.slice(0,4);
52     thisMonth = yearMonth.slice(5);
53     thisMonthTs = new Date(thisYear, thisMonth, 0, 23, 59, 59).toISOString(); // last second in month	 
54	  thisEvent = new pubEvent(viewType + " views", doc._id, "", thisMonthTs, doc.plos_alm.current_stats.views[yearMonth][viewType]); 
55     emit(thisEvent.eventType, thisEvent);
56	 }
57   }
58  }
59
60  for  (i in doc.plos_comments.comments) {
61   thisComment = doc.plos_comments.comments[i];
62   thisEvent = new pubEvent("native comments", doc._id, thisComment.author, thisComment.pub_date, thisComment.content );
63	emit(thisEvent.eventType, thisEvent);
64  }
65 }
66}
67