From d549d2b36f13246cf377163b398f46417770a5e1 Mon Sep 17 00:00:00 2001 From: Pavel Jankoski Date: Wed, 8 Nov 2023 00:13:57 +0100 Subject: [PATCH] console: Use Gateway Stats Batch API --- .../store/middleware/logics/gateways.js | 65 +++++++++---------- sdk/js/src/service/gateways.js | 8 +++ 2 files changed, 40 insertions(+), 33 deletions(-) diff --git a/pkg/webui/console/store/middleware/logics/gateways.js b/pkg/webui/console/store/middleware/logics/gateways.js index 08b2c58c4e..188d356d0c 100644 --- a/pkg/webui/console/store/middleware/logics/gateways.js +++ b/pkg/webui/console/store/middleware/logics/gateways.js @@ -118,39 +118,38 @@ const getGatewaysLogic = createRequestLogic({ if (options.withStatus) { const gsConfig = selectGsConfig() const consoleGsAddress = getHostFromUrl(gsConfig.base_url) - - entities = await Promise.all( - data.gateways.map(gateway => { - const gatewayServerAddress = getHostFromUrl(gateway.gateway_server_address) - - if (!Boolean(gatewayServerAddress)) { - return Promise.resolve({ ...gateway, status: 'unknown' }) - } - - if (gatewayServerAddress !== consoleGsAddress) { - return Promise.resolve({ ...gateway, status: 'other-cluster' }) - } - - const id = getGatewayId(gateway) - return tts.Gateways.getStatisticsById(id) - .then(stats => { - let status = 'unknown' - if (Boolean(stats) && Boolean(stats.connected_at)) { - status = 'connected' - } else if (Boolean(stats) && Boolean(stats.disconnected_at)) { - status = 'disconnected' - } - return { ...gateway, status } - }) - .catch(err => { - if (err && err.code === 5) { - return { ...gateway, status: 'disconnected' } - } - - return { ...gateway, status: 'unknown' } - }) - }), - ) + const gatewayIds = entities.map(e => e.ids) + const gatewaysStats = await tts.Gateways.getBatchStatistics(gatewayIds) + + entities = data.gateways.map(gateway => { + const gatewayServerAddress = getHostFromUrl(gateway.gateway_server_address) + + if (!Boolean(gatewayServerAddress)) { + return { ...gateway, status: 'unknown' } + } + + if (gatewayServerAddress !== consoleGsAddress) { + return { ...gateway, status: 'other-cluster' } + } + + if (!gatewaysStats?.entries) { + return { ...gateway, status: 'disconnected' } + } + + const id = getGatewayId(gateway) + let status = 'unknown' + + if (Boolean(gatewaysStats.entries[id]) && Boolean(gatewaysStats.entries[id].connected_at)) { + status = 'connected' + } else if ( + !Boolean(gatewaysStats.entries[id]) || + Boolean(gatewaysStats.entries[id].disconnected_at) + ) { + status = 'disconnected' + } + + return { ...gateway, status } + }) } return { diff --git a/sdk/js/src/service/gateways.js b/sdk/js/src/service/gateways.js index 2a566e5c58..fabee28929 100644 --- a/sdk/js/src/service/gateways.js +++ b/sdk/js/src/service/gateways.js @@ -221,6 +221,14 @@ class Gateways { return Marshaler.payloadSingleResponse(response) } + async getBatchStatistics(gatewayIds) { + const response = await this._api.Gs.BatchGetGatewayConnectionStats(undefined, { + gateway_ids: gatewayIds + }) + + return Marshaler.payloadSingleResponse(response) + } + async getRightsById(gatewayId) { const result = await this._api.GatewayAccess.ListRights({ routeParams: { gateway_id: gatewayId },