Compute using a raw algorithm on a Data Set

Compute using a raw algorithm on a Data Set

Requirements

This is a continuation of the React App Setup tutorial, so make sure you have done all the steps described in there.

  1. React App Setup

Open src/Compute.js from your marketplace/ folder.

Define Raw Code

Sometime, you just need to quickly run an test algorithm. Instead of publishing it as an asset, you can use the code directly.

To do that, we are going to use a textbox for copy/paste and a button to show/hide it.

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
        <div>
          <button onClick={showDivAlgo}>Show/Hide Raw Algorithm</button>
          <p style={{ display: isAlgoInputVisible ? 'block' : 'none' }}>
            <Label htmlFor="jobStatus">Raw Algorithm</Label>
            <textarea
              style={{ width: '100%' }}
              rows="10"
              value={textRawAlgo}
              onChange={updateRawAlgoCode}
            />
          </p>
        </div>

Define Algorithm MetaData

We need to define all the requirments for the algorithm:

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/asset-compute.js
 */
export const rawAlgoMeta = {
  rawcode: `console.log('Hello world'!)`,
  format: 'docker-image',
  version: '0.1',
  container: {
    entrypoint: 'node $ALGO',
    image: 'node',
    tag: '10'
  }
}

and them import it to our Compute.js:

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
import { assetAlgo, rawAlgoMeta } from './asset-compute'

Define Compute Output

Let’s define some options for our upcoming job:

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
        <p>
          <Label htmlFor="publishOutputState">
            <input
              type="checkbox"
              id="publishOutputState"
              checked={publishOutputState}
              onChange={handlePublishOutputState}
            />
            Publish Output into the Marketplace
          </Label>
          <Label htmlFor="publishLogState">
            <input
              type="checkbox"
              id="publishLogState"
              checked={publishLogState}
              onChange={handlePublishLogState}
            />
            Publish Algorithm Logs into the Marketplace
          </Label>
        </p>

and use them

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
      const computeOutput = {
        publishAlgorithmLog: publishLogState,
        publishOutput: publishOutputState,
        brizoAddress: ocean.config.brizoAddress,
        brizoUri: ocean.config.brizoUri,
        metadataUri: ocean.config.aquariusUri,
        nodeUri: ocean.config.nodeUri,
        owner: accounts[0].getId(),
        secretStoreUri: ocean.config.secretStoreUri
      }

Order the dataset

Next, we have to order the dataset that we are going to compute upon. We are going to use the ddoAssetId, which was set during publishing of the asset.

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
      const agreement = await ocean.compute.order(accounts[0], ddoAssetId)

Start the compute job

We need a function to start the job:

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
  async function startCompute(algorithmId, algorithmMeta) {
    try {
      const accounts = await ocean.accounts.list()
      const computeOutput = {
        publishAlgorithmLog: publishLogState,
        publishOutput: publishOutputState,
        brizoAddress: ocean.config.brizoAddress,
        brizoUri: ocean.config.brizoUri,
        metadataUri: ocean.config.aquariusUri,
        nodeUri: ocean.config.nodeUri,
        owner: accounts[0].getId(),
        secretStoreUri: ocean.config.secretStoreUri
      }
      console.log(computeOutput)
      // order the compute service
      const agreement = await ocean.compute.order(accounts[0], ddoAssetId)
      setAgreementId(agreement)
      // start a compute job
      const status = await ocean.compute.start(
        accounts[0],
        agreement,
        algorithmId,
        algorithmMeta,
        computeOutput
      )
      setJobId(status.jobId)
      console.log(status)
      alert('Compute job created. You can query for its status now.')
    } catch (error) {
      console.error(error.message)
    }
  }

Get the pasted code:

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
  // order and start the compute service with a passed raw algorithm
  async function startWithRawAlgo() {
    rawAlgoMeta.rawcode = textRawAlgo
    return startCompute(undefined, rawAlgoMeta)
  }

The last thing we need is a button inside the render() function:

/**
 * https://github.com/oceanprotocol/react-tutorial/blob/107d1fa7d0c583cc8042339f1f5090ff9ee0920b/src/Compute.js
 */
        <button onClick={startWithRawAlgo} disabled={!ddoAssetId}>
          Order and start compute service with raw algorithm
        </button>
      </ComputeSection>

** Notice that the button will be disabled if there were no previous published Datasets.

Move on to Get Status of a Compute Job.