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