From 3a9283aaf38ae95a12b049cc4da11659955987e2 Mon Sep 17 00:00:00 2001 From: cpl121 <100352899+cpl121@users.noreply.github.com> Date: Thu, 21 Mar 2024 12:39:51 +0100 Subject: [PATCH] feat: update network interfaces (#8210) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: update network interfaces WIP * fix: metrics in node info popup * fix: update network metrics store * fix: update node info interface --------- Co-authored-by: Begoña Álvarez de la Cruz --- .../components/popups/NodeInfoPopup.svelte | 53 +++++++++++-------- .../actions/getAndUpdateNetworkMetrics.ts | 19 +++++++ .../shared/lib/core/network/actions/index.ts | 1 + .../core/network/actions/network-polling.ts | 7 ++- .../helpers/getNetworkStatusFromNodeInfo.ts | 11 ++-- .../shared/lib/core/network/stores/index.ts | 1 + .../network/stores/network-metrics.store.ts | 8 +++ .../network/stores/network-status.store.ts | 7 +-- .../core/wallet/actions/getNetworkMetrics.ts | 7 +++ .../shared/lib/core/wallet/actions/index.ts | 1 + 10 files changed, 84 insertions(+), 31 deletions(-) create mode 100644 packages/shared/lib/core/network/actions/getAndUpdateNetworkMetrics.ts create mode 100644 packages/shared/lib/core/network/stores/network-metrics.store.ts create mode 100644 packages/shared/lib/core/wallet/actions/getNetworkMetrics.ts diff --git a/packages/desktop/components/popups/NodeInfoPopup.svelte b/packages/desktop/components/popups/NodeInfoPopup.svelte index f16f1f1c511..1fd875b78b3 100644 --- a/packages/desktop/components/popups/NodeInfoPopup.svelte +++ b/packages/desktop/components/popups/NodeInfoPopup.svelte @@ -2,12 +2,12 @@ import { onMount } from 'svelte' import { Button, Checkbox, CopyableBox, Spinner, Text } from '@ui' import { formatNumber, localize } from '@core/i18n' - import { INode } from '@core/network' + import { INode, INodeInfoResponse } from '@core/network' import { closePopup } from '@auxiliary/popup' import { showAppNotification } from '@auxiliary/notification' import { resolveObjectPath, setClipboard } from '@core/utils' - import { INodeInfo } from '@iota/sdk/out/types' - import { getNodeInfo } from '@core/wallet/actions' + import { NetworkMetricsResponse } from '@iota/sdk/out/types' + import { getNetworkMetrics, getNodeInfo } from '@core/wallet' enum NodeInfoTab { General = 'general', @@ -31,17 +31,12 @@ // features: { localeKey: 'general.features', nodeInfoPath: 'features' }, }, [NodeInfoTab.Metrics]: { - blocksPerSecond: { localeKey: 'metrics.blocksPerSecond', nodeInfoPath: 'metrics.blocksPerSecond' }, + blocksPerSecond: { localeKey: 'metrics.blocksPerSecond', nodeInfoPath: 'blocksPerSecond' }, confirmedBlocksPerSecond: { localeKey: 'metrics.confirmedBlocksPerSecond', - nodeInfoPath: 'metrics.confirmedBlocksPerSecond', + nodeInfoPath: 'confirmedBlocksPerSecond', }, - confirmationRate: { localeKey: 'metrics.confirmationRate', nodeInfoPath: 'metrics.confirmationRate' }, - // latestSlot: { localeKey: 'metrics.latestSlot', nodeInfoPath: 'status.latestSlot.index' }, - // confirmedSlot: { - // localeKey: 'metrics.confirmedSlot', - // nodeInfoPath: 'status.confirmedSlot.index', - // }, + confirmationRate: { localeKey: 'metrics.confirmationRate', nodeInfoPath: 'confirmationRate' }, }, [NodeInfoTab.Protocol]: { network: { localeKey: 'protocol.network', nodeInfoPath: 'protocolParameters[0].parameters.networkName' }, @@ -69,7 +64,8 @@ }, } - let nodeInfo: INodeInfo + let nodeInfo: INodeInfoResponse + let networkMetrics: NetworkMetricsResponse function processNodeInfoMapTab( _nodeInfoTab: NodeInfoTab, @@ -80,20 +76,20 @@ let nodeInfoValue = '' if (key === 'url') { nodeInfoValue = node.url - } else { - nodeInfoValue = resolveObjectPath(nodeInfo, nodeInfoTabObject[key]?.nodeInfoPath, null) - if (key === 'confirmationRate' || key === 'blocksPerSecond' || key === 'confirmedBlocksPerSecond') { - const numberValue = Number(nodeInfoValue) - if (numberValue >= 0) { - if (key === 'confirmationRate') { - nodeInfoValue = `${formatNumber(Math.min(numberValue, 100), 1, 1)}%` - } else { - nodeInfoValue = formatNumber(numberValue, 1, 1) - } + } else if (_nodeInfoTab === NodeInfoTab.Metrics) { + nodeInfoValue = resolveObjectPath(networkMetrics, nodeInfoTabObject[key]?.nodeInfoPath, null) + const numberValue = Number(nodeInfoValue) + if (numberValue >= 0) { + if (key === 'confirmationRate') { + nodeInfoValue = `${formatNumber(Math.min(numberValue, 100), 1, 1)}%` } else { - nodeInfoValue = '' + nodeInfoValue = formatNumber(numberValue, 1, 1) } + } else { + nodeInfoValue = '' } + } else { + nodeInfoValue = resolveObjectPath(nodeInfo, nodeInfoTabObject[key]?.nodeInfoPath, null) } return { @@ -124,6 +120,17 @@ message: localize(err.error), }) }) + getNetworkMetrics() + .then((networkMetricsResponse) => { + networkMetrics = networkMetricsResponse + }) + .catch((err) => { + closePopup() + showAppNotification({ + type: 'error', + message: localize(err.error), + }) + }) }) diff --git a/packages/shared/lib/core/network/actions/getAndUpdateNetworkMetrics.ts b/packages/shared/lib/core/network/actions/getAndUpdateNetworkMetrics.ts new file mode 100644 index 00000000000..3056deacbaa --- /dev/null +++ b/packages/shared/lib/core/network/actions/getAndUpdateNetworkMetrics.ts @@ -0,0 +1,19 @@ +import { getNetworkMetrics } from '@core/wallet/actions' +import { NetworkMetricsResponse } from '@iota/sdk' +import { setNetworkMetrics } from '../stores' + +export async function getAndUpdateNetworkMetrics(forwardErrors = false): Promise { + let networkMetricsResponse: NetworkMetricsResponse + try { + networkMetricsResponse = await getNetworkMetrics() + setNetworkMetrics(networkMetricsResponse) + return networkMetricsResponse + } catch (err) { + setNetworkMetrics(undefined) + if (forwardErrors) { + return Promise.reject(err) + } else { + console.error(err) + } + } +} diff --git a/packages/shared/lib/core/network/actions/index.ts b/packages/shared/lib/core/network/actions/index.ts index 061e6dc7779..fae6b8ddc6c 100644 --- a/packages/shared/lib/core/network/actions/index.ts +++ b/packages/shared/lib/core/network/actions/index.ts @@ -8,3 +8,4 @@ export * from './showNetworkIssueNotification' export * from './toggleDisabledNodeInClientOptions' export * from './togglePrimaryNodeInClientOptions' export * from './updateClientOptions' +export * from './getAndUpdateNetworkMetrics' diff --git a/packages/shared/lib/core/network/actions/network-polling.ts b/packages/shared/lib/core/network/actions/network-polling.ts index 3da4d89331b..f190a07f22e 100644 --- a/packages/shared/lib/core/network/actions/network-polling.ts +++ b/packages/shared/lib/core/network/actions/network-polling.ts @@ -1,4 +1,5 @@ import { NETWORK_STATUS_POLL_INTERVAL } from '../constants' +import { getAndUpdateNetworkMetrics } from './getAndUpdateNetworkMetrics' import { getAndUpdateNodeInfo } from './getAndUpdateNodeInfo' let pollInterval: number @@ -8,7 +9,11 @@ let pollInterval: number */ export async function pollNetworkStatus(): Promise { await getAndUpdateNodeInfo() - pollInterval = window.setInterval(() => void getAndUpdateNodeInfo(), NETWORK_STATUS_POLL_INTERVAL) + await getAndUpdateNetworkMetrics() + pollInterval = window.setInterval(() => { + getAndUpdateNodeInfo() + getAndUpdateNetworkMetrics() + }, NETWORK_STATUS_POLL_INTERVAL) } export function clearNetworkPoll(): void { diff --git a/packages/shared/lib/core/network/helpers/getNetworkStatusFromNodeInfo.ts b/packages/shared/lib/core/network/helpers/getNetworkStatusFromNodeInfo.ts index c993c604b7c..129df88cec2 100644 --- a/packages/shared/lib/core/network/helpers/getNetworkStatusFromNodeInfo.ts +++ b/packages/shared/lib/core/network/helpers/getNetworkStatusFromNodeInfo.ts @@ -1,7 +1,7 @@ import { MILLISECONDS_PER_SECOND, SECONDS_PER_MINUTE } from '@core/utils' import { NetworkHealth } from '../enums' import { INetworkStatus } from '../interfaces' -import { INodeInfo } from '@iota/sdk/out/types' +import { INodeInfo, NetworkMetricsResponse } from '@iota/sdk/out/types' import { getUnixTimestampFromNodeInfoAndSlotIndex } from './getSlotInfoFromNodeProtocolParameters' /** @@ -11,7 +11,10 @@ import { getUnixTimestampFromNodeInfoAndSlotIndex } from './getSlotInfoFromNodeP * @param {IStardustNodeInfo} nodeInfo * @returns {INetworkStatus} */ -export function getNetworkStatusFromNodeInfo(nodeInfo: INodeInfo): INetworkStatus { +export function getNetworkStatusFromNodeInfo( + nodeInfo: INodeInfo, + networkMetrics: NetworkMetricsResponse +): INetworkStatus { let health = NetworkHealth.Down const unixTimestamp = getUnixTimestampFromNodeInfoAndSlotIndex( nodeInfo.protocolParameters[0].parameters, @@ -31,8 +34,8 @@ export function getNetworkStatusFromNodeInfo(nodeInfo: INodeInfo): INetworkStatu } return { - messagesPerSecond: nodeInfo.metrics.blocksPerSecond, - confirmationRate: nodeInfo.metrics.confirmationRate, + messagesPerSecond: networkMetrics.blocksPerSecond, + confirmationRate: networkMetrics.confirmationRate, health, currentSlot: nodeInfo.status.latestConfirmedBlockSlot, } diff --git a/packages/shared/lib/core/network/stores/index.ts b/packages/shared/lib/core/network/stores/index.ts index 953591a9fe9..e7d86760a6a 100644 --- a/packages/shared/lib/core/network/stores/index.ts +++ b/packages/shared/lib/core/network/stores/index.ts @@ -1,2 +1,3 @@ export * from './network-status.store' export * from './node-info.store' +export * from './network-metrics.store' diff --git a/packages/shared/lib/core/network/stores/network-metrics.store.ts b/packages/shared/lib/core/network/stores/network-metrics.store.ts new file mode 100644 index 00000000000..ee5664ce39b --- /dev/null +++ b/packages/shared/lib/core/network/stores/network-metrics.store.ts @@ -0,0 +1,8 @@ +import { NetworkMetricsResponse } from '@iota/sdk/out/types' +import { writable } from 'svelte/store' + +export const networkMetrics = writable(undefined) + +export function setNetworkMetrics(newNetworkMetrics: NetworkMetricsResponse | undefined): void { + return networkMetrics.set(newNetworkMetrics) +} diff --git a/packages/shared/lib/core/network/stores/network-status.store.ts b/packages/shared/lib/core/network/stores/network-status.store.ts index 436eda5891f..b6f6c4afd21 100644 --- a/packages/shared/lib/core/network/stores/network-status.store.ts +++ b/packages/shared/lib/core/network/stores/network-status.store.ts @@ -3,10 +3,11 @@ import { SLOT_NOT_FOUND } from '../constants' import { NetworkHealth } from '../enums/network-health.enum' import { getNetworkStatusFromNodeInfo } from '../helpers' import { nodeInfo } from './node-info.store' +import { networkMetrics } from './network-metrics.store' -export const networkStatus = derived([nodeInfo], ([$nodeInfo]) => { - if ($nodeInfo) { - return getNetworkStatusFromNodeInfo($nodeInfo) +export const networkStatus = derived([nodeInfo, networkMetrics], ([$nodeInfo, $networkMetrics]) => { + if ($nodeInfo && $networkMetrics) { + return getNetworkStatusFromNodeInfo($nodeInfo, $networkMetrics) } else { return { messagesPerSecond: 0, diff --git a/packages/shared/lib/core/wallet/actions/getNetworkMetrics.ts b/packages/shared/lib/core/wallet/actions/getNetworkMetrics.ts new file mode 100644 index 00000000000..d15fcc86d01 --- /dev/null +++ b/packages/shared/lib/core/wallet/actions/getNetworkMetrics.ts @@ -0,0 +1,7 @@ +import { NetworkMetricsResponse } from '@iota/sdk/out/types' +import { getClient } from './getClient' + +export async function getNetworkMetrics(): Promise { + const client = await getClient() + return client.getNetworkMetrics() +} diff --git a/packages/shared/lib/core/wallet/actions/index.ts b/packages/shared/lib/core/wallet/actions/index.ts index 8dde6d065e0..4007985c487 100644 --- a/packages/shared/lib/core/wallet/actions/index.ts +++ b/packages/shared/lib/core/wallet/actions/index.ts @@ -40,4 +40,5 @@ export * from './setNextSelectedWallet' export * from './restoreBackup' export * from './getOutputRewards' export * from './getCommitteInfo' +export * from './getNetworkMetrics' export * from './getTotalWalletBalance'