index.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import React from "react";
  2. import { Button } from "react-bootstrap";
  3. import Ranking from "./Ranking";
  4. import Back from "../Back";
  5. import moment from "moment";
  6. import Test from "./Test";
  7. import axios from "axios";
  8. interface IProps {
  9. assets: string[];
  10. providers: any[];
  11. }
  12. interface IState {}
  13. class Storage extends React.Component<IProps, IState> {
  14. constructor(props: IProps) {
  15. super(props);
  16. this.state = {
  17. startedAt: false,
  18. selectedAssets: [],
  19. hash: "",
  20. number: 10,
  21. loading: {},
  22. assets: [],
  23. speeds: {},
  24. showTest: false,
  25. };
  26. this.startTest = this.startTest.bind(this);
  27. this.setAssetStatus = this.setAssetStatus.bind(this);
  28. this.toggleShowTest = this.toggleShowTest.bind(this);
  29. this.handleChange = this.handleChange.bind(this);
  30. }
  31. componentDidMount() {
  32. this.fetchSpeeds();
  33. //setInterval(this.forceUpdate, 5000);
  34. }
  35. async fetchSpeeds() {
  36. const { data } = await axios.get(`/static/speeds2.json`);
  37. if (data.error) return;
  38. console.log(`received results for`, Object.keys(data).join(`, `));
  39. this.setState({ speeds: data });
  40. }
  41. toggleShowTest() {
  42. this.setState({ showTest: !this.state.showTest });
  43. }
  44. async startTest(test: number) {
  45. const { hash, number } = this.state;
  46. const { assets, providers } = this.props;
  47. const loading = {};
  48. let selectedAssets = [];
  49. if (test === 1) {
  50. selectedAssets.push(assets[0]);
  51. selectedAssets.push(hash);
  52. return this.setState({ loading, startedAt: moment(), selectedAssets });
  53. }
  54. for (let i = 0; i < number; i++) {
  55. const id = Math.round(Math.random() * assets.length);
  56. const asset = assets.slice(id, id + 1)[0];
  57. if (selectedAssets.find((a) => a === asset)) i--;
  58. else selectedAssets.push(asset);
  59. }
  60. loading[`${providers[0].url}-${selectedAssets[0]}`] = "loading";
  61. await this.setState({ loading, startedAt: moment(), selectedAssets });
  62. this.loadWaitingAsset(selectedAssets, providers);
  63. }
  64. setAssetStatus(id: string, provider: string, status: string) {
  65. console.debug(id, provider, status);
  66. const tag = `${provider}-${id}`;
  67. const { loading } = this.state;
  68. if (status === `loading`)
  69. loading[tag] = { provider, id, status, startedAt: moment() };
  70. else {
  71. loading[tag].finishedAt = moment();
  72. loading[tag].status = status;
  73. this.loadWaitingAsset();
  74. }
  75. this.setState({ loading });
  76. }
  77. loadWaitingAsset(
  78. selectedAssets = this.state.selectedAssets,
  79. providers = this.props.providers
  80. ) {
  81. const { loading } = this.state;
  82. let provider: string;
  83. const asset = selectedAssets.find((a) => {
  84. const waiting = providers.find((p) =>
  85. loading[`${p.url}-${a}`] ? false : true
  86. );
  87. if (!waiting) return false;
  88. provider = waiting.url;
  89. return true;
  90. });
  91. if (!asset) return this.testFinished();
  92. this.setAssetStatus(asset, provider, "loading");
  93. }
  94. testFinished() {
  95. // TODO
  96. }
  97. handleChange(e) {
  98. this.setState({ [e.target.name]: e.target.value });
  99. }
  100. render() {
  101. const {
  102. speeds,
  103. selectedAssets,
  104. hash,
  105. number,
  106. loading,
  107. showTest,
  108. } = this.state;
  109. const { providers, assets } = this.props;
  110. return (
  111. <div className="m-2 p-2 bg-light">
  112. <h2>Storage Providers Ranking</h2>
  113. {showTest ? (
  114. <Test
  115. handleChange={this.handleChange}
  116. startTest={this.startTest}
  117. setAssetStatus={this.setAssetStatus}
  118. assets={assets}
  119. hash={hash}
  120. loading={loading}
  121. number={number}
  122. providers={providers}
  123. selectedAssets={selectedAssets}
  124. />
  125. ) : (
  126. <Button
  127. variant="outline-dark float-right"
  128. onClick={this.toggleShowTest}
  129. >
  130. Perform your own test
  131. </Button>
  132. )}
  133. <div className="d-flex flex-wrap ">
  134. {Object.keys(speeds).map((location) => (
  135. <Ranking
  136. key={location}
  137. location={location}
  138. providers={speeds[location]}
  139. />
  140. ))}
  141. </div>
  142. <Back />
  143. </div>
  144. );
  145. }
  146. }
  147. export default Storage;