diff --git a/php/classes/AmdaAction.php b/php/classes/AmdaAction.php index 8342efa..6c5399f 100644 --- a/php/classes/AmdaAction.php +++ b/php/classes/AmdaAction.php @@ -842,9 +842,9 @@ class AmdaAction } /* -* Get Jobs en cours +* Get running jobs */ - public function getJobs($obj = null) + public function getJobs($obj = null) { require_once(INTEGRATION_SRC_DIR."RequestManager.php"); return $this->executeRequest($obj, FunctionTypeEnumClass::PROCESSRUNNINGINFO); @@ -866,7 +866,7 @@ class AmdaAction if (isset($obj->username) && isset($obj->password) && isset($obj->sessionID)) { $dd = new WSUserMgr(); - $dd->init($obj->username,$obj->password,$obj->sessionID); + $dd->init($obj->username,$obj->password,$obj->sessionID, false); // return array('success' => false, "message" => "AKKA-KERNEL-INT - WS support not implemented"); } else diff --git a/php/classes/WebServer.php b/php/classes/WebServer.php index e243aea..105b403 100644 --- a/php/classes/WebServer.php +++ b/php/classes/WebServer.php @@ -113,8 +113,6 @@ class WebServer else $this->userPWD = 'impexfp7'; -// - return array('success' => true, 'vars' => $vars); } @@ -495,7 +493,7 @@ class WebServer } - public function getStatus($data) { + public function getStatus($id) { $obj = (object)array( "username" => $this->userID, "password" => $this->userPWD, @@ -522,17 +520,23 @@ class WebServer // "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); + if (intval($jobs['nInProgress']) > 0) { + foreach ($jobs['jobsInProgress'] as $job) { + if ($job['id'] == $id) { + return ['success' => true, 'status' => 'in_progress']; + } + } + } + if (intval($jobs['nFinished']) > 0) { + foreach ($jobs['jobsFinished'] as $job) { + if ($job['id'] == $id) { + $vars = $this->getVarsFromRunningPath($job['runningPath']); + $resTempFilePath = USERPATH . $this->userID . '/RES/' . $job['folder'] . '/' . $job['result'] . $vars['kernelExtension']; + $resOutputFilePath = WSRESULT.$vars['dataFileName'].$vars['wsExtension']; + return $this->finishDownloadRequest($job['id'], $resTempFilePath, $resOutputFilePath); + } } } - return ['success' => false, 'message' => 'No job found for this id.']; } @@ -597,7 +601,9 @@ class WebServer public function getNewToken() { - $timeStamp = (new DateTime())->getTimestamp(); + require_once(INTEGRATION_SRC_DIR."RequestManager.php"); + + $timeStamp = (new DateTime())->getTimestamp(); // generate token from timeStamp and some salt $newToken = md5(1321 * (int)($timeStamp / timeLimitQuery)); return array('success' => true, 'token' => $newToken); @@ -838,7 +844,6 @@ class WebServer array("format" => $vars["outputFormat"], "timeFormat" => $timeFormat, "gzip" => $gzip, "stream" => $stream), $dataFileName); - if ($res['success']) return $res; else { if ($this->isSoap) throw new SoapFault("request03", $res['message']); @@ -880,6 +885,49 @@ class WebServer } } + private function getFormatInfo($fileFormat, $timeFormat, $gzip) { + switch ($fileFormat) { + case 'netCDF' : + if ($this->isSoap) { + throw new SoapFault("server01", "netCDF format not implemented"); + } else { + return array('success' => false, 'message' => "netCDF format not implemented"); + } + break; + case 'VOTable' : + $fileFormat = "vot"; + $kernelExtension = ".vot"; + $wsExtension = ".xml"; + break; + case 'ASCII' : + default : + $fileFormat = "ASCII"; + $kernelExtension = ".txt"; + $wsExtension = ".txt"; + } + + switch ($timeFormat) { + case 'unixtime' : + $timeFormat = 'Timestamp'; + break; + default : + $timeFormat = 'YYYY-MM-DDThh:mm:ss'; + } + + if ($gzip == 1) { + $compression = "gzip"; + $kernelExtension .= ".gz"; + $wsExtension .= ".gz"; + } else + $compression = ""; + + return ['success' => true, + 'kernelExtension' => $kernelExtension, + 'wsExtension' => $wsExtension, + 'fileFormat' => $fileFormat, + 'timeFormat' => $timeFormat, + 'compression' => $compression]; + } protected function doDownloadRequest($interval, $paramList, $user, $formatInfo, $dataFileName) { @@ -888,40 +936,7 @@ class WebServer else $structure = 2; // not sampling - $fileExtension = ""; - switch ($formatInfo['format']) { - case 'netCDF' : - if (!$jobMgr) { - if ($this->isSoap) throw new SoapFault("server01", "netCDF format not implemented"); - else return array('success' => false, 'message' => "netCDF format not implemented"); - } - break; - case 'VOTable' : - $fileformat = "vot"; - $kernelExtension = ".vot"; - $wsExtension = ".xml"; - break; - case 'ASCII' : - default : - $fileformat = "ASCII"; - $kernelExtension = ".txt"; - $wsExtension = ".txt"; - } - - switch ($formatInfo['timeFormat']) { - case 'unixtime' : - $timeformat = 'Timestamp'; - break; - default : - $timeformat = 'YYYY-MM-DDThh:mm:ss'; - } - - if ($formatInfo['gzip'] == 1) { - $compression = "gzip"; - $kernelExtension .= ".gz"; - $wsExtension .= ".gz"; - } else - $compression = ""; + $fileInfo = $this->getFormatInfo($formatInfo['format'], $formatInfo['timeFormat'], $formatInfo['gzip']); require_once(INTEGRATION_SRC_DIR . "RequestManager.php"); IHMConfigClass::setUserName($this->userID); @@ -966,18 +981,19 @@ class WebServer "startDate" => $interval['startTime'], "stopDate" => $interval['stopTime'], "list" => $params, - "fileformat" => $fileformat, - "timeformat" => $timeformat, - "compression" => $compression, - "extension" => $wsExtension, - "disablebatch" => false + "fileformat" => $fileInfo['fileFormat'], + "timeformat" => $fileInfo['timeFormat'], + "compression" => $fileInfo['compression'], + "extension" => $fileInfo['wsExtension'], + "disablebatch" => false, + "dataFileName" => $this->dataFileName ); if (!isset($this->requestManager)) $this->requestManager = new RequestManagerClass(); try { - $downloadResult = $this->requestManager->runIHMRequest($this->userID, $this->getIPclient(), FunctionTypeEnumClass::PARAMS, $obj); + $downloadResult = $this->requestManager->runIHMRequest($this->userID, $this->getIPclient(), FunctionTypeEnumClass::PARAMS, $obj); } catch (Exception $e) { if ($this->isSoap) throw new SoapFault("server02", 'Exception detected : ' . $e->getMessage()); else return array('success' => false, 'message' => 'Exception detected : ' . $e->getMessage()); @@ -991,43 +1007,37 @@ class WebServer if($downloadResult['status'] == 'in_progress') { return ['success' => true, 'status' => 'in_progress', 'id' => $downloadResult['id']]; } elseif ($downloadResult['status'] == 'done') { - return $this->finishDownloadRequest($downloadResult); + $resTempFilePath = USERPATH . $this->userID . '/RES/' . $downloadResult['folder'] . '/' . $downloadResult['result'] . $fileInfo['kernelExtension']; + $resOutputFilePath = WSRESULT.$this->dataFileName.$fileInfo['wsExtension']; + return $this->finishDownloadRequest($downloadResult['id'], $resTempFilePath, $resOutputFilePath); } else { return ['success' => false, 'message' => 'Unknown status ' . $downloadResult['status'] . '.']; } } - private function finishDownloadRequest($downloadResult) + private function finishDownloadRequest($id, $resTempFilePath, $resOutputFilePath) { - 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); + if (!file_exists($resTempFilePath)) { + if ($this->isSoap) throw new SoapFault("server04", 'Cannot retrieve result file ' . $resTempFilePath); 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); + rename($resTempFilePath, $resOutputFilePath); + chmod($resOutputFilePath, 0664); + $outputURL = 'http://' . str_replace(BASE_PATH, $_SERVER['SERVER_NAME'] . APACHE_ALIAS, $resOutputFilePath); - $obj = (object)array( - 'id' => $downloadResult['id'] + $obj = (object)array( + 'id' => $id ); + require_once(INTEGRATION_SRC_DIR . "RequestManager.php"); + if (!isset($this->requestManager)) + $this->requestManager = new RequestManagerClass(); + try { $downloadResult = $this->requestManager->runIHMRequest($this->userID, $this->getIPclient(), FunctionTypeEnumClass::PROCESSDELETE, $obj); } catch (Exception $e) { - //Nothing to do + error_log("Can not delete file $resOutputFilePath: $e"); } return array('success' => true, 'status' => 'done', 'dataFileURLs' => $outputURL); @@ -1035,7 +1045,6 @@ class WebServer protected function timeIntervalToDuration($startTime, $stopTime) { - $duration = strtotime($stopTime) - strtotime($startTime); $durationDay = intval($duration / (86400)); $duration = $duration - $durationDay * 86400; @@ -1077,7 +1086,52 @@ class WebServer } } - private function compress($srcName, $dstName) + private function getVarsFromRunningPath($runningPath) + { + $filePath = $runningPath . "request_0.xml"; + if (file_exists($filePath)) { + $requestXml = new DOMDocument(); + $requestXml->load($filePath); + } else { + return ['success' => false, 'message' => 'Failed to open request file.']; + } + + $fileFormat = $requestXml->getElementsByTagName('fileFormat')->item(0)->nodeValue; + $timeFormat = $requestXml->getElementsByTagName('timeFormat')->item(0)->nodeValue; + $gzip = 0; // todo $requestXml->getElementsByTagName('gzip')->item(0)->nodeValue; + + // get kernelExtension, wsExtension, fileFormat, timeFormat, compression: + $vars = $this->getFormatInfo($fileFormat, $timeFormat, $gzip); + + require_once(INTEGRATION_SRC_DIR . "InputOutput/IHMImpl/Tools/CommonClass.php"); + $cc = new CommonClass(); + + $vars['parameterID'] = $requestXml->getElementsByTagName('param')->item(0)->getAttribute('id'); + + $ddStart = $requestXml->getElementsByTagName('startTime')->item(0)->nodeValue; + $ddInterval = $requestXml->getElementsByTagName('timeInterval')->item(0)->nodeValue; + $vars['startTime'] = $cc->DDTimeToIso($ddStart); + $vars['stopTime'] = $cc->DDStartIntervalToStopIso($ddStart, $ddInterval); + + $sampling = $requestXml->getElementsByTagName('sampling')->item(0); + if (!is_null($sampling)) + $vars['sampling'] = $sampling->nodeValue; + + if (in_array(null, $vars, true)) { + return ['success' => false, + 'message' => 'Can not create data file name because a value is missing in the request file.']; + } + + $getDataFileName = $this->getDataFileName($vars, false); + if (!$getDataFileName['success']) { + return $getDataFileName; + } + $vars['dataFileName'] = $getDataFileName['fileName']; + + return $vars; + } + + private function compress($srcName, $dstName) { $fp = fopen($srcName, "r"); diff --git a/php/rest/getStatus.php b/php/rest/getStatus.php index f146e43..c74f3cd 100644 --- a/php/rest/getStatus.php +++ b/php/rest/getStatus.php @@ -13,6 +13,6 @@ 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); + $result = $amda_ws->getStatus($_GET['id']); } echo json_encode($result); \ No newline at end of file -- libgit2 0.21.2