Explorar el Código

Merge pull request #529 from traumschule/storage

34.II-3: Assets by Storage Providers
mochet hace 3 años
padre
commit
4913e0dfe3

+ 4 - 5
contributions/tech/report-generator/README.md

@@ -1,8 +1,7 @@
-# Council + Tokenomics Report Generator
+# Council Minutes generator
 
-This scripts collects some information from Joystream chain. \
-It was created to allow the council to generate a report in the finish of the council round. \
-It takes some minutes to complete the report, multiple runs, with the same block range, will be quicker since it has a "cache" system for the block events.  
+- Counil & Tokenomics Report: `yarn run report  [round|block range]` collects information from the [Joystream chain](https://testnet.joystream.org/). It was created to allow the council to generate a report after the end of a council round. It takes some minutes to cache all events. The next run with the same block range, will be quicker.
+- Storage: `yarn run storage` generates a table with stats how many liaisons each storage worker has (similar to https://joystreamstats.live/storage)
 
 ## Setup
 
@@ -30,5 +29,5 @@ OR
 # Contributors
 
 * [freakstatic](https://github.com/freakstatic)
-* [l1dev](https://git.joystreamstats.live/l1devx)
+* [l1dev](https://git.joystreamstats.live/l1dev)
 * [isonar](https://github.com/singulart)

+ 1 - 0
contributions/tech/report-generator/package.json

@@ -6,6 +6,7 @@
   "scripts": {
     "report": "ts-node src/generator.ts",
     "build": "tsc --build tsconfig.json",
+    "storage": "node src/storage",
     "status": "node lib/status"
   },
   "dependencies": {

+ 75 - 0
contributions/tech/report-generator/src/storage.js

@@ -0,0 +1,75 @@
+const hydraLocation = "https://hydra.joystream.org/graphql";
+const axios = require("axios");
+const fs = require("fs");
+
+const getAssets = async () => {
+  const query = {
+    query: `\nquery {
+  videos (limit:1000000, orderBy:createdAt_DESC){
+    id
+    title
+    updatedAt
+    createdAt
+    createdInBlock
+    mediaDataObject {
+      joystreamContentId
+      liaisonJudgement
+      ipfsContentId
+      liaison {
+        workerId
+        metadata
+        isActive
+      }
+    }
+  }
+}\n`,
+  };
+  console.debug(`Fetching data IDs from ${hydraLocation}`);
+  return axios.post(hydraLocation, query).then(({ data }) => data.data.videos);
+};
+
+const getVideosPerProvider = (assets) => {
+  const providers = {};
+  assets.forEach((a) => {
+    if (!a.mediaDataObject?.liaison) return;
+    const liaison = a.mediaDataObject.liaison;
+    const worker = liaison.workerId;
+
+    if (!providers[worker])
+      providers[worker] = { first: a.createdAt, ...liaison, videos: [] };
+
+    providers[worker].videos.push(a);
+
+    if (providers[worker].first > a.createdAt)
+      providers[worker].first = a.createdAt;
+  });
+
+  const lastProviderAdded = Object.values(providers).reduce(
+    (max, provider) => (max > provider.first ? max : provider.first),
+    Object.values(providers)[0].first
+  );
+
+  return Object.values(providers)
+    .map((p) => {
+      // find liaisons after last proivder was added
+      const recent = p.videos.filter((v) => v.createdAt > lastProviderAdded);
+      return { ...p, recent };
+    })
+    .sort((a, b) => b.recent.length - a.recent.length);
+};
+
+const writeTable = (providers) => {
+  let table = `| WorkerId | Metadata | Videos | First Liaison | Recent* | Pending |\n|---|---|---|---|---|---|\n`;
+  providers.forEach((p) => {
+    const date = p.first.split("T")[0];
+    table += `| ${p.workerId} | ${p.metadata} | ${p.videos.length} | ${date} | ${p.recent.length} |\n`;
+  });
+  return table + `\n* Since last provider was added.`;
+};
+
+getAssets().then((assets) => {
+  const liaisons = getVideosPerProvider(assets);
+  fs.writeFileSync("liaisons.md", writeTable(liaisons));
+  fs.writeFileSync("liaisons.json", JSON.stringify(liaisons));
+  console.log("Wrote liaisons.md and liaisons.json");
+});