From 9b69cb352b7e273adf20c608e5fee40b02f229fd Mon Sep 17 00:00:00 2001 From: Nathanaël Jourdane Date: Wed, 31 Jan 2018 13:59:24 +0100 Subject: [PATCH] Implement batch mode for getParameter REST api --- php/classes/AmdaAction.php | 5 ++--- php/classes/RequestMgr.php | 26 ++++++++++++++------------ php/classes/UserMgr.php | 6 +----- php/classes/WebServer.php | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------- php/rest/getParameter.php | 12 +++--------- php/rest/getStatus.php | 17 ++++++++--------- 6 files changed, 102 insertions(+), 64 deletions(-) diff --git a/php/classes/AmdaAction.php b/php/classes/AmdaAction.php index 05f39c5..8342efa 100644 --- a/php/classes/AmdaAction.php +++ b/php/classes/AmdaAction.php @@ -844,7 +844,7 @@ class AmdaAction /* * Get Jobs en cours */ - public function getJobs($obj) + public function getJobs($obj = null) { require_once(INTEGRATION_SRC_DIR."RequestManager.php"); return $this->executeRequest($obj, FunctionTypeEnumClass::PROCESSRUNNINGINFO); @@ -909,8 +909,7 @@ class AmdaAction try { $res = $requestManager->runIHMRequest($this->user, $res['userHost'], $function, $obj); - } catch (Exception $e) - { + } catch (Exception $e) { return array('success' => false, 'message' => 'Exception detected : '.$e->getMessage()); } return $res; diff --git a/php/classes/RequestMgr.php b/php/classes/RequestMgr.php index 7fe4363..bfad57a 100644 --- a/php/classes/RequestMgr.php +++ b/php/classes/RequestMgr.php @@ -247,7 +247,8 @@ class RequestMgr extends AmdaObjectMgr if ($obj->timesrc != 'Interval') break; foreach ($obj->list as $param) - { + { + //TODO not in code spase___IMPEX_ !!! //TODO other models (tsyganenko etc) if (preg_match("#^spase___IMPEX_#", $param->paramid)) @@ -255,14 +256,15 @@ class RequestMgr extends AmdaObjectMgr if ($param->template_args->url_XYZ) $params[] = $param->template_args->url_XYZ; } } - if (count($params) > 0) + if (isset($params) && count($params) > 0) { + // tab is not defined, iterate over $obj->tabs? $argsTab['param'] = array_unique($params); $argsTab['startTime'] = $tab->{'multi-plot-linked'} ? $obj->startDate : $tab->startDate; $argsTab['stopTime'] = $tab->{'multi-plot-linked'} ? $obj->startDate : $tab->stopDate; } if (count($argsTab) > 0) $args[] = $argsTab; - + break; case 'statistics' : return array('success' => true); @@ -270,23 +272,23 @@ class RequestMgr extends AmdaObjectMgr default : return array('success' => false, 'message' => "unknown action ".$obj->nodeType); } - - if (count($args) === 0) return array('success' => true); - + + if (count($args) === 0) return array('success' => true); + try { $client = new SoapClient(DD_WSDL); } catch (SoapFault $exception) - { + { return array('success' => false, 'message' => $exception->faultstring); } - + $orbitsXml = new DomDocument("1.0"); $orbitsXml->load(orbitsAllXml); $tr = array('_' => ':'); - - foreach ($args as $tab) + + foreach ($args as $tab) { $startTime = $tab['startTime']; $stopTime = $tab['stopTime']; @@ -315,8 +317,8 @@ class RequestMgr extends AmdaObjectMgr return array('success' => false, 'message' => "Too big interval for IMPEX request : ".IMPEX_INTERVAL_LIMIT/86400.." day limit!"); } } - - return array('success' => true); + + return array('success' => true); } } ?> diff --git a/php/classes/UserMgr.php b/php/classes/UserMgr.php index 0269fe6..03a65e5 100644 --- a/php/classes/UserMgr.php +++ b/php/classes/UserMgr.php @@ -491,13 +491,8 @@ class UserMgr public function ddCheckUser() { - error_log("Checking user..."); $this->IP = $this->getIPclient(); - error_log("User: " . $this->user); - error_log("IP: " . $this->IP); - $cmdCheckUser = DDBIN."DDCheckUser ".$this->IP." ".$this->user." 1> /dev/null 2> /dev/null"; - error_log("cmdCheckUser: " . $cmdCheckUser); system($cmdCheckUser, $res); return $res; } @@ -612,6 +607,7 @@ class UserMgr public function dirSize($dir) { $handle = opendir($dir); + $mas = 1024 * 1024 * 200; // fixme while ($file = readdir($handle)) { diff --git a/php/classes/WebServer.php b/php/classes/WebServer.php index a14e9ff..e243aea 100644 --- a/php/classes/WebServer.php +++ b/php/classes/WebServer.php @@ -113,7 +113,7 @@ class WebServer else $this->userPWD = 'impexfp7'; - +// return array('success' => true, 'vars' => $vars); } @@ -503,7 +503,37 @@ class WebServer ); $aa = new AmdaAction(); - return $aa->getJobs($obj); + $jobs = $aa->getJobs($obj); + +// job = +// { +// "success":true, +// "id":"process_aiaoh7_1517403874_1372", +// "name":"download_data_1517403886", +// "status":"in_progress", +// "jobType":"download", +// "info":" ", +// "start":"31-01-2018 13:04:34", +// "stop":"unknown", +// "folder":"DDpTDRrk_", +// "result":"result_pTDRrk", +// "format":"unknown", +// "compression":"", +// "sendToSamp":false +// } + + foreach ($jobs['jobsInProgress'] as $job) { + if ($job['id'] == $data['id']) { + return ['success' => true, 'status' => 'in_progress']; + } + } + foreach ($jobs['jobsFinished'] as $job) { + if ($job['id'] == $data['id']) { + return $this->finishDownloadRequest($job); + } + } + + return ['success' => false, 'message' => 'No job found for this id.']; } public function getParameterList($data) @@ -576,7 +606,6 @@ class WebServer ///////////////////////////////////////START GET DATASET /////////////////////////////// public function getParameter($data) { - $multiParam = false; $res = $this->init($data); @@ -940,6 +969,7 @@ class WebServer "fileformat" => $fileformat, "timeformat" => $timeformat, "compression" => $compression, + "extension" => $wsExtension, "disablebatch" => false ); @@ -958,31 +988,49 @@ class WebServer else return array('success' => false, 'message' => $downloadResult['message']); } - $resultFile = USERPATH . $this->userID . '/RES/' . $downloadResult['folder'] . '/' . $downloadResult['result']; - $resultFile .= $kernelExtension; - - if (!file_exists($resultFile)) { - if ($this->isSoap) throw new SoapFault("server04", 'Cannot retrieve result file ' . $resultFile); - else return array('success' => false, 'message' => 'Cannot retrieve result file'); - } - - rename($resultFile, WSRESULT . $this->dataFileName . $wsExtension); - $outputFile = WSRESULT . $this->dataFileName . $wsExtension; - chmod($outputFile, 0664); - $outputFile = 'http://' . str_replace(BASE_PATH, $_SERVER['SERVER_NAME'] . APACHE_ALIAS, $outputFile); - - $obj = (object)array( - 'id' => $downloadResult['id'] - ); - - try { - $downloadResult = $this->requestManager->runIHMRequest($this->userID, $this->getIPclient(), FunctionTypeEnumClass::PROCESSDELETE, $obj); - } catch (Exception $e) { - //Nothing to do + if($downloadResult['status'] == 'in_progress') { + return ['success' => true, 'status' => 'in_progress', 'id' => $downloadResult['id']]; + } elseif ($downloadResult['status'] == 'done') { + return $this->finishDownloadRequest($downloadResult); + } else { + return ['success' => false, 'message' => 'Unknown status ' . $downloadResult['status'] . '.']; } + } - return array('success' => true, 'dataFileURLs' => $outputFile); - + private function finishDownloadRequest($downloadResult) + { + require_once(INTEGRATION_SRC_DIR . "RequestManager.php"); + IHMConfigClass::setUserName($this->userID); + if (!isset($this->paramLoader)) + $this->paramLoader = new IHMUserParamLoaderClass(); + if (!isset($this->requestManager)) + $this->requestManager = new RequestManagerClass(); + + $kernelExtension = '.txt'; // TODO: handle other formats + $wsExtension = '.txt'; + $resultFile = USERPATH . $this->userID . '/RES/' . $downloadResult['folder'] . '/' . $downloadResult['result'] . $kernelExtension; + $outputFile = WSRESULT . $this->dataFileName . $wsExtension; + + if (!file_exists($resultFile)) { + if ($this->isSoap) throw new SoapFault("server04", 'Cannot retrieve result file ' . $resultFile); + else return ['success' => false, 'message' => 'Cannot retrieve result file']; + } + + rename($resultFile, $outputFile); + chmod($outputFile, 0664); + $outputURL = 'http://' . str_replace(BASE_PATH, $_SERVER['SERVER_NAME'] . APACHE_ALIAS, $outputFile); + + $obj = (object)array( + 'id' => $downloadResult['id'] + ); + + try { + $downloadResult = $this->requestManager->runIHMRequest($this->userID, $this->getIPclient(), FunctionTypeEnumClass::PROCESSDELETE, $obj); + } catch (Exception $e) { + //Nothing to do + } + + return array('success' => true, 'status' => 'done', 'dataFileURLs' => $outputURL); } protected function timeIntervalToDuration($startTime, $stopTime) diff --git a/php/rest/getParameter.php b/php/rest/getParameter.php index cf89202..25167e8 100644 --- a/php/rest/getParameter.php +++ b/php/rest/getParameter.php @@ -13,17 +13,11 @@ require_once '../config.php'; $amda_ws = new WebServer(); if (!key_exists("token", $_GET)) { - $result = array('success' => false, 'message' => "E01: Authentication is required for this webservice."); + $result = array('success' => false, 'message' => "Authentication is required for this webservice."); } else if ($amda_ws->getNewToken()['token'] != $_GET["token"]) { - $result = array('success' => false, 'message' => "E02: Token expired. Please authenticate again."); + $result = array('success' => false, 'status' => 'expired', 'message' => "Token expired. Please authenticate again."); } else { $result = $amda_ws->getParameter($_GET); } - -if ($result['success']) { - echo $result['dataFileURLs']; -} else { - echo $result['message']; -} - +echo json_encode($result); ?> \ No newline at end of file diff --git a/php/rest/getStatus.php b/php/rest/getStatus.php index e1aa5f0..f146e43 100644 --- a/php/rest/getStatus.php +++ b/php/rest/getStatus.php @@ -6,14 +6,13 @@ * * @version $Id: $ */ -// -// -// 3€/m <50Go -// -require_once '../config.php'; -$amda_ws = new WebServer(); -$result = $amda_ws->getStatus($_GET); +require_once '../config.php'; -echo '
' . var_export($result, true) . '
'; -?> \ No newline at end of file +if (!key_exists("id", $_GET)) { + $result = array('success' => false, 'message' => "You must provide a job id."); +} else { + $amda_ws = new WebServer(); + $result = $amda_ws->getStatus($_GET); +} +echo json_encode($result); \ No newline at end of file -- libgit2 0.21.2