Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Feature/use search #9

Merged
merged 7 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './useConsume'
export * from './useMetadata'
export * from './useCompute'
export * from './useSearch'
28 changes: 28 additions & 0 deletions src/hooks/useCompute/ComputeOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export interface ComputeValue {
entrypoint: string
image: string
tag: string
}
export interface ComputeOption {
name: string
value: ComputeValue
}

export const computeOptions: ComputeOption[] = [
{
name: 'nodejs',
value: {
entrypoint: 'node $ALGO',
image: 'node',
tag: '10'
}
},
{
name: 'python3.7',
value: {
entrypoint: 'python $ALGO',
image: 'oceanprotocol/algo_dockers',
tag: 'python-panda'
}
}
]
76 changes: 76 additions & 0 deletions src/hooks/useCompute/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# `useCompute`

The `OceanProvider` maintains a connection to the Ocean Protocol network in multiple steps:

1. On mount, connect to Aquarius instance right away so any asset metadata can be retrieved before, and independent of any Web3 connections.
2. Once Web3 becomes available, a connection to all Ocean Protocol network components is established.
3. Once Ocean becomes available, spits out some info about it.

Also provides a `useOcean` helper hook to access its context values from any component.

## Usage

```tsx
import React from 'react'
import {
useWeb3,
useOcean,
useMetadata,
useCompute,
computeOptions,
ComputeValue,
readFileContent
} from '@oceanprotocol/react'

const did = 'did:op:0x000000000'

export default function MyComponent() {
// Get web3 from built-in Web3Provider context
const { web3 } = useWeb3()

// Get Ocean instance from built-in OceanProvider context
const { ocean, account } = useOcean()

// Get metadata for this asset
const { title, metadata } = useMetadata(did)


// compute asset
const { compute, computeStep } = useCompute()
// raw code text
const [algorithmRawCode, setAlgorithmRawCode] = useState('')
const [computeContainer, setComputeContainer] = useState<ComputeValue>()
async function handleCompute() {
await consume(did,algorithmRawCode,computeContainer)
}

async function onChangeHandler(event){
const fileText = await readFileContent(files[0])
setAlgorithmRawCode(fileText)
}
async function handleSelectChange(event: any) {
const comType = event.target.value
setComputeContainer(comType)
}

return (
<div>
<h1>{title}</h1>
<p>Price: {web3.utils.fromWei(metadata.main.price)}</p>

<p>Your account: {account}</p>
<label for="computeOptions">Select image to run the algorithm</label>
<select id="computeOptions" onChange={handleSelectChange}>
{computeOptions.map((x) => (
<option value={x.value}>{x.name}</option>
)
}
</select>
<input type="file" name="file" onChange={onChangeHandler}/>
<button onClick={handleCompute}>
{computeStep || 'Start compute job'}
</button>
</div>
)
}
```
2 changes: 2 additions & 0 deletions src/hooks/useCompute/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './useCompute'
export * from './ComputeOptions'
87 changes: 87 additions & 0 deletions src/hooks/useCompute/useCompute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { useState } from 'react'
import { DID, MetaDataAlgorithm } from '@oceanprotocol/squid'
import { useOcean } from '../../providers'
import { ComputeValue } from './ComputeOptions'

interface UseCompute {
compute: (
did: DID,
algorithmRawCode: string,
computeContainer: ComputeValue
) => Promise<void>
computeStep?: number
computeError?: string
}

// TODO: customize for compute
export const computeFeedback: { [key in number]: string } = {
99: 'Decrypting file URL...',
0: '1/3 Asking for agreement signature...',
1: '1/3 Agreement initialized.',
2: '2/3 Asking for two payment confirmations...',
3: '2/3 Payment confirmed. Requesting access...',
4: '3/3 Access granted. Consuming file...'
}
const rawAlgorithmMeta: MetaDataAlgorithm = {
rawcode: `console.log('Hello world'!)`,
format: 'docker-image',
version: '0.1',
container: {
entrypoint: '',
image: '',
tag: ''
}
}

function useCompute(): UseCompute {
const { ocean, account, accountId, config } = useOcean()
const [computeStep, setComputeStep] = useState<number | undefined>()
const [computeError, setComputeError] = useState<string | undefined>()

async function compute(
did: DID | string,
algorithmRawCode: string,
computeContainer: ComputeValue
): Promise<void> {
if (!ocean || !account) return

setComputeError(undefined)

try {
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)
.next((step: number) => setComputeStep(step))

rawAlgorithmMeta.container = computeContainer
rawAlgorithmMeta.rawcode = algorithmRawCode

await ocean.compute.start(
account,
agreement,
undefined,
rawAlgorithmMeta,
computeOutput
)
} catch (error) {
setComputeError(error.message)
} finally {
setComputeStep(undefined)
}
}

return { compute, computeStep, computeError }
}

export { useCompute, UseCompute }
export default UseCompute
1 change: 1 addition & 0 deletions src/hooks/useSearch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useSearch'
69 changes: 69 additions & 0 deletions src/hooks/useSearch/useSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useState } from 'react'
import { Logger } from '@oceanprotocol/squid'
import { useOcean } from '../../providers'
import {
SearchQuery,
Aquarius,
QueryResult
} from '@oceanprotocol/squid/dist/node/aquarius/Aquarius'

// TODO searchText
interface UseSearch {
searchQuery: (query: SearchQuery) => Promise<QueryResult>
getPublishedList: (
account: string,
page: number,
offset: number
) => Promise<QueryResult>
searchError?: string
}

function useSearch(): UseSearch {
const { ocean, account, config } = useOcean()
const [searchError, setSearchError] = useState<string | undefined>()

async function searchQuery(query: SearchQuery): Promise<QueryResult> {
if (!ocean || !account) return

setSearchError(undefined)

try {
const aquarius = new Aquarius(config.aquariusUri as string, Logger)
return await aquarius.queryMetadata(query)
} catch (error) {
setSearchError(error.message)
}
}

async function getPublishedList(
account: string,
page: number,
offset: number
): Promise<QueryResult> {
if (!ocean || !account) return

setSearchError(undefined)

try {
const query = {
page,
offset,
query: {
'publicKey.owner': [account]
},
sort: {
created: -1
}
} as SearchQuery

return await searchQuery(query)
} catch (error) {
setSearchError(error.message)
}
}

return { searchQuery, getPublishedList, searchError }
}

export { useSearch, UseSearch }
export default useSearch
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ import './@types/globals'
export * from './providers'
export * from './hooks'
// export * from './components'

export * from './utils'
13 changes: 13 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function readFileContent(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onerror = () => {
reader.abort()
reject(new DOMException('Problem parsing input file.'))
}
reader.onload = () => {
resolve(reader.result as string)
}
reader.readAsText(file)
})
}