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