Skip to content

Commit

Permalink
Merge pull request #6689 from TheThingsNetwork/feature/5635-stats-bat…
Browse files Browse the repository at this point in the history
…ch-api

Use Gateway Stats Batch API in the Console
  • Loading branch information
PavelJankoski authored Nov 9, 2023
2 parents 57eafd1 + d549d2b commit c2a9417
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
65 changes: 32 additions & 33 deletions pkg/webui/console/store/middleware/logics/gateways.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions sdk/js/src/service/gateways.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit c2a9417

Please sign in to comment.