Files
dev-chiefworks f76abffdcd first commit
2022-05-31 16:21:53 -04:00

145 lines
5.2 KiB
PHP

<?php
$job_name = pathinfo(__FILE__, PATHINFO_FILENAME);
echo "[".date("Y-m-d H:i:s")."] ".$job_name." job is starting.\n";
set_time_limit(0); // No limit!
require('../../backend.php');
require('../common/QuoteApi.php');
$httpAuthToken = $savvyext->cfgReadChar('system.oauth2_token');
$encryptionAlg = $savvyext->cfgReadChar('encryption.algorithm');
$encryptionKey = $savvyext->cfgReadChar('encryption.key');
$encryptionIV = $savvyext->cfgReadChar('encryption.iv');
$baseURL = $savvyext->cfgReadChar('system.api_url');
$db_host = $savvyext->cfgReadChar('database.host');
$db_name = $savvyext->cfgReadChar('database.name');
$db_user = $savvyext->cfgReadChar('database.user');
$db_pass = $savvyext->cfgReadChar('database.pass');
$db_port = $savvyext->cfgReadLong('database.port');
$connstr = "host=${db_host} port=${db_port} dbname=${db_name} user=${db_user} password=${db_pass}";
$conn = pg_connect($connstr);
$country = 'SG';
$member_id = 0;
// ComfortDelGro & Gojek
$transport_providers = [4,5];
// Grab
//$transport_providers = [3];
// Step 1: Load batch files
$origins = load_batch_file("batch_quote_file_origins.txt");
$destinations = load_batch_file("batch_quote_file_destinations.txt");
// Step 2: Geocode addresses
$newOrigins = geocode_addresses($origins);
$newDestinations = geocode_addresses($destinations);
// Step 3: Schedule quotes
$checkQuotes = [];
QuoteApi::$job_name = $job_name;
foreach ($newOrigins as $origin_id=>$origin) {
foreach ($newDestinations as $destination_id=>$destination) {
foreach ($transport_providers as $transport_provider) {
$q = "SELECT * FROM quotes ";
$q.= " WHERE location_start_id=${origin_id} AND location_end_id=${destination_id} ";
$q.= " AND transport_provider_id=${transport_provider} AND cost>0 ";
$q.= " ORDER BY completed DESC LIMIT 1";
$r = pg_query($conn, $q);
if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
echo "[".date("Y-m-d H:i:s")."] Quote ".$f["cost"]." already exists ID #".$f["id"]."\n";
continue;
}
list($res,$id) = QuoteApi::schedule_quote($origin,$destination,$country,$member_id,$transport_provider);
if ($res>0) {
if ($res==2) {
echo "[".date("Y-m-d H:i:s")."] Quote complete! ID #${id}\n";
} else if ($res==1) {
echo "[".date("Y-m-d H:i:s")."] Scheduled quote ID #${id}\n";
$checkQuotes[] = $id;
} else {
echo "[".date("Y-m-d H:i:s")."] Unexpected result for ID #${id} ($res)\n";
}
} else {
echo "[".date("Y-m-d H:i:s")."] schedule_quote failed: ${res}.\n";
echo "[".date("Y-m-d H:i:s")."] $origin,$destination,$country,$member_id,$transport_provider\n";
}
}
}
}
// Step 4: Check quotes
foreach ($checkQuotes as $id) {
list($res,$cost) = QuoteApi::check_quote($id);
echo "[".date("Y-m-d H:i:s")."] Checking quote ID #${id} ($res) cost=${cost}\n";
}
function load_batch_file($filename) {
$results = [];
$fn = fopen($filename,"r");
while(! feof($fn)) {
$result = fgets($fn);
$results[] = trim($result);
}
fclose($fn);
return $results;
}
function geocode_addresses($addresses) {
$geocodedAddresses = [];
foreach ($addresses as $address) {
$params = [
"address" => $address,
"member_id" => 0,
"country" => "SG"
];
$input = http_build_query($params);
list($payload,$decrypted,$result,$body) = main_api_get('/trips/api/geocode/?',$input);
if (is_array($payload) && array_key_exists('geocode',$payload) && is_array($payload['geocode'])
&& array_key_exists('id',$payload['geocode']) && $payload['geocode']['id']>0) {
$geocodedAddresses[$payload['geocode']['id']] = $payload['geocode']['address'];
}
}
return $geocodedAddresses;
}
function main_api_get($endpoint,$input) {
global $baseURL, $encryptionAlg, $encryptionKey, $encryptionIV, $httpAuthToken;
if ($endpoint!="" && $input!="") { // minimal sanity
sleep(1);
$url = $baseURL . $endpoint . $input;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Server-Token ' . $httpAuthToken,
"client_id: BATCH"
)
);
$body = curl_exec($ch);
$result = json_decode($body,true);
$decrypted = openssl_decrypt(
hex2bin(
$result['payload']
),
$encryptionAlg,
$encryptionKey,
OPENSSL_RAW_DATA,
$encryptionIV
);
$payload = json_decode($decrypted, true);
return [$payload,$decrypted,$result,$body];
}
return [NULL,NULL,NULL,NULL];
}
echo "[".date("Y-m-d H:i:s")."] ".$job_name." job complete.\n";