Skip to content

Commit

Permalink
feat: update stats panel (#1139)
Browse files Browse the repository at this point in the history
* feat: update ui for visualizer's stats.

Signed-off-by: Eugene Panteleymonchuk <[email protected]>

* feat: update stats panel.

Signed-off-by: Eugene Panteleymonchuk <[email protected]>

* feat: update stats panel. update styles, use hook.

Signed-off-by: Eugene Panteleymonchuk <[email protected]>

* feat: create networkStats hook, update components to prevent rerender.

Signed-off-by: Eugene Panteleymonchuk <[email protected]>

---------

Signed-off-by: Eugene Panteleymonchuk <[email protected]>
Co-authored-by: Begoña Álvarez de la Cruz <[email protected]>
  • Loading branch information
panteleymonchuk and begonaalvarezd authored Feb 20, 2024
1 parent 59655f3 commit fc127e1
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 102 deletions.
68 changes: 0 additions & 68 deletions client/src/features/visualizer-threejs/Visualizer.scss
Original file line number Diff line number Diff line change
Expand Up @@ -83,74 +83,6 @@
}
}

.stats-panel-container {
display: flex;
position: absolute;
z-index: 1;
top: 90px !important;
left: 20px !important;
bottom: auto !important;
justify-content: left !important;
align-items: center;
pointer-events: none;

.stats-panel {
background: var(--body-background);

.card--value,
.card--label {
text-align: left;
}
.card--label {
justify-content: flex-start;
}
.card--content {
padding: 0;
}
.stats-panel__info {
padding: 0 10px;
display: inline-block;
}
}

@include tablet-down {
top: 60px;
left: 20px;
bottom: auto;
justify-content: left;

.stats-panel {
.card--value,
.card--label {
text-align: left;
}
.card--label {
justify-content: flex-start;
}
.card--content {
padding: 0;
}
.stats-panel__info {
padding: 0 10px;
display: inline-block;
}
}
}

@include phone-down {
left: 10px;
.stats-panel {
.card--value,
.card--label {
font-size: 12px;
}
.stats-panel__info:last-of-type {
display: block;
}
}
}
}

