Skip to content

Commit

Permalink
refactor: rename 'rates' collection to 'quoteRecords'
Browse files Browse the repository at this point in the history
  • Loading branch information
kripod committed Apr 17, 2024
1 parent e1eb602 commit 894f81f
Show file tree
Hide file tree
Showing 80 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"type": "module",
"scripts": {
"fetch:mnb": "tsx ./src/content/rates/_scraper.ts",
"fetch:mnb": "tsx ./src/content/quoteRecords/_scraper.ts",
"dev": "astro dev",
"build": "astro build",
"preview": "astro preview",
Expand Down
4 changes: 2 additions & 2 deletions src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const docsCollection = defineCollection({
schema: docsSchema(),
});

const ratesCollection = defineCollection({
const quoteRecordsCollection = defineCollection({
type: "data",
schema: z.record(z.string(), z.number()),
});

export const collections = {
docs: docsCollection,
rates: ratesCollection,
quoteRecords: quoteRecordsCollection,
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ for (const [currency, rateByDate] of rateByDateByCurrency) {
);

const prevContents = await file.readFile("utf8");
const entries = prevContents
const quotes = prevContents
? Object.entries<number>(JSON.parse(prevContents))
: [];

for (const [date, rate] of rateByDate) {
entries.push([stringifyDate(date), rate]);
quotes.push([stringifyDate(date), rate]);
}
entries.sort(([aDate], [bDate]) => Date.parse(aDate) - Date.parse(bDate));
quotes.sort(([aDate], [bDate]) => Date.parse(aDate) - Date.parse(bDate));

await file.write(
JSON.stringify(Object.fromEntries(entries), null, 2) + "\n",
JSON.stringify(Object.fromEntries(quotes), null, 2) + "\n",
0,
);
}
8 changes: 4 additions & 4 deletions src/pages/api/v1/symbols/[symbol]/index.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import type {
} from "astro";
import { getCollection } from "astro:content";

const rates = await getCollection("rates");
const quoteRecords = await getCollection("quoteRecords");

export const getStaticPaths = (() => {
return rates.map((rate) => ({
params: { symbol: rate.id },
props: rate.data,
return quoteRecords.map((quoteRecord) => ({
params: { symbol: quoteRecord.id },
props: quoteRecord.data,
}));
}) satisfies GetStaticPaths;

Expand Down
8 changes: 4 additions & 4 deletions src/pages/api/v1/symbols/index.json.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { APIRoute } from "astro";
import { getCollection } from "astro:content";

const rates = await getCollection("rates");
const quoteRecords = await getCollection("quoteRecords");

export const GET: APIRoute = () =>
Response.json(
Object.fromEntries(
rates.map((rate) => {
const lastEntry = Object.entries(rate.data).at(-1);
return [rate.id, lastEntry];
quoteRecords.map((quoteRecord) => {
const lastEntry = Object.entries(quoteRecord.data).at(-1);
return [quoteRecord.id, lastEntry];
}),
),
);
10 changes: 6 additions & 4 deletions src/pages/api/v1/years/index.json.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { APIRoute } from "astro";
import { getCollection } from "astro:content";

const rates = await getCollection("rates");
const quoteRecords = await getCollection("quoteRecords");

const currencyPairsByYear = new Map<number, string[]>();
for (const rate of rates) {
for (const quoteRecord of quoteRecords) {
const years = [
...new Set(
Object.keys(rate.data).map((date) => new Date(date).getUTCFullYear()),
Object.keys(quoteRecord.data).map((date) =>
new Date(date).getUTCFullYear(),
),
),
];
for (const year of years) {
Expand All @@ -16,7 +18,7 @@ for (const rate of rates) {
currencyPairs = [];
currencyPairsByYear.set(year, currencyPairs);
}
currencyPairs.push(rate.id);
currencyPairs.push(quoteRecord.id);
}
}

Expand Down

0 comments on commit 894f81f

Please sign in to comment.