|
17 | 17 | * limitations under the License.
|
18 | 18 | */
|
19 | 19 |
|
| 20 | +import neo4j from './neo4j'; |
| 21 | + |
20 | 22 | /**
|
21 | 23 | * A Result instance is used for retrieving request response.
|
22 | 24 | * @access public
|
23 | 25 | */
|
24 |
| - |
25 | 26 | class Result {
|
26 | 27 | /**
|
27 | 28 | * Inject the observer to be used.
|
28 | 29 | * @constructor
|
29 | 30 | * @param {StreamObserver} streamObserver
|
30 | 31 | */
|
31 |
| - constructor(streamObserver) { |
| 32 | + constructor(streamObserver, statement, parameters) { |
32 | 33 | this._streamObserver = streamObserver;
|
33 | 34 | this._p = null;
|
| 35 | + this._statement = statement; |
| 36 | + this._parameters = parameters; |
| 37 | + this.summary = {} |
34 | 38 | }
|
35 | 39 |
|
36 | 40 | /**
|
37 | 41 | * Create and return new Promise
|
38 |
| - * @return {Primise} new Promise. |
| 42 | + * @return {Promise} new Promise. |
39 | 43 | */
|
40 | 44 | _createPromise() {
|
41 | 45 | if(this._p) {
|
@@ -87,8 +91,185 @@ class Result {
|
87 | 91 | * @return
|
88 | 92 | */
|
89 | 93 | 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; |
90 | 100 | this._streamObserver.subscribe(observer);
|
91 | 101 | }
|
| 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 | + } |
92 | 273 | }
|
93 | 274 |
|
94 | 275 | export default Result;
|
0 commit comments