From 2437b72ea338c9a000974eff85d6e00c7dfce237 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 18 Nov 2020 17:51:42 +0100 Subject: [PATCH] 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 --- src/components/pages/Home.tsx | 91 ++++++++++++++++++----------------- src/utils/aquarius.ts | 53 ++++++++++++++++++++ 2 files changed, 100 insertions(+), 44 deletions(-) create mode 100644 src/utils/aquarius.ts diff --git a/src/components/pages/Home.tsx b/src/components/pages/Home.tsx index e3c5462c2..ed1004759 100644 --- a/src/components/pages/Home.tsx +++ b/src/components/pages/Home.tsx @@ -1,12 +1,8 @@ import React, { ReactElement, useEffect, useState } from 'react' import SearchBar from '../molecules/SearchBar' import styles from './Home.module.css' -import { MetadataCache, Logger } from '@oceanprotocol/lib' import AssetQueryList from '../organisms/AssetQueryList' -import { - QueryResult, - SearchQuery -} from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache' +import { QueryResult } from '@oceanprotocol/lib/dist/node/metadatacache/MetadataCache' import Container from '../atoms/Container' import Loader from '../atoms/Loader' import { useOcean } from '@oceanprotocol/react' @@ -15,6 +11,8 @@ import Bookmarks from '../molecules/Bookmarks' import listPartners from '@oceanprotocol/list-datapartners' import Tooltip from '../atoms/Tooltip' import AssetQueryCarousel from '../organisms/AssetQueryCarousel' +import axios from 'axios' +import { queryMetadata } from '../../utils/aquarius' const partnerAccounts = listPartners .map((partner) => partner.accounts.join(',')) @@ -61,16 +59,6 @@ const queryLatest = { 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() { return (
@@ -79,17 +67,41 @@ function LoaderArea() { ) } -function SectionQuery({ +function SectionQueryResult({ title, - result, - loading, + query, action }: { title: ReactElement | string - result: QueryResult - loading: boolean + query: any action?: ReactElement }) { + const { config } = useOcean() + const [result, setResult] = useState() + 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 (

{title}

@@ -106,37 +118,30 @@ function SectionQuery({ export default function HomePage(): ReactElement { const { config } = useOcean() - const [queryResultLatest, setQueryResultLatest] = useState() const [queryResultPartners, setQueryResultPartners] = useState() - const [queryResultHighest, setQueryResultHighest] = useState() const [loading, setLoading] = useState(true) useEffect(() => { if (!config?.metadataCacheUri) return - // TODO: remove any once ocean.js has nativeSearch typings - async function init() { - const queryResultHighest = await getAssets( - queryHighest as any, - config.metadataCacheUri - ) - setQueryResultHighest(queryResultHighest) + const source = axios.CancelToken.source() - const queryResultPartners = await getAssets( + async function init() { + // TODO: remove any once ocean.js has nativeSearch typings + const queryResultPartners = await queryMetadata( queryPartners as any, - config.metadataCacheUri + config.metadataCacheUri, + source.token ) setQueryResultPartners(queryResultPartners) - - const queryResultLatest = await getAssets( - queryLatest as any, - config.metadataCacheUri - ) - setQueryResultLatest(queryResultLatest) setLoading(false) } init() - }, [config.metadataCacheUri]) + + return () => { + source.cancel() + } + }, [config?.metadataCacheUri]) return ( <> @@ -178,16 +183,14 @@ export default function HomePage(): ReactElement {
- - All data sets → diff --git a/src/utils/aquarius.ts b/src/utils/aquarius.ts new file mode 100644 index 000000000..848d3a895 --- /dev/null +++ b/src/utils/aquarius.ts @@ -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 { + try { + const response: AxiosResponse = 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) + } + } +}