Skip to content

Commit

Permalink
fix: strategyDailySnapshots pagination strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
pocin committed Apr 2, 2024
1 parent af6603f commit 12b2033
Showing 1 changed file with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,28 @@ async function fetchStrategyHourlySnapshots() {
async function fetchStrategyDailySnapshots() {
const now = new Date();
// the largest value from the chart time range selector 1W | 1M | 1Y
let since = Math.floor((now.getTime() - ONE_YEAR_MS) / 1000).toString();
const since = Math.floor((now.getTime() - ONE_YEAR_MS) / 1000).toString();
const result: V2StrategySnapshot[] = [];
const itemsPerPage = 1000; // current max page size
const MAX_PAGE_SIZE = 1000; // current max page size
let skip = 0;
while (true) {
// we could be missing data with this pagination strategy if
// the dataset contain snapshots for different strats
// created at the exact same timestamp
// and all such snapshots do not fit in the same page
// imho very unlikely, but possible?
// solution would be to use timestamp_GTE: ${since}
// and deduplicate two consecutive pages
const query = `
query {
strategyDailySnapshots(first: ${itemsPerPage},
strategyDailySnapshots(first: ${MAX_PAGE_SIZE},
orderBy: timestamp,
orderDirection: asc,
where: {timestamp_gt: ${since}}) {
where: {timestamp_gt: ${since}}
skip: ${skip}) {
${QUERIED_FIELDS}
}
}`;
const page = await fetchGenericSubgraph<FetchV2StrategyDailySnapshotResponse>(env.subgraph.templeV2, query);
const itemsOnPage = page.data?.strategyDailySnapshots.length ?? 0;
if (page.data) {
result.push(...page.data.strategyDailySnapshots);
const latestSnapshot = page.data.strategyDailySnapshots.at(-1);
if (!latestSnapshot) break;
since = latestSnapshot.timestamp;
skip += itemsOnPage
}
if (itemsOnPage < itemsPerPage) {
if (itemsOnPage < MAX_PAGE_SIZE) {
break;
}
}
Expand Down

0 comments on commit 12b2033

Please sign in to comment.