From bd881de837c486926b25c64448b5d199acea7697 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Mon, 9 Sep 2024 15:06:10 +0200 Subject: [PATCH] export historical data --- src/routes/etf/[tickerID]/+page.svelte | 110 ++++++++++++++++++---- src/routes/stocks/[tickerID]/+page.svelte | 104 +++++++++++++++++--- 2 files changed, 186 insertions(+), 28 deletions(-) diff --git a/src/routes/etf/[tickerID]/+page.svelte b/src/routes/etf/[tickerID]/+page.svelte index 94c4ce2f..f8732624 100644 --- a/src/routes/etf/[tickerID]/+page.svelte +++ b/src/routes/etf/[tickerID]/+page.svelte @@ -7,6 +7,8 @@ import { onDestroy, onMount } from 'svelte'; import ETFKeyInformation from '$lib/components/ETFKeyInformation.svelte'; import Lazy from '$lib/components/Lazy.svelte'; + import * as DropdownMenu from "$lib/components/shadcn/dropdown-menu/index.js"; + import { Button } from "$lib/components/shadcn/button/index.js"; export let data; export let form; @@ -715,9 +717,44 @@ async function initializePrice() { } - async function exportData() { + async function exportPriceData(timePeriod: string) { + const cachedData = getCache($etfTicker, "exportPriceData" + timePeriod); - await historicalPrice('max'); + if (cachedData) { + return cachedData; + } + else { + const postData = { + ticker: $etfTicker, + timePeriod: timePeriod, + }; + + const response = await fetch(data?.apiURL + "/export-price-data", { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-API-KEY": data?.apiKey, + }, + body: JSON.stringify(postData), + }); + + const output = (await response?.json()) ?? []; + + setCache($etfTicker, output, "exportPriceData" + timePeriod); + return output; + } + } + +async function exportData(timePeriod:string) { + let exportList = []; + if(timePeriod === '1day') { + exportList = await exportPriceData(timePeriod); + + } else if (data?.user?.tier === 'Pro') { + exportList = await exportPriceData(timePeriod); + } else { + return + } const csvRows = []; @@ -725,8 +762,8 @@ async function initializePrice() { csvRows.push('time,open,high,low,close'); // Add data rows - for (const row of output) { - const csvRow = `${row.time},${row.open},${row.high},${row.low},${row.close}`; + for (const row of exportList) { + const csvRow = `${row.date},${row.open},${row.high},${row.low},${row.close}`; csvRows.push(csvRow); } @@ -737,12 +774,11 @@ async function initializePrice() { const a = document.createElement('a'); a.setAttribute('hidden', ''); a.setAttribute('href', url); - a.setAttribute('download', `${$etfTicker}.csv`); + a.setAttribute('download', `${$etfTicker}_${timePeriod}.csv`); document.body.appendChild(a); a.click(); document.body.removeChild(a); } - @@ -901,17 +937,59 @@ async function initializePrice() { - + + + + + + + + + Historical Stock Price + + + + + exportData('30min')} class="cursor-pointer hover:bg-[#27272A]"> + + 30 min + + exportData('1hour')} class="cursor-pointer hover:bg-[#27272A]"> + + 1 hour + + exportData('1day')} class="cursor-pointer hover:bg-[#27272A]"> + 1 day + + + + - diff --git a/src/routes/stocks/[tickerID]/+page.svelte b/src/routes/stocks/[tickerID]/+page.svelte index 9fb4dd9d..b87bd60a 100644 --- a/src/routes/stocks/[tickerID]/+page.svelte +++ b/src/routes/stocks/[tickerID]/+page.svelte @@ -34,13 +34,16 @@ isBeforeMarketOpen, isWeekend, } from "$lib/store"; - import { onDestroy, onMount } from "svelte"; + import { onDestroy } from "svelte"; import BullBearSay from "$lib/components/BullBearSay.svelte"; import NextEarnings from "$lib/components/NextEarnings.svelte"; import CommunitySentiment from "$lib/components/CommunitySentiment.svelte"; import Lazy from "$lib/components/Lazy.svelte"; import { convertTimestamp } from '$lib/utils'; + import * as DropdownMenu from "$lib/components/shadcn/dropdown-menu/index.js"; + import { Button } from "$lib/components/shadcn/button/index.js"; + export let data; export let form; @@ -581,9 +584,44 @@ async function historicalPrice(timePeriod: string) { } } -async function exportData() { + async function exportPriceData(timePeriod: string) { + const cachedData = getCache($stockTicker, "exportPriceData" + timePeriod); - await historicalPrice('max'); + if (cachedData) { + return cachedData; + } + else { + const postData = { + ticker: $stockTicker, + timePeriod: timePeriod, + }; + + const response = await fetch(data?.apiURL + "/export-price-data", { + method: "POST", + headers: { + "Content-Type": "application/json", + "X-API-KEY": data?.apiKey, + }, + body: JSON.stringify(postData), + }); + + const output = (await response?.json()) ?? []; + + setCache($stockTicker, output, "exportPriceData" + timePeriod); + return output; + } + } + +async function exportData(timePeriod:string) { + let exportList = []; + if(timePeriod === '1day') { + exportList = await exportPriceData(timePeriod); + + } else if (data?.user?.tier === 'Pro') { + exportList = await exportPriceData(timePeriod); + } else { + return + } const csvRows = []; @@ -591,8 +629,8 @@ async function exportData() { csvRows.push('time,open,high,low,close'); // Add data rows - for (const row of output) { - const csvRow = `${row.time},${row.open},${row.high},${row.low},${row.close}`; + for (const row of exportList) { + const csvRow = `${row.date},${row.open},${row.high},${row.low},${row.close}`; csvRows.push(csvRow); } @@ -603,7 +641,7 @@ async function exportData() { const a = document.createElement('a'); a.setAttribute('hidden', ''); a.setAttribute('href', url); - a.setAttribute('download', `${$stockTicker}.csv`); + a.setAttribute('download', `${$stockTicker}_${timePeriod}.csv`); document.body.appendChild(a); a.click(); document.body.removeChild(a); @@ -749,17 +787,59 @@ async function exportData() {
-