Skip to content

Commit

Permalink
wip: refactor and optimize a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Jul 26, 2024
1 parent 75b0766 commit 60e65d5
Showing 1 changed file with 52 additions and 35 deletions.
87 changes: 52 additions & 35 deletions src/components/core/utils/statusHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
OceanNodeStatus,
OceanNodeProvider,
OceanNodeIndexer,
StorageTypes
StorageTypes,
OceanNodeConfig
} from '../../../@types/OceanNode.js'
import { existsEnvironmentVariable, getConfiguration } from '../../../utils/index.js'
import { ENVIRONMENT_VARIABLES } from '../../../utils/constants.js'
Expand All @@ -13,6 +14,44 @@ import { OceanNode } from '../../../OceanNode.js'
import { isAddress } from 'ethers'
import { schemas } from '../../database/schemas.js'

function getAdminAddresses(config: OceanNodeConfig) {
const validAddresses = []
if (config.allowedAdmins) {
for (const admin of config.allowedAdmins) {
if (isAddress(admin) === true) {
validAddresses.push(admin)
}
}
if (validAddresses.length === 0) {
CORE_LOGGER.log(
LOG_LEVELS_STR.LEVEL_ERROR,
`Invalid format for ETH address from ALLOWED ADMINS.`
)
}
}
return validAddresses
}
const supportedStorageTypes: StorageTypes = {
url: true,
arwave: existsEnvironmentVariable(ENVIRONMENT_VARIABLES.ARWEAVE_GATEWAY),
ipfs: existsEnvironmentVariable(ENVIRONMENT_VARIABLES.IPFS_GATEWAY)
}

// platform information
const platformInfo = {
cpus: os.cpus().length,
freemem: os.freemem(),
totalmem: os.totalmem(),
loadavg: os.loadavg(),
arch: os.arch(),
machine: os.machine(),
platform: os.platform(),
osType: os.type(),
node: process.version
}

let previousStatus: OceanNodeStatus = null

export async function status(
oceanNode: OceanNode,
nodeId?: string,
Expand All @@ -30,20 +69,6 @@ export async function status(
}
const config = await getConfiguration()

const validAddresses = []
if (config.allowedAdmins) {
for (const admin of config.allowedAdmins) {
if (isAddress(admin) === true) {
validAddresses.push(admin)
}
}
if (validAddresses.length === 0) {
CORE_LOGGER.log(
LOG_LEVELS_STR.LEVEL_ERROR,
`Invalid format for ETH address from ALLOWED ADMINS.`
)
}
}
const status: OceanNodeStatus = {
id: undefined,
publicKey: undefined,
Expand All @@ -53,42 +78,30 @@ export async function status(
p2p: undefined,
provider: [],
indexer: [],
supportedStorage: undefined,
supportedStorage: supportedStorageTypes,
uptime: process.uptime(),
platform: {
cpus: os.cpus().length,
freemem: os.freemem(),
totalmem: os.totalmem(),
loadavg: os.loadavg(),
arch: os.arch(),
machine: os.machine(),
platform: os.platform(),
osType: os.type(),
node: process.version
},
platform: platformInfo,
codeHash: config.codeHash,
allowedAdmins: validAddresses
allowedAdmins: getAdminAddresses(config)
}

// only these 2 might change between requests
status.platform.freemem = os.freemem()
status.platform.loadavg = os.loadavg()

if (nodeId && nodeId !== undefined) {
status.id = nodeId
} else {
// get current node ID
status.id = config.keys.peerId.toString()
}

const supportedStorageTypes: StorageTypes = {
url: true,
arwave: existsEnvironmentVariable(ENVIRONMENT_VARIABLES.ARWEAVE_GATEWAY),
ipfs: existsEnvironmentVariable(ENVIRONMENT_VARIABLES.IPFS_GATEWAY)
}

status.version = process.env.npm_package_version
status.publicKey = Buffer.from(config.keys.publicKey).toString('hex')
status.address = config.keys.ethAddress
status.http = config.hasHttp
status.p2p = config.hasP2P
status.supportedStorage = supportedStorageTypes
// status.supportedStorage = supportedStorageTypes

if (config.supportedNetworks) {
for (const [key, supportedNetwork] of Object.entries(config.supportedNetworks)) {
Expand Down Expand Up @@ -127,5 +140,9 @@ export async function status(
status.c2dClusters = config.c2dClusters
status.supportedSchemas = schemas.ddoSchemas
}

if (!previousStatus) {
previousStatus = status
}
return status
}

0 comments on commit 60e65d5

Please sign in to comment.