Load TrendAnalysis component lazy

This commit is contained in:
MuslemRahimi 2024-06-06 20:23:50 +02:00
parent 117cd9febb
commit fe8dd9e2b7
7 changed files with 102 additions and 59 deletions

View File

@ -1,23 +1,37 @@
<script lang ='ts'> <script lang ='ts'>
import { displayCompanyName, stockTicker, etfTicker, cryptoTicker, assetType} from '$lib/store'; import { displayCompanyName, stockTicker, etfTicker, cryptoTicker, assetType, userRegion, getCache, setCache} from '$lib/store';
import InfoModal from '$lib/components/InfoModal.svelte'; import InfoModal from '$lib/components/InfoModal.svelte';
//import Chart from '$lib/components/Chart.svelte'; //import Chart from '$lib/components/Chart.svelte';
//import Lazy from 'svelte-lazy'; //import Lazy from 'svelte-lazy';
export let trendList; let trendList = [];
export let data; let isLoaded = false;
const usRegion = ['cle1','iad1','pdx1','sfo1'];
let deactivateContent = data?.user?.tier === 'Pro' ? false : true; let apiURL;
userRegion.subscribe(value => {
if (usRegion.includes(value)) {
apiURL = import.meta.env.VITE_USEAST_API_URL;
} else {
apiURL = import.meta.env.VITE_EU_API_URL;
}
});
export let data;
let deactivateContent = data?.user?.tier === 'Pro' ? false : true;
//let showMore = false; //let showMore = false;
//let oneMonthResult; //let oneMonthResult;
let accuracy; let accuracy;
let precision; let precision;
let flowSentiment = 'n/a'; let flowSentiment = 'n/a';
let displayData = 'threeMonth'; let displayData = 'threeMonth';
let lastPrice = 'n/a'; let lastPrice = 'n/a';
function changeStatement(event) function changeStatement(event)
@ -25,15 +39,52 @@ function changeStatement(event)
displayData = event.target.value; displayData = event.target.value;
} }
const getTrendAnalysis = async (ticker) => {
// Get cached data for the specific tickerID
const cachedData = getCache(ticker, 'getTrendAnalysis');
if (cachedData) {
trendList = cachedData;
} else {
const postData = {'ticker': ticker};
// make the POST request to the endpoint
const response = await fetch(apiURL + '/trend-analysis', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
});
trendList = await response.json();
// Cache the data for this specific tickerID with a specific name 'getTrendAnalysis'
setCache(ticker, trendList, 'getTrendAnalysis');
}
};
$: { $: {
if(($assetType === 'stock' ? $stockTicker : $assetType === 'etf' ? $etfTicker : $cryptoTicker) && typeof window !== 'undefined' && trendList?.length !== 0) { if(($assetType === 'stock' ? $stockTicker : $assetType === 'etf' ? $etfTicker : $cryptoTicker) && typeof window !== 'undefined') {
lastPrice = data?.getStockQuote?.price ?? "n/a"; isLoaded = false;
const sample = trendList?.filter(item => item?.label === displayData)?.at(0); const ticker = $assetType === 'stock' ? $stockTicker : $assetType === 'etf' ? $etfTicker : $cryptoTicker;
flowSentiment = sample?.sentiment;
accuracy = sample?.accuracy; const asyncFunctions = [
precision = sample?.precision; getTrendAnalysis(ticker)
];
Promise.all(asyncFunctions)
.then((results) => {
lastPrice = data?.getStockQuote?.price ?? "n/a";
const sample = trendList?.filter(item => item?.label === displayData)?.at(0);
flowSentiment = sample?.sentiment;
accuracy = sample?.accuracy;
precision = sample?.precision;
})
.catch((error) => {
console.error('An error occurred:', error);
});
isLoaded = true;
} }
} }
@ -54,7 +105,8 @@ $: {
id={"trendAnalysisInfo"} id={"trendAnalysisInfo"}
/> />
</div> </div>
{#if isLoaded}
{#if trendList?.length !== 0} {#if trendList?.length !== 0}
<div class="w-full flex flex-col items-start"> <div class="w-full flex flex-col items-start">
<div class="text-white text-sm sm:text-[1rem] mt-1 sm:mt-3 mb-1 w-full"> <div class="text-white text-sm sm:text-[1rem] mt-1 sm:mt-3 mb-1 w-full">
@ -167,6 +219,16 @@ $: {
</h2> </h2>
{/if} {/if}
{:else}
<div class="flex justify-center items-center h-80">
<div class="relative">
<label class="bg-[#202020] rounded-xl h-14 w-14 flex justify-center items-center absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2">
<span class="loading loading-spinner loading-md"></span>
</label>
</div>
</div>
{/if}
</main> </main>
</section> </section>

View File

@ -85,7 +85,6 @@ export const load = async ({ params, locals}) => {
fetchData(apiURL,'/stock-quote',params.tickerID), fetchData(apiURL,'/stock-quote',params.tickerID),
fetchData(apiURL,'/stock-rating',params.tickerID), fetchData(apiURL,'/stock-rating',params.tickerID),
fetchData(apiURL,'/sentiment-analysis',params.tickerID), fetchData(apiURL,'/sentiment-analysis',params.tickerID),
fetchData(apiURL,'/trend-analysis',params.tickerID),
fetchData(apiURL,'/value-at-risk',params.tickerID), fetchData(apiURL,'/value-at-risk',params.tickerID),
fetchData(apiURL,'/historical-price',params.tickerID), fetchData(apiURL,'/historical-price',params.tickerID),
fetchData(apiURL,'/one-day-price',params.tickerID), fetchData(apiURL,'/one-day-price',params.tickerID),
@ -98,7 +97,6 @@ export const load = async ({ params, locals}) => {
getStockQuote, getStockQuote,
getStockTARating, getStockTARating,
getSentimentAnalysis, getSentimentAnalysis,
getTrendAnalysis,
getVaR, getVaR,
getHistoricalPrice, getHistoricalPrice,
getOneDayPrice, getOneDayPrice,
@ -117,7 +115,6 @@ export const load = async ({ params, locals}) => {
getStockQuote, getStockQuote,
getStockTARating, getStockTARating,
getSentimentAnalysis, getSentimentAnalysis,
getTrendAnalysis,
getVaR, getVaR,
getHistoricalPrice, getHistoricalPrice,
getOneDayPrice, getOneDayPrice,

View File

@ -34,7 +34,6 @@
let taRating = {}; let taRating = {};
let varDict = {}; let varDict = {};
let sentimentList = [] let sentimentList = []
let trendList = [];
//============================================// //============================================//
@ -53,7 +52,6 @@
let TARating; let TARating;
let SentimentAnalysis; let SentimentAnalysis;
let TrendAnalysis;
let VaR; let VaR;
//let StockKeyInformation; //let StockKeyInformation;
@ -64,7 +62,6 @@
onMount(async() => { onMount(async() => {
SentimentAnalysis = (await import('$lib/components/SentimentAnalysis.svelte')).default; SentimentAnalysis = (await import('$lib/components/SentimentAnalysis.svelte')).default;
TrendAnalysis = (await import('$lib/components/TrendAnalysis.svelte')).default;
VaR = (await import('$lib/components/VaR.svelte')).default; VaR = (await import('$lib/components/VaR.svelte')).default;
TARating = (await import('$lib/components/TARating.svelte')).default; TARating = (await import('$lib/components/TARating.svelte')).default;
@ -590,7 +587,6 @@ function changeChartType() {
taRating = {}; taRating = {};
varDict={} varDict={}
sentimentList = []; sentimentList = [];
trendList = [];
output = null; output = null;
@ -598,7 +594,6 @@ function changeChartType() {
previousClose = data?.getStockQuote?.previousClose; previousClose = data?.getStockQuote?.previousClose;
taRating = data?.getStockTARating; taRating = data?.getStockTARating;
sentimentList = data?.getSentimentAnalysis; sentimentList = data?.getSentimentAnalysis;
trendList = data?.getTrendAnalysis;
varDict = data?.getVaR; varDict = data?.getVaR;
const asyncFunctions = []; const asyncFunctions = [];
@ -1119,11 +1114,13 @@ afterUpdate(async () => {
</div> </div>
</Lazy> </Lazy>
{#if TrendAnalysis} <Lazy>
<div class="w-full mt-10 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {trendList?.length !== 0 ? '' : 'hidden'}"> <div class="w-full mt-10 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6">
<TrendAnalysis data={data} trendList={trendList}/> {#await import('$lib/components/TrendAnalysis.svelte') then {default: Comp}}
</div> <svelte:component this={Comp} data={data} />
{/if} {/await}
</div>
</Lazy>
{#if SentimentAnalysis} {#if SentimentAnalysis}
<div class="w-full mt-10 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {sentimentList?.length !== 0 ? '' : 'hidden'}"> <div class="w-full mt-10 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {sentimentList?.length !== 0 ? '' : 'hidden'}">

View File

@ -115,7 +115,6 @@ const promises = [
fetchData(apiURL,'/options-bubble',params.tickerID), fetchData(apiURL,'/options-bubble',params.tickerID),
fetchData(apiURL,'/wiim',params.tickerID), fetchData(apiURL,'/wiim',params.tickerID),
fetchData(apiURL,'/sentiment-analysis',params.tickerID), fetchData(apiURL,'/sentiment-analysis',params.tickerID),
fetchData(apiURL,'/trend-analysis',params.tickerID),
fetchData(apiURL,'/value-at-risk',params.tickerID), fetchData(apiURL,'/value-at-risk',params.tickerID),
fetchWatchlist(fastifyURL, locals?.user?.id), fetchWatchlist(fastifyURL, locals?.user?.id),
fetchPortfolio(fastifyURL, locals?.user?.id) fetchPortfolio(fastifyURL, locals?.user?.id)
@ -133,7 +132,6 @@ const promises = [
getOptionsData, getOptionsData,
getWhyPriceMoved, getWhyPriceMoved,
getSentimentAnalysis, getSentimentAnalysis,
getTrendAnalysis,
getVaR, getVaR,
getUserWatchlist, getUserWatchlist,
getUserPortfolio, getUserPortfolio,
@ -157,7 +155,6 @@ const promises = [
getOptionsData, getOptionsData,
getWhyPriceMoved, getWhyPriceMoved,
getSentimentAnalysis, getSentimentAnalysis,
getTrendAnalysis,
getVaR, getVaR,
getUserWatchlist, getUserWatchlist,
getUserPortfolio, getUserPortfolio,

View File

@ -37,7 +37,6 @@
let optionsDict = {}; let optionsDict = {};
let taRating = {}; let taRating = {};
let varDict = {}; let varDict = {};
let trendList = [];
let previousClose = data?.getStockQuote?.previousClose; let previousClose = data?.getStockQuote?.previousClose;
//============================================// //============================================//
@ -66,7 +65,6 @@
let OptionsData; let OptionsData;
let WIIM; let WIIM;
let SentimentAnalysis; let SentimentAnalysis;
let TrendAnalysis;
let VaR; let VaR;
//let ETFKeyInformation; //let ETFKeyInformation;
@ -75,7 +73,6 @@
WIIM = (await import('$lib/components/WIIM.svelte')).default; WIIM = (await import('$lib/components/WIIM.svelte')).default;
SentimentAnalysis = (await import('$lib/components/SentimentAnalysis.svelte')).default; SentimentAnalysis = (await import('$lib/components/SentimentAnalysis.svelte')).default;
TrendAnalysis = (await import('$lib/components/TrendAnalysis.svelte')).default;
VaR = (await import('$lib/components/VaR.svelte')).default; VaR = (await import('$lib/components/VaR.svelte')).default;
OptionsData = (await import('$lib/components/OptionsData.svelte')).default; OptionsData = (await import('$lib/components/OptionsData.svelte')).default;
TARating = (await import('$lib/components/TARating.svelte')).default; TARating = (await import('$lib/components/TARating.svelte')).default;
@ -697,7 +694,6 @@
prePostData = {}; prePostData = {};
taRating = {}; taRating = {};
varDict = {}; varDict = {};
trendList = [];
output = null; output = null;
@ -715,7 +711,6 @@
optionsDict = data?.getOptionsData; optionsDict = data?.getOptionsData;
previousClose = data?.getStockQuote?.previousClose previousClose = data?.getStockQuote?.previousClose
taRating = data?.getStockTARating; taRating = data?.getStockTARating;
trendList = data?.getTrendAnalysis;
varDict = data?.getVaR; varDict = data?.getVaR;
//shareholderList = data?.getShareHolders; //shareholderList = data?.getShareHolders;
@ -1294,11 +1289,13 @@
</div> </div>
</Lazy> </Lazy>
{#if TrendAnalysis} <Lazy>
<div class="w-full mt-10 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {trendList?.length !== 0 ? '' : 'hidden'}"> <div class="w-full mt-10 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6">
<TrendAnalysis data={data} trendList={trendList}/> {#await import('$lib/components/TrendAnalysis.svelte') then {default: Comp}}
</div> <svelte:component this={Comp} data={data} />
{/if} {/await}
</div>
</Lazy>
{#if SentimentAnalysis} {#if SentimentAnalysis}
<div class="w-full mt-10 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {data?.getSentimentAnalysis?.length !== 0 ? '' : 'hidden'}"> <div class="w-full mt-10 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {data?.getSentimentAnalysis?.length !== 0 ? '' : 'hidden'}">

View File

@ -152,7 +152,6 @@ export const load = async ({ params, locals, cookies}) => {
fetchData(apiURL,'/wiim',params.tickerID), fetchData(apiURL,'/wiim',params.tickerID),
fetchData(apiURL,'/top-etf-ticker-holder',params.tickerID), fetchData(apiURL,'/top-etf-ticker-holder',params.tickerID),
fetchData(apiURL,'/sentiment-analysis',params.tickerID), fetchData(apiURL,'/sentiment-analysis',params.tickerID),
fetchData(apiURL,'/trend-analysis',params.tickerID),
fetchData(apiURL,'/fundamental-predictor-analysis',params.tickerID), fetchData(apiURL,'/fundamental-predictor-analysis',params.tickerID),
fetchData(apiURL,'/value-at-risk',params.tickerID), fetchData(apiURL,'/value-at-risk',params.tickerID),
fetchData(apiURL,'/enterprise-values',params.tickerID), fetchData(apiURL,'/enterprise-values',params.tickerID),
@ -181,7 +180,6 @@ export const load = async ({ params, locals, cookies}) => {
getWhyPriceMoved, getWhyPriceMoved,
getTopETFHolder, getTopETFHolder,
getSentimentAnalysis, getSentimentAnalysis,
getTrendAnalysis,
getFundamentalAnalysis, getFundamentalAnalysis,
getVaR, getVaR,
getEnterPriseValues, getEnterPriseValues,
@ -216,7 +214,6 @@ export const load = async ({ params, locals, cookies}) => {
getWhyPriceMoved, getWhyPriceMoved,
getTopETFHolder, getTopETFHolder,
getSentimentAnalysis, getSentimentAnalysis,
getTrendAnalysis,
getFundamentalAnalysis, getFundamentalAnalysis,
getVaR, getVaR,
getEnterPriseValues, getEnterPriseValues,

View File

@ -47,7 +47,6 @@
let varDict = {}; let varDict = {};
let enterpriseValues = []; let enterpriseValues = [];
let sentimentList = [] let sentimentList = []
let trendList = [];
let fundamentalAnalysisDict = {}; let fundamentalAnalysisDict = {};
let communitySentiment = {}; let communitySentiment = {};
@ -75,7 +74,6 @@
let OptionsData; let OptionsData;
let WIIM; let WIIM;
let SentimentAnalysis; let SentimentAnalysis;
let TrendAnalysis;
let PriceAnalysis; let PriceAnalysis;
let FundamentalAnalysis; let FundamentalAnalysis;
let VaR; let VaR;
@ -95,7 +93,6 @@
*/ */
WIIM = (await import('$lib/components/WIIM.svelte')).default; WIIM = (await import('$lib/components/WIIM.svelte')).default;
SentimentAnalysis = (await import('$lib/components/SentimentAnalysis.svelte')).default; SentimentAnalysis = (await import('$lib/components/SentimentAnalysis.svelte')).default;
TrendAnalysis = (await import('$lib/components/TrendAnalysis.svelte')).default;
//PriceAnalysis = (await import('$lib/components/PriceAnalysis.svelte')).default; //PriceAnalysis = (await import('$lib/components/PriceAnalysis.svelte')).default;
FundamentalAnalysis = (await import('$lib/components/FundamentalAnalysis.svelte')).default; FundamentalAnalysis = (await import('$lib/components/FundamentalAnalysis.svelte')).default;
VaR = (await import('$lib/components/VaR.svelte')).default; VaR = (await import('$lib/components/VaR.svelte')).default;
@ -651,7 +648,6 @@ function changeChartType() {
varDict={} varDict={}
enterpriseValues = [] enterpriseValues = []
sentimentList = []; sentimentList = [];
trendList = [];
fundamentalAnalysisDict = {}; fundamentalAnalysisDict = {};
communitySentiment = {} communitySentiment = {}
output = null; output = null;
@ -669,7 +665,6 @@ function changeChartType() {
marketMoods = data?.getBullBearSay; marketMoods = data?.getBullBearSay;
taRating = data?.getStockTARating; taRating = data?.getStockTARating;
sentimentList = data?.getSentimentAnalysis; sentimentList = data?.getSentimentAnalysis;
trendList = data?.getTrendAnalysis;
varDict = data?.getVaR; varDict = data?.getVaR;
enterpriseValues = data?.getEnterPriseValues; enterpriseValues = data?.getEnterPriseValues;
fundamentalAnalysisDict = data?.getFundamentalAnalysis; fundamentalAnalysisDict = data?.getFundamentalAnalysis;
@ -1240,12 +1235,13 @@ function changeChartType() {
</div> </div>
</Lazy> </Lazy>
<Lazy>
{#if TrendAnalysis} <div class="w-full mt-10 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6">
<div class="w-full mt-10 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {trendList?.length !== 0 ? '' : 'hidden'}"> {#await import('$lib/components/TrendAnalysis.svelte') then {default: Comp}}
<TrendAnalysis data={data} trendList={trendList}/> <svelte:component this={Comp} data={data} />
</div> {/await}
{/if} </div>
</Lazy>
{#if FundamentalAnalysis} {#if FundamentalAnalysis}
<div class="w-full mt-10 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {Object?.keys(fundamentalAnalysisDict)?.length !== 0 ? '' : 'hidden'}"> <div class="w-full mt-10 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {Object?.keys(fundamentalAnalysisDict)?.length !== 0 ? '' : 'hidden'}">