channels.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const fs = require("fs");
  2. const axios = require("axios");
  3. const moment = require("moment");
  4. const {
  5. ch,
  6. time,
  7. sleep,
  8. loadResults,
  9. headAsset,
  10. sendResult,
  11. printFailed,
  12. writeTable,
  13. query,
  14. } = require("./util");
  15. // config
  16. const QN = `https://ipfs.joystreamstats.live/graphql`;
  17. const resultsUrl = `https://joystreamstats.live/api/v1/bags/status`;
  18. const resultsFile = `./bag_availability.json`;
  19. const getUrl = (metadata) => metadata?.nodeEndpoint;
  20. const testProviders = async (channelId, object, sUrls, dUrls) => {
  21. const storage = await Promise.all(
  22. sUrls
  23. .filter((url) => url)
  24. .map((url) =>
  25. headAsset(object.id, url, url + `api/v1/files/${object.id}`)
  26. )
  27. );
  28. const distribution = await Promise.all(
  29. dUrls.map((url) =>
  30. headAsset(object.id, url, url + `api/v1/assets/${object.id}`)
  31. )
  32. );
  33. return [storage, distribution];
  34. };
  35. const testBags = async (bags, objects = [], rapid = false) => {
  36. console.debug(
  37. time(),
  38. `Starting rapid test`,
  39. objects.length && `for ${objects}`
  40. );
  41. const start = new Date();
  42. let results = [];
  43. for (const bag of bags.sort((a, b) => ch(b.id) - ch(a.id))) {
  44. if (!bag.objects) continue;
  45. const channelId = ch(bag.id);
  46. const sUrls = bag.storageBuckets.map((sb) => getUrl(sb.operatorMetadata));
  47. const dUrls = bag.distributionBuckets.map((db) =>
  48. getUrl(db.operators[0]?.metadata)
  49. );
  50. for (const object of bag.objects) {
  51. //if (results.length) continue; // TODO
  52. if (objects.length && !objects.includes(object.id)) continue;
  53. const [storage, distribution] = await testProviders(
  54. ch(bag.id),
  55. object,
  56. sUrls,
  57. dUrls
  58. );
  59. const sFailed = storage.filter((b) => b.status !== `success`);
  60. const dFailed = distribution.filter((b) => b.status !== `success`);
  61. const SP =
  62. `${storage.length - sFailed.length}/${storage.length} ` +
  63. (sFailed.length ? `( ` + printFailed(sFailed) + ` )` : "OK");
  64. const DP =
  65. `${distribution.length - dFailed.length}/${distribution.length} ` +
  66. (dFailed.length ? `( ` + printFailed(dFailed) + ` )` : "OK");
  67. console.log(`${time()} ${channelId} ${object.id} SP:${SP} DP:${DP}`);
  68. const result = { channelId, storage, distribution };
  69. sendResult(result, resultsUrl);
  70. results.push(result);
  71. }
  72. if (!rapid) await sleep(Math.randon * 10.0);
  73. }
  74. const duration = (moment().diff(start) / 1000).toFixed(3) + `s`;
  75. console.log(time(), `Finished rapid test in `, duration);
  76. if (!objects.length) {
  77. fs.writeFileSync(resultsFile, JSON.stringify(results));
  78. console.log(time(), `Wrote results to ${resultsFile} `);
  79. }
  80. };
  81. // start
  82. const bagIds = process.argv.slice(2);
  83. if (bagIds.length) console.log(`selected bags`, bagIds);
  84. const old = loadResults(resultsFile);
  85. console.debug(time(), `Fetching bags with buckets and objects\n`, query);
  86. axios
  87. .post(QN, { query })
  88. .then(({ data }) => {
  89. const bags = data.data.storageBags;
  90. if (!bags.length)
  91. return console.error(time(), `No bags received.`, data.error);
  92. console.log(time(), `Received list with ${bags.length} bags.`);
  93. //bags.forEach((b) => printBagBuckets(b));
  94. let selected = bags;
  95. if (bagIds.length) {
  96. console.log(time(), `Selecting bags with id`, bagIds);
  97. selected = bags.filter(
  98. (b) => !bagIds.length || bagIds.find((id) => id === ch(b.id))
  99. );
  100. }
  101. if (old.length) {
  102. console.log(time(), `Testing formerly failed assets`);
  103. const ids = old.reduce(
  104. (ids, bag) =>
  105. ids.concat(
  106. ...bag.storage.map((r) => r.objectId),
  107. ...bag.distribution.map((r) => r.objectId)
  108. ),
  109. []
  110. );
  111. console.log(ids);
  112. testBags(selected, ids);
  113. } else testBags(selected);
  114. })
  115. .catch((e) => console.error(e.message, e.response?.data));