react/src/hooks/useCompute/useCompute.ts

95 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-04-28 13:55:41 +02:00
import { useState } from 'react'
import { DID, MetaDataAlgorithm, Logger } from '@oceanprotocol/squid'
2020-04-28 13:55:41 +02:00
import { useOcean } from '../../providers'
2020-05-04 14:38:41 +02:00
import { ComputeValue } from './ComputeOptions'
2020-05-08 13:11:30 +02:00
import { feedback } from './../../utils'
2020-05-27 16:12:24 +02:00
import { LoggerInstance } from '@oceanprotocol/squid/dist/node/utils/Logger'
2020-04-28 13:55:41 +02:00
interface UseCompute {
2020-05-04 14:38:41 +02:00
compute: (
2020-05-14 14:42:11 +02:00
did: DID | string,
2020-05-04 14:38:41 +02:00
algorithmRawCode: string,
computeContainer: ComputeValue
) => Promise<void>
computeStep?: number
2020-05-08 13:11:30 +02:00
computeStepText?: string
2020-05-04 14:38:41 +02:00
computeError?: string
isLoading: boolean
2020-04-28 13:55:41 +02:00
}
// TODO: customize for compute
2020-05-04 14:51:10 +02:00
export const computeFeedback: { [key in number]: string } = {
2020-05-08 13:11:30 +02:00
...feedback,
2020-05-14 17:07:43 +02:00
4: '3/3 Access granted. Starting job...'
2020-04-28 13:55:41 +02:00
}
const rawAlgorithmMeta: MetaDataAlgorithm = {
2020-05-04 14:38:41 +02:00
rawcode: `console.log('Hello world'!)`,
format: 'docker-image',
version: '0.1',
container: {
entrypoint: '',
image: '',
tag: ''
}
2020-04-28 13:55:41 +02:00
}
function useCompute(): UseCompute {
2020-05-04 14:38:41 +02:00
const { ocean, account, accountId, config } = useOcean()
const [computeStep, setComputeStep] = useState<number | undefined>()
2020-05-08 13:11:30 +02:00
const [computeStepText, setComputeStepText] = useState<string | undefined>()
2020-05-04 14:38:41 +02:00
const [computeError, setComputeError] = useState<string | undefined>()
const [isLoading, setIsLoading] = useState(false)
2020-04-28 13:55:41 +02:00
2020-05-04 14:38:41 +02:00
async function compute(
did: DID | string,
algorithmRawCode: string,
computeContainer: ComputeValue
): Promise<void> {
if (!ocean || !account) return
2020-04-28 13:55:41 +02:00
2020-05-04 14:38:41 +02:00
setComputeError(undefined)
2020-04-28 13:55:41 +02:00
2020-05-04 14:38:41 +02:00
try {
setIsLoading(true)
2020-05-04 14:38:41 +02:00
const computeOutput = {
publishAlgorithmLog: false,
publishOutput: false,
brizoAddress: config.brizoAddress,
brizoUri: config.brizoUri,
metadataUri: config.aquariusUri,
nodeUri: config.nodeUri,
owner: accountId,
secretStoreUri: config.secretStoreUri
}
const agreement = await ocean.compute
.order(account, did as string)
2020-05-11 13:47:21 +02:00
.next((step: number) => {
setComputeStep(step)
setComputeStepText(computeFeedback[step])
})
2020-05-27 16:12:24 +02:00
2020-05-04 14:38:41 +02:00
rawAlgorithmMeta.container = computeContainer
rawAlgorithmMeta.rawcode = algorithmRawCode
2020-05-08 13:11:30 +02:00
setComputeStep(4)
setComputeStepText(computeFeedback[4])
2020-05-04 14:38:41 +02:00
await ocean.compute.start(
account,
agreement,
undefined,
rawAlgorithmMeta,
computeOutput
)
} catch (error) {
2020-05-14 17:07:43 +02:00
Logger.error(error)
2020-05-04 14:38:41 +02:00
setComputeError(error.message)
} finally {
setComputeStep(undefined)
setIsLoading(false)
2020-04-28 13:55:41 +02:00
}
2020-05-04 14:38:41 +02:00
}
2020-04-28 13:55:41 +02:00
return { compute, computeStep, computeStepText, computeError, isLoading }
2020-04-28 13:55:41 +02:00
}
export { useCompute, UseCompute }
export default UseCompute