From beb310585a288076cf77d7477a6249bf0c04ef4f Mon Sep 17 00:00:00 2001 From: avivash Date: Thu, 16 Nov 2023 12:03:24 -0800 Subject: [PATCH] Chore: use SDK for health metrics and add error handling to homestar client getter --- package-lock.json | 14 +++++----- package.json | 2 +- src/components/settings/NodeInfo.svelte | 7 +++-- src/lib/health.ts | 37 ------------------------- src/lib/metrics.ts | 3 +- src/lib/network.ts | 4 ++- src/lib/rpc.ts | 29 +++++++++++++------ src/lib/workflows/index.ts | 4 ++- 8 files changed, 41 insertions(+), 59 deletions(-) delete mode 100644 src/lib/health.ts diff --git a/package-lock.json b/package-lock.json index 04d5e1e..d862d3b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,7 @@ "name": "odd-app-template", "version": "0.1.0", "dependencies": { - "@fission-codes/homestar": "^0.6.0", + "@fission-codes/homestar": "^0.6.1", "@oddjs/odd": "0.37.0", "@open-rpc/client-js": "^1.8.1", "@types/three": "^0.156.0", @@ -558,9 +558,9 @@ } }, "node_modules/@fission-codes/homestar": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/@fission-codes/homestar/-/homestar-0.6.0.tgz", - "integrity": "sha512-B03t2sFLqIvaNtyUYQSedFLC/JBrrMzqTwb45YocXkSlCE4gKgfmdzqVCZTVz7dYkK6d5xlAcHlgsXZTXaXKQQ==", + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/@fission-codes/homestar/-/homestar-0.6.1.tgz", + "integrity": "sha512-epEJQR480oSG6vSvHoV/SgPBF8Nl7qWZ2jT1M7suQRrmxHQdHEGtaWBrH79B0ekgaRl5TsAldW1oeCIa0ncdfw==", "dependencies": { "@ipld/dag-cbor": "^9.0.6", "@ipld/dag-json": "^10.1.5", @@ -7814,9 +7814,9 @@ "integrity": "sha512-JUFJad5lv7jxj926GPgymrWQxxjPYuJNiNjNMzqT+HiuP6Vl3dk5xzG+8sTX96np0ZAluvaMzPsjhHZ5rNuNQQ==" }, "@fission-codes/homestar": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/@fission-codes/homestar/-/homestar-0.6.0.tgz", - "integrity": "sha512-B03t2sFLqIvaNtyUYQSedFLC/JBrrMzqTwb45YocXkSlCE4gKgfmdzqVCZTVz7dYkK6d5xlAcHlgsXZTXaXKQQ==", + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/@fission-codes/homestar/-/homestar-0.6.1.tgz", + "integrity": "sha512-epEJQR480oSG6vSvHoV/SgPBF8Nl7qWZ2jT1M7suQRrmxHQdHEGtaWBrH79B0ekgaRl5TsAldW1oeCIa0ncdfw==", "requires": { "@ipld/dag-cbor": "^9.0.6", "@ipld/dag-json": "^10.1.5", diff --git a/package.json b/package.json index 1c12e9c..54fffa0 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ ] }, "dependencies": { - "@fission-codes/homestar": "^0.6.0", + "@fission-codes/homestar": "^0.6.1", "@oddjs/odd": "0.37.0", "@open-rpc/client-js": "^1.8.1", "@types/three": "^0.156.0", diff --git a/src/components/settings/NodeInfo.svelte b/src/components/settings/NodeInfo.svelte index 0bcf3a8..dcfe867 100644 --- a/src/components/settings/NodeInfo.svelte +++ b/src/components/settings/NodeInfo.svelte @@ -2,8 +2,8 @@ import clipboardCopy from 'clipboard-copy' import { onDestroy, onMount } from 'svelte' - import { requestHealth } from '$lib/health' import { addNotification } from '$lib/notifications' + import { getHomestarClient } from '$lib/rpc' import ClipboardIcon from '$components/icons/ClipboardIcon.svelte' export let compressedView = false @@ -17,12 +17,13 @@ } onMount(async () => { - health = await requestHealth() + const homestar = getHomestarClient() + health = (await homestar.health()).result // Poll until listener addresses are returned if (!health?.nodeInfo?.dynamic?.listeners?.length) { interval = setInterval(async () => { - health = await requestHealth() + health = (await homestar.health()).result if (health?.nodeInfo?.dynamic?.listeners?.length) { clearInterval(interval) } diff --git a/src/lib/health.ts b/src/lib/health.ts deleted file mode 100644 index 1cfc565..0000000 --- a/src/lib/health.ts +++ /dev/null @@ -1,37 +0,0 @@ -// import type { InferError } from '@fission-codes/homestar/types' - -import { - // homestar, - rpcClient -} from '$lib/rpc' - -type Health = { - healthy: boolean - nodeInfo: { - dynamic: { - listeners: string[] - } - static: { - peer_id: string - } - } -} - -type Data = { - label: string - type: string - value: string -} - -export const requestHealth = async (): Promise => { - try { - const result = await rpcClient.request({ method: 'health' }) - // const { error, result } = await homestar.health() - // if (error) { - // throw new Error(error) - // } - return result - } catch (error) { - console.error(error) - } -} diff --git a/src/lib/metrics.ts b/src/lib/metrics.ts index b5b71c9..2fee694 100644 --- a/src/lib/metrics.ts +++ b/src/lib/metrics.ts @@ -1,6 +1,6 @@ // import type { InferError } from '@fission-codes/homestar/types' -import { homestar } from '$lib/rpc' +import { getHomestarClient } from '$lib/rpc' type Metric = { data: Data[] @@ -17,6 +17,7 @@ type Data = { export const requestMetrics = async (): Promise => { try { + const homestar = getHomestarClient() const { error, result } = await homestar.metrics() if (error) { throw new Error(error) diff --git a/src/lib/network.ts b/src/lib/network.ts index 9dc30b5..739ff46 100644 --- a/src/lib/network.ts +++ b/src/lib/network.ts @@ -3,7 +3,7 @@ // import { WebSocket } from 'unws' import { networkStore } from '$lib/stores' -import { homestar } from '$lib/rpc' +import { getHomestarClient } from '$lib/rpc' export type NetworkStore = { loading: boolean @@ -34,6 +34,8 @@ const RECEIPT_SENT = 'network:publishedReceiptPubsub' // }) export const subscribNetworkEvents = async (): Promise => { + const homestar = getHomestarClient() + await homestar.networkEvents(res => { // console.log('node1 res', res) if (res.error) { diff --git a/src/lib/rpc.ts b/src/lib/rpc.ts index d3ab840..99c0989 100644 --- a/src/lib/rpc.ts +++ b/src/lib/rpc.ts @@ -7,6 +7,7 @@ import { Client, WebSocketTransport as OpenRPCWebSocketTransport } from '@open-rpc/client-js' +import { addNotification } from './notifications' const openRPCTransport = new OpenRPCWebSocketTransport( import.meta.env.VITE_WEBSOCKET_ENDPOINT @@ -14,15 +15,27 @@ const openRPCTransport = new OpenRPCWebSocketTransport( const requestManager = new RequestManager([openRPCTransport]) export const rpcClient = new Client(requestManager) -const transport = new WebsocketTransport( - import.meta.env.VITE_WEBSOCKET_ENDPOINT, - { - ws: WebSocket +let homestar +export const getHomestarClient = () => { + try { + if (!homestar) { + const transport = new WebsocketTransport( + import.meta.env.VITE_WEBSOCKET_ENDPOINT, + { + ws: WebSocket + } + ) // WS + homestar = new Homestar({ + transport + }) + } + + return homestar + } catch (error) { + console.error(error) + addNotification(error, 'error') } -) // WS -export const homestar = new Homestar({ - transport, -}) +} /** * Subscribe to WS messages and pass the data to the appropriate handler diff --git a/src/lib/workflows/index.ts b/src/lib/workflows/index.ts index 9881d7f..fbb30f6 100644 --- a/src/lib/workflows/index.ts +++ b/src/lib/workflows/index.ts @@ -5,7 +5,7 @@ import { get as getStore } from 'svelte/store' import type { Receipt, FunctionOperation, Meta } from '$lib/functions' import { addNotification } from '$lib/notifications' -import { homestar } from '$lib/rpc' +import { getHomestarClient } from '$lib/rpc' import { workflowsStore } from '$lib/stores' export type WorkflowsStore = { @@ -213,6 +213,8 @@ export const runWorkflow = async ( } }) + const homestar = getHomestarClient() + await homestar.runWorkflow( { ...payloadToRun, name: runName }, async data => {