From 030d72c2020068742994a500525705d9b1688f69 Mon Sep 17 00:00:00 2001 From: Hickmeister <35031453+Hickmeister@users.noreply.github.com> Date: Tue, 14 Jan 2025 22:49:09 +0000 Subject: [PATCH] Price tracking Changes --- public/viewFilament.php | 209 ++++++++++--------- src/filamentTracker/addFilament.php | 32 ++- src/filamentTracker/getFilamentPrices.php | 4 +- src/filamentTracker/scraper.php | 57 +++-- src/filamentTracker/updateFilamentPrices.php | 6 +- 5 files changed, 178 insertions(+), 130 deletions(-) diff --git a/public/viewFilament.php b/public/viewFilament.php index 6ac14fc..38be033 100644 --- a/public/viewFilament.php +++ b/public/viewFilament.php @@ -67,9 +67,6 @@ - @@ -140,121 +137,135 @@ document.addEventListener("DOMContentLoaded", function () { } fetch('../src/filamentTracker/getFilamentPrices.php') - .then(response => response.json()) - .then(data => { - if (data.status === 'success') { - container.innerHTML = ''; // Clear Skeleton + .then(response => response.json()) + .then(data => { + if (data.status === 'success') { + container.innerHTML = ''; // Clear Skeleton - Object.keys(data.data).forEach(filament => { - const prices = data.data[filament].prices.map(entry => entry.price); - const timestamps = data.data[filament].prices.map(entry => entry.recordedAt); - const latestPrice = prices[prices.length - 1] || 0; + Object.keys(data.data).forEach(filament => { + const filamentData = data.data[filament]; + const prices = filamentData.prices.map(entry => parseFloat(entry.price)); + const timestamps = filamentData.prices.map(entry => entry.recordedAt); + const latestPrice = prices[prices.length - 1] || 0; - // Find the last distinct price (where the price actually changed) - let lastDistinctPrice = latestPrice; - for (let i = prices.length - 2; i >= 0; i--) { - if (prices[i] !== latestPrice) { - lastDistinctPrice = prices[i]; - break; - } + // Find the last distinct price (where the price actually changed) + let lastDistinctPrice = latestPrice; + for (let i = prices.length - 2; i >= 0; i--) { + if (prices[i] !== latestPrice) { + lastDistinctPrice = prices[i]; + break; } + } - const priceDifference = latestPrice - lastDistinctPrice; + const priceDifference = latestPrice - lastDistinctPrice; - // Price Change Indicator - let priceChangeIndicator; - if (priceDifference > 0) { - priceChangeIndicator = ` - +£${priceDifference.toFixed(2)}`; - } else if (priceDifference < 0) { - priceChangeIndicator = ` - -£${Math.abs(priceDifference).toFixed(2)}`; - } else { - priceChangeIndicator = ` - £0.00`; - } + // Price Change Indicator + let priceChangeIndicator; + if (priceDifference > 0) { + priceChangeIndicator = ` + +£${priceDifference.toFixed(2)}`; + } else if (priceDifference < 0) { + priceChangeIndicator = ` + -£${Math.abs(priceDifference).toFixed(2)}`; + } else { + priceChangeIndicator = ` + £0.00`; + } - const amazonUrl = data.data[filament].amazonUrl || '#'; - const discount = data.data[filament].currentDiscount || 0; // Get discount percentage - const chartId = `chart-${filament.replace(/\s+/g, '-')}`; - const chartColor = getRandomColor(); + const amazonUrl = filamentData.amazonUrl || '#'; - // Create Card HTML - const cardHTML = ` -
No filament data available.
'; - } - }) - .catch(error => { - console.error("Error fetching filament data:", error); - }); + const chartElement = document.getElementById(chartId); + if (chartElement) { + const chart = new ApexCharts(chartElement, chartOptions); + chart.render(); + } + }); + } else { + container.innerHTML = 'No filament data available.
'; + } + }) + .catch(error => { + console.error("Error fetching filament data:", error); + }); - showSkeletonLoader(); +showSkeletonLoader(); }); - - -