Browse Source

improve storage providers test

Joystream Stats 3 years ago
parent
commit
22ed8eb5d7

+ 35 - 21
src/components/Storage/Asset.tsx

@@ -1,25 +1,33 @@
-import React, { useEffect } from "react";
+import React from "react";
 import moment from "moment";
 import moment from "moment";
 
 
 const Asset = (props: {
 const Asset = (props: {
-  loadAsset: (string, any, string) => void;
+  setAssetStatus: (string, any, string) => void;
   provider: string;
   provider: string;
   id: string;
   id: string;
   status: string;
   status: string;
   startedAt: any;
   startedAt: any;
   finishedAt: any;
   finishedAt: any;
 }) => {
 }) => {
-  const { loadAsset, provider, id, status, startedAt, finishedAt } = props;
+  const { setAssetStatus, provider, id, status, startedAt, finishedAt } = props;
+
+  const url = `${provider}asset/v0/${id}`;
   const bg = {
   const bg = {
-    undefined: "warning",
+    undefined: "light",
     loading: "warning",
     loading: "warning",
     loaded: "success",
     loaded: "success",
     failed: "danger",
     failed: "danger",
   };
   };
-  //console.log(status, bg[status]);
-  useEffect(() => loadAsset(id, provider, "loading"), []);
 
 
-  const url = `${provider}asset/v0/${id}`;
+  if (!status === "loading")
+    return (
+      <div
+        className={`display-block bg-${bg[status]} p-1 mr-1`}
+        style={{ width: `50px` }}
+      >
+        waiting
+      </div>
+    );
 
 
   return (
   return (
     <div
     <div
@@ -27,22 +35,28 @@ const Asset = (props: {
       style={{ width: `50px` }}
       style={{ width: `50px` }}
     >
     >
       <a href={url} title={id}>
       <a href={url} title={id}>
-        {finishedAt
+        {!status
+          ? "waiting"
+          : finishedAt
           ? moment(finishedAt).diff(startedAt) / 1000
           ? moment(finishedAt).diff(startedAt) / 1000
           : moment().diff(startedAt) / 1000}
           : moment().diff(startedAt) / 1000}
-        s
-        <img
-          onLoad={() =>
-            status === "loaded" || loadAsset(id, provider, "loaded")
-          }
-          onError={() =>
-            status === "failed" || loadAsset(id, provider, "failed")
-          }
-          className="hidden"
-          alt={id}
-          style={{ display: "none" }}
-          src={url}
-        />
+
+        {status === "loading" ? (
+          <img
+            onLoad={() =>
+              status === "loaded" || setAssetStatus(id, provider, "loaded")
+            }
+            onError={() =>
+              status === "failed" || setAssetStatus(id, provider, "failed")
+            }
+            className="hidden"
+            alt={id}
+            style={{ display: "none" }}
+            src={url}
+          />
+        ) : (
+          <div />
+        )}
       </a>
       </a>
     </div>
     </div>
   );
   );

+ 3 - 4
src/components/Storage/Provider.tsx

@@ -2,13 +2,13 @@ import React from "react";
 import Asset from "./Asset";
 import Asset from "./Asset";
 
 
 const Provider = (props: {
 const Provider = (props: {
-  loadAsset: (id: string, provider: string, status: string) => void;
+  setAssetStatus: (id: string, provider: string, status: string) => void;
   loading: any[];
   loading: any[];
   startedAt: string | boolean;
   startedAt: string | boolean;
   test: string[];
   test: string[];
   url: string;
   url: string;
 }) => {
 }) => {
-  const { loadAsset, loading, test, url, startedAt } = props;
+  const { setAssetStatus, loading, test, url, startedAt } = props;
 
 
   return (
   return (
     <div key={url} className="m-2 d-flex flex-row">
     <div key={url} className="m-2 d-flex flex-row">
@@ -23,10 +23,9 @@ const Provider = (props: {
         test.map((a) => (
         test.map((a) => (
           <Asset
           <Asset
             key={`${url}-${a}`}
             key={`${url}-${a}`}
-            loadAsset={loadAsset}
+            setAssetStatus={setAssetStatus}
             id={a}
             id={a}
             provider={url}
             provider={url}
-            startedAt={startedAt}
             {...loading[`${url}-${a}`]}
             {...loading[`${url}-${a}`]}
           />
           />
         ))}
         ))}

+ 38 - 17
src/components/Storage/Test.tsx

@@ -3,19 +3,42 @@ import { Button } from "react-bootstrap";
 import Provider from "./Provider";
 import Provider from "./Provider";
 import Loading from "../Loading";
 import Loading from "../Loading";
 
 
-const Test = () => {
+const Test = (props: {
+  handleChange: () => void;
+  startTest: (number) => void;
+  setAssetStatus: (id: string, provider: string, status: string) => void;
+  assets: string[];
+  hash: string;
+  number: number;
+  providers;
+  selectedAssets: string[];
+  loading: {};
+}) => {
+  const {
+    handleChange,
+    startTest,
+    setAssetStatus,
+    assets,
+    loading,
+    providers,
+    hash,
+    number,
+    selectedAssets,
+  } = props;
+  if (!providers.length) return <Loading target="providers" />;
+  if (!assets.length) return <Loading target="assets" />;
   return (
   return (
-    <div>
+    <div className="m-2 p-2 bg-secondary">
       <div className="form-group">
       <div className="form-group">
         <input
         <input
           className="form-control"
           className="form-control"
           name="hash"
           name="hash"
           value={hash}
           value={hash}
-          onChange={this.handleChange}
+          onChange={handleChange}
         />
         />
         <Button
         <Button
           variant="success"
           variant="success"
-          onClick={() => this.startTest(1)}
+          onClick={() => startTest(1)}
           disabled={hash === ``}
           disabled={hash === ``}
         >
         >
           Test selected resource
           Test selected resource
@@ -26,23 +49,21 @@ const Test = () => {
           className="form-control"
           className="form-control"
           name="number"
           name="number"
           value={number}
           value={number}
-          onChange={this.handleChange}
+          onChange={handleChange}
         />
         />
-        <Button variant="warning" onClick={() => this.startTest(2)}>
+        <Button variant="warning" onClick={() => startTest(2)}>
           Test {number} out of {assets.length} assets
           Test {number} out of {assets.length} assets
         </Button>
         </Button>
       </div>
       </div>
-      {(providers.length &&
-        providers.map((p) => (
-          <Provider
-            key={p.url}
-            loadAsset={this.loadAsset}
-            test={selectedAssets}
-            loading={loading}
-            startedAt={startedAt}
-            {...p}
-          />
-        ))) || <Loading target="provider list" />}
+      {providers.map((p) => (
+        <Provider
+          key={p.url}
+          setAssetStatus={setAssetStatus}
+          test={selectedAssets}
+          loading={loading}
+          {...p}
+        />
+      ))}
     </div>
     </div>
   );
   );
 };
 };

+ 52 - 18
src/components/Storage/index.tsx

@@ -27,7 +27,7 @@ class Storage extends React.Component<IProps, IState> {
       showTest: false,
       showTest: false,
     };
     };
     this.startTest = this.startTest.bind(this);
     this.startTest = this.startTest.bind(this);
-    this.loadAsset = this.loadAsset.bind(this);
+    this.setAssetStatus = this.setAssetStatus.bind(this);
     this.toggleShowTest = this.toggleShowTest.bind(this);
     this.toggleShowTest = this.toggleShowTest.bind(this);
     this.handleChange = this.handleChange.bind(this);
     this.handleChange = this.handleChange.bind(this);
   }
   }
@@ -48,9 +48,9 @@ class Storage extends React.Component<IProps, IState> {
     this.setState({ showTest: !this.state.showTest });
     this.setState({ showTest: !this.state.showTest });
   }
   }
 
 
-  startTest(test: number) {
+  async startTest(test: number) {
     const { hash, number } = this.state;
     const { hash, number } = this.state;
-    const { assets } = this.props;
+    const { assets, providers } = this.props;
     const loading = {};
     const loading = {};
     let selectedAssets = [];
     let selectedAssets = [];
 
 
@@ -67,21 +67,46 @@ class Storage extends React.Component<IProps, IState> {
       if (selectedAssets.find((a) => a === asset)) i--;
       if (selectedAssets.find((a) => a === asset)) i--;
       else selectedAssets.push(asset);
       else selectedAssets.push(asset);
     }
     }
-    this.setState({ loading, startedAt: moment(), selectedAssets });
+    loading[`${providers[0].url}-${selectedAssets[0]}`] = "loading";
+    await this.setState({ loading, startedAt: moment(), selectedAssets });
+    this.loadWaitingAsset(selectedAssets, providers);
   }
   }
 
 
-  loadAsset(id: string, provider: string, status: string) {
-    let { startedAt } = this.state;
+  setAssetStatus(id: string, provider: string, status: string) {
+    console.debug(id, provider, status);
     const tag = `${provider}-${id}`;
     const tag = `${provider}-${id}`;
-    const value =
-      status === `loading`
-        ? { provider, id, status, startedAt }
-        : { provider, id, status, finishedAt: moment() };
-
     const { loading } = this.state;
     const { loading } = this.state;
-    loading[tag] = value;
+
+    if (status === `loading`)
+      loading[tag] = { provider, id, status, startedAt: moment() };
+    else {
+      loading[tag].finishedAt = moment();
+      loading[tag].status = status;
+      this.loadWaitingAsset();
+    }
     this.setState({ loading });
     this.setState({ loading });
   }
   }
+  loadWaitingAsset(
+    selectedAssets = this.state.selectedAssets,
+    providers = this.props.providers
+  ) {
+    const { loading } = this.state;
+    let provider: string;
+    const asset = selectedAssets.find((a) => {
+      const waiting = providers.find((p) =>
+        loading[`${p.url}-${a}`] ? false : true
+      );
+      if (!waiting) return false;
+      provider = waiting.url;
+      return true;
+    });
+
+    if (!asset) return this.testFinished();
+    this.setAssetStatus(asset, provider, "loading");
+  }
+  testFinished() {
+    // TODO
+  }
 
 
   handleChange(e) {
   handleChange(e) {
     this.setState({ [e.target.name]: e.target.value });
     this.setState({ [e.target.name]: e.target.value });
@@ -94,22 +119,31 @@ class Storage extends React.Component<IProps, IState> {
       hash,
       hash,
       number,
       number,
       loading,
       loading,
-      startedAt,
       showTest,
       showTest,
     } = this.state;
     } = this.state;
     const { providers, assets } = this.props;
     const { providers, assets } = this.props;
 
 
-    //if (!providers.length) return <Loading target="storage providers" />;
-    //if (!assets.length) return <Loading target="assets" />;
-
     return (
     return (
       <div className="m-2 p-2 bg-light">
       <div className="m-2 p-2 bg-light">
         <h2>Storage Providers Ranking</h2>
         <h2>Storage Providers Ranking</h2>
 
 
         {showTest ? (
         {showTest ? (
-          <Test />
+          <Test
+            handleChange={this.handleChange}
+            startTest={this.startTest}
+            setAssetStatus={this.setAssetStatus}
+            assets={assets}
+            hash={hash}
+            loading={loading}
+            number={number}
+            providers={providers}
+            selectedAssets={selectedAssets}
+          />
         ) : (
         ) : (
-          <Button variant="outline-dark float-right">
+          <Button
+            variant="outline-dark float-right"
+            onClick={this.toggleShowTest}
+          >
             Perform your own test
             Perform your own test
           </Button>
           </Button>
         )}
         )}