diff --git a/src/brizo/Brizo.ts b/src/brizo/Brizo.ts index 82354e4..771b72d 100644 --- a/src/brizo/Brizo.ts +++ b/src/brizo/Brizo.ts @@ -95,7 +95,7 @@ export class Brizo extends Instantiable { return destination } - public async executeService( + public async computeService( agreementId: string, serviceEndpoint: string, account: Account, @@ -105,16 +105,16 @@ export class Brizo extends Instantiable { ): Promise { const signature = await this.createSignature(account, agreementId) - let executeUrl = serviceEndpoint - executeUrl += `&signature=${signature}` - executeUrl += `&serviceAgreementId=${noZeroX(agreementId)}` - executeUrl += `&consumerAddress=${account.getId()}` - executeUrl += `&algorithmDID=${algorithmDid}` - executeUrl += `&algorithm=${algorithm}` - executeUrl += `&algorithmMeta=${algorithmMeta}` + let url = serviceEndpoint + url += `&signature=${signature}` + url += `&serviceAgreementId=${noZeroX(agreementId)}` + url += `&consumerAddress=${account.getId()}` + url += `&algorithmDID=${algorithmDid}` + url += `&algorithm=${algorithm}` + url += `&algorithmMeta=${algorithmMeta}` - const result: { workflowId: string } = await this.ocean.utils.fetch - .post(executeUrl, '') + const result: { jobId: string } = await this.ocean.utils.fetch + .post(url, '') .then((response: any) => { if (response.ok) { return response.json() @@ -134,7 +134,7 @@ export class Brizo extends Instantiable { throw error }) - return result.workflowId + return result.jobId } public async createSignature(account: Account, agreementId: string): Promise { diff --git a/src/ddo/MetaData.ts b/src/ddo/MetaData.ts index f16bae6..b98796e 100644 --- a/src/ddo/MetaData.ts +++ b/src/ddo/MetaData.ts @@ -145,12 +145,11 @@ export interface MetaDataMain { name: string /** - * Type of the Asset. Helps to filter by the type of asset, - * initially ("dataset", "algorithm", "container", "workflow", "other"). + * Type of the Asset. Helps to filter by the type of asset ("dataset" or "algorithm"). * @type {string} * @example "dataset" */ - type: 'dataset' | 'algorithm' | 'container' | 'workflow' | 'other' + type: 'dataset' | 'algorithm' /** * The date on which the asset was created by the originator in diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 1bfead7..38d7e99 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -2,6 +2,7 @@ import { OceanAccounts } from './OceanAccounts' import { OceanAgreements } from './OceanAgreements' import { OceanAssets } from './OceanAssets' import { OceanAuth } from './OceanAuth' +import { OceanCompute } from './OceanCompute' import { OceanSecretStore } from './OceanSecretStore' import { OceanTokens } from './OceanTokens' import { OceanVersions } from './OceanVersions' @@ -45,6 +46,7 @@ export class Ocean extends Instantiable { instance.accounts = await OceanAccounts.getInstance(instanceConfig) instance.auth = await OceanAuth.getInstance(instanceConfig) instance.assets = await OceanAssets.getInstance(instanceConfig) + instance.compute = await OceanCompute.getInstance(instanceConfig) instance.agreements = await OceanAgreements.getInstance(instanceConfig) instance.secretStore = await OceanSecretStore.getInstance(instanceConfig) instance.tokens = await OceanTokens.getInstance(instanceConfig) @@ -97,6 +99,12 @@ export class Ocean extends Instantiable { */ public agreements: OceanAgreements + /** + * Ocean compute submodule + * @type {OceanCompute} + */ + public compute: OceanCompute + /** * Ocean secretStore submodule * @type {OceanSecretStore} diff --git a/src/ocean/OceanAssets.ts b/src/ocean/OceanAssets.ts index abc1c14..ec38e2f 100644 --- a/src/ocean/OceanAssets.ts +++ b/src/ocean/OceanAssets.ts @@ -364,38 +364,6 @@ export class OceanAssets extends Instantiable { }) } - /** - * Start the execution of a compute job. - * @param {string} agreementId ID of the agreement. - * @param {DDO} computeDdo DDO of the compute asset. - * @param {Account} consumerAccount Consumer account. - * @param {string} algorithmDid The asset DID (of type `algorithm`) which consist of `did:op:` and the `assetId` hex str (without `0x` prefix). - * @param {string} algorithm The text of the algorithm to run in the compute job (e.g. a jupyter notebook) - * @param {MetaData} algorithmMeta Metadata about the algorithm being run if `algorithm` is being used. This is ignored when `algorithmDID` is specified. - * @return {Promise} Returns Workflow ID - */ - public async execute( - agreementId: string, - computeDdo: DDO, - consumerAccount: Account, - algorithmDid: string, - algorithm: string, - algorithmMeta?: MetaData - ): Promise { - const { serviceEndpoint } = computeDdo.findServiceByType('compute') - - const workflowId = await this.ocean.brizo.executeService( - agreementId, - serviceEndpoint, - consumerAccount, - algorithmDid, - algorithm, - algorithmMeta - ) - - return workflowId - } - /** * Returns the owner of a asset. * @param {string} did Decentralized ID. diff --git a/src/ocean/OceanCompute.ts b/src/ocean/OceanCompute.ts new file mode 100644 index 0000000..d159a85 --- /dev/null +++ b/src/ocean/OceanCompute.ts @@ -0,0 +1,55 @@ +import { Instantiable, InstantiableConfig } from '../Instantiable.abstract' +import { MetaData } from '../ddo/MetaData' +import Account from './Account' +import { DDO } from '../ddo/DDO' + +/** + * Compute submodule of Ocean Protocol. + */ +export class OceanCompute extends Instantiable { + /** + * Returns the instance of OceanCompute. + * @return {Promise} + */ + public static async getInstance(config: InstantiableConfig): Promise { + const instance = new OceanCompute() + instance.setInstanceConfig(config) + + return instance + } + + /** + * Start the execution of a compute job. + * @param {string} agreementId The service agreement ID. + * @param {string} datasetDid The DID of the dataset asset (of type `dataset`) to run the algorithm on. + * @param {number} serviceIndex ID of the compute service within the dataset DDO. + * @param {Account} consumerAccount The account of the consumer ordering the service. + * @param {string} algorithmDid The DID of the algorithm asset (of type `algorithm`) to run on the asset. + * @param {string} algorithmRaw The raw text of the algorithm to run in the compute job (e.g. a jupyter notebook) or a valid URL to fetch the algorithm. + * @param {MetaData} algorithmMeta Metadata about the algorithm being run if `algorithm` is being used. This is ignored when `algorithmDid` is specified. + * @return {Promise} Returns compute job ID + */ + public async run( + agreementId: string, + datasetDid: string, + serviceIndex: number, + consumerAccount: Account, + algorithmDid: string, + algorithmRaw?: string, + algorithmMeta?: MetaData + ): Promise { + const ddo: DDO = await this.ocean.assets.resolve(datasetDid) + const { serviceEndpoint } = ddo.findServiceById(serviceIndex) + + const jobId = await this.ocean.brizo.computeService( + agreementId, + serviceEndpoint, + consumerAccount, + algorithmDid, + algorithmRaw, + algorithmMeta + ) + + return jobId + } +}