<commit_before>const MongoClient = require('mongodb').MongoClient;
const roomMap = {
  'C0D2Z96AG': 'hubot-testing',
  'C03AFTJHG': 'general',
  'C4YTEUTJ6': 'generaltsos',
  'CCE6AE5GE': 'investing',
  'CEFH3HW1M': 'whatsplaying'
};

module.exports = function (robot) {

  MongoClient.connect(process.env.MONGODB_URL, {
      useNewUrlParser: true
    },
    function (err, client) {
      if (err) {
        robot.logger.error('MongoStats was unable to connect to slack_messages connection!');
        robot.logger.error(err);
      }

      robot.logger.debug('MongoStats - Successfully authenticated with mongo');

      let db = client.db(process.env.MONGODB_DBNAME);
      let collection = db.collection('slack_messages');

      robot.respond(/give us the stats/i, function (msg) {
        var content = {};

        let endTime = new Date();
        endTime.setUTCHours(0, 0, 0);
        let startTime = new Date(endTime - (7 * 24 * 60 * 60 * 1000));
        content.start = startTime.toDateString();
        content.end = endTime.toDateString();
        let timestampQuery = {
          $gte: startTime.getTime(),
          $lte: endTime.getTime()
        };

        getMessageCount(collection, timestampQuery, function (error, result) {
          content.messageCount = result || -1;
          respondWithContent(msg, content);
        });
        
        getRoomsAggregate(collection, timestampQuery, function (error, result) {
          content.roomCounts = result || [];
          respondWithContent(msg, content);
        });
        
        getUsersAggregate(collection, timestampQuery, function (error, result) {
          content.userCounts = result || [];
          respondWithContent(msg, content);
        });

      });
    });
};

function getUsersAggregate(collection, timestampQuery, callback) {
  let userPipeline = [{ $match: { timestamp: timestampQuery } },
    { $group: { _id: "$user", count: { $sum: 1 } } }];
  collection.aggregate(userPipeline).toArray(callback);
}

function getRoomsAggregate(collection, timestampQuery, callback) {
  let roomPipeline = [{ $match: { timestamp: timestampQuery } },
    { $group: { _id: "$room", count: { $sum: 1 } } }];
  collection.aggregate(roomPipeline).toArray(callback);
}

function getMessageCount(collection, timestampQuery, callback) {
  let query = {
    timestamp: timestampQuery
  };
  collection.countDocuments(query, {}, callback);
}

function respondWithContent(robot, content) {
  if (robot && content && content.start &&
    content.end && content.messageCount &&
    content.roomCounts && content.userCounts) {
    var response = [];
    response.push('*Last Week Messages Statistics*');
    response.push(`*Timeframe:* _${content.start} - ${content.end}_`);
    response.push(`*Total Message Count:* _${content.messageCount}_`);
    response.push(`*Stats by User:*`);
    content.userCounts.sort(compareAggregateResults).forEach(function(user, index) {
      response.push(`_${index+1}. ${user._id}: ${user.count} ${optionalMedal(index)}_`);
    });
    response.push(`*Stats by Room:*`);
    content.roomCounts.sort(compareAggregateResults).forEach(function(room, index) {
      response.push(`_${index+1}. ${room._id}: ${room.count} ${optionalMedal(index)}_`);
    });
    robot.send(response.join('\n'));
  }
}

function compareAggregateResults(a, b) {
  return b.count - a.count;
}

function optionalMedal(index) {
  if (index === 0) {
    return ':gold:';
  } else if (index === 1) {
    return ':silver:';
  } else if (index === 2) {
    return ':bronze:';
  } else {
    return '';
  }
}<commit_msg>add secondary command for mongo stats.<commit_after>const MongoClient = require('mongodb').MongoClient;
const roomMap = {
  'C0D2Z96AG': 'hubot-testing',
  'C03AFTJHG': 'general',
  'C4YTEUTJ6': 'generaltsos',
  'CCE6AE5GE': 'investing',
  'CEFH3HW1M': 'whatsplaying'
};

module.exports = function (robot) {

  MongoClient.connect(process.env.MONGODB_URL, {
      useNewUrlParser: true
    },
    function (err, client) {
      if (err) {
        robot.logger.error('MongoStats was unable to connect to slack_messages connection!');
        robot.logger.error(err);
      }

      robot.logger.debug('MongoStats - Successfully authenticated with mongo');

      let db = client.db(process.env.MONGODB_DBNAME);
      let collection = db.collection('slack_messages');

      robot.respond(/(give us the stats|what are the stats)/i, function (msg) {
        var content = {};

        let endTime = new Date();
        endTime.setUTCHours(0, 0, 0);
        let startTime = new Date(endTime - (7 * 24 * 60 * 60 * 1000));
        content.start = startTime.toDateString();
        content.end = endTime.toDateString();
        let timestampQuery = {
          $gte: startTime.getTime(),
          $lte: endTime.getTime()
        };

        getMessageCount(collection, timestampQuery, function (error, result) {
          content.messageCount = result || -1;
          respondWithContent(msg, content);
        });
        
        getRoomsAggregate(collection, timestampQuery, function (error, result) {
          content.roomCounts = result || [];
          respondWithContent(msg, content);
        });
        
        getUsersAggregate(collection, timestampQuery, function (error, result) {
          content.userCounts = result || [];
          respondWithContent(msg, content);
        });

      });
    });
};

function getUsersAggregate(collection, timestampQuery, callback) {
  let userPipeline = [{ $match: { timestamp: timestampQuery } },
    { $group: { _id: "$user", count: { $sum: 1 } } }];
  collection.aggregate(userPipeline).toArray(callback);
}

function getRoomsAggregate(collection, timestampQuery, callback) {
  let roomPipeline = [{ $match: { timestamp: timestampQuery } },
    { $group: { _id: "$room", count: { $sum: 1 } } }];
  collection.aggregate(roomPipeline).toArray(callback);
}

function getMessageCount(collection, timestampQuery, callback) {
  let query = {
    timestamp: timestampQuery
  };
  collection.countDocuments(query, {}, callback);
}

function respondWithContent(robot, content) {
  if (robot && content && content.start &&
    content.end && content.messageCount &&
    content.roomCounts && content.userCounts) {
    var response = [];
    response.push('*Last Week Messages Statistics*');
    response.push(`*Timeframe:* _${content.start} - ${content.end}_`);
    response.push(`*Total Message Count:* _${content.messageCount}_`);
    response.push(`*Stats by User:*`);
    content.userCounts.sort(compareAggregateResults).forEach(function(user, index) {
      response.push(`_${index+1}. ${user._id}: ${user.count} ${optionalMedal(index)}_`);
    });
    response.push(`*Stats by Room:*`);
    content.roomCounts.sort(compareAggregateResults).forEach(function(room, index) {
      response.push(`_${index+1}. ${room._id}: ${room.count} ${optionalMedal(index)}_`);
    });
    robot.send(response.join('\n'));
  }
}

function compareAggregateResults(a, b) {
  return b.count - a.count;
}

function optionalMedal(index) {
  if (index === 0) {
    return ':gold:';
  } else if (index === 1) {
    return ':silver:';
  } else if (index === 2) {
    return ':bronze:';
  } else {
    return '';
  }
}
{'fraction_non_alphanumeric': 0.1551438053097345, 'fraction_numerical': 0.009402654867256638, 'mean_word_length': 3.3002378121284184, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3477648', 'n_tokens_mistral': 2285, 'n_tokens_neox': 2235, 'n_words': 448}