-
Notifications
You must be signed in to change notification settings - Fork 4
feat: implement cache subgraph integration with fallback mechanism #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for pintomoney ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
| } | ||
| }) | ||
| .filter((v) => v !== undefined); | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stable function reference here. either pull this out, or in a useCallback()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but remove config.resultTimestamp() as a dependency
| const historicalQueryKey = [ | ||
| `cache_historical_${keyName}`, | ||
| { chainId, season: currentSeason, variables: historicalVars }, | ||
| ]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memoize this
| let historicalData: SeasonalChartData[] | undefined = historical.data; | ||
|
|
||
| // Fill sparse data gaps | ||
| if (sparseData && historical.data) { | ||
| let lastValue: SeasonalChartData; | ||
| historicalData = historical.data.flatMap((v, i) => { | ||
| let returnData: SeasonalChartData[] = []; | ||
| const gapSize = i === 0 ? 0 : v.season - lastValue.season; | ||
| if (gapSize > 1) { | ||
| returnData = Array.from({ length: gapSize - 1 }, (_, i) => ({ | ||
| ...lastValue, | ||
| season: lastValue.season + i + 1, | ||
| timestamp: new Date(lastValue.timestamp.getTime() + 3600 * 1000 * (i + 1)), | ||
| })); | ||
| } | ||
| returnData = [...returnData, v]; | ||
| if (i === historical.data?.length - 1 && v.season < config.toSeason) { | ||
| const missingSize = config.toSeason - v.season; | ||
| const missingData: SeasonalChartData[] = Array.from({ length: missingSize }, (_, i) => ({ | ||
| ...v, | ||
| season: v.season + i + 1, | ||
| timestamp: new Date(v.timestamp.getTime() + 3600 * 1000 * (i + 1)), | ||
| })); | ||
| returnData = [...returnData, ...missingData]; | ||
| } | ||
| lastValue = v; | ||
| return returnData; | ||
| }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs stable reference. try to avoid 'let'
| const lastFetchedTimestamp = queryInfo?.state?.dataUpdatedAt; | ||
| return config.convertResult(v, lastFetchedTimestamp ? new Date(lastFetchedTimestamp) : new Date()); | ||
| }); | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs stable reference
| }); | ||
| } | ||
|
|
||
| const currentQueryKey = [`cache_current_${keyName}`, { chainId, season: currentSeason, variables: currentVars }]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stable reference
| export function useCacheWithSGFallback(cacheResult: UseSeasonalResult, sgResult: UseSeasonalResult): UseSeasonalResult { | ||
| // If cache is still loading, show loading state | ||
| if (cacheResult.isLoading) { | ||
| return { isLoading: true, isError: false, data: undefined }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pull this out to a stable ref
| document: BeanstalkSeasonalFieldDocument, | ||
| buildWhere: (from, to) => buildSeasonRangeWhere(from, to, { field }), | ||
| resultKey: "fieldHourlySnapshots", | ||
| resultTimestamp: (entry) => new Date(Number(entry.createdAt) * 1000), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create stable reference these. Maybe pull this out into a utils folder?
parseSGResultTimestamp<T extends { createdAt: string }>({ createdAt }: T) => {...}
This pull request introduces a new "cache" subgraph and updates the application to use new cache-based queries and hooks for seasonal data throughout the explorer and stats pages. It also adds several new GraphQL query files specifically targeting the cache subgraph, and updates configuration and constants to support the new endpoints.
The most important changes are:
Cache Subgraph Integration:
subgraphsconstant for all supported chains insrc/constants/subgraph.ts.codegen.tsto generate types and operations for both the main and cache subgraphs, and to separate cache-related.graphqlfiles.GraphQL Query Additions for Cache:
.cache.graphqlquery files undersrc/queries/beanstalk/and its subfolders, targeting cache data for advanced charts, seasonal tables, silo assets, field data, and active farmers. [1] [2] [3] [4] [5] [6] [7]Migration to Cache-based Hooks in Seasonal Data:
FarmerExplorer.tsx,FieldExplorer.tsx,SiloExplorer.tsx,SiloStats.tsx,Temperature.tsx) to use new*Cachehooks fromseasonalDataHooks, ensuring all seasonal data is now fetched from the cache subgraph. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17]These changes collectively improve data reliability and performance by centralizing seasonal data queries on a dedicated cache subgraph and ensure the frontend is aligned with the new backend structure.