changeset 17:f6af2c8347de

send multiple cypher queries in one request.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Thu, 21 Jan 2016 19:30:57 +0100
parents 7d82ca32833c
children 65bb467abcc6
files app/query.service.ts
diffstat 1 files changed, 41 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/app/query.service.ts	Thu Jan 21 18:47:57 2016 +0100
+++ b/app/query.service.ts	Thu Jan 21 19:30:57 2016 +0100
@@ -66,7 +66,7 @@
                 UNWIND labels AS label 
                 RETURN DISTINCT label ORDER BY label`;
 
-        var res = this.fetchCypherResult(query);
+        var res = this.fetchCypherResults([query]);
         res.subscribe(
             data => {
                 console.debug("neo4j data=", data);
@@ -172,13 +172,25 @@
         /*
          * run query for result table
          */
-        var resQuery = this.state.resultCypherQuery;
-        var queryParams = this.state.cypherQueryParams;
-        var resRes = this.fetchCypherResult(resQuery, queryParams);
-        resRes.subscribe(
+        var queries = [this.state.resultCypherQuery];
+        var params = [this.state.cypherQueryParams];
+        if (this.state.attributesCypherQuery) {
+            queries.push(this.state.attributesCypherQuery);
+            params.push(this.state.cypherQueryParams);
+        }
+        if (this.state.relationsCypherQuery) {
+            queries.push(this.state.relationsCypherQuery);
+            params.push(this.state.cypherQueryParams);
+        }
+        var res = this.fetchCypherResults(queries, params);
+        res.subscribe(
             data => {
                 console.debug("neo4j result data=", data);
-                this.state.results = data.results[0].data.map(elem => elem.row[0]);
+                var resIdx = 0;
+                /*
+                 * results for result table
+                 */
+                this.state.results = data.results[resIdx].data.map(elem => elem.row[0]);
                 this.state.numResults = this.state.results.length;
                 // count all types
                 var resTypes = {};
@@ -197,53 +209,42 @@
                 this.state.resultInfo = info;
                 // save info also in last step
                 this.state.steps[this.state.steps.length-1].resultInfo = info;
+                /*
+                 * results for attribute list
+                 */
+                if (this.state.attributesCypherQuery) {
+                    resIdx += 1;
+                    this.state.nextQueryAttributes = data.results[resIdx].data.map(elem => elem.row[0])
+                    .filter(elem => elem[0] != "_" && !this.excludedAttributes[elem]);
+                }
+                /*
+                 * results for relations list
+                 */
+                if (this.state.relationsCypherQuery) {
+                    resIdx += 1;
+                    this.state.nextQueryRelations = data.results[resIdx].data.map(elem => elem.row[0])
+                    .filter(elem => elem[0] != "_");
+                }
             },
             err => console.error("neo4j result error=", err),
             () => console.debug('neo4j result query Complete')
         );
-        /*
-         * run query for attribute list
-         */
-        if (this.state.attributesCypherQuery) {
-            var attRes = this.fetchCypherResult(this.state.attributesCypherQuery, queryParams);
-            attRes.subscribe(
-                data => {
-                    console.debug("neo4j att data=", data);
-                    this.state.nextQueryAttributes = data.results[0].data.map(elem => elem.row[0])
-                    .filter(elem => elem[0] != "_" && !this.excludedAttributes[elem]);
-                },
-                err => console.error("neo4j att error=", err),
-                () => console.debug('neo4j att query Complete')
-            );
-        }
-        /*
-         * run query for relations list
-         */
-        if (this.state.relationsCypherQuery) {
-            var attRes = this.fetchCypherResult(this.state.relationsCypherQuery, queryParams);
-            attRes.subscribe(
-                data => {
-                    console.debug("neo4j rel data=", data);
-                    this.state.nextQueryRelations = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_");
-                },
-                err => console.error("neo4j rel error=", err),
-                () => console.debug('neo4j rel query Complete')
-            );
-        }
     }
     
-    fetchCypherResult(query: string, params = {}) {
-        console.debug("fetching cypher query: ", query);
+    fetchCypherResults(queries: string[], params=[{}]) {
+        console.debug("fetching cypher queries: ", queries);
         var headers = new Headers();
         headers.append('Authorization', 'Basic ' + btoa('neo4j' + ':' + 'neo5j'));
         headers.append('Content-Type', 'application/json');
         headers.append('Accept', 'application/json');
         // put headers in options
         var opts = {'headers': headers};
+        // unpack queries into statements
+        var statements = queries.map((q, i) => {
+            return {'statement': q, 'parameters': (params[i])?params[i]:{}};
+        });
         // create POST data from query
-        var data = JSON.stringify({'statements': [
-            {'statement': query, 'parameters': params}
-            ]});
+        var data = JSON.stringify({'statements': statements});
         // make post request asynchronously
         var resp = this._http.post(this.neo4jBaseUrl+'/transaction/commit', data, opts)
         // filter result as JSON