The official leaked source code of the Dyno Discord Bot.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.4 KiB

4 years ago
  1. const progress = require('cli-progress');
  2. const db = require('../src/core/database.js');
  3. const config = require('../src/core/config');
  4. const redis = require('../src/core/redis');
  5. const clientId = config.client.id;
  6. const shardCount = 1152;
  7. const multibar = new progress.MultiBar({
  8. format: '{name} |{bar}| {percentage}% | {duration_formatted} | {value}/{total}',
  9. barCompleteChar: '\u2588',
  10. barIncompleteChar: '\u2591',
  11. clearOnComplete: false,
  12. stopOnComplete: true,
  13. hideCursor: true,
  14. });
  15. let mongoBar;
  16. let redisBar;
  17. const buffer = [];
  18. async function flushToRedis() {
  19. const batchSize = 1000;
  20. const batchCount = Math.ceil(buffer.length / batchSize);
  21. let barProgress = 0;
  22. redisBar = multibar.create(batchCount, barProgress, { name: 'Buffer -> Redis' });
  23. let itemsInPipeline = 0;
  24. let pipeline = redis.pipeline();
  25. for (let e of buffer) {
  26. const shardId = ~~((e._id / 4194304) % shardCount);
  27. pipeline.hset(`guild_activity:${clientId}:${shardCount}:${shardId}`, e._id, e.lastActive);
  28. itemsInPipeline++;
  29. if (itemsInPipeline >= batchSize) {
  30. await pipeline.exec();
  31. pipeline = redis.pipeline();
  32. itemsInPipeline = 0;
  33. barProgress++;
  34. redisBar.update(barProgress);
  35. }
  36. }
  37. if (itemsInPipeline > 0) {
  38. await pipeline.exec();
  39. }
  40. barProgress++;
  41. redisBar.update(barProgress);
  42. redisBar.stop();
  43. }
  44. async function fetchMongoDocs() {
  45. const coll = await db.collection('servers');
  46. const count = await coll.count({ deleted: false });
  47. let i = 0;
  48. mongoBar = multibar.create(count, i, { name: 'Mongo -> Buffer' });
  49. coll.find({ deleted: false }, { projection: { lastActive: 1 } }).forEach((doc) => {
  50. i++;
  51. if (!doc.lastActive) {
  52. return mongoBar.update(i);
  53. }
  54. buffer.push(doc);
  55. mongoBar.update(i);
  56. },
  57. (err) => {
  58. if (err) {
  59. console.error(err);
  60. process.exit(1);
  61. }
  62. mongoBar.stop();
  63. flushToRedis();
  64. });
  65. }
  66. setTimeout(fetchMongoDocs, 5000);
  67. // models.Server.countDocuments({ deleted: false }).then(count => {
  68. // let i = 0, p = 0;
  69. // mongoBar = multibar.create(count, i, { name: 'Mongo -> Buffer' });
  70. // models.Server.find({ deleted: false }, { lastActive: 1 })
  71. // .cursor()
  72. // .on('data', doc => {
  73. // i++;
  74. // if (!doc.lastActive) {
  75. // return mongoBar.update(i);
  76. // }
  77. // buffer.push(doc);
  78. // mongoBar.update(i);
  79. // })
  80. // .on('end', () => {
  81. // mongoBar.stop();
  82. // flushToRedis();
  83. // });
  84. // });