From ef064827fd6a53fe1a045d0d86b54ea254e92fa5 Mon Sep 17 00:00:00 2001 From: Andrey Dolgolev Date: Mon, 10 Jan 2022 12:46:46 +0200 Subject: [PATCH] Add correct refreshing status. --- backend/moonstreamapi/routes/dashboards.py | 2 +- crawlers/mooncrawl/mooncrawl/api.py | 17 ++++++---- frontend/pages/dashboard/[dashboardId].js | 7 +++- frontend/src/components/SubscriptionReport.js | 15 ++++++-- frontend/src/core/hooks/usePresignedURL.js | 34 ++++++++++++++----- 5 files changed, 54 insertions(+), 21 deletions(-) diff --git a/backend/moonstreamapi/routes/dashboards.py b/backend/moonstreamapi/routes/dashboards.py index cddc57c7..5c3706fc 100644 --- a/backend/moonstreamapi/routes/dashboards.py +++ b/backend/moonstreamapi/routes/dashboards.py @@ -428,7 +428,7 @@ async def get_dashboard_data_links_handler( ExpiresIn=300, HttpMethod="GET", ) - stats[subscription.id][timescale] = stats_presigned_url + stats[subscription.id][timescale] = {"url": stats_presigned_url} except Exception as err: logger.warning( f"Can't generate S3 presigned url in stats endpoint for Bucket:{MOONSTREAM_S3_SMARTCONTRACTS_ABI_BUCKET}, Key:{result_key} get error:{err}" diff --git a/crawlers/mooncrawl/mooncrawl/api.py b/crawlers/mooncrawl/mooncrawl/api.py index aec43cff..5da86b37 100644 --- a/crawlers/mooncrawl/mooncrawl/api.py +++ b/crawlers/mooncrawl/mooncrawl/api.py @@ -139,22 +139,25 @@ async def status_handler( try: result_key = f'{MOONSTREAM_S3_SMARTCONTRACTS_ABI_PREFIX}/{dashboard.blockchain_by_subscription_id[subscription.resource_data["subscription_type_id"]]}/contracts_data/{subscription.resource_data["address"]}/{stats_update.dashboard_id}/v1/{timescale}.json' + + object = s3_client.head_object( + Bucket=subscription.resource_data["bucket"], Key=result_key + ) + stats_presigned_url = s3_client.generate_presigned_url( "get_object", Params={ - # "IfModifiedSince": datetime(2015, 1, 1), - # "IfUnmodifiedSince": datetime(2015, 1, 1), - "IfMatch": "757ab3614c58b5a184457d34dd104238", "Bucket": subscription.resource_data["bucket"], "Key": result_key, }, ExpiresIn=300, HttpMethod="GET", ) - print("stats_presigned_url", stats_presigned_url) - presigned_urls_response[subscription.id][ - timescale - ] = stats_presigned_url + + presigned_urls_response[subscription.id][timescale] = { + "url": stats_presigned_url, + "headers": {"If-Modified-Since": object["LastModified"]}, + } except Exception as err: logger.warning( f"Can't generate S3 presigned url in stats endpoint for Bucket:{subscription.resource_data['bucket']}, Key:{result_key} get error:{err}" diff --git a/frontend/pages/dashboard/[dashboardId].js b/frontend/pages/dashboard/[dashboardId].js index 233b7db7..445a918d 100644 --- a/frontend/pages/dashboard/[dashboardId].js +++ b/frontend/pages/dashboard/[dashboardId].js @@ -41,6 +41,7 @@ const Analytics = () => { const router = useRouter(); const overlay = useContext(OverlayContext); const { dashboardId } = router.params; + const [refreshingStatus, setRefreshingStatus] = useState(false); const { dashboardCache, dashboardLinksCache, @@ -94,6 +95,7 @@ const Analytics = () => { timeRange: timeRange, dashboardId: dashboardCache.data.id, }); + setRefreshingStatus(true); }; return ( @@ -157,6 +159,7 @@ const Analytics = () => { icon={} /> } variant="ghost" colorScheme="green" @@ -197,9 +200,11 @@ const Analytics = () => { ); diff --git a/frontend/src/components/SubscriptionReport.js b/frontend/src/components/SubscriptionReport.js index fa13275b..52d29fbc 100644 --- a/frontend/src/components/SubscriptionReport.js +++ b/frontend/src/components/SubscriptionReport.js @@ -21,17 +21,26 @@ timeMap[HOUR_KEY] = "hour"; timeMap[DAY_KEY] = "day"; timeMap[WEEK_KEY] = "week"; -const SubscriptionReport = ({ timeRange, url, id, refetchLinks }) => { +const SubscriptionReport = ({ + timeRange, + presignedRequest, + id, + refetchLinks, + refreshingStatus: refreshingStatus, + setRefreshingStatus: setRefreshingStatus, +}) => { const { data, isLoading, failureCount } = usePresignedURL({ - url: url, + presignedRequest: presignedRequest, isEnabled: true, id: id, cacheType: `${timeRange} subscription_report`, requestNewURLCallback: refetchLinks, hideToastOn404: true, + refreshingStatus: refreshingStatus, + setRefreshingStatus: setRefreshingStatus, }); const plotMinW = "250px"; - console.log(data, isLoading); + console.log(refreshingStatus); if (failureCount < 1 && (!data || isLoading)) return ; if (failureCount >= 1 && (!data || isLoading)) { return ( diff --git a/frontend/src/core/hooks/usePresignedURL.js b/frontend/src/core/hooks/usePresignedURL.js index 3d32eaa1..90dfd082 100644 --- a/frontend/src/core/hooks/usePresignedURL.js +++ b/frontend/src/core/hooks/usePresignedURL.js @@ -4,34 +4,50 @@ import { useToast } from "."; import axios from "axios"; const usePresignedURL = ({ - url, + presignedRequest, cacheType, id, requestNewURLCallback, isEnabled, hideToastOn404, + refreshingStatus, + setRefreshingStatus, }) => { const toast = useToast(); const getFromPresignedURL = async () => { - const response = await axios({ - url: url, + let request_parameters = { + url: presignedRequest.url, // You can uncomment this to use mockupsLibrary in development // url: `https://example.com/s3`, + headers: {}, method: "GET", - }); + }; + console.log(presignedRequest); + if ("headers" in presignedRequest) { + Object.keys(presignedRequest.headers).map((key) => { + request_parameters["headers"][key] = presignedRequest.headers[key]; + }); + } + + const response = await axios(request_parameters); return response.data; }; - const { data, isLoading, error, failureCount, refetch, dataUpdatedAt } = - useQuery(["presignedURL", cacheType, id, url], getFromPresignedURL, { + const { data, isLoading, error, failureCount, refetch } = useQuery( + ["presignedURL", cacheType, id, presignedRequest.url], + getFromPresignedURL, + { ...queryCacheProps, refetchOnMount: false, refetchOnWindowFocus: false, refetchOnReconnect: false, staleTime: Infinity, - enabled: isEnabled && url ? true : false, + enabled: isEnabled && presignedRequest.url ? true : false, keepPreviousData: true, + onSuccess: (e) => { + setRefreshingStatus(false); + }, onError: (e) => { if ( e?.response?.data?.includes("Request has expired") || @@ -43,14 +59,14 @@ const usePresignedURL = ({ !hideToastOn404 && toast(error, "error"); } }, - }); + } + ); return { data, isLoading, error, failureCount, - dataUpdatedAt, refetch, }; };