Skip to content

Commit

Permalink
feat: update network interfaces (#8210)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
cpl121 and begonaalvarezd authored Mar 21, 2024
1 parent 58cbb06 commit 3a9283a
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 31 deletions.
53 changes: 30 additions & 23 deletions packages/desktop/components/popups/NodeInfoPopup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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' },
Expand Down Expand Up @@ -69,7 +64,8 @@
},
}
let nodeInfo: INodeInfo
let nodeInfo: INodeInfoResponse
let networkMetrics: NetworkMetricsResponse
function processNodeInfoMapTab(
_nodeInfoTab: NodeInfoTab,
Expand All @@ -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 {
Expand Down Expand Up @@ -124,6 +120,17 @@
message: localize(err.error),
})
})
getNetworkMetrics()
.then((networkMetricsResponse) => {
networkMetrics = networkMetricsResponse
})
.catch((err) => {
closePopup()
showAppNotification({
type: 'error',
message: localize(err.error),
})
})
})
</script>

Expand Down
Original file line number Diff line number Diff line change
@@ -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<NetworkMetricsResponse | undefined> {
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)
}
}
}
1 change: 1 addition & 0 deletions packages/shared/lib/core/network/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './showNetworkIssueNotification'
export * from './toggleDisabledNodeInClientOptions'
export * from './togglePrimaryNodeInClientOptions'
export * from './updateClientOptions'
export * from './getAndUpdateNetworkMetrics'
7 changes: 6 additions & 1 deletion packages/shared/lib/core/network/actions/network-polling.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NETWORK_STATUS_POLL_INTERVAL } from '../constants'
import { getAndUpdateNetworkMetrics } from './getAndUpdateNetworkMetrics'
import { getAndUpdateNodeInfo } from './getAndUpdateNodeInfo'

let pollInterval: number
Expand All @@ -8,7 +9,11 @@ let pollInterval: number
*/
export async function pollNetworkStatus(): Promise<void> {
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand All @@ -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,
Expand All @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions packages/shared/lib/core/network/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './network-status.store'
export * from './node-info.store'
export * from './network-metrics.store'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NetworkMetricsResponse } from '@iota/sdk/out/types'
import { writable } from 'svelte/store'

export const networkMetrics = writable<NetworkMetricsResponse | undefined>(undefined)

export function setNetworkMetrics(newNetworkMetrics: NetworkMetricsResponse | undefined): void {
return networkMetrics.set(newNetworkMetrics)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions packages/shared/lib/core/wallet/actions/getNetworkMetrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NetworkMetricsResponse } from '@iota/sdk/out/types'
import { getClient } from './getClient'

export async function getNetworkMetrics(): Promise<NetworkMetricsResponse> {
const client = await getClient()
return client.getNetworkMetrics()
}
1 change: 1 addition & 0 deletions packages/shared/lib/core/wallet/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ export * from './setNextSelectedWallet'
export * from './restoreBackup'
export * from './getOutputRewards'
export * from './getCommitteInfo'
export * from './getNetworkMetrics'
export * from './getTotalWalletBalance'

0 comments on commit 3a9283a

Please sign in to comment.