From 1f1a3385e3ebd9a186cfa5ff9798e14822fead9b Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Tue, 30 Jun 2026 14:32:39 +0300 Subject: [PATCH] Update: 2026-06-30 14:32:38 --- backend/bot/cron_empty_results_to_db.php | 75 ++++++++++++++++++++++++ backend/schema_primary.sql | 17 ++++++ 2 files changed, 92 insertions(+) create mode 100644 backend/bot/cron_empty_results_to_db.php diff --git a/backend/bot/cron_empty_results_to_db.php b/backend/bot/cron_empty_results_to_db.php new file mode 100644 index 00000000..16f9e663 --- /dev/null +++ b/backend/bot/cron_empty_results_to_db.php @@ -0,0 +1,75 @@ +getMessage() . "\n"); +} + +$resultsFile = __DIR__ . '/results.json'; + +if (!file_exists($resultsFile)) { + die("No results file found.\n"); +} + +$content = file_get_contents($resultsFile); +$data = json_decode($content, true); + +if (empty($data) || !is_array($data)) { + die("No data to process or invalid JSON.\n"); +} + +$insertedCount = 0; +$stmt = $con->prepare("INSERT INTO scraped_competitor_prices (task_id, app_name, start_location, end_location, price_amount, currency) VALUES (?, ?, ?, ?, ?, ?)"); + +foreach ($data as $row) { + $taskId = $row['task_id'] ?? null; + $appName = $row['app'] ?? 'Unknown'; + $startLoc = $row['start_location'] ?? ''; + $endLoc = $row['end_location'] ?? ''; + $priceStr = $row['price'] ?? ''; + + // Extract numeric amount and currency from string like "2.5 JOD" + $amount = 0.0; + $currency = 'JOD'; + + // Match numeric parts (including decimals) and text parts + if (preg_match('/([\d\.]+)\s*([A-Za-z]+|د\.ا)/', $priceStr, $matches)) { + $amount = (float)$matches[1]; + $currency = $matches[2]; + if ($currency === 'د.ا') { + $currency = 'JOD'; + } + } else { + $amount = (float)$priceStr; + } + + // Ignore invalid entries without an amount + if ($amount <= 0) { + continue; + } + + if ($stmt->execute([$taskId, $appName, $startLoc, $endLoc, $amount, $currency])) { + $insertedCount++; + } +} + +// Clear the JSON file after successfully inserting data +file_put_contents($resultsFile, json_encode([], JSON_PRETTY_PRINT)); + +echo "Successfully inserted $insertedCount records into the database and cleared results.json.\n"; diff --git a/backend/schema_primary.sql b/backend/schema_primary.sql index 5231a533..543ee8fe 100644 --- a/backend/schema_primary.sql +++ b/backend/schema_primary.sql @@ -1946,3 +1946,20 @@ CREATE TABLE IF NOT EXISTS `market_health_reports` ( PRIMARY KEY (`id`), KEY `idx_country_date` (`country_code`, `report_date`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +-- +-- Table structure for table `scraped_competitor_prices` +-- +CREATE TABLE IF NOT EXISTS `scraped_competitor_prices` ( + `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + `task_id` varchar(100) DEFAULT NULL, + `app_name` varchar(100) NOT NULL, + `start_location` varchar(255) NOT NULL, + `end_location` varchar(255) NOT NULL, + `price_amount` decimal(8,2) NOT NULL, + `currency` varchar(10) NOT NULL DEFAULT 'JOD', + `scraped_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + KEY `idx_app_name` (`app_name`), + KEY `idx_start_location` (`start_location`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;