add lazy loading to SentimentAnalysis component

This commit is contained in:
MuslemRahimi 2024-06-07 14:52:44 +02:00
parent 1a3d5a86e1
commit b6e3c4f423
7 changed files with 88 additions and 43 deletions

View File

@ -1,9 +1,22 @@
<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';
export let sentimentList = []; let sentimentList = [];
let isLoaded = false;
const usRegion = ['cle1','iad1','pdx1','sfo1'];
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; export let data;
let oneMonthResult; let oneMonthResult;
@ -25,11 +38,50 @@
'10': '-mt-[200px]', '10': '-mt-[200px]',
} }
const getSentimentAnalysis = async (ticker) => {
// Get cached data for the specific tickerID
const cachedData = getCache(ticker, 'getSentimentAnalysis');
if (cachedData) {
sentimentList = cachedData;
} else {
const postData = {'ticker': ticker};
// make the POST request to the endpoint
const response = await fetch(apiURL + '/sentiment-analysis', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
});
sentimentList = await response.json();
// Cache the data for this specific tickerID with a specific name 'getSentimentAnalysis'
setCache(ticker, sentimentList, 'getSentimentAnalysis');
}
};
$: { $: {
if($assetType === 'stock' ? $stockTicker : $assetType === 'etf' ? $etfTicker : $cryptoTicker && typeof window !== 'undefined' && sentimentList?.length !== 0) { if($assetType === 'stock' ? $stockTicker : $assetType === 'etf' ? $etfTicker : $cryptoTicker && typeof window !== 'undefined') {
oneMonthResult = sentimentList?.at(1)?.value;
outlook = oneMonthResult > 5 ? 'Positive' : oneMonthResult < 5 ? 'Negative' : 'Neutral'; isLoaded= false;
oneYearResult = sentimentList?.at(-1)?.value; const ticker = $assetType === 'stock' ? $stockTicker : $assetType === 'etf' ? $etfTicker : $cryptoTicker;
const asyncFunctions = [
getSentimentAnalysis(ticker)
];
Promise.all(asyncFunctions)
.then((results) => {
oneMonthResult = sentimentList?.at(1)?.value;
outlook = oneMonthResult > 5 ? 'Positive' : oneMonthResult < 5 ? 'Negative' : 'Neutral';
oneYearResult = sentimentList?.at(-1)?.value;
})
.catch((error) => {
console.error('An error occurred:', error);
});
isLoaded = true;
} }
} }
</script> </script>
@ -49,7 +101,7 @@ $: {
id={"sentimentAnalysisInfo"} id={"sentimentAnalysisInfo"}
/> />
</div> </div>
{#if isLoaded}
{#if sentimentList?.length !== 0} {#if sentimentList?.length !== 0}
@ -178,6 +230,15 @@ $: {
</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

@ -84,7 +84,6 @@ export const load = async ({ params, locals}) => {
fetchData(apiURL,'/crypto-profile',params.tickerID), fetchData(apiURL,'/crypto-profile',params.tickerID),
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,'/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),
@ -96,7 +95,6 @@ export const load = async ({ params, locals}) => {
getCryptoProfile, getCryptoProfile,
getStockQuote, getStockQuote,
getStockTARating, getStockTARating,
getSentimentAnalysis,
getVaR, getVaR,
getHistoricalPrice, getHistoricalPrice,
getOneDayPrice, getOneDayPrice,
@ -114,7 +112,6 @@ export const load = async ({ params, locals}) => {
getCryptoProfile, getCryptoProfile,
getStockQuote, getStockQuote,
getStockTARating, getStockTARating,
getSentimentAnalysis,
getVaR, getVaR,
getHistoricalPrice, getHistoricalPrice,
getOneDayPrice, getOneDayPrice,

View File

@ -33,7 +33,6 @@
let previousClose = data?.getStockQuote?.previousClose; let previousClose = data?.getStockQuote?.previousClose;
let taRating = {}; let taRating = {};
let varDict = {}; let varDict = {};
let sentimentList = []
//============================================// //============================================//
@ -51,7 +50,6 @@
let TARating; let TARating;
let SentimentAnalysis;
let VaR; let VaR;
//let StockKeyInformation; //let StockKeyInformation;
@ -61,7 +59,6 @@
onMount(async() => { onMount(async() => {
SentimentAnalysis = (await import('$lib/components/SentimentAnalysis.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;
@ -586,14 +583,12 @@ function changeChartType() {
pastPriceList = []; pastPriceList = [];
taRating = {}; taRating = {};
varDict={} varDict={}
sentimentList = [];
output = null; output = null;
cryptoProfile = data?.getCryptoProfile; cryptoProfile = data?.getCryptoProfile;
previousClose = data?.getStockQuote?.previousClose; previousClose = data?.getStockQuote?.previousClose;
taRating = data?.getStockTARating; taRating = data?.getStockTARating;
sentimentList = data?.getSentimentAnalysis;
varDict = data?.getVaR; varDict = data?.getVaR;
const asyncFunctions = []; const asyncFunctions = [];
@ -1122,12 +1117,13 @@ afterUpdate(async () => {
</div> </div>
</Lazy> </Lazy>
{#if SentimentAnalysis} <Lazy>
<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 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6">
<SentimentAnalysis data={data} sentimentList={sentimentList}/> {#await import('$lib/components/SentimentAnalysis.svelte') then {default: Comp}}
</div> <svelte:component this={Comp} data={data} />
{/if} {/await}
</div>
</Lazy>
{#if VaR} {#if VaR}
<div class="w-full sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {Object?.keys(varDict)?.length !== 0 ? '' : 'hidden'}"> <div class="w-full sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {Object?.keys(varDict)?.length !== 0 ? '' : 'hidden'}">
<VaR data={data} varDict={varDict}/> <VaR data={data} varDict={varDict}/>

View File

@ -114,7 +114,6 @@ const promises = [
fetchData(apiURL,'/stock-rating', params.tickerID), fetchData(apiURL,'/stock-rating', params.tickerID),
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,'/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)
@ -131,7 +130,6 @@ const promises = [
getStockTARating, getStockTARating,
getOptionsData, getOptionsData,
getWhyPriceMoved, getWhyPriceMoved,
getSentimentAnalysis,
getVaR, getVaR,
getUserWatchlist, getUserWatchlist,
getUserPortfolio, getUserPortfolio,
@ -154,7 +152,6 @@ const promises = [
getStockTARating, getStockTARating,
getOptionsData, getOptionsData,
getWhyPriceMoved, getWhyPriceMoved,
getSentimentAnalysis,
getVaR, getVaR,
getUserWatchlist, getUserWatchlist,
getUserPortfolio, getUserPortfolio,

View File

@ -64,7 +64,6 @@
let SectorSegmentation; let SectorSegmentation;
let OptionsData; let OptionsData;
let WIIM; let WIIM;
let SentimentAnalysis;
let VaR; let VaR;
//let ETFKeyInformation; //let ETFKeyInformation;
@ -72,7 +71,6 @@
onMount(async() => { onMount(async() => {
WIIM = (await import('$lib/components/WIIM.svelte')).default; WIIM = (await import('$lib/components/WIIM.svelte')).default;
SentimentAnalysis = (await import('$lib/components/SentimentAnalysis.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;
@ -1296,11 +1294,13 @@
</div> </div>
</Lazy> </Lazy>
{#if SentimentAnalysis} <Lazy>
<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">
<SentimentAnalysis data={data} sentimentList={data?.getSentimentAnalysis}/> {#await import('$lib/components/SentimentAnalysis.svelte') then {default: Comp}}
<svelte:component this={Comp} data={data} />
{/await}
</div> </div>
{/if} </Lazy>
{#if VaR} {#if VaR}
<div class="w-full sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {Object?.keys(varDict)?.length !== 0 ? '' : 'hidden'}"> <div class="w-full sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {Object?.keys(varDict)?.length !== 0 ? '' : 'hidden'}">

View File

@ -148,7 +148,6 @@ export const load = async ({ params, locals, cookies}) => {
fetchData(apiURL,'/bull-bear-say',params.tickerID), fetchData(apiURL,'/bull-bear-say',params.tickerID),
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,'/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,'/historical-price',params.tickerID), fetchData(apiURL,'/historical-price',params.tickerID),
@ -172,7 +171,6 @@ export const load = async ({ params, locals, cookies}) => {
getBullBearSay, getBullBearSay,
getWhyPriceMoved, getWhyPriceMoved,
getTopETFHolder, getTopETFHolder,
getSentimentAnalysis,
getFundamentalAnalysis, getFundamentalAnalysis,
getVaR, getVaR,
getHistoricalPrice, getHistoricalPrice,
@ -202,7 +200,6 @@ export const load = async ({ params, locals, cookies}) => {
getBullBearSay, getBullBearSay,
getWhyPriceMoved, getWhyPriceMoved,
getTopETFHolder, getTopETFHolder,
getSentimentAnalysis,
getFundamentalAnalysis, getFundamentalAnalysis,
getVaR, getVaR,
getHistoricalPrice, getHistoricalPrice,

View File

@ -41,7 +41,6 @@
let marketMoods = {} let marketMoods = {}
let taRating = {}; let taRating = {};
let varDict = {}; let varDict = {};
let sentimentList = []
let fundamentalAnalysisDict = {}; let fundamentalAnalysisDict = {};
let communitySentiment = {}; let communitySentiment = {};
@ -65,7 +64,6 @@
let Correlation; let Correlation;
let OptionsData; let OptionsData;
let WIIM; let WIIM;
let SentimentAnalysis;
let FundamentalAnalysis; let FundamentalAnalysis;
let VaR; let VaR;
@ -81,7 +79,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;
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;
@ -627,7 +624,6 @@ function changeChartType() {
marketMoods = {}; marketMoods = {};
taRating = {}; taRating = {};
varDict={} varDict={}
sentimentList = [];
fundamentalAnalysisDict = {}; fundamentalAnalysisDict = {};
communitySentiment = {} communitySentiment = {}
output = null; output = null;
@ -641,7 +637,6 @@ function changeChartType() {
previousClose = data?.getStockQuote?.previousClose; previousClose = data?.getStockQuote?.previousClose;
marketMoods = data?.getBullBearSay; marketMoods = data?.getBullBearSay;
taRating = data?.getStockTARating; taRating = data?.getStockTARating;
sentimentList = data?.getSentimentAnalysis;
varDict = data?.getVaR; varDict = data?.getVaR;
fundamentalAnalysisDict = data?.getFundamentalAnalysis; fundamentalAnalysisDict = data?.getFundamentalAnalysis;
communitySentiment = data?.getCommunitySentiment; communitySentiment = data?.getCommunitySentiment;
@ -1225,11 +1220,13 @@ function changeChartType() {
</div> </div>
{/if} {/if}
{#if SentimentAnalysis} <Lazy>
<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 sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6">
<SentimentAnalysis data={data} sentimentList={sentimentList}/> {#await import('$lib/components/SentimentAnalysis.svelte') then {default: Comp}}
</div> <svelte:component this={Comp} data={data} />
{/if} {/await}
</div>
</Lazy>
{#if VaR} {#if VaR}
<div class="w-full sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {Object?.keys(varDict)?.length !== 0 ? '' : 'hidden'}"> <div class="w-full sm:mt-5 m-auto sm:pl-6 sm:pb-6 sm:pt-6 {Object?.keys(varDict)?.length !== 0 ? '' : 'hidden'}">