-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from oceanprotocol/issue-227-get-environments
Issue 227 get environments
- Loading branch information
Showing
8 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Readable } from 'stream' | ||
import { P2PCommandResponse } from '../../@types' | ||
import { CORE_LOGGER } from '../../utils/logging/common.js' | ||
import { Handler } from './handler.js' | ||
import { GetEnvironments } from '../../utils/constants.js' | ||
import { getConfiguration } from '../../utils/config.js' | ||
import axios from 'axios' | ||
|
||
export class GetEnvironmentsHandler extends Handler { | ||
async handle(task: GetEnvironments): Promise<P2PCommandResponse> { | ||
try { | ||
CORE_LOGGER.logMessage( | ||
'File Info Request recieved with arguments: ' + JSON.stringify(task, null, 2), | ||
true | ||
) | ||
const response: any[] = [] | ||
const config = await getConfiguration() | ||
const { c2dClusters } = config | ||
for (const cluster of c2dClusters) { | ||
CORE_LOGGER.logMessage( | ||
`Requesting environment from Operator URL: ${cluster.url}`, | ||
true | ||
) | ||
const url = `${cluster.url}api/v1/operator/environments?chain_id=${task.chainId}` | ||
const { data } = await axios.get(url) | ||
const { hash } = cluster | ||
for (const item of data) { | ||
item.id = hash + '-' + item.id | ||
} | ||
response.push(...data) | ||
} | ||
|
||
CORE_LOGGER.logMessage( | ||
'File Info Response: ' + JSON.stringify(response, null, 2), | ||
true | ||
) | ||
|
||
return { | ||
stream: Readable.from(JSON.stringify(response)), | ||
status: { | ||
httpStatus: 200 | ||
} | ||
} | ||
} catch (error) { | ||
CORE_LOGGER.error(error.message) | ||
return { | ||
stream: null, | ||
status: { | ||
httpStatus: 500, | ||
error: error.message | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import express from 'express' | ||
import { GetEnvironmentsHandler } from '../core/compute.js' | ||
import { streamToObject } from '../../utils/util.js' | ||
import { PROTOCOL_COMMANDS } from '../../utils/constants.js' | ||
import { Readable } from 'stream' | ||
import { HTTP_LOGGER } from '../../utils/logging/common.js' | ||
import { LOG_LEVELS_STR } from '../../utils/logging/Logger.js' | ||
|
||
export const computeRoutes = express.Router() | ||
|
||
computeRoutes.get('/api/services/computeEnvironments', async (req, res) => { | ||
try { | ||
HTTP_LOGGER.logMessage( | ||
`GET computeEnvironments request received with query: ${JSON.stringify(req.query)}`, | ||
true | ||
) | ||
const chainId = parseInt(req.query.chainId as string) | ||
|
||
if (isNaN(chainId) || chainId < 1) { | ||
HTTP_LOGGER.logMessage( | ||
`Invalid chainId: ${chainId} on GET computeEnvironments request`, | ||
true | ||
) | ||
return res.status(400).send('Invalid chainId') | ||
} | ||
|
||
const getEnvironmentsTask = { | ||
command: PROTOCOL_COMMANDS.GET_COMPUTE_ENVIRONMENTS, | ||
chainId, | ||
node: req.query.node as string | ||
} | ||
const response = await new GetEnvironmentsHandler().handle(getEnvironmentsTask) // get compute environments | ||
const computeEnvironments = await streamToObject(response.stream as Readable) | ||
|
||
// check if computeEnvironments is a valid json object and not empty | ||
if (computeEnvironments && computeEnvironments.length > 0) { | ||
res.json(computeEnvironments) | ||
} else { | ||
HTTP_LOGGER.logMessage(`Compute environments not found`, true) | ||
res.status(404).send('Compute environments not found') | ||
} | ||
} catch (error) { | ||
HTTP_LOGGER.log(LOG_LEVELS_STR.LEVEL_ERROR, `Error: ${error}`) | ||
res.status(500).send('Internal Server Error') | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { expect, assert } from 'chai' | ||
import { GetEnvironmentsHandler } from '../../components/core/compute.js' | ||
import { getConfiguration } from '../../utils/config.js' | ||
import { Database } from '../../components/database/index.js' | ||
import { OceanNode } from '../../OceanNode.js' | ||
import { PROTOCOL_COMMANDS } from '../../utils/constants.js' | ||
import { OceanNodeConfig } from '../../@types/OceanNode.js' | ||
import { Readable } from 'stream' | ||
import { streamToObject } from '../../utils/util.js' | ||
|
||
describe('Compute', () => { | ||
let config: OceanNodeConfig | ||
let dbconn: Database | ||
let oceanNode: OceanNode | ||
|
||
before(async () => { | ||
config = await getConfiguration(true) // Force reload the configuration | ||
dbconn = await new Database(config.dbConfig) | ||
oceanNode = await OceanNode.getInstance(dbconn) | ||
}) | ||
|
||
it('Sets up compute envs', async () => { | ||
assert(oceanNode, 'Failed to instantiate OceanNode') | ||
assert(config.c2dClusters, 'Failed to get c2dClusters') | ||
}) | ||
|
||
it('Get compute environments', async () => { | ||
const getEnvironmentsTask = { | ||
command: PROTOCOL_COMMANDS.GET_COMPUTE_ENVIRONMENTS, | ||
chainId: 8996 | ||
} | ||
const response = await new GetEnvironmentsHandler(oceanNode).handle( | ||
getEnvironmentsTask | ||
) | ||
|
||
assert(response, 'Failed to get response') | ||
assert(response.status.httpStatus === 200, 'Failed to get 200 response') | ||
assert(response.stream, 'Failed to get stream') | ||
expect(response.stream).to.be.instanceOf(Readable) | ||
|
||
const computeEnvironments = await streamToObject(response.stream as Readable) | ||
|
||
for (const computeEnvironment of computeEnvironments) { | ||
assert(computeEnvironment.id, 'id missing in computeEnvironments') | ||
assert( | ||
computeEnvironment.consumerAddress, | ||
'consumerAddress missing in computeEnvironments' | ||
) | ||
assert(computeEnvironment.lastSeen, 'lastSeen missing in computeEnvironments') | ||
assert(computeEnvironment.id.startsWith('0x'), 'id should start with 0x') | ||
assert(computeEnvironment.cpuNumber > 0, 'cpuNumber missing in computeEnvironments') | ||
assert(computeEnvironment.ramGB > 0, 'ramGB missing in computeEnvironments') | ||
assert(computeEnvironment.diskGB > 0, 'diskGB missing in computeEnvironments') | ||
assert(computeEnvironment.maxJobs > 0, 'maxJobs missing in computeEnvironments') | ||
assert( | ||
computeEnvironment.maxJobDuration > 0, | ||
'maxJobDuration missing in computeEnvironments' | ||
) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters