From 7944301f2ec9728cd135dea512c2bcba52dafc68 Mon Sep 17 00:00:00 2001 From: Michael Hackstein Date: Mon, 30 Jun 2014 16:51:29 +0200 Subject: [PATCH] Fixed requesting of random vertices in graph viewer. Warning for empty graph will now displayed only if the graph is really empty, not on bad luck --- .../js/graphViewer/graph/gharialAdapter.js | 40 ++++++------------- .../frontend/js/graphViewer/graphViewer.js | 2 +- .../js/graphViewer/ui/graphViewerUI.js | 2 +- 3 files changed, 14 insertions(+), 30 deletions(-) diff --git a/js/apps/system/aardvark/frontend/js/graphViewer/graph/gharialAdapter.js b/js/apps/system/aardvark/frontend/js/graphViewer/graph/gharialAdapter.js index 26ad8c6102..fe3b332c63 100644 --- a/js/apps/system/aardvark/frontend/js/graphViewer/graph/gharialAdapter.js +++ b/js/apps/system/aardvark/frontend/js/graphViewer/graph/gharialAdapter.js @@ -123,7 +123,8 @@ function GharialAdapter(nodes, edges, viewer, config) { if (query !== queries.getAllGraphs) { bindVars.graph = graphName; if (query !== queries.connectedEdges - && query !== queries.childrenCentrality) { + && query !== queries.childrenCentrality + && query !== queries.randomVertices) { bindVars.dir = direction; } } @@ -154,28 +155,11 @@ function GharialAdapter(nodes, edges, viewer, config) { }, getNRandom = function(n, callback) { - var list = [], - i = 0, - rand, - onSuccess = function(data) { - list.push(data.document || {}); - if (list.length === n) { - callback(list); - } - }; - for (i = 0; i < n; i++) { - rand = Math.floor(Math.random() * nodeCollections.length); - $.ajax({ - cache: false, - type: 'PUT', - url: api.any, - data: JSON.stringify({ - collection: nodeCollections[rand] - }), - contentType: "application/json", - success: onSuccess - }); - } + sendQuery(queries.randomVertices, { + limit: n + }, function(res) { + callback(res); + }); }, parseResultOfTraversal = function (result, callback) { @@ -250,6 +234,7 @@ function GharialAdapter(nodes, edges, viewer, config) { + "})"; queries.childrenCentrality = "RETURN LENGTH(GRAPH_EDGES(@graph, @id, {direction: any}))"; queries.connectedEdges = "RETURN GRAPH_EDGES(@graph, @id)"; + queries.randomVertices = "FOR x IN GRAPH_VERTICES(@graph, {}) SORT RAND() LIMIT @limit RETURN x"; self.explore = absAdapter.explore; @@ -259,12 +244,11 @@ function GharialAdapter(nodes, edges, viewer, config) { self.loadRandomNode = function(callback) { getNRandom(1, function(list) { - var r = list[0]; - if (r._id) { - self.loadInitialNode(r._id, callback); - return; + if (list.length === 0) { + callback({errorCode: 404}); + return; } - return; + self.loadInitialNode(list[0]._id, callback); }); }; diff --git a/js/apps/system/aardvark/frontend/js/graphViewer/graphViewer.js b/js/apps/system/aardvark/frontend/js/graphViewer/graphViewer.js index 02934440cd..eee0dc5936 100644 --- a/js/apps/system/aardvark/frontend/js/graphViewer/graphViewer.js +++ b/js/apps/system/aardvark/frontend/js/graphViewer/graphViewer.js @@ -211,7 +211,7 @@ function GraphViewer(svg, width, height, adapterConfig, config) { this.loadGraphWithRandomStart = function(callback) { adapter.loadRandomNode(function (node) { - if (node.errorCode) { + if (node.errorCode && node.errorCode === 404) { callback(node); return; } diff --git a/js/apps/system/aardvark/frontend/js/graphViewer/ui/graphViewerUI.js b/js/apps/system/aardvark/frontend/js/graphViewer/ui/graphViewerUI.js index 8a12445340..a8cad0cec8 100644 --- a/js/apps/system/aardvark/frontend/js/graphViewer/ui/graphViewerUI.js +++ b/js/apps/system/aardvark/frontend/js/graphViewer/ui/graphViewerUI.js @@ -453,7 +453,7 @@ function GraphViewerUI(container, adapterConfig, optWidth, optHeight, viewerConf } else { graphViewer.loadGraphWithRandomStart(function(node) { if (node && node.errorCode) { - alert("Sorry your graph seems to be empty"); + alertError("Sorry your graph seems to be empty"); } }); }