diff --git a/src/lib/api/fetchGrowThePie.ts b/src/lib/api/fetchGrowThePie.ts index 3ad53e8ccfd..08a4c40f5d3 100644 --- a/src/lib/api/fetchGrowThePie.ts +++ b/src/lib/api/fetchGrowThePie.ts @@ -11,25 +11,39 @@ type DataItem = { const TXCOSTS_MEDIAN_USD = "txcosts_median_usd" const TXCOUNT = "txcount" +const ACTIVE_ADDRESSES = "aa_last7d" export const fetchGrowThePie = async (): Promise => { const url = "https://api.growthepie.xyz/v1/fundamentals.json" - const response = await fetch(url) + const response = await fetch(url, { cache: "no-store" }) if (!response.ok) { console.log(response.status, response.statusText) throw new Error("Failed to fetch growthepie data") } const data: DataItem[] = await response.json() - const mostRecentDate = data.reduce((latest, item) => { + // Get the date 7 days ago + const sevenDaysAgo = new Date() + sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7) + + // Filter data to only include the last 7 days and the metrics we need + const filteredData = data.filter((item) => { + const itemDate = new Date(item.date) + return ( + itemDate >= sevenDaysAgo && + [TXCOSTS_MEDIAN_USD, TXCOUNT, ACTIVE_ADDRESSES].includes(item.metric_key) + ) + }) + + const mostRecentDate = filteredData.reduce((latest, item) => { const itemDate = new Date(item.date) return itemDate > new Date(latest) ? item.date : latest - }, data[0].date) + }, filteredData[0].date) - const activeAddresses = data + const activeAddresses = filteredData .filter((item) => item.date === mostRecentDate) - .filter((item) => item.metric_key === "aa_last7d") + .filter((item) => item.metric_key === ACTIVE_ADDRESSES) .reduce((acc, item) => { return { ...acc, @@ -37,7 +51,7 @@ export const fetchGrowThePie = async (): Promise => { } }, {}) - const mostRecentData = data.filter( + const mostRecentData = filteredData.filter( (item) => item.date === mostRecentDate && [TXCOSTS_MEDIAN_USD, TXCOUNT].includes(item.metric_key)