Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update network interfaces #8210

Merged
merged 6 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions packages/desktop/components/popups/NodeInfoPopup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
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 { INodeInfo, NetworkMetricsResponse } from '@iota/sdk/out/types'
evavirseda marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -71,6 +66,7 @@
}

let nodeInfo: INodeInfo
let networkMetrics: NetworkMetricsResponse

function processNodeInfoMapTab(
_nodeInfoTab: NodeInfoTab,
Expand All @@ -81,20 +77,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 @@ -125,6 +121,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 @@ -9,3 +9,4 @@ export * from './toggleDisabledNodeInClientOptions'
export * from './toggleLocalPowInClientOptions'
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,3 +40,4 @@ export * from './setNextSelectedWallet'
export * from './restoreBackup'
export * from './getOutputRewards'
export * from './getCommitteInfo'
export * from './getNetworkMetrics'
Loading