util.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const fs = require("fs");
  2. const axios = require("axios");
  3. const moment = require("moment");
  4. const query = `query { storageBags(limit:10000){ id objects{id} storageBuckets {id operatorMetadata{nodeEndpoint}} distributionBuckets{id operators{metadata{nodeEndpoint}}} }}`;
  5. const gb = (bytes) => (bytes / 1024 ** 3).toFixed(1);
  6. const ch = (id) => id.split(":")[2];
  7. const time = () => `[` + moment().format(`HH:mm:ss`) + `]`;
  8. const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
  9. const printBagBuckets = ({ id, storageBuckets, distributionBuckets }) => {
  10. const sb = storageBuckets.map((b) => b.id).join(", ");
  11. const db = distributionBuckets.map((b) => b.id).join(", ");
  12. console.log(time(), `${ch(id)} storage: [ ${sb} ] distribution: [ ${db} ]`);
  13. };
  14. const logChannel = ({ id, size, title, description }) =>
  15. console.log(`${id} ${gb(size)}gb [${title}] ${description}`);
  16. const sendResult = (result, url) =>
  17. axios
  18. .post(url, result)
  19. .then(({ data }) => data.error && console.error(time(), data.error))
  20. .catch((e) => console.error(time(), e.message));
  21. const loadResults = (file) => {
  22. try {
  23. fs.stat(file);
  24. require(file);
  25. } catch {
  26. return [];
  27. }
  28. };
  29. const headAsset = (objectId, endpoint, url) => {
  30. const start = new Date();
  31. return axios
  32. .head(url)
  33. .then(({ data }) => {
  34. const timestamp = new Date();
  35. const latency = moment(timestamp).diff(start);
  36. const status = data || `success`;
  37. return { objectId, endpoint, url, timestamp, latency, status };
  38. })
  39. .catch((e) => {
  40. const timestamp = new Date();
  41. const latency = moment(timestamp).diff(start);
  42. const status = e.message + e.response?.data;
  43. return { objectId, endpoint, url, timestamp, latency, status };
  44. });
  45. };
  46. // channel sizes
  47. const printFailed = (list) => list.map((r) => r.url).join(` `);
  48. const addRow = ({ id, size, title, description }) =>
  49. `| ${id} | ${gb(size)} | ${title} | ${description
  50. ?.split("\n")
  51. .join(" ")
  52. .slice(0, 60)} |`;
  53. const writeTable = (channels) => {
  54. const rows = channels
  55. .map((channel) => {
  56. let size =
  57. channel.videos?.reduce(
  58. (sum, video) => (sum += +video.media?.size),
  59. 0
  60. ) || 0;
  61. return { ...channel, size };
  62. })
  63. .filter(({ size }) => size > 0.1 * 1024 ** 3)
  64. .sort((a, b) => b.size - a.size)
  65. .map((c) => addRow(c));
  66. const table =
  67. `| # | GB | Title | Description |\n|---|---|---|---|\n` + rows.join(`\n`);
  68. fs.writeFileSync("channels.md", table);
  69. console.log(`wrote channels.md`);
  70. };
  71. module.exports = {
  72. query,
  73. ch,
  74. sleep,
  75. time,
  76. loadResults,
  77. headAsset,
  78. sendResult,
  79. printFailed,
  80. writeTable,
  81. };