From fa2df40340808f2a9b2f8980faf655e4c8a85cc2 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Mon, 23 Sep 2024 10:33:15 +0200 Subject: [PATCH] performance fix --- src/lib/components/RevenueSegmentation.svelte | 16 +++--------- src/routes/+page.svelte | 5 +++- .../stocks/[tickerID]/stats/+page.server.ts | 25 +++++++++++++++++-- .../stocks/[tickerID]/stats/+page.svelte | 2 +- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/lib/components/RevenueSegmentation.svelte b/src/lib/components/RevenueSegmentation.svelte index 56a70c34..c8e8bbb4 100644 --- a/src/lib/components/RevenueSegmentation.svelte +++ b/src/lib/components/RevenueSegmentation.svelte @@ -8,10 +8,10 @@ import InfoModal from "$lib/components/InfoModal.svelte"; export let userTier; + export let revenueSegmentation = []; let isLoaded = false; - let revenueSegmentation = []; let data = []; let geographicList = []; @@ -119,7 +119,7 @@ $revenueSegmentationComponent = false; } } - + /* const getRevenueSegmentation = async (ticker) => { // Get cached data for the specific tickerID const cachedData = getCache(ticker, "getRevenueSegmentation"); @@ -142,6 +142,7 @@ setCache(ticker, revenueSegmentation, "getRevenueSegmentation"); } }; + */ $: { if ($stockTicker && typeof window !== "undefined") { @@ -151,16 +152,7 @@ geographicList = []; totalProductRevenue = 0; totalGeographicRevenue = 0; - const ticker = $stockTicker; - - const asyncFunctions = [getRevenueSegmentation(ticker)]; - Promise.all(asyncFunctions) - .then((results) => { - prepareData(revenueSegmentation); - }) - .catch((error) => { - console.error("An error occurred:", error); - }); + prepareData(revenueSegmentation); isLoaded = true; } } diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 5608b8e5..a1bf60d5 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -540,7 +540,7 @@ onMount( async() => { - + + + diff --git a/src/routes/stocks/[tickerID]/stats/+page.server.ts b/src/routes/stocks/[tickerID]/stats/+page.server.ts index 1999743a..1c911547 100644 --- a/src/routes/stocks/[tickerID]/stats/+page.server.ts +++ b/src/routes/stocks/[tickerID]/stats/+page.server.ts @@ -3,9 +3,9 @@ import { validateData } from "$lib/utils"; import { loginUserSchema, registerUserSchema } from "$lib/schemas"; export const load = async ({ locals, params }) => { - const getQuantStats = async () => { - const { apiKey, apiURL } = locals; + const { apiKey, apiURL } = locals; + const getQuantStats = async () => { const postData = { ticker: params.tickerID, }; @@ -25,9 +25,30 @@ export const load = async ({ locals, params }) => { return output; }; + const getRevenueSegmentation = async () => { + const postData = { + ticker: params.tickerID, + }; + + // make the POST request to the endpoint + const response = await fetch(apiURL + "/revenue-segmentation", { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-API-KEY": apiKey, + }, + body: JSON.stringify(postData), + }); + + const output = await response.json(); + + return output; + }; + // Make sure to return a promise return { getQuantStats: await getQuantStats(), + getRevenueSegmentation: await getRevenueSegmentation(), }; }; diff --git a/src/routes/stocks/[tickerID]/stats/+page.svelte b/src/routes/stocks/[tickerID]/stats/+page.svelte index cd945ee6..fa8ac2ac 100644 --- a/src/routes/stocks/[tickerID]/stats/+page.svelte +++ b/src/routes/stocks/[tickerID]/stats/+page.svelte @@ -208,7 +208,7 @@ updateYearRange()
- +