Skip to content

Commit

Permalink
perf(api): optimize year-specific endpoint generation
Browse files Browse the repository at this point in the history
  • Loading branch information
kripod committed Apr 17, 2024
1 parent fdce542 commit 09198d8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20
21
21 changes: 14 additions & 7 deletions src/pages/api/v1/years/[year].json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@ const quoteYears = new Set(
);

export const getStaticPaths = (() => {
const quotesByYearBySymbol = new Map(
quoteRecords.map((quoteRecord) => {
const quotes = Object.entries(quoteRecord.data);
return [
quoteRecord.id,
Map.groupBy(quotes, ([date]) => new Date(date).getUTCFullYear()),
];
}),
);

return [...quoteYears].map((year) => ({
params: { year: year.toString() },
props: Object.fromEntries(
quoteRecords.map((quoteRecord) => {
const quotes = Object.entries(quoteRecord.data);
const years = quotes.map(([date]) => new Date(date).getUTCFullYear());
const start = years.indexOf(year);
const end = years.lastIndexOf(year) + 1;
[...quotesByYearBySymbol].map(([symbol, quotesByYear]) => {
const quotes = quotesByYear.get(year);
return [
quoteRecord.id,
start >= 0 ? Object.fromEntries(quotes.slice(start, end)) : undefined,
symbol,
quotes != null ? Object.fromEntries(quotes) : undefined,
];
}),
),
Expand Down

0 comments on commit 09198d8

Please sign in to comment.