Skip to content

Commit dfb4a5b

Browse files
committed
Merge pull request #5 from oskarhane/basic-metadata
Expose basic metadata through summarize method.
2 parents aa7be48 + dce1ae4 commit dfb4a5b

File tree

4 files changed

+204
-23
lines changed

4 files changed

+204
-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.statistics.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.statistics.nodesCreated());
52+
})
53+
.catch(function(error) {
54+
console.log(error);
55+
})
56+
.then(function(){
57+
promiseSession.close();
58+
});

lib/result.js

Lines changed: 159 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,160 @@ 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 = {text: statement, parameters};
126+
this.statementType = statementType;
127+
this.statistics = new StatementStatistics(statistics || {});
128+
}
129+
}
130+
131+
/**
132+
* Get statistical information for a {Result}.
133+
* @access public
134+
*/
135+
class StatementStatistics {
136+
/**
137+
* Structurize the statistics
138+
* @constructor
139+
* @param {Object} statistics - Result statistics
140+
*/
141+
constructor(statistics) {
142+
this._stats = {
143+
nodesCreated: 0,
144+
nodesDelete: 0,
145+
relationshipsCreated: 0,
146+
relationshipsDeleted: 0,
147+
propertiesSet: 0,
148+
labelsAdded: 0,
149+
labelsRemoved: 0,
150+
indexesAdded: 0,
151+
indexesRemoved: 0,
152+
constraintsAdded: 0,
153+
constraintsRemoved: 0
154+
}
155+
Object.keys(statistics).forEach((index) => {
156+
let val = neo4j.isInt(statistics[index]) ? statistics[index].toInt() : statistics[index];
157+
//To camelCase
158+
this._stats[index.replace(/(\-\w)/g, (m) => m[1].toUpperCase())] = val;
159+
});
160+
}
161+
162+
/**
163+
* Did the database get updated?
164+
* @return {boolean}
165+
*/
166+
containsUpdates() {
167+
return Object.keys(this._stats).reduce((last, current) => {
168+
return last + this._stats[current];
169+
}, 0) > 0;
170+
}
171+
172+
/**
173+
* @return {Number} - Number of nodes created.
174+
*/
175+
nodesCreated() {
176+
return this._stats.nodesCreated;
177+
}
178+
179+
/**
180+
* @return {Number} - Number of nodes deleted.
181+
*/
182+
nodesDeleted() {
183+
return this._stats.nodesDeleted;
184+
}
185+
186+
/**
187+
* @return {Number} - Number of relationships created.
188+
*/
189+
relationshipsCreated() {
190+
return this._stats.relationshipsCreated;
191+
}
192+
193+
/**
194+
* @return {Number} - Number of nodes deleted.
195+
*/
196+
relationshipsDeleted() {
197+
return this._stats.relationshipsDeleted;
198+
}
199+
200+
/**
201+
* @return {Number} - Number of properties set.
202+
*/
203+
propertiesSet() {
204+
return this._stats.propertiesSet;
205+
}
206+
207+
/**
208+
* @return {Number} - Number of labels added.
209+
*/
210+
labelsAdded() {
211+
return this._stats.labelsAdded;
212+
}
213+
214+
/**
215+
* @return {Number} - Number of labels removed.
216+
*/
217+
labelsRemoved() {
218+
return this._stats.labelsRemoved;
219+
}
220+
221+
/**
222+
* @return {Number} - Number of indexes added.
223+
*/
224+
indexesAdded() {
225+
return this._stats.indexesAdded;
226+
}
227+
228+
/**
229+
* @return {Number} - Number of indexes removed.
230+
*/
231+
indexesRemoved() {
232+
return this._stats.indexesRemoved;
233+
}
234+
235+
/**
236+
* @return {Number} - Number of contraints added.
237+
*/
238+
constraintsAdded() {
239+
return this._stats.constraintsAdded;
240+
}
241+
242+
/**
243+
* @return {Number} - Number of contraints removed.
244+
*/
245+
constraintsRemoved() {
246+
return this._stats.constraintsRemoved;
247+
}
92248
}
93249

94250
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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,23 @@ 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 {prop:{prop}}) RETURN n";
59+
var params = {prop: "string"}
60+
// When & Then
61+
var result = driver.session().run( statement, params );
62+
result.then(function( records ) {
63+
var sum = result.summarize();
64+
expect(sum.statement.text).toBe( statement );
65+
expect(sum.statement.parameters).toBe( params );
66+
expect(sum.statistics.containsUpdates()).toBe(true);
67+
expect(sum.statistics.nodesCreated()).toBe(1);
68+
expect(sum.statementType).toBe('rw');
69+
driver.close();
70+
done();
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)