Pārlūkot izejas kodu

review comments and some cleaning

nexusfallout 3 gadi atpakaļ
vecāks
revīzija
a4a7c16a16
3 mainītis faili ar 33 papildinājumiem un 44 dzēšanām
  1. 7 6
      storagesize-bot/db.json
  2. 21 35
      storagesize-bot/index.js
  3. 5 3
      storagesize-bot/readme.md

+ 7 - 6
storagesize-bot/db.json

@@ -1,8 +1,9 @@
 {
-  "config": [
-    {
-      "timeStamp": "2021-07-18T14:26:41Z",
-      "size": "479GB"
-    }
-  ]
+  "config": {
+    "timeStamp": "2021-07-21T18:42:22Z",
+    "size": "592GB"
+  },
+  "dataURL": "https://analytics.dapplooker.com/api/public/dashboard/c70b56bd-09a0-4472-a557-796afdc64d3b/card/155",
+  "storageProviderChannelName": "storage-provider",
+  "timeLimit": 5
 }

+ 21 - 35
storagesize-bot/index.js

@@ -18,9 +18,11 @@ const client = new Client();
 await db.read();
 
 // If file.json doesn't exist, db.data will be null
-// Set default data
-// db.data |= { config: [] };
-// db.write();
+if (db.data === null) {
+  // Set default data
+  db.data = { config: {} };
+  db.write();
+}
 
 //INIT
 
@@ -30,67 +32,51 @@ client.on("ready", () => {
 });
 
 const _init = async () => {
-  const channel = client.channels.cache.find(
-    (channel) => channel.name === "storage-provider"
+  const storageProviderChannel = client.channels.cache.find(
+    (channel) => channel.name === db?.data?.storageProviderChannelName
   );
-  const job = schedule.scheduleJob("0 */2 * * *", async function (fireDate) {
+  schedule.scheduleJob(`0 */${db?.data?.timeLimit} * * *`, async function () {
     const response = await generateSize();
-    channel.send(`Current storage size: ${response}`);
+    storageProviderChannel.send(`Current storage size: ${response}`);
   });
 };
 
 //UTILS
 
 const generateSize = async () => {
-  let res;
-  const isOld = isCacheOld();
-  if (isOld) {
-    res = await getSizeFromJoystream();
+  if (isCacheOld()) {
+    return await getSizeFromAPI();
   }
-  let size = isOld ? res : db.data.config[0].size;
-  return Promise.resolve(size);
+  return Promise.resolve(db?.data?.config?.size);
 };
 
 const generateMsg = async (msg = "", user = "") => {
   let size = await generateSize();
-  msg && msg.edit(`<@${user.id}>, Current storage size: ${size}`);
+  msg.edit(`<@${user.id}>, Current storage size: ${size}`);
 };
 
-const bytesToSize = (bytes) => {
-  const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
-  if (bytes === 0) return "n/a";
-  const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
-  if (i === 0) return `${bytes} ${sizes[i]}`;
-  return `${(bytes / 1024 ** i).toFixed(1)} ${sizes[i]}`;
-};
-
-const getSizeFromJoystream = async () => {
+const getSizeFromAPI = async () => {
   return axios
-    .get(
-      "https://analytics.dapplooker.com/api/public/dashboard/c70b56bd-09a0-4472-a557-796afdc64d3b/card/155?parameters=%5B%5D"
-    )
+    .get(db.data?.dataURL)
     .then((response) => {
-      // handle success
-      // const res = bytesToSize(response.data.media.size);
-      const res = Math.round(response.data.data.rows[0][0] / 1000) + "GB";
-      db.data.config[0] = { timeStamp: moment.utc().format(), size: `${res}` };
+      const res = Math.round(response?.data?.data?.rows[0][0]) + "GB";
+      db.data.config = { timeStamp: moment.utc().format(), size: `${res}` };
       db.write();
       return res;
     })
     .catch((error) => {
-      // handle error
-      console.log(error);
+      console.log("API failed", error);
     });
 };
 
 const isCacheOld = () => {
-  let lastTime = db.data.config[0].timeStamp;
-  console.log(JSON.stringify(lastTime));
+  //returns true if the timestamp is older than 2 hours
+  let lastTime = db.data?.config?.timeStamp;
   if (lastTime) {
     const today = moment();
     const diff = today.diff(lastTime, "minutes");
     console.log(diff);
-    return diff > 120 ? true : false;
+    return diff > 120;
   }
   return true;
 };

+ 5 - 3
storagesize-bot/readme.md

@@ -1,12 +1,14 @@
 # Storage size notification bot for discord
 
-## Running the bot locally
+Sends actual volume for Joystream Storage Providers to a channel every X hours or via PM with `/storagesize`.
+
+## Development
 
 - Take a copy of .env.example and add token of the bot and rename to .env
 - `npm install`
 - `nodemon`
 
-## Deploying it
+## Deployment
 
 - Take a copy of .env.example and add token of the bot and rename to .env
 - `npm install`
@@ -14,4 +16,4 @@
   - `npm install pm2@latest -g`
     or
     `yarn global add pm2`
-  - pm2 start ./index.js
+  - `pm2 start --name storagesize node index.js`