1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

merged compute into subgraph-util-methods

This commit is contained in:
Bogdan Fazakas 2021-04-14 12:08:30 +03:00
commit d52d644850
6 changed files with 187 additions and 64 deletions
.gitignorepackage-lock.jsonpackage.json
src
components
organisms/AssetActions/Compute
pages/History
hooks

2
.gitignore vendored
View File

@ -12,5 +12,5 @@ public/storybook
.artifacts
.vercel
repo-metadata.json
networks-metadata.json
content/networks-metadata.json
src/@types/apollo

6
package-lock.json generated
View File

@ -3625,9 +3625,9 @@
"integrity": "sha512-0dasqU7PL8xZOxvn6ID2IJ6TgQZbkcwtZqiMiYvtsv9+fQr/n13qXYXwkmNEjQb3YL4IeSVUnHehk5KpGRPFWA=="
},
"@oceanprotocol/lib": {
"version": "0.13.0",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.13.0.tgz",
"integrity": "sha512-+LoN16bo1hcqfxsxLV22ZEbObdif0RJXg5GlndlF/VQGNVrSm+XYxUp+si5HCWa8sBeYH/hOSQRnrD8YXnyWrw==",
"version": "0.14.1",
"resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.14.1.tgz",
"integrity": "sha512-NnMt5une64e/ZTEvUQeSs9E9s0Y6TgVni7T/Vhfhnubjrfn/dKkidRYpPpuUr18T2PHfmLtghBlhRLixoYkWKg==",
"requires": {
"@ethereum-navigator/navigator": "^0.5.2",
"@oceanprotocol/contracts": "^0.5.10",

View File

@ -27,7 +27,7 @@
"@coingecko/cryptoformat": "^0.4.2",
"@loadable/component": "^5.14.1",
"@oceanprotocol/art": "^3.0.0",
"@oceanprotocol/lib": "^0.13.0",
"@oceanprotocol/lib": "^0.14.1",
"@oceanprotocol/typographies": "^0.1.0",
"@portis/web3": "^3.0.3",
"@sindresorhus/slugify": "^1.0.0",

View File

@ -13,7 +13,6 @@ import File from '../../../atoms/File'
import Alert from '../../../atoms/Alert'
import Web3Feedback from '../../../molecules/Wallet/Feedback'
import { useSiteMetadata } from '../../../../hooks/useSiteMetadata'
import checkPreviousOrder from '../../../../utils/checkPreviousOrder'
import { useOcean } from '../../../../providers/Ocean'
import { useWeb3 } from '../../../../providers/Web3'
import { usePricing } from '../../../../hooks/usePricing'
@ -27,6 +26,7 @@ import {
getInitialValues,
validationSchema
} from '../../../../models/FormStartComputeDataset'
import { ComputeAlgorithm } from '@oceanprotocol/lib/dist/node/ocean/interfaces/Compute'
import { AssetSelectionAsset } from '../../../molecules/FormFields/AssetSelection'
import { SearchQuery } from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
import axios from 'axios'
@ -299,10 +299,15 @@ export default function Compute({
? selectedAlgorithmAsset.findServiceByType('access')
: selectedAlgorithmAsset.findServiceByType('compute')
const computeAlgorithm: ComputeAlgorithm = {
did: selectedAlgorithmAsset.id,
serviceIndex: serviceAlgo.index,
dataToken: selectedAlgorithmAsset.dataToken
}
const allowed = await ocean.compute.isOrderable(
ddo.id,
computeService.index,
selectedAlgorithmAsset.id
computeAlgorithm
)
Logger.log('[compute] Is data set orderable?', allowed)
@ -350,8 +355,7 @@ export default function Compute({
accountId,
ddo.id,
computeService.index,
undefined,
undefined,
computeAlgorithm,
marketFeeAddress
)
@ -386,6 +390,7 @@ export default function Compute({
return
}
computeAlgorithm.transferTxId = algorithmAssetOrderId
Logger.log('[compute] Starting compute job.')
const output = {}
@ -394,13 +399,10 @@ export default function Compute({
assetOrderId,
ddo.dataToken,
account,
algorithmId,
undefined,
computeAlgorithm,
output,
`${computeService.index}`,
computeService.type,
algorithmAssetOrderId,
selectedAlgorithmAsset.dataToken
computeService.type
)
if (!response) {

View File

@ -5,11 +5,34 @@ import Button from '../../atoms/Button'
import ComputeDetails from './ComputeDetails'
import { ComputeJobMetaData } from '../../../@types/ComputeJobMetaData'
import { Link } from 'gatsby'
import { Logger } from '@oceanprotocol/lib'
import { DDO, Logger, ServiceCommon, ServiceCompute } from '@oceanprotocol/lib'
import Dotdotdot from 'react-dotdotdot'
import Table from '../../atoms/Table'
import { useOcean } from '../../../providers/Ocean'
import { gql, useQuery } from '@apollo/client'
import { useWeb3 } from '../../../providers/Web3'
import { queryMetadata } from '../../../utils/aquarius'
import axios, { CancelToken } from 'axios'
import { ComputeOrders } from '../../../@types/apollo/ComputeOrders'
import web3 from 'web3'
import AssetTitle from '../../molecules/AssetListTitle'
const getComputeOrders = gql`
query ComputeOrders($user: String!) {
tokenOrders(
orderBy: timestamp
orderDirection: desc
where: { payer: $user }
) {
id
serviceId
datatokenId {
address
}
tx
timestamp
}
}
`
function DetailsButton({ row }: { row: ComputeJobMetaData }): ReactElement {
const [isDialogOpen, setIsDialogOpen] = useState(false)
@ -34,7 +57,7 @@ export function Status({ children }: { children: string }): ReactElement {
const columns = [
{
name: 'Data Set',
selector: function getAssetRow(row: ComputeJobMetaData) {
selector: function getAssetRow(row: ComputeAsset) {
return (
<Dotdotdot clamp={2}>
<Link to={`/asset/${row.did}`}>{row.assetName}</Link>
@ -44,84 +67,178 @@ const columns = [
},
{
name: 'Created',
selector: function getTimeRow(row: ComputeJobMetaData) {
selector: function getTimeRow(row: ComputeAsset) {
return <Time date={row.dateCreated} isUnix relative />
}
},
{
name: 'Finished',
selector: function getTimeRow(row: ComputeJobMetaData) {
selector: function getTimeRow(row: ComputeAsset) {
return <Time date={row.dateFinished} isUnix relative />
}
},
{
name: 'Status',
selector: function getStatus(row: ComputeJobMetaData) {
selector: function getStatus(row: ComputeAsset) {
return <Status>{row.statusText}</Status>
}
},
{
name: 'Actions',
selector: function getActions(row: ComputeJobMetaData) {
selector: function getActions(row: ComputeAsset) {
return <DetailsButton row={row} />
}
}
]
async function getAssetMetadata(
queryDtList: string,
metadataCacheUri: string,
cancelToken: CancelToken,
timestamps: number[]
): Promise<DDO[]> {
const queryDid = {
page: 1,
offset: 100,
query: {
query_string: {
query: `(${queryDtList}) AND service.attributes.main.type:dataset AND service.type:compute`,
fields: ['dataToken']
}
}
}
const result = await queryMetadata(queryDid, metadataCacheUri, cancelToken)
return result.results
}
interface ComputeAsset extends ComputeJobMetaData {
did: string
assetName: string
timestamp: number
type: string
}
export default function ComputeJobs(): ReactElement {
const { ocean, account } = useOcean()
const [jobs, setJobs] = useState<ComputeJobMetaData[]>()
const { ocean, account, config } = useOcean()
const { accountId } = useWeb3()
const [isLoading, setIsLoading] = useState(false)
const [jobs, setJobs] = useState<ComputeAsset[]>([])
const { data } = useQuery<ComputeOrders>(getComputeOrders, {
variables: {
user: accountId?.toLowerCase()
}
})
useEffect(() => {
async function getTitle(did: string) {
const ddo = await ocean.metadataCache.retrieveDDO(did)
const metadata = ddo.findServiceByType('metadata')
return metadata.attributes.main.name
}
if (data === undefined || !config?.metadataCacheUri) return
async function getJobs() {
if (!ocean || !account) return
setIsLoading(true)
try {
const orderHistory = await ocean.assets.getOrderHistory(
account,
'compute',
100
)
const jobs: ComputeJobMetaData[] = []
for (let i = 0; i < orderHistory.length; i++) {
const assetName = await getTitle(orderHistory[i].did)
setIsLoading(true)
const dtList = []
const dtTimestamps = []
const computeJobs: ComputeAsset[] = []
for (let i = 0; i < data.tokenOrders.length; i++) {
dtList.push(data.tokenOrders[i].datatokenId.address)
dtTimestamps.push(data.tokenOrders[i].timestamp)
}
const queryDtList = JSON.stringify(dtList)
.replace(/,/g, ' ')
.replace(/"/g, '')
.replace(/(\[|\])/g, '')
try {
const source = axios.CancelToken.source()
const assets = await getAssetMetadata(
queryDtList,
config.metadataCacheUri,
source.token,
dtTimestamps
)
const providers: ServiceCompute[] = []
for (let i = 0; i < data.tokenOrders.length; i++) {
try {
const did = web3.utils
.toChecksumAddress(data.tokenOrders[i].datatokenId.address)
.replace('0x', 'did:op:')
const ddo = assets.filter((x) => x.id === did)[0]
if (!ddo) continue
const service = ddo.service.filter(
(x: ServiceCommon) => x.index === data.tokenOrders[i].serviceId
)[0]
if (!service || service.type !== 'compute') continue
const { serviceEndpoint } = service
const wasProviderQueried =
providers.filter((x) => x.serviceEndpoint === serviceEndpoint)
.length > 0
if (wasProviderQueried) continue
providers.push(service as ServiceCompute)
// eslint-disable-next-line no-empty
} catch (err) {
console.log(err)
}
}
for (let i = 0; i < providers.length; i++) {
const computeJob = await ocean.compute.status(
account,
orderHistory[i].did,
undefined,
undefined,
orderHistory[i].transactionHash,
providers[i],
undefined,
undefined,
false
)
computeJob.forEach((item) => {
jobs.push({
did: orderHistory[i].did,
jobId: item.jobId,
dateCreated: item.dateCreated,
dateFinished: item.dateFinished,
assetName: assetName,
status: item.status,
statusText: item.statusText,
algorithmLogUrl: '',
resultsUrls: []
})
computeJob.sort((a, b) => {
if (a.dateCreated > b.dateCreated) {
return -1
}
if (a.dateCreated < b.dateCreated) {
return 1
}
return 0
})
for (let j = 0; j < computeJob.length; j++) {
const job = computeJob[j]
const did = job.inputDID[0]
const ddo = assets.filter((x) => x.id === did)[0]
if (!ddo) continue
const serviceMetadata = ddo.service.filter(
(x: any) => x.type === 'metadata'
)[0]
const compJob = {
did: did,
jobId: job.jobId,
dateCreated: job.dateCreated,
dateFinished: job.dateFinished,
assetName: serviceMetadata.attributes.main.name,
status: job.status,
statusText: job.statusText,
algorithmLogUrl: '',
resultsUrls: [],
timestamp: data.tokenOrders[i].timestamp,
type: ''
} as ComputeAsset
computeJobs.push(compJob)
}
}
const jobsSorted = jobs.sort((a, b) => {
if (a.dateCreated > b.dateCreated) return -1
if (a.dateCreated < b.dateCreated) return 1
return 0
})
setJobs(jobsSorted)
setJobs(computeJobs)
} catch (error) {
Logger.log(error.message)
} finally {
@ -129,7 +246,7 @@ export default function ComputeJobs(): ReactElement {
}
}
getJobs()
}, [ocean, account])
}, [ocean, account, data, config?.metadataCacheUri])
return (
<Table

View File

@ -1,7 +1,10 @@
import { useState } from 'react'
import { Logger, ServiceCompute } from '@oceanprotocol/lib'
import { MetadataAlgorithm } from '@oceanprotocol/lib/dist/node/ddo/interfaces/MetadataAlgorithm'
import { ComputeJob } from '@oceanprotocol/lib/dist/node/ocean/interfaces/ComputeJob'
import {
ComputeJob,
ComputeAlgorithm
} from '@oceanprotocol/lib/dist/node/ocean/interfaces/Compute'
import { computeFeedback } from '../utils/feedback'
import { useOcean } from '../providers/Ocean'
import { useWeb3 } from '../providers/Web3'
@ -97,6 +100,9 @@ function useCompute(): UseCompute {
setStep(0)
rawAlgorithmMeta.container = computeContainer
rawAlgorithmMeta.rawcode = algorithmRawCode
const computeAlgorithm: ComputeAlgorithm = {
meta: rawAlgorithmMeta
}
const output = {}
if (!orderId) {
const userOwnedTokens = await ocean.accounts.getTokenBalance(
@ -118,8 +124,7 @@ function useCompute(): UseCompute {
accountId,
did,
computeService.index,
undefined,
rawAlgorithmMeta,
computeAlgorithm,
marketFeeAddress
)
setStep(1)
@ -132,8 +137,7 @@ function useCompute(): UseCompute {
orderId,
dataTokenAddress,
account,
undefined,
rawAlgorithmMeta,
computeAlgorithm,
output,
`${computeService.index}`,
computeService.type