comparison app/query.service.ts @ 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
comparison
equal deleted inserted replaced
16:7d82ca32833c 17:f6af2c8347de
64 setupIsmiObjectTypes() { 64 setupIsmiObjectTypes() {
65 var query = `MATCH (n) WITH DISTINCT labels(n) AS labels 65 var query = `MATCH (n) WITH DISTINCT labels(n) AS labels
66 UNWIND labels AS label 66 UNWIND labels AS label
67 RETURN DISTINCT label ORDER BY label`; 67 RETURN DISTINCT label ORDER BY label`;
68 68
69 var res = this.fetchCypherResult(query); 69 var res = this.fetchCypherResults([query]);
70 res.subscribe( 70 res.subscribe(
71 data => { 71 data => {
72 console.debug("neo4j data=", data); 72 console.debug("neo4j data=", data);
73 this.ismiObjectTypes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_"); 73 this.ismiObjectTypes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_");
74 console.debug("ismi types=", this.ismiObjectTypes); 74 console.debug("ismi types=", this.ismiObjectTypes);
170 this.createCypherQuery(); 170 this.createCypherQuery();
171 this.state.resultInfo = 'loading...'; 171 this.state.resultInfo = 'loading...';
172 /* 172 /*
173 * run query for result table 173 * run query for result table
174 */ 174 */
175 var resQuery = this.state.resultCypherQuery; 175 var queries = [this.state.resultCypherQuery];
176 var queryParams = this.state.cypherQueryParams; 176 var params = [this.state.cypherQueryParams];
177 var resRes = this.fetchCypherResult(resQuery, queryParams); 177 if (this.state.attributesCypherQuery) {
178 resRes.subscribe( 178 queries.push(this.state.attributesCypherQuery);
179 params.push(this.state.cypherQueryParams);
180 }
181 if (this.state.relationsCypherQuery) {
182 queries.push(this.state.relationsCypherQuery);
183 params.push(this.state.cypherQueryParams);
184 }
185 var res = this.fetchCypherResults(queries, params);
186 res.subscribe(
179 data => { 187 data => {
180 console.debug("neo4j result data=", data); 188 console.debug("neo4j result data=", data);
181 this.state.results = data.results[0].data.map(elem => elem.row[0]); 189 var resIdx = 0;
190 /*
191 * results for result table
192 */
193 this.state.results = data.results[resIdx].data.map(elem => elem.row[0]);
182 this.state.numResults = this.state.results.length; 194 this.state.numResults = this.state.results.length;
183 // count all types 195 // count all types
184 var resTypes = {}; 196 var resTypes = {};
185 this.state.results.forEach((r) => { 197 this.state.results.forEach((r) => {
186 if (resTypes[r.type] == null) { 198 if (resTypes[r.type] == null) {
195 } 207 }
196 info = info.substr(0, info.length-1); 208 info = info.substr(0, info.length-1);
197 this.state.resultInfo = info; 209 this.state.resultInfo = info;
198 // save info also in last step 210 // save info also in last step
199 this.state.steps[this.state.steps.length-1].resultInfo = info; 211 this.state.steps[this.state.steps.length-1].resultInfo = info;
212 /*
213 * results for attribute list
214 */
215 if (this.state.attributesCypherQuery) {
216 resIdx += 1;
217 this.state.nextQueryAttributes = data.results[resIdx].data.map(elem => elem.row[0])
218 .filter(elem => elem[0] != "_" && !this.excludedAttributes[elem]);
219 }
220 /*
221 * results for relations list
222 */
223 if (this.state.relationsCypherQuery) {
224 resIdx += 1;
225 this.state.nextQueryRelations = data.results[resIdx].data.map(elem => elem.row[0])
226 .filter(elem => elem[0] != "_");
227 }
200 }, 228 },
201 err => console.error("neo4j result error=", err), 229 err => console.error("neo4j result error=", err),
202 () => console.debug('neo4j result query Complete') 230 () => console.debug('neo4j result query Complete')
203 ); 231 );
204 /* 232 }
205 * run query for attribute list 233
206 */ 234 fetchCypherResults(queries: string[], params=[{}]) {
207 if (this.state.attributesCypherQuery) { 235 console.debug("fetching cypher queries: ", queries);
208 var attRes = this.fetchCypherResult(this.state.attributesCypherQuery, queryParams);
209 attRes.subscribe(
210 data => {
211 console.debug("neo4j att data=", data);
212 this.state.nextQueryAttributes = data.results[0].data.map(elem => elem.row[0])
213 .filter(elem => elem[0] != "_" && !this.excludedAttributes[elem]);
214 },
215 err => console.error("neo4j att error=", err),
216 () => console.debug('neo4j att query Complete')
217 );
218 }
219 /*
220 * run query for relations list
221 */
222 if (this.state.relationsCypherQuery) {
223 var attRes = this.fetchCypherResult(this.state.relationsCypherQuery, queryParams);
224 attRes.subscribe(
225 data => {
226 console.debug("neo4j rel data=", data);
227 this.state.nextQueryRelations = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_");
228 },
229 err => console.error("neo4j rel error=", err),
230 () => console.debug('neo4j rel query Complete')
231 );
232 }
233 }
234
235 fetchCypherResult(query: string, params = {}) {
236 console.debug("fetching cypher query: ", query);
237 var headers = new Headers(); 236 var headers = new Headers();
238 headers.append('Authorization', 'Basic ' + btoa('neo4j' + ':' + 'neo5j')); 237 headers.append('Authorization', 'Basic ' + btoa('neo4j' + ':' + 'neo5j'));
239 headers.append('Content-Type', 'application/json'); 238 headers.append('Content-Type', 'application/json');
240 headers.append('Accept', 'application/json'); 239 headers.append('Accept', 'application/json');
241 // put headers in options 240 // put headers in options
242 var opts = {'headers': headers}; 241 var opts = {'headers': headers};
242 // unpack queries into statements
243 var statements = queries.map((q, i) => {
244 return {'statement': q, 'parameters': (params[i])?params[i]:{}};
245 });
243 // create POST data from query 246 // create POST data from query
244 var data = JSON.stringify({'statements': [ 247 var data = JSON.stringify({'statements': statements});
245 {'statement': query, 'parameters': params}
246 ]});
247 // make post request asynchronously 248 // make post request asynchronously
248 var resp = this._http.post(this.neo4jBaseUrl+'/transaction/commit', data, opts) 249 var resp = this._http.post(this.neo4jBaseUrl+'/transaction/commit', data, opts)
249 // filter result as JSON 250 // filter result as JSON
250 .map(res => res.json()); 251 .map(res => res.json());
251 // return Observable 252 // return Observable