From 55bc9c059d8aca31c35692c6bff75805f5b4a29f Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 24 Mar 2014 12:15:36 -0700 Subject: [PATCH] show stats for node at /nodes/UUID --- domain-server/resources/web/js/tables.js | 2 +- domain-server/src/DomainServer.cpp | 36 ++++++++++++++++++++---- domain-server/src/DomainServerNodeData.h | 2 ++ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/domain-server/resources/web/js/tables.js b/domain-server/resources/web/js/tables.js index d0855d7967..5080087692 100644 --- a/domain-server/resources/web/js/tables.js +++ b/domain-server/resources/web/js/tables.js @@ -42,7 +42,7 @@ $(document).ready(function(){ $(document.body).on('click', '.glyphicon-remove', function(){ // fire off a delete for this node $.ajax({ - url: "/node/" + $(this).data('uuid'), + url: "/nodes/" + $(this).data('uuid'), type: 'DELETE', success: function(result) { console.log("Succesful request to delete node."); diff --git a/domain-server/src/DomainServer.cpp b/domain-server/src/DomainServer.cpp index 86bc557a74..1b867501d2 100644 --- a/domain-server/src/DomainServer.cpp +++ b/domain-server/src/DomainServer.cpp @@ -655,7 +655,7 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QString& const QString JSON_MIME_TYPE = "application/json"; const QString URI_ASSIGNMENT = "/assignment"; - const QString URI_NODE = "/node"; + const QString URI_NODES = "/nodes"; if (connection->requestOperation() == QNetworkAccessManager::GetOperation) { if (path == "/assignments.json") { @@ -702,7 +702,7 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QString& // we've processed this request return true; - } else if (path == "/nodes.json") { + } else if (path == QString("%1.json").arg(URI_NODES)) { // setup the JSON QJsonObject rootJSON; QJsonObject nodesJSON; @@ -723,14 +723,36 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QString& // send the response connection->respond(HTTPConnection::StatusCode200, nodesDocument.toJson(), qPrintable(JSON_MIME_TYPE)); + + return true; + } else { + const QString NODE_REGEX_STRING = + QString("\\%1\\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\\/?$").arg(URI_NODES); + QRegExp nodeShowRegex(NODE_REGEX_STRING); + + if (nodeShowRegex.indexIn(path) != -1) { + QUuid matchingUUID = QUuid(nodeShowRegex.cap(1)); + + // see if we have a node that matches this ID + SharedNodePointer matchingNode = NodeList::getInstance()->nodeWithUUID(matchingUUID); + if (matchingNode) { + // create a QJsonDocument with the stats QJsonObject + QJsonDocument statsDocument(reinterpret_cast(matchingNode->getLinkedData()) + ->getStatsJSONObject()); + + // send the resposne + connection->respond(HTTPConnection::StatusCode200, statsDocument.toJson(), qPrintable(JSON_MIME_TYPE)); + + // tell the caller we processed the request + return true; + } + } } } else if (connection->requestOperation() == QNetworkAccessManager::PostOperation) { if (path == URI_ASSIGNMENT) { // this is a script upload - ask the HTTPConnection to parse the form data QList formData = connection->parseFormData(); - - // check how many instances of this assignment the user wants by checking the ASSIGNMENT-INSTANCES header const QString ASSIGNMENT_INSTANCES_HEADER = "ASSIGNMENT-INSTANCES"; @@ -770,13 +792,15 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QString& // respond with a 200 code for successful upload connection->respond(HTTPConnection::StatusCode200); + + return true; } } else if (connection->requestOperation() == QNetworkAccessManager::DeleteOperation) { - if (path.startsWith(URI_NODE)) { + if (path.startsWith(URI_NODES)) { // this is a request to DELETE a node by UUID // pull the UUID from the url - QUuid deleteUUID = QUuid(path.mid(URI_NODE.size() + sizeof('/'))); + QUuid deleteUUID = QUuid(path.mid(URI_NODES.size() + sizeof('/'))); if (!deleteUUID.isNull()) { SharedNodePointer nodeToKill = NodeList::getInstance()->nodeWithUUID(deleteUUID); diff --git a/domain-server/src/DomainServerNodeData.h b/domain-server/src/DomainServerNodeData.h index 8035147bef..20531839f4 100644 --- a/domain-server/src/DomainServerNodeData.h +++ b/domain-server/src/DomainServerNodeData.h @@ -19,6 +19,8 @@ public: DomainServerNodeData(); int parseData(const QByteArray& packet) { return 0; } + const QJsonObject& getStatsJSONObject() const { return _statsJSONObject; } + void parseJSONStatsPacket(const QByteArray& statsPacket); void setStaticAssignmentUUID(const QUuid& staticAssignmentUUID) { _staticAssignmentUUID = staticAssignmentUUID; }