Commit bae6f5dad7efba2b2c64c4389bd7c47ccbe8a19a

Authored by Nathanael Jourdane
1 parent 9b69cb35

bugFix fileName for webservice batch mode

php/classes/AmdaAction.php
... ... @@ -842,9 +842,9 @@ class AmdaAction
842 842 }
843 843  
844 844 /*
845   -* Get Jobs en cours
  845 +* Get running jobs
846 846 */
847   - public function getJobs($obj = null)
  847 + public function getJobs($obj = null)
848 848 {
849 849 require_once(INTEGRATION_SRC_DIR."RequestManager.php");
850 850 return $this->executeRequest($obj, FunctionTypeEnumClass::PROCESSRUNNINGINFO);
... ... @@ -866,7 +866,7 @@ class AmdaAction
866 866 if (isset($obj->username) && isset($obj->password) && isset($obj->sessionID))
867 867 {
868 868 $dd = new WSUserMgr();
869   - $dd->init($obj->username,$obj->password,$obj->sessionID);
  869 + $dd->init($obj->username,$obj->password,$obj->sessionID, false);
870 870 // return array('success' => false, "message" => "AKKA-KERNEL-INT - WS support not implemented");
871 871 }
872 872 else
... ...
php/classes/WebServer.php
... ... @@ -113,8 +113,6 @@ class WebServer
113 113 else
114 114 $this->userPWD = 'impexfp7';
115 115  
116   -//
117   -
118 116 return array('success' => true, 'vars' => $vars);
119 117 }
120 118  
... ... @@ -495,7 +493,7 @@ class WebServer
495 493  
496 494 }
497 495  
498   - public function getStatus($data) {
  496 + public function getStatus($id) {
499 497 $obj = (object)array(
500 498 "username" => $this->userID,
501 499 "password" => $this->userPWD,
... ... @@ -522,17 +520,23 @@ class WebServer
522 520 // "sendToSamp":false
523 521 // }
524 522  
525   - foreach ($jobs['jobsInProgress'] as $job) {
526   - if ($job['id'] == $data['id']) {
527   - return ['success' => true, 'status' => 'in_progress'];
528   - }
529   - }
530   - foreach ($jobs['jobsFinished'] as $job) {
531   - if ($job['id'] == $data['id']) {
532   - return $this->finishDownloadRequest($job);
  523 + if (intval($jobs['nInProgress']) > 0) {
  524 + foreach ($jobs['jobsInProgress'] as $job) {
  525 + if ($job['id'] == $id) {
  526 + return ['success' => true, 'status' => 'in_progress'];
  527 + }
  528 + }
  529 + }
  530 + if (intval($jobs['nFinished']) > 0) {
  531 + foreach ($jobs['jobsFinished'] as $job) {
  532 + if ($job['id'] == $id) {
  533 + $vars = $this->getVarsFromRunningPath($job['runningPath']);
  534 + $resTempFilePath = USERPATH . $this->userID . '/RES/' . $job['folder'] . '/' . $job['result'] . $vars['kernelExtension'];
  535 + $resOutputFilePath = WSRESULT.$vars['dataFileName'].$vars['wsExtension'];
  536 + return $this->finishDownloadRequest($job['id'], $resTempFilePath, $resOutputFilePath);
  537 + }
533 538 }
534 539 }
535   -
536 540 return ['success' => false, 'message' => 'No job found for this id.'];
537 541 }
538 542  
... ... @@ -597,7 +601,9 @@ class WebServer
597 601  
598 602 public function getNewToken()
599 603 {
600   - $timeStamp = (new DateTime())->getTimestamp();
  604 + require_once(INTEGRATION_SRC_DIR."RequestManager.php");
  605 +
  606 + $timeStamp = (new DateTime())->getTimestamp();
601 607 // generate token from timeStamp and some salt
602 608 $newToken = md5(1321 * (int)($timeStamp / timeLimitQuery));
603 609 return array('success' => true, 'token' => $newToken);
... ... @@ -838,7 +844,6 @@ class WebServer
838 844 array("format" => $vars["outputFormat"], "timeFormat" => $timeFormat, "gzip" => $gzip, "stream" => $stream),
839 845 $dataFileName);
840 846  
841   -
842 847 if ($res['success']) return $res;
843 848 else {
844 849 if ($this->isSoap) throw new SoapFault("request03", $res['message']);
... ... @@ -880,6 +885,49 @@ class WebServer
880 885 }
881 886 }
882 887  
  888 + private function getFormatInfo($fileFormat, $timeFormat, $gzip) {
  889 + switch ($fileFormat) {
  890 + case 'netCDF' :
  891 + if ($this->isSoap) {
  892 + throw new SoapFault("server01", "netCDF format not implemented");
  893 + } else {
  894 + return array('success' => false, 'message' => "netCDF format not implemented");
  895 + }
  896 + break;
  897 + case 'VOTable' :
  898 + $fileFormat = "vot";
  899 + $kernelExtension = ".vot";
  900 + $wsExtension = ".xml";
  901 + break;
  902 + case 'ASCII' :
  903 + default :
  904 + $fileFormat = "ASCII";
  905 + $kernelExtension = ".txt";
  906 + $wsExtension = ".txt";
  907 + }
  908 +
  909 + switch ($timeFormat) {
  910 + case 'unixtime' :
  911 + $timeFormat = 'Timestamp';
  912 + break;
  913 + default :
  914 + $timeFormat = 'YYYY-MM-DDThh:mm:ss';
  915 + }
  916 +
  917 + if ($gzip == 1) {
  918 + $compression = "gzip";
  919 + $kernelExtension .= ".gz";
  920 + $wsExtension .= ".gz";
  921 + } else
  922 + $compression = "";
  923 +
  924 + return ['success' => true,
  925 + 'kernelExtension' => $kernelExtension,
  926 + 'wsExtension' => $wsExtension,
  927 + 'fileFormat' => $fileFormat,
  928 + 'timeFormat' => $timeFormat,
  929 + 'compression' => $compression];
  930 + }
883 931  
884 932 protected function doDownloadRequest($interval, $paramList, $user, $formatInfo, $dataFileName)
885 933 {
... ... @@ -888,40 +936,7 @@ class WebServer
888 936 else
889 937 $structure = 2; // not sampling
890 938  
891   - $fileExtension = "";
892   - switch ($formatInfo['format']) {
893   - case 'netCDF' :
894   - if (!$jobMgr) {
895   - if ($this->isSoap) throw new SoapFault("server01", "netCDF format not implemented");
896   - else return array('success' => false, 'message' => "netCDF format not implemented");
897   - }
898   - break;
899   - case 'VOTable' :
900   - $fileformat = "vot";
901   - $kernelExtension = ".vot";
902   - $wsExtension = ".xml";
903   - break;
904   - case 'ASCII' :
905   - default :
906   - $fileformat = "ASCII";
907   - $kernelExtension = ".txt";
908   - $wsExtension = ".txt";
909   - }
910   -
911   - switch ($formatInfo['timeFormat']) {
912   - case 'unixtime' :
913   - $timeformat = 'Timestamp';
914   - break;
915   - default :
916   - $timeformat = 'YYYY-MM-DDThh:mm:ss';
917   - }
918   -
919   - if ($formatInfo['gzip'] == 1) {
920   - $compression = "gzip";
921   - $kernelExtension .= ".gz";
922   - $wsExtension .= ".gz";
923   - } else
924   - $compression = "";
  939 + $fileInfo = $this->getFormatInfo($formatInfo['format'], $formatInfo['timeFormat'], $formatInfo['gzip']);
925 940  
926 941 require_once(INTEGRATION_SRC_DIR . "RequestManager.php");
927 942 IHMConfigClass::setUserName($this->userID);
... ... @@ -966,18 +981,19 @@ class WebServer
966 981 "startDate" => $interval['startTime'],
967 982 "stopDate" => $interval['stopTime'],
968 983 "list" => $params,
969   - "fileformat" => $fileformat,
970   - "timeformat" => $timeformat,
971   - "compression" => $compression,
972   - "extension" => $wsExtension,
973   - "disablebatch" => false
  984 + "fileformat" => $fileInfo['fileFormat'],
  985 + "timeformat" => $fileInfo['timeFormat'],
  986 + "compression" => $fileInfo['compression'],
  987 + "extension" => $fileInfo['wsExtension'],
  988 + "disablebatch" => false,
  989 + "dataFileName" => $this->dataFileName
974 990 );
975 991  
976 992 if (!isset($this->requestManager))
977 993 $this->requestManager = new RequestManagerClass();
978 994  
979 995 try {
980   - $downloadResult = $this->requestManager->runIHMRequest($this->userID, $this->getIPclient(), FunctionTypeEnumClass::PARAMS, $obj);
  996 + $downloadResult = $this->requestManager->runIHMRequest($this->userID, $this->getIPclient(), FunctionTypeEnumClass::PARAMS, $obj);
981 997 } catch (Exception $e) {
982 998 if ($this->isSoap) throw new SoapFault("server02", 'Exception detected : ' . $e->getMessage());
983 999 else return array('success' => false, 'message' => 'Exception detected : ' . $e->getMessage());
... ... @@ -991,43 +1007,37 @@ class WebServer
991 1007 if($downloadResult['status'] == 'in_progress') {
992 1008 return ['success' => true, 'status' => 'in_progress', 'id' => $downloadResult['id']];
993 1009 } elseif ($downloadResult['status'] == 'done') {
994   - return $this->finishDownloadRequest($downloadResult);
  1010 + $resTempFilePath = USERPATH . $this->userID . '/RES/' . $downloadResult['folder'] . '/' . $downloadResult['result'] . $fileInfo['kernelExtension'];
  1011 + $resOutputFilePath = WSRESULT.$this->dataFileName.$fileInfo['wsExtension'];
  1012 + return $this->finishDownloadRequest($downloadResult['id'], $resTempFilePath, $resOutputFilePath);
995 1013 } else {
996 1014 return ['success' => false, 'message' => 'Unknown status ' . $downloadResult['status'] . '.'];
997 1015 }
998 1016 }
999 1017  
1000   - private function finishDownloadRequest($downloadResult)
  1018 + private function finishDownloadRequest($id, $resTempFilePath, $resOutputFilePath)
1001 1019 {
1002   - require_once(INTEGRATION_SRC_DIR . "RequestManager.php");
1003   - IHMConfigClass::setUserName($this->userID);
1004   - if (!isset($this->paramLoader))
1005   - $this->paramLoader = new IHMUserParamLoaderClass();
1006   - if (!isset($this->requestManager))
1007   - $this->requestManager = new RequestManagerClass();
1008   -
1009   - $kernelExtension = '.txt'; // TODO: handle other formats
1010   - $wsExtension = '.txt';
1011   - $resultFile = USERPATH . $this->userID . '/RES/' . $downloadResult['folder'] . '/' . $downloadResult['result'] . $kernelExtension;
1012   - $outputFile = WSRESULT . $this->dataFileName . $wsExtension;
1013   -
1014   - if (!file_exists($resultFile)) {
1015   - if ($this->isSoap) throw new SoapFault("server04", 'Cannot retrieve result file ' . $resultFile);
  1020 + if (!file_exists($resTempFilePath)) {
  1021 + if ($this->isSoap) throw new SoapFault("server04", 'Cannot retrieve result file ' . $resTempFilePath);
1016 1022 else return ['success' => false, 'message' => 'Cannot retrieve result file'];
1017 1023 }
1018 1024  
1019   - rename($resultFile, $outputFile);
1020   - chmod($outputFile, 0664);
1021   - $outputURL = 'http://' . str_replace(BASE_PATH, $_SERVER['SERVER_NAME'] . APACHE_ALIAS, $outputFile);
  1025 + rename($resTempFilePath, $resOutputFilePath);
  1026 + chmod($resOutputFilePath, 0664);
  1027 + $outputURL = 'http://' . str_replace(BASE_PATH, $_SERVER['SERVER_NAME'] . APACHE_ALIAS, $resOutputFilePath);
1022 1028  
1023   - $obj = (object)array(
1024   - 'id' => $downloadResult['id']
  1029 + $obj = (object)array(
  1030 + 'id' => $id
1025 1031 );
1026 1032  
  1033 + require_once(INTEGRATION_SRC_DIR . "RequestManager.php");
  1034 + if (!isset($this->requestManager))
  1035 + $this->requestManager = new RequestManagerClass();
  1036 +
1027 1037 try {
1028 1038 $downloadResult = $this->requestManager->runIHMRequest($this->userID, $this->getIPclient(), FunctionTypeEnumClass::PROCESSDELETE, $obj);
1029 1039 } catch (Exception $e) {
1030   - //Nothing to do
  1040 + error_log("Can not delete file $resOutputFilePath: $e");
1031 1041 }
1032 1042  
1033 1043 return array('success' => true, 'status' => 'done', 'dataFileURLs' => $outputURL);
... ... @@ -1035,7 +1045,6 @@ class WebServer
1035 1045  
1036 1046 protected function timeIntervalToDuration($startTime, $stopTime)
1037 1047 {
1038   -
1039 1048 $duration = strtotime($stopTime) - strtotime($startTime);
1040 1049 $durationDay = intval($duration / (86400));
1041 1050 $duration = $duration - $durationDay * 86400;
... ... @@ -1077,7 +1086,52 @@ class WebServer
1077 1086 }
1078 1087 }
1079 1088  
1080   - private function compress($srcName, $dstName)
  1089 + private function getVarsFromRunningPath($runningPath)
  1090 + {
  1091 + $filePath = $runningPath . "request_0.xml";
  1092 + if (file_exists($filePath)) {
  1093 + $requestXml = new DOMDocument();
  1094 + $requestXml->load($filePath);
  1095 + } else {
  1096 + return ['success' => false, 'message' => 'Failed to open request file.'];
  1097 + }
  1098 +
  1099 + $fileFormat = $requestXml->getElementsByTagName('fileFormat')->item(0)->nodeValue;
  1100 + $timeFormat = $requestXml->getElementsByTagName('timeFormat')->item(0)->nodeValue;
  1101 + $gzip = 0; // todo $requestXml->getElementsByTagName('gzip')->item(0)->nodeValue;
  1102 +
  1103 + // get kernelExtension, wsExtension, fileFormat, timeFormat, compression:
  1104 + $vars = $this->getFormatInfo($fileFormat, $timeFormat, $gzip);
  1105 +
  1106 + require_once(INTEGRATION_SRC_DIR . "InputOutput/IHMImpl/Tools/CommonClass.php");
  1107 + $cc = new CommonClass();
  1108 +
  1109 + $vars['parameterID'] = $requestXml->getElementsByTagName('param')->item(0)->getAttribute('id');
  1110 +
  1111 + $ddStart = $requestXml->getElementsByTagName('startTime')->item(0)->nodeValue;
  1112 + $ddInterval = $requestXml->getElementsByTagName('timeInterval')->item(0)->nodeValue;
  1113 + $vars['startTime'] = $cc->DDTimeToIso($ddStart);
  1114 + $vars['stopTime'] = $cc->DDStartIntervalToStopIso($ddStart, $ddInterval);
  1115 +
  1116 + $sampling = $requestXml->getElementsByTagName('sampling')->item(0);
  1117 + if (!is_null($sampling))
  1118 + $vars['sampling'] = $sampling->nodeValue;
  1119 +
  1120 + if (in_array(null, $vars, true)) {
  1121 + return ['success' => false,
  1122 + 'message' => 'Can not create data file name because a value is missing in the request file.'];
  1123 + }
  1124 +
  1125 + $getDataFileName = $this->getDataFileName($vars, false);
  1126 + if (!$getDataFileName['success']) {
  1127 + return $getDataFileName;
  1128 + }
  1129 + $vars['dataFileName'] = $getDataFileName['fileName'];
  1130 +
  1131 + return $vars;
  1132 + }
  1133 +
  1134 + private function compress($srcName, $dstName)
1081 1135 {
1082 1136  
1083 1137 $fp = fopen($srcName, "r");
... ...
php/rest/getStatus.php
... ... @@ -13,6 +13,6 @@ if (!key_exists("id", $_GET)) {
13 13 $result = array('success' => false, 'message' => "You must provide a job id.");
14 14 } else {
15 15 $amda_ws = new WebServer();
16   - $result = $amda_ws->getStatus($_GET);
  16 + $result = $amda_ws->getStatus($_GET['id']);
17 17 }
18 18 echo json_encode($result);
19 19 \ No newline at end of file
... ...