Update Filament Dryer

This commit is contained in:
Hickmeister
2025-01-15 17:50:57 +00:00
parent cc4e915cf5
commit 053662c2e8
3 changed files with 454 additions and 329 deletions
+27 -20
View File
@@ -9,35 +9,42 @@ include '../src/session_check.php';
header('Content-Type: application/json');
try {
// Fetch all filament prices with the latest discount
$stmt = $pdo->query("
SELECT ft.filamentName,
// Query 1: Fetch filament details and price history
$stmt1 = $pdo->query("
SELECT ft.id AS filamentId,
ft.filamentName,
ft.brand,
ft.material,
ft.color,
ft.amazonUrl,
fp.price,
fp.currentDiscount,
fp.recordedAt
FROM filamentTracker ft
JOIN filamentPriceHistory fp ON ft.id = fp.filamentId
JOIN (
SELECT filamentId, recordedAt
FROM (
SELECT filamentId, recordedAt,
ROW_NUMBER() OVER (PARTITION BY filamentId ORDER BY recordedAt DESC) as rn
FROM filamentPriceHistory
) ranked
WHERE rn <= 380
) filtered ON fp.filamentId = filtered.filamentId AND fp.recordedAt = filtered.recordedAt
ORDER BY ft.filamentName, fp.recordedAt ASC
ORDER BY ft.filamentName ASC, fp.recordedAt ASC;
");
$filaments = $stmt1->fetchAll(PDO::FETCH_ASSOC);
$filaments = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Query 2: Fetch the latest discount for each filament
$stmt2 = $pdo->query("
SELECT filamentId,
JSON_OBJECT(
'discount', JSON_OBJECT('value', MAX(JSON_EXTRACT(currentDiscount, '$.discount.value')), 'type', 'percentage'),
'voucher', JSON_OBJECT('value', MAX(JSON_EXTRACT(currentDiscount, '$.voucher.value')), 'type', MAX(JSON_UNQUOTE(JSON_EXTRACT(currentDiscount, '$.voucher.type'))))
) AS currentDiscount
FROM filamentPriceHistory
GROUP BY filamentId;
");
$discounts = $stmt2->fetchAll(PDO::FETCH_ASSOC);
// Create a mapping of filamentId to discounts
$discountMap = [];
foreach ($discounts as $discount) {
$discountMap[$discount['filamentId']] = json_decode($discount['currentDiscount'], true);
}
// Format data for response
$result = [];
// Format data for charts (grouped by filament)
foreach ($filaments as $filament) {
$name = $filament['filamentName'];
@@ -48,15 +55,15 @@ try {
'color' => $filament['color'],
'amazonUrl' => $filament['amazonUrl'],
'prices' => [],
'currentDiscount' => $filament['currentDiscount'] ? json_decode($filament['currentDiscount'], true) : [
'currentDiscount' => $discountMap[$filament['filamentId']] ?? [
'discount' => ['value' => 0, 'type' => 'none'],
'voucher' => ['value' => 0, 'type' => 'none']
] // Decode JSON format and provide fallback
]
];
}
$result[$name]['prices'][] = [
'price' => $filament['price'],
'price' => (float)$filament['price'],
'recordedAt' => $filament['recordedAt']
];
}