Skip to content

Commit 39bbd11

Browse files
committed
Expose basic metadata through summarize method.
1 parent d0991d5 commit 39bbd11

File tree

4 files changed

+227
-23
lines changed

4 files changed

+227
-23
lines changed

examples/node.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,42 @@ var params = {
1717
var driver = neo4j.driver("neo4j://localhost");
1818

1919
var streamSession = driver.session();
20-
streamSession.run(statement.join(' '), params).subscribe({
20+
var streamResult = streamSession.run(statement.join(' '), params);
21+
streamResult.subscribe({
2122
onNext: function(record) {
2223
// On receipt of RECORD
2324
for(var i in record) {
2425
console.log(i);
2526
console.log(record[i]);
2627
}
27-
}, onCompleted: function(metadata) {
28-
// On receipt of header summary message
28+
}, onCompleted: function() {
29+
var summary = streamResult.summarize();
30+
//Print number of nodes created
2931
console.log('');
30-
console.log(metadata);
32+
console.log(summary.updateStatistics().nodesCreated());
3133
streamSession.close();
3234
}, onError: function(error) {
3335
console.log(error);
3436
}
3537
});
3638

3739
var promiseSession = driver.session();
38-
promiseSession.run(statement.join(' '), params)
39-
.then(function(records){
40-
records.forEach(function(record) {
41-
for(var i in record) {
42-
console.log(i);
43-
console.log(record[i]);
44-
}
45-
})
46-
})
47-
.catch(function(error) {
48-
console.log(error);
49-
})
50-
.then(function(){
51-
promiseSession.close();
40+
var promiseResult = promiseSession.run(statement.join(' '), params);
41+
promiseResult.then(function(records) {
42+
records.forEach(function(record) {
43+
for(var i in record) {
44+
console.log(i);
45+
console.log(record[i]);
46+
}
5247
});
48+
var summary = promiseResult.summarize();
49+
//Print number of nodes created
50+
console.log('');
51+
console.log(summary.updateStatistics().nodesCreated());
52+
})
53+
.catch(function(error) {
54+
console.log(error);
55+
})
56+
.then(function(){
57+
promiseSession.close();
58+
});

lib/result.js

Lines changed: 184 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,29 @@
1717
* limitations under the License.
1818
*/
1919

20+
import neo4j from './neo4j';
21+
2022
/**
2123
* A Result instance is used for retrieving request response.
2224
* @access public
2325
*/
24-
2526
class Result {
2627
/**
2728
* Inject the observer to be used.
2829
* @constructor
2930
* @param {StreamObserver} streamObserver
3031
*/
31-
constructor(streamObserver) {
32+
constructor(streamObserver, statement, parameters) {
3233
this._streamObserver = streamObserver;
3334
this._p = null;
35+
this._statement = statement;
36+
this._parameters = parameters;
37+
this.summary = {}
3438
}
3539

3640
/**
3741
* Create and return new Promise
38-
* @return {Primise} new Promise.
42+
* @return {Promise} new Promise.
3943
*/
4044
_createPromise() {
4145
if(this._p) {
@@ -87,8 +91,185 @@ class Result {
8791
* @return
8892
*/
8993
subscribe(observer) {
94+
let onCompletedOriginal = observer.onCompleted;
95+
let onCompletedWrapper = (metadata) => {
96+
this.summary = new ResultSummary(this._statement, this._parameters, metadata.type, metadata.stats);
97+
onCompletedOriginal(metadata);
98+
}
99+
observer.onCompleted = onCompletedWrapper;
90100
this._streamObserver.subscribe(observer);
91101
}
102+
103+
/**
104+
* Get a metadata summary for the statement.
105+
* @return {ResultSummary} - A ResultSummary class.
106+
*/
107+
summarize() {
108+
return this.summary;
109+
}
110+
}
111+
112+
/**
113+
* A ResultSummary instance contains structured metadata for a {Result}.
114+
* @access public
115+
*/
116+
class ResultSummary {
117+
/**
118+
* @constructor
119+
* @param {string} statement - The statement this summary is for
120+
* @param {Object} parameters - Parameters for the statement
121+
* @param {string} statementType - How did the statement effect the database
122+
* @param {Object} statistics - Result statistics
123+
*/
124+
constructor(statement, parameters, statementType, statistics) {
125+
this.statement = statement;
126+
this._parameters = parameters;
127+
this.statementType = statementType;
128+
this._statistics = new StatementStatistics(statistics || {});
129+
}
130+
131+
hasPlan() {
132+
133+
}
134+
135+
hasProfile() {
136+
137+
}
138+
139+
plan() {
140+
141+
}
142+
143+
profile() {
144+
145+
}
146+
147+
/**
148+
* Get the statistics
149+
* @return {StatementStatistics}
150+
*/
151+
updateStatistics() {
152+
return this._statistics;
153+
}
154+
}
155+
156+
/**
157+
* Get statistical information for a {Result}.
158+
* @access public
159+
*/
160+
class StatementStatistics {
161+
/**
162+
* Structurize the statistics
163+
* @constructor
164+
* @param {Object} statistics - Result statistics
165+
*/
166+
constructor(statistics) {
167+
this._stats = {};
168+
this._stats.nodesCreated = 0;
169+
this._stats.nodesDeleted = 0;
170+
this._stats.relationshipsCreated = 0;
171+
this._stats.relationshipsDeleted = 0;
172+
this._stats.propertiesSet = 0;
173+
this._stats.labelsAdded = 0;
174+
this._stats.labelsRemoved = 0;
175+
this._stats.indexesAdded = 0;
176+
this._stats.indexesRemoved = 0;
177+
this._stats.constraintsAdded = 0;
178+
this._stats.constraintsRemoved = 0;
179+
180+
Object.keys(statistics).forEach((index) => {
181+
let val = neo4j.isInt(statistics[index]) ? statistics[index].toInt() : statistics[index];
182+
//To camelCase
183+
this._stats[index.replace(/(\-\w)/g, function(m){return m[1].toUpperCase();})] = val;
184+
});
185+
}
186+
187+
/**
188+
* Did the database get updated?
189+
* @return {boolean}
190+
*/
191+
containsUpdates() {
192+
return Object.keys(this._stats).reduce((last, current) => {
193+
return last + this._stats[current];
194+
}, 0) > 0;
195+
}
196+
197+
/**
198+
* @return {Number} - Number of nodes created.
199+
*/
200+
nodesCreated() {
201+
return this._stats.nodesCreated;
202+
}
203+
204+
/**
205+
* @return {Number} - Number of nodes deleted.
206+
*/
207+
nodesDeleted() {
208+
return this._stats.nodesDeleted;
209+
}
210+
211+
/**
212+
* @return {Number} - Number of relationships created.
213+
*/
214+
relationshipsCreated() {
215+
return this._stats.relationshipsCreated;
216+
}
217+
218+
/**
219+
* @return {Number} - Number of nodes deleted.
220+
*/
221+
relationshipsDeleted() {
222+
return this._stats.relationshipsDeleted;
223+
}
224+
225+
/**
226+
* @return {Number} - Number of properties set.
227+
*/
228+
propertiesSet() {
229+
return this._stats.propertiesSet;
230+
}
231+
232+
/**
233+
* @return {Number} - Number of labels added.
234+
*/
235+
labelsAdded() {
236+
return this._stats.labelsAdded;
237+
}
238+
239+
/**
240+
* @return {Number} - Number of labels removed.
241+
*/
242+
labelsRemoved() {
243+
return this._stats.labelsRemoved;
244+
}
245+
246+
/**
247+
* @return {Number} - Number of indexes added.
248+
*/
249+
indexesAdded() {
250+
return this._stats.indexesAdded;
251+
}
252+
253+
/**
254+
* @return {Number} - Number of indexes removed.
255+
*/
256+
indexesRemoved() {
257+
return this._stats.indexesRemoved;
258+
}
259+
260+
/**
261+
* @return {Number} - Number of contraints added.
262+
*/
263+
constraintsAdded() {
264+
return this._stats.constraintsAdded;
265+
}
266+
267+
/**
268+
* @return {Number} - Number of contraints removed.
269+
*/
270+
constraintsRemoved() {
271+
return this._stats.constraintsRemoved;
272+
}
92273
}
93274

94275
export default Result;

lib/session.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Session {
4848
this._conn.run( statement, params || {}, streamObserver );
4949
this._conn.pullAll( streamObserver );
5050
this._conn.sync();
51-
return new Result( streamObserver );
51+
return new Result( streamObserver, statement, params );
5252
}
5353

5454
/**

test/session.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,21 @@ describe('session', function() {
5151
}
5252
)
5353
});
54-
});
54+
55+
it('should expose summarize method ', function(done) {
56+
// Given
57+
var driver = neo4j.driver("neo4j://localhost");
58+
var statement = "CREATE (n:Label) RETURN n";
59+
// When & Then
60+
var result = driver.session().run( statement );
61+
result.then(function( records ) {
62+
var sum = result.summarize();
63+
expect(sum.statement).toBe( statement );
64+
expect(sum.updateStatistics().containsUpdates()).toBe(true);
65+
expect(sum.updateStatistics().nodesCreated()).toBe(1);
66+
expect(sum.statementType).toBe('rw');
67+
driver.close();
68+
done();
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)