PageRenderTime 28ms CodeModel.GetById 15ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/database_queries/event_counts.js

http://github.com/hpiwowar/alt-metrics_stats
JavaScript | 135 lines | 117 code | 13 blank | 5 comment | 34 complexity | 8db98c969711abce01103f4ce57ab770 MD5 | raw file
  1function(doc){
  2 totalViews = function(viewsObj, viewType){
  3  var total = 0;
  4  for (i in viewsObj) {
  5   total += parseInt(viewsObj[i][viewType]);
  6  }
  7  return total;
  8 }
  9 
 10 count = function(obj){
 11  if (typeof obj == "number") {
 12   return obj;
 13  }
 14  else if (typeof obj == "string") {
 15   return obj;
 16  }
 17  else if (typeof obj == "array") {
 18   return obj.length;
 19  }
 20  else if (typeof obj == "object") {
 21   var size = 0, key;
 22    for (key in obj) {
 23     if (obj.hasOwnProperty(key)) size++;
 24    }
 25    return size;   
 26	}
 27  else {
 28   return 0;
 29  }
 30 }
 31
 32 cleanText = function(dirty){
 33   dirtRegex = /\n|\r|\t|\| +/gi
 34   clean = [];
 35   if (typeof dirty == "object"){
 36    for (var i in dirty) {
 37     clean.push(dirty[i].replace(dirtRegex, " ").replace(/ +/g, " "));
 38    }
 39   }
 40   else if (typeof dirty == "string"){
 41	   clean = dirty.replace(dirtRegex, " ").replace(/ +/g, " ");
 42   }
 43   else {
 44	    clean = " ";
 45   }
 46   return clean;
 47 }
 48 
 49 var ret = {};
 50 if (doc.pubDate) {
 51  ret.doi                         = doc._id;
 52  ret.pubDate                     = doc.pubDate;
 53  ret.journal                     = doc.namespace.slice(-4)
 54  ret.articleType                 = (doc.articleType) ? cleanText(doc.articleType) : "NA";
 55  ret.authorsCount                = (doc.authors) ? count(doc.authors) : "NA";
 56  ret.f1000Factor                 = doc.F1000.factor;
 57  ret.backtweetsCount             = (doc.backtweets) ? count(doc.backtweets.tweets) : "NA";
 58  ret.deliciousCount              = (doc.delicious) ? count(doc.delicious.bookmarks) : "NA";
 59  ret.pmid                        = (doc.pmid) ? doc.pmid : "NA";
 60  ret.plosSubjectTags             = (doc.subjects) ? cleanText(doc.subjects).join("|") : "NA";
 61  ret.plosSubSubjectTags          = (doc.subSubjects) ? cleanText(doc.subSubjects).join("|") : "NA";
 62  
 63    
 64  // Facebook
 65  if (doc.facebook.current_stats) {
 66   ret.facebookShareCount         = count(doc.facebook.current_stats.share_count);
 67   ret.facebookLikeCount          = count(doc.facebook.current_stats.like_count);
 68   ret.facebookCommentCount       = count(doc.facebook.current_stats.comment_count);
 69   ret.facebookClickCount         = count(doc.facebook.current_stats.click_count);
 70  }
 71  else {
 72   ret.facebookShareCount         = "NA";
 73   ret.facebookLikeCount          = "NA";
 74   ret.facebookCommentCount       = "NA";
 75   ret.facebookClickCount         = "NA"; 
 76  }
 77  
 78  
 79  // Mendeley
 80  try {
 81   if (doc.mendeley.article_info == {}) {
 82       ret.mendeleyReadersCount   = 0;
 83	}
 84   else {
 85	    ret.mendeleyReadersCount   = count(doc.mendeley.article_info.stats.readers);
 86	}
 87  } catch(e) {
 88       ret.mendeleyReadersCount   = "NA";
 89  }
 90  
 91  
 92  // PLoS ALM
 93  if (doc.plos_alm.current_stats){
 94   ret.almBlogsCount              = count(doc.plos_alm.current_stats.blogs);
 95   ret.pdfDownloadsCount          = totalViews(doc.plos_alm.current_stats.views, "pdf");
 96   ret.xmlDownloadsCount          = totalViews(doc.plos_alm.current_stats.views, "xml");
 97   ret.htmlDownloadsCount         = totalViews(doc.plos_alm.current_stats.views, "html");
 98   ret.almCiteULikeCount          = count(doc.plos_alm.current_stats.citeulike);
 99   ret.almScopusCount             = count(doc.plos_alm.current_stats.scopus);
100   ret.almPubMedCentralCount      = count(doc.plos_alm.current_stats.pub_med);
101   ret.almCrossRefCount           = count(doc.plos_alm.current_stats.crossref);
102  }
103  else {
104   ret.almBlogsCount              = "NA";
105   ret.pdfDownloadsCount          = "NA";
106   ret.xmlDownloadsCount          = "NA";
107   ret.htmlDownloadsCount         = "NA";
108   ret.almCiteULikeCount          = "NA";
109   ret.almScopusCount             = "NA";
110   ret.almPubMedCentralCount      = "NA";
111   ret.almCrossRefCount           = "NA";
112  }
113  
114  // PLoS comments
115  if (doc.plos_comments.comments){
116   ret.plosCommentCount           = count(doc.plos_comments.comments);
117   ret.plosCommentResponsesCount  = count(doc.plos_comments.comments.filter(function(x){ return x.position > 0; }));
118  }
119  else {
120   ret.plosCommentCount           = "NA";
121   ret.plosCommentResponsesCount  = "NA";
122  }
123  
124  // wikipedia
125  if (doc.wikipedia) {
126   ret.wikipediaCites             = count(doc.wikipedia.latest_stats.filter(function(x){ return x.ns === 0; }));
127  }
128  else {
129   ret.wikipediaCites             = "NA";
130  }
131  
132  emit(doc._id, ret);
133 }
134}
135