.info-panel {
background: var(--body-background);
display: flex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import CameraControls from "./CameraControls";
import "./Visualizer.scss";

const features = {
statsEnabled: true,
statsEnabled: false,
cameraControls: true,
};

Expand Down
69 changes: 65 additions & 4 deletions client/src/features/visualizer-threejs/wrapper/KeyPanel.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@import "../../../scss/variables";
@import "../../../scss/themes";

.key-panel-container {
.info-container {
display: flex;
position: absolute;
z-index: 1;
Expand All @@ -13,19 +13,23 @@
left: 30px;
justify-content: center;
pointer-events: none;
gap: 20px;

.key-panel {
.card {
background: var(--body-background);
padding: 16px 32px;
}

.key-panel-list {
display: flex;
flex-direction: row;
flex-wrap: wrap;
padding: 16px;
gap: 32px;

.key-panel-item {
display: flex;
flex-direction: row;
align-items: center;
margin: 0 16px;

@include desktop-down {
width: 110px;
Expand All @@ -48,4 +52,61 @@
}
}
}

.stats-panel-container {
display: flex;
z-index: 1;
align-items: center;
pointer-events: none;

.card--label {
justify-content: flex-start;
}
.card--content {
padding: 0;
}
.stats-panel__info {
justify-content: center;
display: flex;
flex-direction: column;
align-items: center;
}

@include tablet-down {
top: 60px;
left: 20px;
bottom: auto;
justify-content: left;

.stats-panel {
.card--value,
.card--label {
text-align: left;
}
.card--label {
justify-content: flex-start;
}
.card--content {
padding: 0;
}
.stats-panel__info {
padding: 0 10px;
display: inline-block;
}
}
}

@include phone-down {
left: 10px;
.stats-panel {
.card--value,
.card--label {
font-size: 12px;
}
.stats-panel__info:last-of-type {
display: block;
}
}
}
}
}
12 changes: 8 additions & 4 deletions client/src/features/visualizer-threejs/wrapper/KeyPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from "react";
import React, { memo } from "react";
import { BlockState } from "@iota/sdk-wasm-nova/web";

import "./KeyPanel.scss";
import StatsPanel from "~features/visualizer-threejs/wrapper/StatsPanel";

export const KeyPanel: React.FC = () => {
export const KeyPanel = ({ network }: { network: string }) => {
const statuses: {
label: string;
state: BlockState;
Expand Down Expand Up @@ -42,8 +43,8 @@ export const KeyPanel: React.FC = () => {
];

return (
<div className="key-panel-container">
<div className="card key-panel">
<div className="info-container">
<div className="card key-panel-list">
{statuses.map((s) => {
return (
<div className="key-panel-item" key={s.state}>
Expand All @@ -58,6 +59,9 @@ export const KeyPanel: React.FC = () => {
);
})}
</div>
<StatsPanel network={network} />
</div>
);
};

export default memo(KeyPanel);
31 changes: 10 additions & 21 deletions client/src/features/visualizer-threejs/wrapper/StatsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
import React from "react";
import { useNetworkStats } from "~helpers/stardust/hooks/useNetworkStats";

export const StatsPanel: React.FC<{ readonly blocksCount: number; readonly network: string }> = ({ blocksCount, network }) => {
const [blocksPerSecond, confirmedBlocksPerSecond, confirmedBlocksPerSecondPercent] = useNetworkStats(network);
import React, { memo } from "react";
import { useNetworkStats } from "~helpers/nova/hooks/useNetworkStats";

export const StatsPanel = ({ network }: { network: string }) => {
const [blocksPerSecond] = useNetworkStats(network);
return (
<div className="stats-panel-container">
<div className="card stats-panel">
<div className="card--content">
<div className="stats-panel__info">
<div className="card--label">Blocks</div>
<div className="card--value">{blocksCount}</div>
</div>
<div className="stats-panel__info">
<div className="card--label">BPS / CBPS</div>
<div className="card--value">
{blocksPerSecond} / {confirmedBlocksPerSecond}
</div>
</div>
<div className="stats-panel__info">
<div className="card--label">Referenced Rate</div>
<div className="card--value">{confirmedBlocksPerSecondPercent}</div>
</div>
<div className="card">
<div className="stats-panel__info">
<div className="card--label">BPS</div>
<div className="card--value green">{blocksPerSecond}</div>
</div>
</div>
</div>
);
};

export default memo(StatsPanel);
7 changes: 3 additions & 4 deletions client/src/features/visualizer-threejs/wrapper/Wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import React from "react";
import Modal from "~/app/components/Modal";
import { TSelectFeedItemNova, TSelectNode } from "~/app/types/visualizer.types";
import { INetwork } from "~/models/config/INetwork";
import { KeyPanel } from "./KeyPanel";
import KeyPanel from "./KeyPanel";
import mainHeader from "~assets/modals/visualizer/main-header.json";
import { SelectedFeedInfo } from "./SelectedFeedInfo";
import { StatsPanel } from "./StatsPanel";

export const Wrapper = ({
blocksCount,
Expand Down Expand Up @@ -68,9 +67,9 @@ export const Wrapper = ({
)}
</div>
</div>
<StatsPanel blocksCount={blocksCount} network={network} />

{selectedFeedItem && <SelectedFeedInfo networkConfig={networkConfig} network={network} selectedFeedItem={selectedFeedItem} />}
<KeyPanel />
<KeyPanel network={network} />
</div>
);

Expand Down
52 changes: 52 additions & 0 deletions client/src/helpers/nova/hooks/useNetworkStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { useEffect, useState } from "react";
import { useIsMounted } from "~helpers/hooks/useIsMounted";
import { ServiceFactory } from "~factories/serviceFactory";
import { NOVA } from "~models/config/protocolVersion";
import { NovaApiClient } from "~services/nova/novaApiClient";

/**
* Periodicaly refresh network stats.
* @param network The network in context.
* @returns The network stats.
*/
export function useNetworkStats(network: string): [string] {
const isMounted = useIsMounted();
const [apiClient] = useState(ServiceFactory.get<NovaApiClient>(`api-client-${NOVA}`));
const [updateTimerId, setUpdateTimerId] = useState<NodeJS.Timer | null>(null);
const [blocksPerSecond, setBlocksPerSecond] = useState<string>("--");

useEffect(() => {
if (network) {
updateNetworkStats();
}

return () => {
if (updateTimerId) {
clearTimeout(updateTimerId);
setUpdateTimerId(null);
}
};
}, [network]);

const updateNetworkStats = () => {
if (isMounted && apiClient && network) {
apiClient
.stats({
network,
includeHistory: true,
})
.then((ips) => {
const itemsPerSecond = ips.itemsPerSecond ?? 0;
setBlocksPerSecond(itemsPerSecond >= 0 ? itemsPerSecond.toFixed(2) : "--");
})
.catch((err) => {
console.error(err);
})
.finally(() => {
setUpdateTimerId(setTimeout(async () => updateNetworkStats(), 4000));
});
}
};

return [blocksPerSecond];
}

0 comments on commit fc127e1

Please sign in to comment.