<commit_before>// Recalculate post count in section
//
'use strict';


const ObjectId = require('mongoose').Types.ObjectId;
const Promise  = require('bluebird');


module.exports = function (N) {
  N.wire.on('init:jobs', function register_forum_section_post_count_update() {
    N.queue.registerTask({
      name: 'forum_section_post_count_update',

      // 15 minute delay by default
      postponeDelay: 15 * 60 * 1000,

      taskID: section_id => String(section_id),

      process: Promise.coroutine(function* (section_str_id) {
        let topic_statuses = N.models.forum.Topic.statuses;
        let section_id = new ObjectId(section_str_id);

        // Get visible topic and post count in section
        let visivle_cnt = yield N.models.forum.Topic
                                  .aggregate({
                                    $match: {
                                      section: section_id,
                                      st: { $in: topic_statuses.LIST_VISIBLE }
                                    }
                                  })
                                  .group({
                                    _id: null,
                                    post_count: { $sum: '$cache.post_count' },
                                    topic_count: { $sum: 1 }
                                  })
                                  .exec();

        visivle_cnt = visivle_cnt[0] || { post_count: 0, topic_count: 0 };

        // Get hb topic and post count in section
        let hb_cnt = yield N.models.forum.Topic
                              .aggregate({
                                $match: {
                                  section: section_id,
                                  st: topic_statuses.HB
                                }
                              })
                              .group({
                                _id: null,
                                post_count: { $sum: '$cache.post_count' },
                                topic_count: { $sum: 1 }
                              })
                              .exec();

        hb_cnt = hb_cnt[0] || { post_count: 0, topic_count: 0 };

        // Get topic and post count in children sections
        let children_cnt = yield N.models.forum.Section
                                    .aggregate({ $match: { parent: section_id } })
                                    .group({
                                      _id: null,
                                      post_count: { $sum: '$cache.post_count' },
                                      post_count_hb: { $sum: '$cache_hb.post_count' },
                                      topic_count: { $sum: '$cache.topic_count' },
                                      topic_count_hb: { $sum: '$cache_hb.topic_count' }
                                    })
                                    .exec();

        children_cnt = children_cnt[0] || { post_count: 0, post_count_hb: 0, topic_count: 0, topic_count_hb: 0 };

        let update_data = {
          'cache.post_count': visivle_cnt.post_count + children_cnt.post_count,
          'cache_hb.post_count': visivle_cnt.post_count + hb_cnt.post_count + children_cnt.post_count_hb,
          'cache.topic_count': visivle_cnt.topic_count + children_cnt.topic_count,
          'cache_hb.topic_count': visivle_cnt.topic_count + hb_cnt.topic_count + children_cnt.topic_count_hb
        };

        let section = yield N.models.forum.Section.findOneAndUpdate({ _id: section_id }, update_data);

        if (section && section.parent) {
          // Postpone parent count update
          N.queue.forum_section_post_count_update(section.parent).postpone();
        }
      })
    });
  });
};<commit_msg>typo fix<commit_after>// Recalculate post count in section
//
'use strict';


const ObjectId = require('mongoose').Types.ObjectId;
const Promise  = require('bluebird');


module.exports = function (N) {
  N.wire.on('init:jobs', function register_forum_section_post_count_update() {
    N.queue.registerTask({
      name: 'forum_section_post_count_update',

      // 15 minute delay by default
      postponeDelay: 15 * 60 * 1000,

      taskID: section_id => String(section_id),

      process: Promise.coroutine(function* (section_str_id) {
        let topic_statuses = N.models.forum.Topic.statuses;
        let section_id = new ObjectId(section_str_id);

        // Get visible topic and post count in section
        let visible_cnt = yield N.models.forum.Topic
                                  .aggregate({
                                    $match: {
                                      section: section_id,
                                      st: { $in: topic_statuses.LIST_VISIBLE }
                                    }
                                  })
                                  .group({
                                    _id: null,
                                    post_count: { $sum: '$cache.post_count' },
                                    topic_count: { $sum: 1 }
                                  })
                                  .exec();

        visible_cnt = visible_cnt[0] || { post_count: 0, topic_count: 0 };

        // Get hb topic and post count in section
        let hb_cnt = yield N.models.forum.Topic
                              .aggregate({
                                $match: {
                                  section: section_id,
                                  st: topic_statuses.HB
                                }
                              })
                              .group({
                                _id: null,
                                post_count: { $sum: '$cache.post_count' },
                                topic_count: { $sum: 1 }
                              })
                              .exec();

        hb_cnt = hb_cnt[0] || { post_count: 0, topic_count: 0 };

        // Get topic and post count in children sections
        let children_cnt = yield N.models.forum.Section
                                    .aggregate({ $match: { parent: section_id } })
                                    .group({
                                      _id: null,
                                      post_count: { $sum: '$cache.post_count' },
                                      post_count_hb: { $sum: '$cache_hb.post_count' },
                                      topic_count: { $sum: '$cache.topic_count' },
                                      topic_count_hb: { $sum: '$cache_hb.topic_count' }
                                    })
                                    .exec();

        children_cnt = children_cnt[0] || { post_count: 0, post_count_hb: 0, topic_count: 0, topic_count_hb: 0 };

        let update_data = {
          'cache.post_count': visible_cnt.post_count + children_cnt.post_count,
          'cache_hb.post_count': visible_cnt.post_count + hb_cnt.post_count + children_cnt.post_count_hb,
          'cache.topic_count': visible_cnt.topic_count + children_cnt.topic_count,
          'cache_hb.topic_count': visible_cnt.topic_count + hb_cnt.topic_count + children_cnt.topic_count_hb
        };

        let section = yield N.models.forum.Section.findOneAndUpdate({ _id: section_id }, update_data);

        if (section && section.parent) {
          // Postpone parent count update
          N.queue.forum_section_post_count_update(section.parent).postpone();
        }
      })
    });
  });
};
{'fraction_non_alphanumeric': 0.09456010745466756, 'fraction_numerical': 0.0061786433848220286, 'mean_word_length': 1.1482977495672244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7482275', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1874, 'n_words': 375}