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

directly hit Aquarius API for front-page queries (#262)

* directly hit Aquarius for front-page queries

* refactor, use custom queryMetadata util

* parallelize front-page queries
This commit is contained in:
Matthias Kretschmann 2020-11-18 17:51:42 +01:00 committed by GitHub
parent 476d13f009
commit 2437b72ea3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 44 deletions

View File

@ -1,12 +1,8 @@
import React, { ReactElement, useEffect, useState } from 'react' import React, { ReactElement, useEffect, useState } from 'react'
import SearchBar from '../molecules/SearchBar' import SearchBar from '../molecules/SearchBar'
import styles from './Home.module.css' import styles from './Home.module.css'
import { MetadataCache, Logger } from '@oceanprotocol/lib'
import AssetQueryList from '../organisms/AssetQueryList' import AssetQueryList from '../organisms/AssetQueryList'
import { import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
QueryResult,
SearchQuery
} from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
import Container from '../atoms/Container' import Container from '../atoms/Container'
import Loader from '../atoms/Loader' import Loader from '../atoms/Loader'
import { useOcean } from '@oceanprotocol/react' import { useOcean } from '@oceanprotocol/react'
@ -15,6 +11,8 @@ import Bookmarks from '../molecules/Bookmarks'
import listPartners from '@oceanprotocol/list-datapartners' import listPartners from '@oceanprotocol/list-datapartners'
import Tooltip from '../atoms/Tooltip' import Tooltip from '../atoms/Tooltip'
import AssetQueryCarousel from '../organisms/AssetQueryCarousel' import AssetQueryCarousel from '../organisms/AssetQueryCarousel'
import axios from 'axios'
import { queryMetadata } from '../../utils/aquarius'
const partnerAccounts = listPartners const partnerAccounts = listPartners
.map((partner) => partner.accounts.join(',')) .map((partner) => partner.accounts.join(','))
@ -61,16 +59,6 @@ const queryLatest = {
sort: { created: -1 } sort: { created: -1 }
} }
async function getAssets(query: SearchQuery, metadataCacheUri: string) {
try {
const metadataCache = new MetadataCache(metadataCacheUri, Logger)
const result = await metadataCache.queryMetadata(query)
return result
} catch (error) {
Logger.error(error.message)
}
}
function LoaderArea() { function LoaderArea() {
return ( return (
<div className={styles.loaderWrap}> <div className={styles.loaderWrap}>
@ -79,17 +67,41 @@ function LoaderArea() {
) )
} }
function SectionQuery({ function SectionQueryResult({
title, title,
result, query,
loading,
action action
}: { }: {
title: ReactElement | string title: ReactElement | string
result: QueryResult query: any
loading: boolean
action?: ReactElement action?: ReactElement
}) { }) {
const { config } = useOcean()
const [result, setResult] = useState<QueryResult>()
const [loading, setLoading] = useState(true)
useEffect(() => {
if (!config?.metadataCacheUri) return
const source = axios.CancelToken.source()
async function init() {
// TODO: remove any once ocean.js has nativeSearch typings
const result = await queryMetadata(
query as any,
config.metadataCacheUri,
source.token
)
setResult(result)
setLoading(false)
}
init()
return () => {
source.cancel()
}
}, [config?.metadataCacheUri, query])
return ( return (
<section className={styles.section}> <section className={styles.section}>
<h3>{title}</h3> <h3>{title}</h3>
@ -106,37 +118,30 @@ function SectionQuery({
export default function HomePage(): ReactElement { export default function HomePage(): ReactElement {
const { config } = useOcean() const { config } = useOcean()
const [queryResultLatest, setQueryResultLatest] = useState<QueryResult>()
const [queryResultPartners, setQueryResultPartners] = useState<QueryResult>() const [queryResultPartners, setQueryResultPartners] = useState<QueryResult>()
const [queryResultHighest, setQueryResultHighest] = useState<QueryResult>()
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
useEffect(() => { useEffect(() => {
if (!config?.metadataCacheUri) return if (!config?.metadataCacheUri) return
// TODO: remove any once ocean.js has nativeSearch typings const source = axios.CancelToken.source()
async function init() {
const queryResultHighest = await getAssets(
queryHighest as any,
config.metadataCacheUri
)
setQueryResultHighest(queryResultHighest)
const queryResultPartners = await getAssets( async function init() {
// TODO: remove any once ocean.js has nativeSearch typings
const queryResultPartners = await queryMetadata(
queryPartners as any, queryPartners as any,
config.metadataCacheUri config.metadataCacheUri,
source.token
) )
setQueryResultPartners(queryResultPartners) setQueryResultPartners(queryResultPartners)
const queryResultLatest = await getAssets(
queryLatest as any,
config.metadataCacheUri
)
setQueryResultLatest(queryResultLatest)
setLoading(false) setLoading(false)
} }
init() init()
}, [config.metadataCacheUri])
return () => {
source.cancel()
}
}, [config?.metadataCacheUri])
return ( return (
<> <>
@ -178,16 +183,14 @@ export default function HomePage(): ReactElement {
<Bookmarks /> <Bookmarks />
</section> </section>
<SectionQuery <SectionQueryResult
title="Highest Liquidity Pools" title="Highest Liquidity Pools"
loading={loading} query={queryHighest}
result={queryResultHighest}
/> />
<SectionQuery <SectionQueryResult
title="New Data Sets" title="New Data Sets"
loading={loading} query={queryLatest}
result={queryResultLatest}
action={ action={
<Button style="text" to="/search"> <Button style="text" to="/search">
All data sets All data sets

53
src/utils/aquarius.ts Normal file
View File

@ -0,0 +1,53 @@
import { DDO, Logger } from '@oceanprotocol/lib'
import {
QueryResult,
SearchQuery
} from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache'
import axios, { CancelToken, AxiosResponse } from 'axios'
// TODO: import directly from ocean.js somehow.
// Transforming Aquarius' direct response is needed for getting actual DDOs
// and not just strings of DDOs. For now, taken from
// https://github.com/oceanprotocol/ocean.js/blob/main/src/metadatacache/MetadataCache.ts#L361-L375
export function transformQueryResult(
{
results,
page,
total_pages: totalPages,
total_results: totalResults
}: any = {
result: [],
page: 0,
total_pages: 0,
total_results: 0
}
): QueryResult {
return {
results: (results || []).map((ddo: DDO) => new DDO(ddo as DDO)),
page,
totalPages,
totalResults
}
}
export async function queryMetadata(
query: SearchQuery,
metadataCacheUri: string,
cancelToken: CancelToken
): Promise<QueryResult> {
try {
const response: AxiosResponse<QueryResult> = await axios.post(
`${metadataCacheUri}/api/v1/aquarius/assets/ddo/query`,
{ ...query, cancelToken }
)
if (!response || response.status !== 200 || !response.data) return
return transformQueryResult(response.data)
} catch (error) {
if (axios.isCancel(error)) {
Logger.log(error.message)
} else {
Logger.error(error.message)
}
}
}