From 33a89e216d78480c08aecf32ce0a0ca75f00a13a Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Wed, 14 Aug 2024 10:56:33 +0200 Subject: [PATCH] load senate images from s3 bucket instead of vercel static folder to be cost efficient --- .../insider/congress-trading/+page.svelte | 104 ++++++------------ 1 file changed, 31 insertions(+), 73 deletions(-) diff --git a/src/routes/stocks/[tickerID]/insider/congress-trading/+page.svelte b/src/routes/stocks/[tickerID]/insider/congress-trading/+page.svelte index 9513b609..b904875e 100644 --- a/src/routes/stocks/[tickerID]/insider/congress-trading/+page.svelte +++ b/src/routes/stocks/[tickerID]/insider/congress-trading/+page.svelte @@ -20,6 +20,9 @@ let isLoaded = false; let images = {}; + let cloudFrontUrl = import.meta.env.VITE_IMAGE_URL; + + function backToTop() { window.scrollTo({ top: 0, @@ -36,17 +39,6 @@ function changeStructure() { } -const replacements = { - 'Thomas_Tuberville': 'Tommy_Tuberville', - 'Patrick_Toomey': 'Pat_Toomey', - 'Thomas_Carper': 'Tom_Carper', - 'Shelley_Moore_Capito': 'Shelley_Capito', - 'Christopher_Coons': 'Chris_Coons', - 'Daniel_Sullivan': 'Dan_Sullivan', - 'William_Cassidy': 'Bill_Cassidy', - 'Angus_King_.': 'Angus_King', -}; - const district = { 'Tommy_Tuberville': 'Alabama', 'Sheldon_Whitehouse': 'Rhode Island', @@ -64,15 +56,7 @@ const district = { 'Bill_Cassidy': 'Louisiana', } -function replaceMultipleStrings(inputString, replacements) { - // Create a regular expression pattern by joining the keys of the replacements object with '|' - const pattern = new RegExp(Object?.keys(replacements)?.join('|'), 'gi'); - // Replace occurrences of the pattern with the corresponding values in the replacements object - const resultString = inputString.replace(pattern, match => replacements[match]); - - return resultString; -} async function infiniteHandler({ detail: { loaded, complete } }) { @@ -86,69 +70,42 @@ async function infiniteHandler({ detail: { loaded, complete } }) } } - // Function to load images only when they are viewed - async function loadImages() { - const imageFiles = import.meta.glob('$lib/images/senator/*.png'); - const imagesPromises = []; - for (const [path, resolver] of Object?.entries(imageFiles)) { - const imageNameMatch = path.match(/\/([^/]+)\.png$/); - if (imageNameMatch && imageNameMatch[1] !== 'default-avatar') { - imagesPromises?.push(resolver()?.then(module => { - images[imageNameMatch[1]] = module.default; - })); - } - } - - await Promise?.all(imagesPromises); - } - - -let fullName; onMount(async () => { - isLoaded = false; - await loadImages(); - rawData?.forEach(item => { - let representative = item?.representative || ''; - representative = representative?.replace('Jr', '') - .replace(/Dr./g, '') - .replace(/Dr_/g, '') - - const fullName = representative?.replace(/(\s(?:Dr\s)?\w(?:\.|(?=\s)))?\s/g, '_').trim(); - item.image = images[fullName] || defaultAvatar; - item.representative = fullName?.replace(/_/g, ' '); - }); - - rawData = rawData?.map(item => { - const party = getPartyForPoliticians(item?.representative); - return { - ...item, - party: party - }; -}); + rawData.forEach(item => { + const representative = item?.representative || ''; + const fullName = representative.replace(/(\s(?:Dr\s)?\w(?:\.|(?=\s)))?\s/g, '_').trim(); + item.representative = fullName.replace(/_/g, ' '); + }); -// Count the occurrences of "Republican" and "Democrat" -const partyCounts = rawData?.reduce((counts, item) => { - counts[item?.party] = (counts[item?.party] || 0) + 1; - return counts; -}, {}); + rawData = rawData?.map(item => { + const party = getPartyForPoliticians(item?.representative); + return { + ...item, + party: party + }; + }); -const typeCounts = rawData?.reduce((counts, item) => { - counts[item?.type] = (counts[item?.type ] || 0) + 1; - return counts; -}, {}); + // Count the occurrences of "Republican" and "Democrat" + const partyCounts = rawData.reduce((counts, item) => { + counts[item?.party] = (counts[item?.party] || 0) + 1; + return counts; + }, {}); -partyRatio = partyCounts['Democratic'] > 0 && partyCounts['Republican'] === undefined ? 1 : partyCounts['Democratic'] === undefined ? 0 : partyCounts["Democratic"]/partyCounts["Republican"]; -buySellRatio = typeCounts['Bought'] > 0 && typeCounts['Sold'] === undefined ? 1 : typeCounts['Bought'] === undefined ? 0 : typeCounts["Bought"]/typeCounts["Sold"]; + const typeCounts = rawData.reduce((counts, item) => { + counts[item?.type] = (counts[item?.type ] || 0) + 1; + return counts; + }, {}); -senateTradingList = rawData?.slice(0,20) ?? []; + partyRatio = partyCounts['Democratic'] > 0 && partyCounts['Republican'] === undefined ? 1 : partyCounts['Democratic'] === undefined ? 0 : partyCounts["Democratic"] / partyCounts["Republican"]; + buySellRatio = typeCounts['Bought'] > 0 && typeCounts['Sold'] === undefined ? 1 : typeCounts['Bought'] === undefined ? 0 : typeCounts["Bought"] / typeCounts["Sold"]; + senateTradingList = rawData.slice(0, 20) ?? []; -isLoaded = true; - + isLoaded = true; }); @@ -302,7 +259,8 @@ isLoaded = true;
- + +
{item?.representative?.replace('_',' ')} @@ -355,7 +313,7 @@ isLoaded = true;
- +
{item?.representative?.replace('_',' ')}