Skip to content

Commit

Permalink
queryAllRemoteJobs method in, and several fixes, re #11392
Browse files Browse the repository at this point in the history
  • Loading branch information
FedeMPouzols committed Apr 1, 2015
1 parent 5a75600 commit 039e675
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 29 deletions.
@@ -1,7 +1,7 @@
#ifndef MANTID_REMOTEJOBMANAGERS_MANTIDWEBSERVICEAPIJOBMANAGER_H
#define MANTID_REMOTEJOBMANAGERS_MANTIDWEBSERVICEAPIJOBMANAGER_H

#include "MantidKernel/IRemoteJobManager.h"
#include "MantidAPI/IRemoteJobManager.h"
#include "MantidRemoteJobManagers/MantidWebServiceAPIHelper.h"

namespace Mantid {
Expand Down Expand Up @@ -34,9 +34,9 @@ File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport MantidWebServiceAPIJobManager
: public Mantid::Kernel::IRemoteJobManager {
: public Mantid::API::IRemoteJobManager {
public:
void authenticate(std::string &username, std::string &password);
void authenticate(const std::string &username, const std::string &password);

std::string
submitRemoteJob(const std::string &transactionID, const std::string &runnable,
Expand All @@ -47,13 +47,13 @@ class DLLExport MantidWebServiceAPIJobManager
const std::string &remoteFileName,
const std::string &localFileName);

std::vector<Mantid::Kernel::IRemoteJobManager::RemoteJobInfo>
std::vector<Mantid::API::IRemoteJobManager::RemoteJobInfo>
queryAllRemoteJobs() const;

std::vector<std::string>
queryRemoteFile(const std::string &transactionID) const;

Mantid::Kernel::IRemoteJobManager::RemoteJobInfo
Mantid::API::IRemoteJobManager::RemoteJobInfo
queryRemoteJob(const std::string &jobID) const;

std::string startRemoteTransaction();
Expand Down
@@ -1,14 +1,16 @@
#include "MantidAPI/RemoteJobManagerFactory.h"
#include "MantidKernel/Logger.h"
#include "MantidRemoteJobManagers/MantidWebServiceAPIHelper.h"
#include "MantidRemoteJobManagers/MantidWebServiceAPIJobManager.h"
#include "MantidRemoteJobManagers/SimpleJSON.h"

#include <fstream>

namespace Mantid {
namespace RemoteJobManagers {

// Register the manager into the RemoteJobManagerFactory
// TODO Factory TODO
// DECLARE_REMOTEJOBMANAGER(MantidWebServiceAPIJobManager);
// Register this job manager into the RemoteJobManagerFactory
DECLARE_REMOTEJOBMANAGER(MantidWebServiceAPIJobManager)

namespace {
// static logger object
Expand All @@ -19,7 +21,7 @@ using namespace Mantid::Kernel;

void MantidWebServiceAPIJobManager::abortRemoteJob(const std::string &jobID) {
std::istream &respStream =
m_helper.httpGet("/abort", std::string("JobID=") + JobID);
m_helper.httpGet("/abort", std::string("JobID=") + jobID);

if (m_helper.lastStatus() != Poco::Net::HTTPResponse::HTTP_OK) {
JSONObject resp;
Expand All @@ -30,8 +32,8 @@ void MantidWebServiceAPIJobManager::abortRemoteJob(const std::string &jobID) {
}
}

void MantidWebServiceAPIJobManager::authenticate(std::string &username,
std::string &password) {
void MantidWebServiceAPIJobManager::authenticate(const std::string &username,
const std::string &password) {
MantidWebServiceAPIHelper helper;

std::istream &respStream =
Expand All @@ -49,24 +51,20 @@ void MantidWebServiceAPIJobManager::downloadRemoteFile(
const std::string &transactionID, const std::string &remoteFileName,
const std::string &localFileName) {

std::istream &respStream = m_helper.httpGet(
"/download", std::string("TransID=") + getPropertyValue("TransactionID") +
"&File=" + getPropertyValue("RemoteFileName"));
std::istream &respStream =
m_helper.httpGet("/download", std::string("TransID=") + transactionID +
"&File=" + remoteFileName);

if (m_helper.lastStatus() == Poco::Net::HTTPResponse::HTTP_OK) {

std::string localFileName = getPropertyValue("LocalFileName");
std::ofstream outfile(localFileName.c_str());
if (outfile.good()) {
outfile << respStream.rdbuf();
outfile.close();
g_log.information() << "Downloaded '"
<< getPropertyValue("RemoteFileName") << "' to '"
<< getPropertyValue("LocalFileName") << "'"
<< std::endl;
g_log.information() << "Downloaded '" << remoteFileName << "' to '"
<< localFileName << "'" << std::endl;
} else {
throw(std::runtime_error(
std::string("Failed to open " + getPropertyValue("LocalFileName"))));
throw(std::runtime_error(std::string("Failed to open " + localFileName)));
}
} else {
JSONObject resp;
Expand All @@ -77,19 +75,105 @@ void MantidWebServiceAPIJobManager::downloadRemoteFile(
}
}

std::vector<IRemoteJobManager::RemoteJobInfo>
std::vector<Mantid::API::IRemoteJobManager::RemoteJobInfo>
MantidWebServiceAPIJobManager::queryAllRemoteJobs() const {

TODO
std::istream &respStream = m_helper.httpGet(std::string("/query"));
JSONObject resp;
try {
initFromStream(resp, respStream);
} catch (JSONParseException &) {
// Nobody else knows what a JSONParseException is, so rethrow as a
// runtime_error
throw(std::runtime_error("Error parsing data returned from the server. "
"This probably indicates a server-side error of "
"some kind."));
}

if (Poco::Net::HTTPResponse::HTTP_OK != m_helper.lastStatus()) {
std::string errMsg;
resp["Err_Msg"].getValue(errMsg);
throw(std::runtime_error(errMsg));
}

std::vector<Mantid::API::IRemoteJobManager::RemoteJobInfo> result;

std::vector<std::string> jobIds;
std::vector<std::string> jobStatusStrs;
std::vector<std::string> jobNames;
std::vector<std::string> scriptNames;
std::vector<std::string> transIds;
std::vector<std::string> submitDates;
std::vector<std::string> startDates;
std::vector<std::string> completionDates;

JSONObject::const_iterator it = resp.begin();
while (it != resp.end()) {
jobIds.push_back((*it).first);
JSONObject jobData;
(*it).second.getValue(jobData);

std::string value;
jobData["JobStatus"].getValue(value);
jobStatusStrs.push_back(value);

jobData["JobName"].getValue(value);
jobNames.push_back(value);

jobData["ScriptName"].getValue(value);
scriptNames.push_back(value);

jobData["TransID"].getValue(value);
transIds.push_back(value);

// The time stuff is actually an optional extension. We could check the
// info
// URL and see if the server implements it, but it's easier to just look
// in
// the output and see if the values are there...
if (jobData.find("SubmitDate") != jobData.end()) {
jobData["SubmitDate"].getValue(value);
submitDates.push_back(value);

jobData["StartDate"].getValue(value);
startDates.push_back(value);

jobData["CompletionDate"].getValue(value);
completionDates.push_back(value);
} else {
// push back empty strings just so all the array properties have the
// same
// number of elements
submitDates.push_back("");
startDates.push_back("");
completionDates.push_back("");
}

return std::vector<IRemoteJobManager::RemoteJobInfo>();
++it;
}

// this is done here, very inefficiently, to avoid messing up the last loop
for (size_t i = 0; i < resp.size(); ++i) {
Mantid::API::IRemoteJobManager::RemoteJobInfo info;
info.id = jobIds[i];
info.status = jobStatusStrs[i];
info.name = jobNames[i];
info.runnableName = scriptNames[i];
info.transactionID = transIds[i];
info.submitDate = DateAndTime(submitDates[i]);
info.startDate = DateAndTime(startDates[i]);
infos.completionTime = DateAndTime(completionDates[i]);
result.push_back(info);
}

return result;
}

std::vector<std::string> MantidWebServiceAPIJobManager::queryRemoteFile(
const std::string &transactionID) const {

std::istream &respStream = m_helper.httpGet(
"/files", std::string("TransID=") + transactionID);
std::istream &respStream =
m_helper.httpGet("/files", std::string("TransID=") + transactionID);
JSONObject resp;
initFromStream(resp, respStream);
std::vector<std::string> filenames;
Expand All @@ -103,7 +187,6 @@ std::vector<std::string> MantidWebServiceAPIJobManager::queryRemoteFile(
filenames.push_back(oneFile);
}

setProperty("FileNames", filenames);
} else {
std::string errMsg;
resp["Err_Msg"].getValue(errMsg);
Expand All @@ -113,10 +196,10 @@ std::vector<std::string> MantidWebServiceAPIJobManager::queryRemoteFile(
return filenames;
}

IRemoteJobManager::RemoteJobInfo
Mantid::API::IRemoteJobManager::RemoteJobInfo
MantidWebServiceAPIJobManager::queryRemoteJob(const std::string &jobID) const {

return IRemoteJobManager::RemoteJobInfo();
return Mantid::API::IRemoteJobManager::RemoteJobInfo();
}

std::string MantidWebServiceAPIJobManager::startRemoteTransaction() {
Expand Down
Expand Up @@ -26,6 +26,18 @@ class MantidWebServiceAPIHelperTest : public CxxTest::TestSuite {

TS_THROWS_NOTHING(MantidWebServiceAPIHelper h);
}

void test_defaultValues() {
MantidWebServiceAPIHelper h;

std::string sts;
TS_THROWS_NOTHING(sts = h.lastStatus());
TS_ASSERT_EQUALS(lastStatus(), Poco::Net::HTTP_OK);

std::string reason;
TS_THROWS_NOTHING(reason = h.lastStatusReason());
TS_ASSERT_EQUALS(reason, "");
}
};

#endif // MANTID_REMOTEJOGMANAGERS_MANTIDWEBSERVICEAPIHELPERTEST_H_

0 comments on commit 039e675

Please sign in to comment.