From 1d3fd92bf2a0e55a7130349df94013eaaa828b92 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sat, 15 Jun 2024 11:08:06 +0200 Subject: [PATCH] bugfixing: add historical data back --- .../stocks/[tickerID]/insider/+page.svelte | 2 +- src/routes/stocks/[tickerID]/insider/+page.ts | 46 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/routes/stocks/[tickerID]/insider/+page.svelte b/src/routes/stocks/[tickerID]/insider/+page.svelte index 1e781a2f..4fa2a716 100644 --- a/src/routes/stocks/[tickerID]/insider/+page.svelte +++ b/src/routes/stocks/[tickerID]/insider/+page.svelte @@ -251,7 +251,7 @@ const handleMessage = async (event) => { const loadWorker = async () => { const SyncWorker = await import('./workers/insiderWorker?worker'); syncWorker = new SyncWorker.default(); - syncWorker.postMessage({ message: data?.getInsiderTrading, historicalPrice: data?.getHistoricalPrice?.MAX}); + syncWorker.postMessage({ message: data?.getInsiderTrading, historicalPrice: data?.getHistoricalPrice}); syncWorker.onmessage = handleMessage; diff --git a/src/routes/stocks/[tickerID]/insider/+page.ts b/src/routes/stocks/[tickerID]/insider/+page.ts index b5a35ded..44b1559c 100644 --- a/src/routes/stocks/[tickerID]/insider/+page.ts +++ b/src/routes/stocks/[tickerID]/insider/+page.ts @@ -121,9 +121,53 @@ export const load = async ({ params }) => { return sums; }; + +async function historicalPrice() { + let output; + + const cachedData = getCache(params.tickerID, 'historicalPrice'+'max'); + if (cachedData) { + output = cachedData; + } else { + const postData = { + ticker: params.tickerID, + timePeriod: 'max', + }; + + const response = await fetch(apiURL+'/historical-price', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(postData) + }); + + output = await response?.json() ?? []; + + /* + const mapData = (data) => data?.map(({ time, open, high, low, close }) => ({ + time: Date.parse(time), + open, + high, + low, + close + })); + */ + + setCache(params.tickerID, output, 'historicalPrice'+'max'); + + } + + return output; + + } + + + // Make sure to return a promise return { getInsiderTrading: await getInsiderTrading(), - getInsiderTradingStatistics: await getInsiderTradingStatistics() + getInsiderTradingStatistics: await getInsiderTradingStatistics(), + getHistoricalPrice: await historicalPrice() }; }; \ No newline at end of file