This commit is contained in:
Panayiotis Lipiridis 2021-02-07 14:33:26 +02:00
parent bc9c8f7ee2
commit 1ca985aa4a
7 changed files with 35 additions and 19 deletions

View File

@ -73,7 +73,7 @@ export const getDefaultAppState = (): Omit<
zenModeEnabled: false, zenModeEnabled: false,
zoom: { value: 1 as NormalizedZoomValue, translation: { x: 0, y: 0 } }, zoom: { value: 1 as NormalizedZoomValue, translation: { x: 0, y: 0 } },
viewModeEnabled: false, viewModeEnabled: false,
networkSpeed: "…", networkSpeed: 0,
}; };
}; };

View File

@ -1032,7 +1032,7 @@ class App extends React.Component<ExcalidrawProps, AppState> {
return; return;
} }
const speed = await getNetworkSpeed(); const speed = await getNetworkSpeed();
const networkSpeed = speed === "-1" ? "Error!" : speed; const networkSpeed = speed;
this.setState({ networkSpeed }); this.setState({ networkSpeed });
if (this.netStatsIntervalId) { if (this.netStatsIntervalId) {
clearTimeout(this.netStatsIntervalId); clearTimeout(this.netStatsIntervalId);

View File

@ -11,7 +11,7 @@ import { t } from "../i18n";
import useIsMobile from "../is-mobile"; import useIsMobile from "../is-mobile";
import { getTargetElements } from "../scene"; import { getTargetElements } from "../scene";
import { AppState } from "../types"; import { AppState } from "../types";
import { debounce, getVersion, nFormatter } from "../utils"; import { debounce, formatSpeed, getVersion, nFormatter } from "../utils";
import { close } from "./icons"; import { close } from "./icons";
import { Island } from "./Island"; import { Island } from "./Island";
import "./Stats.scss"; import "./Stats.scss";
@ -182,7 +182,13 @@ export const Stats = (props: {
</tr> </tr>
<tr> <tr>
<td>{t("stats.speed")}</td> <td>{t("stats.speed")}</td>
<td>{props.appState.networkSpeed}</td> <td>
{props.appState.networkSpeed === 0
? "…"
: props.appState.networkSpeed > 0
? formatSpeed(props.appState.networkSpeed)
: t("stats.error")}
</td>
</tr> </tr>
</> </>
) : null} ) : null}

View File

@ -231,6 +231,7 @@
"collaborators": "Collaborators", "collaborators": "Collaborators",
"element": "Element", "element": "Element",
"elements": "Elements", "elements": "Elements",
"error": "Error",
"height": "Height", "height": "Height",
"scene": "Scene", "scene": "Scene",
"selected": "Selected", "selected": "Selected",

View File

@ -1,38 +1,36 @@
const IMAGE_URL = `${process.env.REACT_APP_SOCKET_SERVER_URL}/test256.png`; const IMAGE_URL = `${process.env.REACT_APP_SOCKET_SERVER_URL}/test256.png`;
const IMAGE_SIZE_BYTES = 141978; const IMAGE_SIZE_BYTES = 141978;
const calculateSpeed = (startTime: number, endTime: number) => { const getSpeed = (
imageSize: number,
startTime: number,
endTime: number,
): number => {
const duration = (endTime - startTime) / 1000; const duration = (endTime - startTime) / 1000;
const imageSizeInBits = IMAGE_SIZE_BYTES * 8; if (duration > 0) {
let speed = imageSizeInBits / duration; return imageSize / duration;
// source: en.wikipedia.org/wiki/Data-rate_units#Conversion_table
const suffix = ["B/s", "kB/s", "MB/s", "GB/s"];
let index = 0;
while (speed > 1000) {
index++;
speed = speed / 1000;
} }
return `${speed.toFixed(index > 1 ? 1 : 0)} ${suffix[index]}`; return 0;
}; };
const processImage = (): Promise<string> => { const processImage = (): Promise<number> => {
return new Promise((resolve) => { return new Promise((resolve) => {
const image = new Image(); const image = new Image();
let endTime: number; let endTime: number;
image.onload = () => { image.onload = () => {
endTime = new Date().getTime(); endTime = new Date().getTime();
const speed = calculateSpeed(startTime, endTime); const speed = getSpeed(IMAGE_SIZE_BYTES, startTime, endTime);
resolve(speed); resolve(speed);
}; };
image.onerror = () => { image.onerror = () => {
resolve("-1"); resolve(-1);
}; };
const startTime = new Date().getTime(); const startTime = new Date().getTime();
image.src = `${IMAGE_URL}?t=${startTime}`; image.src = `${IMAGE_URL}?t=${startTime}`;
}); });
}; };
export const getNetworkSpeed = async (): Promise<string> => { export const getNetworkSpeed = async (): Promise<number> => {
return await processImage(); return await processImage();
}; };

View File

@ -88,7 +88,7 @@ export type AppState = {
appearance: "light" | "dark"; appearance: "light" | "dark";
gridSize: number | null; gridSize: number | null;
viewModeEnabled: boolean; viewModeEnabled: boolean;
networkSpeed: string; networkSpeed: number;
/** top-most selected groups (i.e. does not include nested groups) */ /** top-most selected groups (i.e. does not include nested groups) */
selectedGroupIds: { [groupId: string]: boolean }; selectedGroupIds: { [groupId: string]: boolean };

View File

@ -363,6 +363,17 @@ export const nFormatter = (num: number, digits: number): string => {
); );
}; };
export const formatSpeed = (speed: number): string => {
// source: en.wikipedia.org/wiki/Data-rate_units#Conversion_table
const suffix = ["B/s", "kB/s", "MB/s", "GB/s"];
let index = 0;
while (speed > 1000) {
index++;
speed = speed / 1000;
}
return `${speed.toFixed(index > 1 ? 1 : 0)} ${suffix[index]}`;
};
export const getVersion = () => { export const getVersion = () => {
return ( return (
document.querySelector<HTMLMetaElement>('meta[name="version"]')?.content || document.querySelector<HTMLMetaElement>('meta[name="version"]')?.content ||