-
Notifications
You must be signed in to change notification settings - Fork 16
refactor(ENSAnalytics): Renaming ..items.. to ..records..
#1382
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@namehash/ens-referrals": minor | ||
| "@ensnode/ensnode-sdk": minor | ||
| "ensapi": minor | ||
| --- | ||
|
|
||
| Renamed `itemsPerPage` to `recordsPerPage` and `paginationContext` to `pageContext` in referrer leaderboard APIs to align with registrar actions terminology. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,7 @@ export interface ReferrerLeaderboardPageParams { | |
| * @invariant Must be a positive integer (>= 1) and less than or equal to {@link REFERRERS_PER_LEADERBOARD_PAGE_MAX} | ||
| * @default {@link REFERRERS_PER_LEADERBOARD_PAGE_DEFAULT} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if we could standardize the use of this constant too. I think no need for a distinct constant for the referrer's leaderboard page. |
||
| */ | ||
| itemsPerPage?: number; | ||
| recordsPerPage?: number; | ||
| } | ||
|
|
||
| const validateReferrerLeaderboardPageParams = (params: ReferrerLeaderboardPageParams): void => { | ||
|
|
@@ -41,17 +41,17 @@ const validateReferrerLeaderboardPageParams = (params: ReferrerLeaderboardPagePa | |
| `Invalid ReferrerLeaderboardPageParams: ${params.page}. page must be a positive integer.`, | ||
| ); | ||
| } | ||
| if (params.itemsPerPage !== undefined && !isPositiveInteger(params.itemsPerPage)) { | ||
| if (params.recordsPerPage !== undefined && !isPositiveInteger(params.recordsPerPage)) { | ||
| throw new Error( | ||
| `Invalid ReferrerLeaderboardPageParams: ${params.itemsPerPage}. itemsPerPage must be a positive integer.`, | ||
| `Invalid ReferrerLeaderboardPageParams: ${params.recordsPerPage}. recordsPerPage must be a positive integer.`, | ||
| ); | ||
| } | ||
| if ( | ||
| params.itemsPerPage !== undefined && | ||
| params.itemsPerPage > REFERRERS_PER_LEADERBOARD_PAGE_MAX | ||
| params.recordsPerPage !== undefined && | ||
| params.recordsPerPage > REFERRERS_PER_LEADERBOARD_PAGE_MAX | ||
| ) { | ||
| throw new Error( | ||
| `Invalid ReferrerLeaderboardPageParams: ${params.itemsPerPage}. itemsPerPage must be less than or equal to ${REFERRERS_PER_LEADERBOARD_PAGE_MAX}.`, | ||
| `Invalid ReferrerLeaderboardPageParams: ${params.recordsPerPage}. recordsPerPage must be less than or equal to ${REFERRERS_PER_LEADERBOARD_PAGE_MAX}.`, | ||
| ); | ||
| } | ||
| }; | ||
|
|
@@ -61,7 +61,7 @@ export const buildReferrerLeaderboardPageParams = ( | |
| ): Required<ReferrerLeaderboardPageParams> => { | ||
| const result = { | ||
| page: params.page ?? 1, | ||
| itemsPerPage: params.itemsPerPage ?? REFERRERS_PER_LEADERBOARD_PAGE_DEFAULT, | ||
| recordsPerPage: params.recordsPerPage ?? REFERRERS_PER_LEADERBOARD_PAGE_DEFAULT, | ||
| } satisfies Required<ReferrerLeaderboardPageParams>; | ||
| validateReferrerLeaderboardPageParams(result); | ||
| return result; | ||
|
|
@@ -82,7 +82,7 @@ export interface ReferrerLeaderboardPageContext extends Required<ReferrerLeaderb | |
|
|
||
| /** | ||
| * Indicates if there is a next page available | ||
| * @invariant true if and only if (`page` * `itemsPerPage` < `total`) | ||
| * @invariant true if and only if (`page` * `recordsPerPage` < `total`) | ||
| */ | ||
| hasNext: boolean; | ||
|
|
||
|
|
@@ -123,14 +123,14 @@ export const validateReferrerLeaderboardPageContext = ( | |
| `Invalid ReferrerLeaderboardPageContext: total must be a non-negative integer but is ${context.totalRecords}.`, | ||
| ); | ||
| } | ||
| const startIndex = (context.page - 1) * context.itemsPerPage; | ||
| const endIndex = startIndex + context.itemsPerPage; | ||
| const startIndex = (context.page - 1) * context.recordsPerPage; | ||
| const endIndex = startIndex + context.recordsPerPage; | ||
|
|
||
| if (!context.hasNext && endIndex < context.totalRecords) { | ||
| throw new Error( | ||
| `Invalid ReferrerLeaderboardPageContext: if hasNext is false, endIndex (${endIndex}) must be greater than or equal to total (${context.totalRecords}).`, | ||
| ); | ||
| } else if (context.hasNext && context.page * context.itemsPerPage >= context.totalRecords) { | ||
| } else if (context.hasNext && context.page * context.recordsPerPage >= context.totalRecords) { | ||
| throw new Error( | ||
| `Invalid ReferrerLeaderboardPageContext: if hasNext is true, endIndex (${endIndex}) must be less than total (${context.totalRecords}).`, | ||
| ); | ||
|
|
@@ -154,7 +154,7 @@ export const buildReferrerLeaderboardPageContext = ( | |
|
|
||
| const totalRecords = leaderboard.referrers.size; | ||
|
|
||
| const totalPages = Math.max(1, Math.ceil(totalRecords / materializedParams.itemsPerPage)); | ||
| const totalPages = Math.max(1, Math.ceil(totalRecords / materializedParams.recordsPerPage)); | ||
|
|
||
| if (materializedParams.page > totalPages) { | ||
| throw new Error( | ||
|
|
@@ -174,8 +174,8 @@ export const buildReferrerLeaderboardPageContext = ( | |
| } satisfies ReferrerLeaderboardPageContext; | ||
| } | ||
|
|
||
| const startIndex = (materializedParams.page - 1) * materializedParams.itemsPerPage; | ||
| const maxTheoreticalIndexOnPage = startIndex + (materializedParams.itemsPerPage - 1); | ||
| const startIndex = (materializedParams.page - 1) * materializedParams.recordsPerPage; | ||
| const maxTheoreticalIndexOnPage = startIndex + (materializedParams.recordsPerPage - 1); | ||
| const endIndex = Math.min(maxTheoreticalIndexOnPage, totalRecords - 1); | ||
| const hasNext = maxTheoreticalIndexOnPage < totalRecords - 1; | ||
| const hasPrev = materializedParams.page > 1; | ||
|
|
@@ -205,9 +205,9 @@ export interface ReferrerLeaderboardPage { | |
|
|
||
| /** | ||
| * Ordered list of {@link AwardedReferrerMetrics} for the {@link ReferrerLeaderboardPage} | ||
| * described by `paginationContext` within the related {@link ReferrerLeaderboard}. | ||
| * described by `pageContext` within the related {@link ReferrerLeaderboard}. | ||
| * | ||
| * @invariant Array will be empty if `paginationContext.totalRecords` is 0. | ||
| * @invariant Array will be empty if `pageContext.totalRecords` is 0. | ||
| * @invariant Array entries are ordered by `rank` (descending). | ||
| */ | ||
| referrers: AwardedReferrerMetrics[]; | ||
|
|
@@ -221,7 +221,7 @@ export interface ReferrerLeaderboardPage { | |
| * The {@link ReferrerLeaderboardPageContext} of this {@link ReferrerLeaderboardPage} relative to the overall | ||
| * {@link ReferrerLeaderboard}. | ||
| */ | ||
| paginationContext: ReferrerLeaderboardPageContext; | ||
| pageContext: ReferrerLeaderboardPageContext; | ||
|
|
||
| /** | ||
| * The {@link UnixTimestamp} of when the data used to build the {@link ReferrerLeaderboardPage} was accurate as of. | ||
|
|
@@ -230,22 +230,22 @@ export interface ReferrerLeaderboardPage { | |
| } | ||
|
|
||
| export const getReferrerLeaderboardPage = ( | ||
| paginationParams: ReferrerLeaderboardPageParams, | ||
| pageParams: ReferrerLeaderboardPageParams, | ||
| leaderboard: ReferrerLeaderboard, | ||
| ): ReferrerLeaderboardPage => { | ||
| const paginationContext = buildReferrerLeaderboardPageContext(paginationParams, leaderboard); | ||
| const pageContext = buildReferrerLeaderboardPageContext(pageParams, leaderboard); | ||
|
|
||
| let referrers: AwardedReferrerMetrics[]; | ||
|
|
||
| if ( | ||
| paginationContext.totalRecords > 0 && | ||
| typeof paginationContext.startIndex !== "undefined" && | ||
| typeof paginationContext.endIndex !== "undefined" | ||
| pageContext.totalRecords > 0 && | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an opportunity for us to minimize duplicated logic / types / etc.. for pageContext logic across all our APIs? It seems we can standardize both the pagination params in requests and the pagination context in responses. What do you think? |
||
| typeof pageContext.startIndex !== "undefined" && | ||
| typeof pageContext.endIndex !== "undefined" | ||
| ) { | ||
| // extract the referrers from the leaderboard in the range specified by `paginationContext`. | ||
| // extract the referrers from the leaderboard in the range specified by `pageContext`. | ||
| referrers = Array.from(leaderboard.referrers.values()).slice( | ||
| paginationContext.startIndex, | ||
| paginationContext.endIndex + 1, // For `slice`, this is exclusive of the element at the index 'end'. We need it to be inclusive, hence plus one. | ||
| pageContext.startIndex, | ||
| pageContext.endIndex + 1, // For `slice`, this is exclusive of the element at the index 'end'. We need it to be inclusive, hence plus one. | ||
| ); | ||
| } else { | ||
| referrers = []; | ||
|
|
@@ -255,7 +255,7 @@ export const getReferrerLeaderboardPage = ( | |
| rules: leaderboard.rules, | ||
| referrers, | ||
| aggregatedMetrics: leaderboard.aggregatedMetrics, | ||
| paginationContext, | ||
| pageContext, | ||
| accurateAsOf: leaderboard.accurateAsOf, | ||
| }; | ||
| }; | ||
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.
Can we replace these constants for the referrer leaderboard page with the same constants we used for other related APIs? In other words, can we make it so that all our APIs that have pagination use the same constants for pagination rules?