Advertisement
Guest User

HabraCamp Stats.js

a guest
Aug 2nd, 2012
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var commentsList = $('div.comment_item').map(function (i) {
  2.     var id = this.id.match(/\d+/)[0],
  3.         rate = $("#voting_" + id + " span.score").attr('title').match(/↑(\d+) и ↓(\d+)/i);
  4.     return {
  5.         id: id,
  6.         pluses: +rate[1],
  7.         minuses: +rate[2],
  8.         rating: +rate[1] - rate[2],
  9.         answers: $('#reply_comments_' + id + '>div').size(),
  10.         author: $('div.info[rel=' + id + '] a.username', this).html(),
  11.         message: $(this).children('div.message').html()
  12.     }
  13. }).get(),
  14.     commentsListFirstLevel = commentsList.filter(function (o) {
  15.         return $('#comment_' + o.id).parent('#comments').size() == 1
  16.     }),
  17.     commentsListNotFirstLevel = commentsList.filter(function (o) {
  18.         return $('#comment_' + o.id).parent('#comments').size() == 0
  19.     }),
  20.     buildTop = function (title, list, mapper, orderBy, limit) {
  21.         limit = limit || 10;
  22.         if (orderBy) {
  23.             list.sort(function (a, b) {
  24.                 return b[orderBy] - a[orderBy]
  25.             })
  26.         };
  27.         log('<spoiler title="' + title.replace(/%n/g, limit) + '"><ol><li>' + list.slice(0, limit).map(mapper).join('</li>\n<li>') + "</li></ol></spoiler>");
  28.     },
  29.     log = function (msg) {
  30.         log.msg = (log.msg || "") + msg;
  31.     },
  32.     generateURL = function (o) {
  33.         var msg = o.message.replace(/<[^<>]+>|\s+/ig, ' ').trim();
  34.         return '<a href="http://habrahabr.ru/post/148795/#comment_' + o.id + '">' + msg.substring(0, 64).trim().replace(/\s+/g, ' ') + (msg.length > 64 ? '…' : '') + '</a> — <hh user="' + o.author + '" />'
  35.     }, plural = function (number, one, two, five) {
  36.         one = number + ' ' + one;
  37.         two = number + ' ' + two;
  38.         five = number + ' ' + five;
  39.         number = Math.abs(number);
  40.         number %= 100;
  41.         if (number >= 5 && number <= 20) {
  42.             return five;
  43.         }
  44.         number %= 10;
  45.         if (number == 1) {
  46.             return one;
  47.         }
  48.         if (number >= 2 && number <= 4) {
  49.             return two;
  50.         }
  51.         return five;
  52.     };
  53.  
  54. log.print = function () {
  55.     console.log(log.msg)
  56. };
  57.  
  58. log("<h4>Топы по комментариям первого уровня</h4>");
  59. buildTop('Топ %n по ответам', commentsListFirstLevel, function (o) {
  60.     return generateURL(o) + ' (' + plural(o.pluses, "ответ", "ответа", "ответов") + ', рейтинг ' + o.rating + ')'
  61. }, 'answers');
  62.  
  63. buildTop('Топ %n по плюсам', commentsListFirstLevel, function (o) {
  64.     return generateURL(o) + ' (' + plural(o.pluses, "плюс", "плюса", "плюсов") + ', рейтинг ' + o.rating + ')'
  65. }, 'pluses');
  66.  
  67. buildTop('Топ %n по минусам', commentsListFirstLevel, function (o) {
  68.     return generateURL(o) + ' (' + plural(o.minuses, "минус", "минуса", "минусов") + ', рейтинг ' + o.rating + ')'
  69. }, 'minuses');
  70.  
  71. log("\n<h4>Топы по всем комментариям, исключая комментарии первого уровня</h4>");
  72. buildTop('Топ %n по ответам', commentsListNotFirstLevel, function (o) {
  73.     return generateURL(o) + ' (' + plural(o.pluses, "ответ", "ответа", "ответов") + ', рейтинг ' + o.rating + ')'
  74. }, 'answers');
  75.  
  76. buildTop('Топ %n по плюсам', commentsListNotFirstLevel, function (o) {
  77.     return generateURL(o) + ' (' + plural(o.pluses, "плюс", "плюса", "плюсов") + ', рейтинг ' + o.rating + ')'
  78. }, 'pluses');
  79.  
  80. buildTop('Топ %n по минусам', commentsListNotFirstLevel, function (o) {
  81.     return generateURL(o) + ' (' + plural(o.minuses, "минус", "минуса", "минусов") + ', рейтинг ' + o.rating + ')'
  82. }, 'minuses');
  83.  
  84. var users = {};
  85. commentsList.forEach(function (o) {
  86.     users[o.author] = (o.author in users ? users[o.author] : 0) + 1
  87. });
  88.  
  89. buildTop("\nТоп %n пользователей по количеству комментариев", Object.keys(users).map(function (u) {
  90.     return {
  91.         username: u,
  92.         comments: users[u]
  93.     }
  94. }), function (o) {
  95.     return '<hh user="' + o.username + '" /> — ' + plural(o.comments, "комментарий", "комментария", "комментариев");
  96. }, 'comments');
  97.  
  98. var unappreciated = commentsList.filter(function (o) {
  99.     return o.answers == 0 && $('#comment_' + o.id).parent('#comments').size() == 1
  100. });
  101.  
  102. log("\n<h4>Общая статистика</h4>");
  103. log("\nВсего выдано плюсов: " + commentsList.reduce(function (a, o) {
  104.     return a + o.pluses
  105. }, 0));
  106. log("\nВсего выдано минусов: " + commentsList.reduce(function (a, o) {
  107.     return a + o.minuses
  108. }, 0));
  109. log("\nХабраюзеров: " + Object.keys(users).length);
  110. log("\nСредний рейтинг: " + (commentsList.reduce(function (a, o) {
  111.     return a + o.pluses - o.minuses
  112. }, 0) / commentsList.length).toFixed(3));
  113. log('\nОбделённых вниманием: ' + unappreciated.length);
  114. buildTop('Обделённые вниманием, по рейтингу', unappreciated, function (o) {
  115.     return generateURL(o) + ' (рейтинг ' + (o.pluses - o.minuses) + ')'
  116. }, 'rating', 100);
  117.  
  118. log("Время сбора статистики: " + new Date);
  119. log.print();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement