292 lines
11 KiB
PHP
292 lines
11 KiB
PHP
<?php
|
|
set_time_limit(0); // No limit!
|
|
|
|
require('../backend.php');
|
|
|
|
$httpAuthToken = "99dfe35fcb7de1ee";
|
|
$encryptionAlg = "aes-256-ctr";
|
|
$encryptionKey = "1234567890abcdef1234567890abcdef";
|
|
$encryptionIV = "1234567890abcdef";
|
|
|
|
$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);
|
|
|
|
//$_GET['id'] = 8946; // DEBUG
|
|
if (isset($_GET['id']) && $_GET['id']>0) {
|
|
$q = "select a.id,a.duration,a.distance,a.location_start_id,a.location_end_id,";
|
|
$q.= "b.address as location_start,b.latitude as location_start_lat,b.longitude AS location_start_lng,";
|
|
$q.= "c.address as location_end,c.latitude as location_end_lat,c.longitude AS location_end_lng";
|
|
$q.= " from parsedemail_item a, address b, address c ";
|
|
$q.= " where a.id=".((int)$_GET['id'])." and b.id=a.location_start_id and c.id=a.location_end_id";
|
|
$r = pg_query($conn,$q);
|
|
if ($r && pg_num_rows($r) && $f=pg_fetch_assoc($r)) {
|
|
$payload = "{
|
|
\"origin\":\"".$f["location_start"]."\",
|
|
\"destination\":\"".$f["location_end"]."\",
|
|
\"member_id\":13,
|
|
\"transport_provider_id\":5,
|
|
\"trackedemail_item_id\":".$f["id"].",
|
|
\"country\":\"SG\",
|
|
\"group_quote_id\":0
|
|
}";
|
|
$encrypted_payload = bin2hex(
|
|
openssl_encrypt(
|
|
$payload,
|
|
$encryptionAlg,
|
|
$encryptionKey,
|
|
OPENSSL_RAW_DATA,
|
|
$encryptionIV
|
|
));
|
|
$postdata = "{\"encrypted_payload\": \"${encrypted_payload}\"}";
|
|
$url = "http://svrsavvy.sworks.float.sg/SAVVY/trips/api/quote";
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
|
|
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',
|
|
'Content-Length: ' . strlen($postdata),
|
|
'Authorization: Server-Token ' . $httpAuthToken)
|
|
);
|
|
|
|
$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);
|
|
|
|
error_log($body);
|
|
|
|
if (is_array($payload) && array_key_exists('id',$payload) && $payload['id']>0) {
|
|
if (array_key_exists('cost',$payload) && $payload['cost']>0) {
|
|
echo $payload['cost'];
|
|
exit();
|
|
}
|
|
if (array_key_exists('completed',$payload) && $payload['completed']!='') {
|
|
http_response_code(404);
|
|
echo "Quote service failed for " . $_GET['id'];
|
|
}
|
|
$startTime = time();
|
|
while (true) {
|
|
sleep(1);
|
|
$url = "http://svrsavvy.sworks.float.sg/SAVVY/trips/api/quote/".$payload['id'];
|
|
$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)
|
|
);
|
|
|
|
$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);
|
|
if (is_array($payload) && array_key_exists('id',$payload) && $payload['id']>0) {
|
|
if (array_key_exists('cost',$payload) && $payload['cost']>0) {
|
|
echo $payload['cost'];
|
|
exit();
|
|
}
|
|
if (array_key_exists('completed',$payload) && $payload['completed']!='') {
|
|
http_response_code(404);
|
|
echo "Quote service failed for " . $_GET['id'];
|
|
exit();
|
|
}
|
|
} else {
|
|
http_response_code(404);
|
|
echo "Quote check failed for " . $_GET['id'];
|
|
exit();
|
|
}
|
|
// >15 minutes? Give up :,(
|
|
if ((time()-$startTime)>900) {
|
|
http_response_code(404);
|
|
echo "Quote takes too long for " . $_GET['id'];
|
|
exit();
|
|
}
|
|
}
|
|
} else {
|
|
http_response_code(404);
|
|
echo "Quote start failed for " . $_GET['id'];
|
|
if (is_array($payload)) {
|
|
echo "<br/>".json_encode($payload);
|
|
} else {
|
|
echo "<br/>".$body;
|
|
}
|
|
}
|
|
} else {
|
|
http_response_code(404);
|
|
echo "Invalid ID " . $_GET['id'];
|
|
}
|
|
exit();
|
|
}
|
|
|
|
$offset = 100;
|
|
$limit = 100;
|
|
|
|
$q = "select a.id,a.duration,a.distance,a.location_start_id,a.location_end_id,";
|
|
$q.= "b.address as location_start,b.latitude as location_start_lat,b.longitude AS location_start_lng,";
|
|
$q.= "c.address as location_end,c.latitude as location_end_lat,c.longitude AS location_end_lng";
|
|
$q.= " from parsedemail_item a, address b, address c ";
|
|
$q.= " where a.dup_id is null and b.id=a.location_start_id and c.id=a.location_end_id";
|
|
$q.= " and b.country='SG' AND c.country='SG' offset ${offset} limit ${limit}";
|
|
//echo $q;
|
|
$r = pg_query($conn,$q);
|
|
/*
|
|
savvy=> \d address
|
|
Table "public.address"
|
|
Column | Type | Modifiers
|
|
----------------+------------------------+------------------------------------------------------
|
|
id | bigint | not null default nextval('address_id_seq'::regclass)
|
|
address | character varying(200) |
|
|
latitude | numeric |
|
|
longitude | numeric |
|
|
timezone | integer |
|
|
geocoding_date | date |
|
|
postal | character varying(40) |
|
|
country | character varying(2) | default 'SG'::character varying
|
|
geometry | geography(Point,4326) |
|
|
description | character varying(100) |
|
|
*/
|
|
?>
|
|
|
|
<!doctype html>
|
|
<html lang="en" dir="ltr">
|
|
<head>
|
|
<title>Gojek Quote Load Test</title>
|
|
</head>
|
|
<body>
|
|
<button id="start" onclick="return startLoad(this);">Start <img src="1-0.gif" alt="Loading..." /> loading</button>
|
|
<div id="source" style="overflow:scroll; height:600px;">
|
|
<table border=0>
|
|
<thead>
|
|
<th>N#</th>
|
|
<th>ID</th>
|
|
<th>Duration</th>
|
|
<th>Distance</th>
|
|
<th>Address From</th>
|
|
<th>Address To</th>
|
|
<th>Quote</th>
|
|
<th>Elapsed</th>
|
|
<thead>
|
|
<tbody>
|
|
<?
|
|
$quotes = [];
|
|
if ($r && pg_num_rows($r)) {
|
|
$ps = $pe = NULL; $j=1;
|
|
while ($f=pg_fetch_assoc($r)) {
|
|
if ($ps==$f["location_start"] && $pe==$f["location_end"]) continue;
|
|
if ($f["location_start"]=="" || $f["location_end"]=="") continue;
|
|
$quotes[] = $f["id"];
|
|
?>
|
|
<tr>
|
|
<td><?=$j++;?></td>
|
|
<td><?=$f["id"]?></td>
|
|
<td align=right><?=sprintf("%0.02f",$f["duration"]/60)?></td>
|
|
<td align=right><?=sprintf("%0.02f",$f["distance"]>999?($f["distance"]/1000):$f["distance"])?></td>
|
|
<td><?=$f["location_start"]?></td>
|
|
<td><?=$f["location_end"]?></td>
|
|
<td><div id="quote<?=$f["id"]?>">N/A</div></td>
|
|
<td><div id="elapsed<?=$f["id"]?>">0 sec</div></td>
|
|
</tr>
|
|
<? $ps = $f["location_start"]; $pe = $f["location_end"]; }} ?>
|
|
<tbody>
|
|
</table>
|
|
</div>
|
|
<div id="console" style="overflow:scroll; height:600px; background:#eee;">
|
|
</div>
|
|
<script type="text/javascript">
|
|
<!--
|
|
var ids = [<?=implode(',',$quotes)?>];
|
|
var running = 0;
|
|
var started = null;
|
|
function startLoad(btn) {
|
|
if (!confirm('Are you sure you want to start load?')) {
|
|
return false;
|
|
}
|
|
btn.disabled = true;
|
|
started = new Date();
|
|
ids.forEach(id=>{
|
|
//console.log(id);
|
|
//document.getElementById('console').innerHTML += id + '<br/>';
|
|
quote(id);
|
|
});
|
|
return false;
|
|
}
|
|
function quote(id) {
|
|
var xmlhttp = new XMLHttpRequest();
|
|
if (xmlhttp!=null) {
|
|
var url = "?id="+id;
|
|
var start = new Date();
|
|
xmlhttp.onreadystatechange = function()
|
|
{
|
|
if(this.readyState==4 && this.status == 200)
|
|
{
|
|
// Get data from the server's response
|
|
parseResponse(this.responseText,id,start);
|
|
}
|
|
else if (this.readyState == 4 && this.status != 200)
|
|
{
|
|
parseResponse('Error',id,start);
|
|
document.getElementById('console').innerHTML += this.status + '<br/>\r';
|
|
document.getElementById('console').innerHTML += this.responseXML + '<br/>\r';
|
|
document.getElementById('console').innerHTML += this.statusText + '<br/>\r';
|
|
document.getElementById('console').innerHTML += this.responseText + '<br/>\r';
|
|
//alert(this.responseXML+'/'+this.responseText+'/'+this.statusText);
|
|
//alert(this.getAllResponseHeaders());
|
|
}
|
|
}
|
|
document.getElementById('elapsed'+id).innerHTML = '<img src="1-0.gif" alt="Loading..." />';
|
|
document.getElementById('console').innerHTML += 'Starting #' + id + ' on '+ start +'<br/>\r';
|
|
//alert(url);
|
|
xmlhttp.open("GET",url,true);
|
|
xmlhttp.send(null);
|
|
running++;
|
|
}
|
|
}
|
|
function parseResponse(response,id,start) {
|
|
var c = ((new Date()).getTime() - start.getTime())/1000;
|
|
document.getElementById('quote'+id).innerHTML = response;
|
|
document.getElementById('elapsed'+id).innerHTML = c;
|
|
document.getElementById('console').innerHTML += 'Complete #' + id + ' in ' + c + ' seconds<br/>\r';
|
|
running--;
|
|
if (running<1) {
|
|
var c = ((new Date()).getTime() - started.getTime())/1000;
|
|
running = 0;
|
|
document.getElementById('start').disabled = false;
|
|
document.getElementById('console').innerHTML += 'All complete in ' + c + ' seconds.<br/>\n';
|
|
}
|
|
}
|
|
// -->
|
|
</script>
|
|
</body>
|
|
</html>
|