Skip to content

Commit

Permalink
more refactoring and add missing definitions on compute env
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-ocean committed Feb 19, 2025
1 parent 2b98d19 commit 9184f3e
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 33 deletions.
5 changes: 3 additions & 2 deletions ComputeExamples.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ import {
configHelperNetworks,
ConfigHelper,
getEventFromTx,
amountToUnits
amountToUnits,
isDefined
} from '@oceanprotocol/lib'
```

Expand Down Expand Up @@ -594,7 +595,7 @@ Now, let's check that we successfully published a algorithm (create NFT + Datato
let's check the free compute environment
```Typescript
const computeEnv = computeEnvs[resolvedDatasetDdo.chainId].find(
(ce) => ce.priceMin === 0
(ce) => ce.priceMin === 0 || isDefined(ce.free)
)
console.log('Free compute environment = ', computeEnv)
```
Expand Down
60 changes: 41 additions & 19 deletions src/@types/Compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,30 @@ export interface ComputeEnvFees {
export interface ComputeEnvFeesStructure {
[chainId: string]: ComputeEnvFees
}
export interface ComputeEnvironment {

export interface ComputeResourceRequest {
id: string
amount: number
}

export interface ComputeResource {
id: ComputeResourceType
type?: string
kind?: string
total: number // total number of specific resource
min: number // min number of resource needed for a job
max: number // max number of resource for a job
inUse?: number // for display purposes
}

export interface ComputeEnvironmentFreeOptions {
// only if a compute env exposes free jobs
storageExpiry?: number
maxJobDuration?: number
maxJobs?: number // maximum number of simultaneous free jobs
resources?: ComputeResource[]
}
export interface ComputeEnvironment {
// legacy
// cpuNumber: number
// cpuType: string
Expand All @@ -55,22 +77,27 @@ export interface ComputeEnvironment {
// ramGB: number
// diskGB: number
// priceMin: number
totalCpu: number // total cpu available for jobs
maxCpu: number // max cpu for a single job. Imagine a K8 cluster with two nodes, each node with 10 cpus. Total=20, but at most you can allocate 10 cpu for a job
totalRam: number // total gb of RAM
maxRam: number // max allocatable GB RAM for a single job.
maxDisk: number // max GB of disck allocatable for a single job
fees: ComputeEnvFeesStructure
// totalCpu: number // total cpu available for jobs
// maxCpu: number // max cpu for a single job. Imagine a K8 cluster with two nodes, each node with 10 cpus. Total=20, but at most you can allocate 10 cpu for a job
// totalRam: number // total gb of RAM
// maxRam: number // max allocatable GB RAM for a single job.
// maxDisk: number // max GB of disck allocatable for a single job
// currentJobs: number
// lastSeen: number
// legacy
id: string
description: string
currentJobs: number
maxJobs: number
consumerAddress: string
storageExpiry: number
maxJobDuration: number
lastSeen: number
free: boolean
platform?: RunningPlatform[] // array due to k8 support
storageExpiry?: number // amount of seconds for storage
minJobDuration?: number // min billable seconds for a paid job
maxJobDuration?: number // max duration in seconds for a paid job
maxJobs?: number // maximum number of simultaneous paid jobs
runningJobs: number // amount of running jobs (paid jobs)
runningfreeJobs?: number // amount of running jobs (free jobs)
fees: ComputeEnvFeesStructure
resources?: ComputeResource[]
free?: ComputeEnvironmentFreeOptions
platform?: RunningPlatform
}

export interface ComputeResult {
Expand Down Expand Up @@ -159,8 +186,3 @@ export interface ComputeAlgorithm {
algocustomdata?: { [key: string]: any }
userdata?: { [key: string]: any }
}

export interface ComputeResourceRequest {
id: string
amount: number
}
4 changes: 4 additions & 0 deletions src/utils/General.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export async function sleep(ms: number) {
setTimeout(resolve, ms)
})
}

export function isDefined(something: any): boolean {
return something !== undefined && something !== null
}
12 changes: 9 additions & 3 deletions test/integration/ComputeExamples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ import {
configHelperNetworks,
ConfigHelper,
getEventFromTx,
amountToUnits
amountToUnits,
isDefined
} from '../../src'
/// ```

