Add /worker/licenseplates route to retrieve unique license plate statistics

- Introduced new endpoint `/worker/licenseplates` to fetch and analyze distinct license plates from multiple database tables.
- Added functionality to format license plates by removing non-alphanumeric characters and converting them to uppercase.
- Integrated response to include counted plates and total unique plates.
- Improved database query logic to handle multiple tables and columns for license plate retrieval.
This commit is contained in:
Jeppe Bundgaard
2025-09-23 09:14:34 +02:00
parent 09fbbc11bb
commit 8604cf39de
+40
View File
@@ -16,5 +16,45 @@ class workerRoute
$response, $router;
$response->success(['message' => 'Worker is running', 'status' => 'OK', 'time' => date('Y-m-d H:i:s'), 'timezone' => date_default_timezone_get(), 'host' => gethostname(), 'version' => '1.0.1', 'routes' => $router->countRoutes()]);
});
$this->get('/worker/licenseplates', function () {
global /** @var router $router */
$response, $db;
$response->error('This endpoint is disabled for security reasons.', 403);
$counted_plates = []; // Array to hold counted license plates
// This is used to fetch all UNIQUE license plates from the database tables:
// 'customer_vehicles' -> 'reg' column
// 'orders' -> 'reg_1', 'reg_2', 'reg_3' columns
// 'bookings' -> 'regNrTraekker', 'regNrTrailer' columns
// 'plate_scans' -> 'plate' column
$tables_and_columns = [
'customer_vehicles' => ['reg'],
'orders' => ['reg_1', 'reg_2', 'reg_3'],
'bookings' => ['regNrTraekker', 'regNrTrailer'],
'plate_scans' => ['plate'],
];
foreach ($tables_and_columns as $table => $columns) {
foreach ($columns as $column) {
$results = $db->query("SELECT DISTINCT $column FROM $table WHERE $column IS NOT NULL AND $column != ''");
foreach ($results as $row) {
$formatted_plate = $this->FORMAT_LICENSE_PLATE($row[$column]);
if ($formatted_plate !== '') {
if (!isset($counted_plates[$formatted_plate])) {
$counted_plates[$formatted_plate] = 0;
}
$counted_plates[$formatted_plate]++;
}
}
}
}
$response->success(['counted_license_plates' => $counted_plates, 'total_unique_plates' => count($counted_plates)]);
});
}
protected function FORMAT_LICENSE_PLATE(string $plate): string
{
// Remove all non-alphanumeric characters
$cleaned = preg_replace('/[^A-Za-z0-9]/', '', $plate);
// Convert to uppercase
return strtoupper($cleaned);
}
}