mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
new ocean.compute interface
This commit is contained in:
parent
619cd42db7
commit
5bad5fa9ec
@ -36,7 +36,7 @@ export class Brizo extends Instantiable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public getComputeEndpoint() {
|
public getComputeEndpoint() {
|
||||||
return `${this.url}${apiPath}/exec`
|
return `${this.url}${apiPath}/compute`
|
||||||
}
|
}
|
||||||
|
|
||||||
public async initializeServiceAgreement(
|
public async initializeServiceAgreement(
|
||||||
@ -96,45 +96,64 @@ export class Brizo extends Instantiable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async computeService(
|
public async computeService(
|
||||||
agreementId: string,
|
method: string,
|
||||||
serviceEndpoint: string,
|
serviceAgreementId: string,
|
||||||
account: Account,
|
consumerAccount: Account,
|
||||||
algorithmDid: string,
|
algorithmDid?: string,
|
||||||
algorithm: string,
|
algorithmMeta?: MetaData,
|
||||||
algorithmMeta?: MetaData
|
jobId?: string
|
||||||
): Promise<string> {
|
): Promise<any> {
|
||||||
const signature = await this.createSignature(account, agreementId)
|
const signature = await this.createSignature(consumerAccount, serviceAgreementId)
|
||||||
|
const address = consumerAccount.getId()
|
||||||
|
|
||||||
let url = serviceEndpoint
|
// construct Brizo URL
|
||||||
|
let url = this.getComputeEndpoint()
|
||||||
url += `&signature=${signature}`
|
url += `&signature=${signature}`
|
||||||
url += `&serviceAgreementId=${noZeroX(agreementId)}`
|
url += `&consumerAddress=${address}`
|
||||||
url += `&consumerAddress=${account.getId()}`
|
url += `&serviceAgreementId=${noZeroX(serviceAgreementId)}`
|
||||||
url += `&algorithmDID=${algorithmDid}`
|
url += algorithmDid && `&algorithmDid=${algorithmDid}`
|
||||||
url += `&algorithm=${algorithm}`
|
url += algorithmMeta && `&algorithmMeta=${algorithmMeta}`
|
||||||
url += `&algorithmMeta=${algorithmMeta}`
|
url += jobId && `&jobId=${jobId}`
|
||||||
|
|
||||||
const result: { jobId: string } = await this.ocean.utils.fetch
|
// switch fetch method
|
||||||
.post(url, '')
|
let fetch
|
||||||
|
|
||||||
|
switch (method) {
|
||||||
|
case 'post':
|
||||||
|
fetch = this.ocean.utils.fetch.post(url, '')
|
||||||
|
break
|
||||||
|
case 'put':
|
||||||
|
fetch = this.ocean.utils.fetch.put(url, '')
|
||||||
|
break
|
||||||
|
case 'delete':
|
||||||
|
fetch = this.ocean.utils.fetch.delete(url)
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
fetch = this.ocean.utils.fetch.get(url)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await fetch
|
||||||
.then((response: any) => {
|
.then((response: any) => {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return response.json()
|
return response.json()
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
'Executing compute job failed:',
|
'Compute job failed:',
|
||||||
response.status,
|
response.status,
|
||||||
response.statusText
|
response.statusText
|
||||||
)
|
)
|
||||||
|
|
||||||
return null
|
return null
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch((error: Error) => {
|
||||||
this.logger.error('Error executing compute job')
|
this.logger.error('Error with compute job')
|
||||||
this.logger.error(error)
|
this.logger.error(error.message)
|
||||||
throw error
|
throw error
|
||||||
})
|
})
|
||||||
|
|
||||||
return result.jobId
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createSignature(account: Account, agreementId: string): Promise<string> {
|
public async createSignature(account: Account, agreementId: string): Promise<string> {
|
||||||
|
@ -367,7 +367,7 @@ export class OceanAssets extends Instantiable {
|
|||||||
/**
|
/**
|
||||||
* Returns the owner of a asset.
|
* Returns the owner of a asset.
|
||||||
* @param {string} did Decentralized ID.
|
* @param {string} did Decentralized ID.
|
||||||
* @return {Promise<string>} Returns Agreement ID
|
* @return {Promise<string>} Returns Account ID
|
||||||
*/
|
*/
|
||||||
public async owner(did: string): Promise<string> {
|
public async owner(did: string): Promise<string> {
|
||||||
const ddo = await this.resolve(did)
|
const ddo = await this.resolve(did)
|
||||||
|
@ -3,6 +3,22 @@ import { MetaData } from '../ddo/MetaData'
|
|||||||
import Account from './Account'
|
import Account from './Account'
|
||||||
import { DDO } from '../ddo/DDO'
|
import { DDO } from '../ddo/DDO'
|
||||||
|
|
||||||
|
export interface ComputeJobStatus {
|
||||||
|
owner: string
|
||||||
|
agreementId: string
|
||||||
|
jobId: string
|
||||||
|
dateCreated: string
|
||||||
|
dateFinished: string
|
||||||
|
status: boolean
|
||||||
|
statusText: string
|
||||||
|
configlogUrl: string
|
||||||
|
publishlogUrl: string
|
||||||
|
algologUrl: string
|
||||||
|
outputsUrl: string[]
|
||||||
|
ddo?: DDO
|
||||||
|
did?: string
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute submodule of Ocean Protocol.
|
* Compute submodule of Ocean Protocol.
|
||||||
*/
|
*/
|
||||||
@ -18,38 +34,130 @@ export class OceanCompute extends Instantiable {
|
|||||||
return instance
|
return instance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts an order of a compute service that is defined in an asset's services.
|
||||||
|
* @param {string} datasetDid The DID of the dataset asset (of type `dataset`) to run the algorithm on.
|
||||||
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
||||||
|
* @return {Promise<string>} Returns The service agreement ID, representation of `bytes32` ID.
|
||||||
|
*/
|
||||||
|
public async order(datasetDid: string, consumerAccount: Account): Promise<string> {
|
||||||
|
const ddo: DDO = await this.ocean.assets.resolve(datasetDid)
|
||||||
|
const { index } = ddo.findServiceByType('compute')
|
||||||
|
|
||||||
|
const agreementId = await this.ocean.assets.order(
|
||||||
|
datasetDid,
|
||||||
|
index,
|
||||||
|
consumerAccount
|
||||||
|
)
|
||||||
|
|
||||||
|
return agreementId
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the execution of a compute job.
|
* Start the execution of a compute job.
|
||||||
* @param {string} agreementId The service agreement ID.
|
* @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 {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} 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.
|
* @param {MetaData} algorithmMeta Metadata about the algorithm being run if `algorithm` is being used. This is ignored when `algorithmDid` is specified.
|
||||||
* @return {Promise<string>} Returns compute job ID
|
* @return {Promise<string>} Returns compute job ID
|
||||||
*/
|
*/
|
||||||
public async run(
|
public async start(
|
||||||
agreementId: string,
|
agreementId: string,
|
||||||
datasetDid: string,
|
|
||||||
serviceIndex: number,
|
|
||||||
consumerAccount: Account,
|
consumerAccount: Account,
|
||||||
algorithmDid: string,
|
algorithmDid?: string,
|
||||||
algorithmRaw?: string,
|
|
||||||
algorithmMeta?: MetaData
|
algorithmMeta?: MetaData
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const ddo: DDO = await this.ocean.assets.resolve(datasetDid)
|
const { jobId } = await this.ocean.brizo.computeService(
|
||||||
const { serviceEndpoint } = ddo.findServiceById(serviceIndex)
|
'post',
|
||||||
|
|
||||||
const jobId = await this.ocean.brizo.computeService(
|
|
||||||
agreementId,
|
agreementId,
|
||||||
serviceEndpoint,
|
|
||||||
consumerAccount,
|
consumerAccount,
|
||||||
algorithmDid,
|
algorithmDid,
|
||||||
algorithmRaw,
|
|
||||||
algorithmMeta
|
algorithmMeta
|
||||||
)
|
)
|
||||||
|
|
||||||
return jobId
|
return jobId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ends a running compute job.
|
||||||
|
* @param {string} agreementId The service agreement ID.
|
||||||
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
||||||
|
* @param {string} jobId The ID of the compute job to be stopped
|
||||||
|
* @return {Promise<ComputeJobStatus>} Returns the new status of a job
|
||||||
|
*/
|
||||||
|
public async stop(
|
||||||
|
agreementId: string,
|
||||||
|
consumerAccount: Account,
|
||||||
|
jobId: string
|
||||||
|
): Promise<string> {
|
||||||
|
const status = await this.ocean.brizo.computeService(
|
||||||
|
'put',
|
||||||
|
agreementId,
|
||||||
|
consumerAccount,
|
||||||
|
jobId
|
||||||
|
)
|
||||||
|
|
||||||
|
return status
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a compute job and all resources associated with the job. If job is running it will be stopped first.
|
||||||
|
* @param {string} agreementId The service agreement ID.
|
||||||
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
||||||
|
* @param {string} jobId The ID of the compute job to be stopped
|
||||||
|
* @return {Promise<ComputeJobStatus>} Returns the new status of a job
|
||||||
|
*/
|
||||||
|
public async delete(
|
||||||
|
agreementId: string,
|
||||||
|
consumerAccount: Account,
|
||||||
|
jobId: string
|
||||||
|
): Promise<string> {
|
||||||
|
const status = await this.ocean.brizo.computeService(
|
||||||
|
'delete',
|
||||||
|
agreementId,
|
||||||
|
consumerAccount,
|
||||||
|
jobId
|
||||||
|
)
|
||||||
|
|
||||||
|
return status
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ends a running compute job and starts it again.
|
||||||
|
* @param {string} agreementId The service agreement ID.
|
||||||
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
||||||
|
* @param {string} jobId The ID of the compute job to be stopped
|
||||||
|
* @return {Promise<ComputeJobStatus>} Returns the new status of a job
|
||||||
|
*/
|
||||||
|
public async restart(
|
||||||
|
agreementId: string,
|
||||||
|
jobId: string,
|
||||||
|
consumerAccount: Account
|
||||||
|
): Promise<string> {
|
||||||
|
await this.stop(agreementId, consumerAccount, jobId)
|
||||||
|
const result = await this.start(agreementId, consumerAccount, jobId)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns information about the status of all compute jobs, or a single compute job.
|
||||||
|
* @param {Account} consumerAccount The account of the consumer ordering the service.
|
||||||
|
* @param {string} agreementId The service agreement ID.
|
||||||
|
* @param {string} jobId The ID of the compute job to be stopped
|
||||||
|
* @return {Promise<ComputeJobStatus>} Returns the status
|
||||||
|
*/
|
||||||
|
public async status(
|
||||||
|
consumerAccount: Account,
|
||||||
|
agreementId?: string,
|
||||||
|
jobId?: string
|
||||||
|
): Promise<ComputeJobStatus[]> {
|
||||||
|
const status = await this.ocean.brizo.computeService(
|
||||||
|
'get',
|
||||||
|
agreementId,
|
||||||
|
consumerAccount,
|
||||||
|
jobId
|
||||||
|
)
|
||||||
|
|
||||||
|
return status
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,6 +43,15 @@ export class WebServiceConnector extends Instantiable {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public delete(url: string): Promise<Response> {
|
||||||
|
return this.fetch(url, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {
|
||||||
|
'Content-type': 'application/json'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
public async downloadFile(
|
public async downloadFile(
|
||||||
url: string,
|
url: string,
|
||||||
destination?: string,
|
destination?: string,
|
||||||
|
Loading…
Reference in New Issue
Block a user