Expand Down Expand Up @@ -577,6 +578,7 @@ describe('Compute-to-data example tests', async () => {
it('9.1 Fetch compute environments from provider', async () => {
/// ```Typescript
computeEnvs = await ProviderInstance.getComputeEnvironments(providerUrl)
console.log('FOUND computeEnvs: ', computeEnvs)
/// ```
/// <!--
assert(computeEnvs, 'No Compute environments found')
Expand All @@ -594,7 +596,7 @@ describe('Compute-to-data example tests', async () => {
/// let's check the free compute environment
/// ```Typescript
const computeEnv = computeEnvs[resolvedDatasetDdo.chainId].find(
(ce) => ce.priceMin === 0
(ce) => ce.priceMin === 0 || isDefined(ce.free)
)
console.log('Free compute environment = ', computeEnv)
/// ```
Expand Down Expand Up @@ -656,7 +658,11 @@ describe('Compute-to-data example tests', async () => {
consumerAccount,
computeEnv.id,
assets,
algo
algo,
null,
null,
null,
true
)

/// ```
Expand Down
19 changes: 10 additions & 9 deletions test/integration/ComputeFlow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
Aquarius,
Datatoken,
sendTx,
amountToUnits
amountToUnits,
isDefined
} from '../../src'
import { ComputeJob, ComputeAsset, ComputeAlgorithm, Files } from '../../src/@types'
import { createAsset, handleComputeOrder } from './helpers'
Expand Down Expand Up @@ -408,7 +409,7 @@ describe('Compute flow tests', async () => {

// we choose the free env
const computeEnv = computeEnvs[resolvedDdoWith5mTimeout.chainId].find(
(ce) => ce.priceMin === 0
(ce) => ce.priceMin === 0 || isDefined(ce.free)
)
assert(computeEnv, 'Cannot find the free compute env')

Expand Down Expand Up @@ -479,7 +480,7 @@ describe('Compute flow tests', async () => {
it('should restart a computeJob without paying anything, because order is valid and providerFees are still valid', async () => {
// we choose the free env
const computeEnv = computeEnvs[resolvedDdoWith5mTimeout.chainId].find(
(ce) => ce.priceMin === 0
(ce) => ce.priceMin === 0 || isDefined(ce.free)
)
assert(computeEnv, 'Cannot find the free compute env')

Expand Down Expand Up @@ -541,7 +542,7 @@ describe('Compute flow tests', async () => {
it('should start a computeJob on a paid environment', async () => {
// we choose the paid env
const computeEnv = computeEnvs[resolvedDdoWith5mTimeout.chainId].find(
(ce) => ce.priceMin !== 0
(ce) => ce.priceMin !== 0 || !isDefined(ce.free)
)
assert(computeEnv, 'Cannot find the paid compute env')

Expand Down Expand Up @@ -618,9 +619,9 @@ describe('Compute flow tests', async () => {
it('should restart a computeJob on paid environment, without paying anything, because order is valid and providerFees are still valid', async () => {
// we choose the paid env
const computeEnv = computeEnvs[resolvedDdoWith5mTimeout.chainId].find(
(ce) => ce.priceMin !== 0
(ce) => ce.priceMin !== 0 || !isDefined(ce.free)
)
assert(computeEnv, 'Cannot find the free compute env')
assert(computeEnv, 'Cannot find the paid compute env')

const assets: ComputeAsset[] = [
{
Expand Down Expand Up @@ -688,7 +689,7 @@ describe('Compute flow tests', async () => {
it('should start a computeJob using the free environment, by paying only providerFee (reuseOrder)', async () => {
// we choose the free env
const computeEnv = computeEnvs[resolvedDdoWith5mTimeout.chainId].find(
(ce) => ce.priceMin === 0
(ce) => ce.priceMin === 0 || isDefined(ce.free)
)
assert(computeEnv, 'Cannot find the free compute env')

Expand Down Expand Up @@ -773,9 +774,9 @@ describe('Compute flow tests', async () => {
it('should start a computeJob using the paid environment, by paying only providerFee (reuseOrder)', async () => {
// we choose the paid env
const computeEnv = computeEnvs[resolvedDdoWith5mTimeout.chainId].find(
(ce) => ce.priceMin !== 0
(ce) => ce.priceMin !== 0 || !isDefined(ce.free)
)
assert(computeEnv, 'Cannot find the free compute env')
assert(computeEnv, 'Cannot find the paid compute env')

const assets: ComputeAsset[] = [
{
Expand Down

0 comments on commit 9184f3e

Please sign in to comment.