/
resultsummary.go
594 lines (510 loc) · 17.2 KB
/
resultsummary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [https://neo4j.com]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package neo4j
import (
"fmt"
"github.com/neo4j/neo4j-go-driver/v5/neo4j/db"
"time"
)
// StatementType defines the type of the statement
type StatementType int
const (
// StatementTypeUnknown identifies an unknown statement type
StatementTypeUnknown StatementType = 0
// StatementTypeReadOnly identifies a read-only statement
StatementTypeReadOnly StatementType = 1
// StatementTypeReadWrite identifies a read-write statement
StatementTypeReadWrite StatementType = 2
// StatementTypeWriteOnly identifies a write-only statement
StatementTypeWriteOnly StatementType = 3
// StatementTypeSchemaWrite identifies a schema-write statement
StatementTypeSchemaWrite StatementType = 4
)
func (st StatementType) String() string {
switch st {
case StatementTypeReadOnly:
return "r"
case StatementTypeReadWrite:
return "rw"
case StatementTypeWriteOnly:
return "w"
case StatementTypeSchemaWrite:
return "s"
default:
return ""
}
}
type ResultSummary interface {
// Server returns basic information about the server where the statement is carried out.
Server() ServerInfo
// Query returns the query that has been executed.
Query() Query
// StatementType returns type of statement that has been executed.
StatementType() StatementType
// Counters returns statistics counts for the statement.
Counters() Counters
// Plan returns statement plan for the executed statement if available, otherwise null.
Plan() Plan
// Profile returns profiled statement plan for the executed statement if available, otherwise null.
Profile() ProfiledPlan
// Notifications returns a slice of notifications produced while executing the statement.
// The list will be empty if no notifications produced while executing the statement.
Notifications() []Notification
// ResultAvailableAfter returns the time it took for the server to make the result available for consumption.
// Since 5.0, this returns a negative duration if the server has not sent the corresponding statistic.
ResultAvailableAfter() time.Duration
// ResultConsumedAfter returns the time it took the server to consume the result.
// Since 5.0, this returns a negative duration if the server has not sent the corresponding statistic.
ResultConsumedAfter() time.Duration
// Database returns information about the database where the result is obtained from
// Returns nil for Neo4j versions prior to v4.
// Returns the default "neo4j" database for Community Edition servers.
Database() DatabaseInfo
}
// Counters contains statistics about the changes made to the database made as part
// of the statement execution.
type Counters interface {
// Whether there were any updates at all, eg. any of the counters are greater than 0.
ContainsUpdates() bool
// The number of nodes created.
NodesCreated() int
// The number of nodes deleted.
NodesDeleted() int
// The number of relationships created.
RelationshipsCreated() int
// The number of relationships deleted.
RelationshipsDeleted() int
PropertiesSet() int
// The number of labels added to nodes.
LabelsAdded() int
// The number of labels removed from nodes.
LabelsRemoved() int
// The number of indexes added to the schema.
IndexesAdded() int
// The number of indexes removed from the schema.
IndexesRemoved() int
// The number of constraints added to the schema.
ConstraintsAdded() int
// The number of constraints removed from the schema.
ConstraintsRemoved() int
// The number of system updates
SystemUpdates() int
// ContainsSystemUpdates indicates whether the query contains system updates
ContainsSystemUpdates() bool
}
type Statement interface {
Query
}
type Query interface {
// Text returns the statement's text.
Text() string
// Parameters returns the statement's parameters.
Parameters() map[string]any
}
// ServerInfo contains basic information of the server.
type ServerInfo interface {
// Address returns the address of the server.
Address() string
Agent() string
ProtocolVersion() db.ProtocolVersion
}
type simpleServerInfo struct {
address string
agent string
protocolVersion db.ProtocolVersion
}
func (s simpleServerInfo) Address() string {
return s.address
}
func (s simpleServerInfo) Agent() string {
return s.agent
}
func (s simpleServerInfo) ProtocolVersion() db.ProtocolVersion {
return s.protocolVersion
}
// DatabaseInfo contains basic information of the database the query result has been obtained from.
type DatabaseInfo interface {
Name() string
}
// Plan describes the actual plan that the database planner produced and used (or will use) to execute your statement.
// This can be extremely helpful in understanding what a statement is doing, and how to optimize it. For more details,
// see the Neo4j Manual. The plan for the statement is a tree of plans - each sub-tree containing zero or more child
// plans. The statement starts with the root plan. Each sub-plan is of a specific operator, which describes what
// that part of the plan does - for instance, perform an index lookup or filter results.
// The Neo4j Manual contains a reference of the available operator types, and these may differ across Neo4j versions.
type Plan interface {
// Operator returns the operation this plan is performing.
Operator() string
// Arguments returns the arguments for the operator used.
// Many operators have arguments defining their specific behavior. This map contains those arguments.
Arguments() map[string]any
// Identifiers returns a list of identifiers used by this plan. Identifiers used by this part of the plan.
// These can be both identifiers introduced by you, or automatically generated.
Identifiers() []string
// Children returns zero or more child plans. A plan is a tree, where each child is another plan.
// The children are where this part of the plan gets its input records - unless this is an operator that
// introduces new records on its own.
Children() []Plan
}
// ProfiledPlan is the same as a regular Plan - except this plan has been executed, meaning it also
// contains detailed information about how much work each step of the plan incurred on the database.
type ProfiledPlan interface {
// Operator returns the operation this plan is performing.
Operator() string
// Arguments returns the arguments for the operator used.
// Many operators have arguments defining their specific behavior. This map contains those arguments.
Arguments() map[string]any
// Identifiers returns a list of identifiers used by this plan. Identifiers used by this part of the plan.
// These can be both identifiers introduced by you, or automatically generated.
Identifiers() []string
// DbHits returns the number of times this part of the plan touched the underlying data stores/
DbHits() int64
// Records returns the number of records this part of the plan produced.
Records() int64
// Children returns zero or more child plans. A plan is a tree, where each child is another plan.
// The children are where this part of the plan gets its input records - unless this is an operator that
// introduces new records on its own.
Children() []ProfiledPlan
PageCacheMisses() int64
PageCacheHits() int64
PageCacheHitRatio() float64
Time() int64
}
// Notification represents notifications generated when executing a statement.
// A notification can be visualized in a client pinpointing problems or other information about the statement.
// Contrary to failures or errors, notifications do not affect the execution of the statement.
type Notification interface {
// Code returns a notification code for the discovered issue of this notification.
Code() string
// Title returns a short summary of this notification.
Title() string
// Description returns a longer description of this notification.
Description() string
// Position returns the position in the statement where this notification points to.
// Not all notifications have a unique position to point to and in that case the position would be set to nil.
Position() InputPosition
// Severity returns the severity level of this notification.
//
// Deprecated: please use SeverityLevel (or RawSeverityLevel) instead. Severity will be removed in 6.0.
Severity() string
// RawSeverityLevel returns the unmapped severity level of this notification.
// This is useful when the driver cannot interpret the severity level returned by the server
// In that case, SeverityLevel returns UnknownSeverity while RawSeverityLevel returns the raw string
RawSeverityLevel() string
// RawCategory returns the unmapped category of this notification.
// This is useful when the driver cannot interpret the category returned by the server
// In that case, Category returns UnknownCategory while RawCategory returns the raw string
RawCategory() string
// SeverityLevel returns the mapped security level of this notification.
// If the severity level is not a known value, SeverityLevel returns UnknownSeverity
// Call RawSeverityLevel to get access to the raw string value
SeverityLevel() NotificationSeverity
// Category returns the mapped category of this notification.
// If the category is not a known value, Category returns UnknownCategory
// Call RawCategory to get access to the raw string value
Category() NotificationCategory
}
// InputPosition contains information about a specific position in a statement
type InputPosition interface {
// Offset returns the character offset referred to by this position; offset numbers start at 0.
Offset() int
// Line returns the line number referred to by this position; line numbers start at 1.
Line() int
// Column returns the column number referred to by this position; column numbers start at 1.
Column() int
}
type NotificationSeverity string
const (
Warning NotificationSeverity = "WARNING"
Information NotificationSeverity = "INFORMATION"
UnknownSeverity NotificationSeverity = "UNKNOWN"
)
type NotificationCategory string
const (
Hint NotificationCategory = "HINT"
Unrecognized NotificationCategory = "UNRECOGNIZED"
Unsupported NotificationCategory = "UNSUPPORTED"
Performance NotificationCategory = "PERFORMANCE"
Deprecation NotificationCategory = "DEPRECATION"
Generic NotificationCategory = "GENERIC"
Security NotificationCategory = "SECURITY"
Topology NotificationCategory = "TOPOLOGY"
UnknownCategory NotificationCategory = "UNKNOWN"
)
type resultSummary struct {
sum *db.Summary
cypher string
params map[string]any
}
func (s *resultSummary) Agent() string {
return s.sum.Agent
}
func (s *resultSummary) ProtocolVersion() db.ProtocolVersion {
return db.ProtocolVersion{
Major: s.sum.Major,
Minor: s.sum.Minor,
}
}
func (s *resultSummary) Server() ServerInfo {
return s
}
func (s *resultSummary) Address() string {
return s.sum.ServerName
}
func (s *resultSummary) Query() Query {
return s
}
func (s *resultSummary) StatementType() StatementType {
return StatementType(s.sum.StmntType)
}
func (s *resultSummary) Text() string {
return s.cypher
}
func (s *resultSummary) Parameters() map[string]any {
return s.params
}
func (s *resultSummary) Counters() Counters {
return s
}
func (s *resultSummary) ContainsUpdates() bool {
if s.sum.ContainsUpdates != nil {
return *s.sum.ContainsUpdates
}
return s.ConstraintsAdded() > 0 || s.ConstraintsRemoved() > 0 ||
s.IndexesAdded() > 0 || s.IndexesRemoved() > 0 ||
s.NodesCreated() > 0 || s.NodesDeleted() > 0 ||
s.RelationshipsCreated() > 0 || s.RelationshipsDeleted() > 0 ||
s.LabelsAdded() > 0 || s.LabelsRemoved() > 0 ||
s.PropertiesSet() > 0
}
func (s *resultSummary) getCounter(n string) int {
if s.sum.Counters == nil {
return 0
}
return s.sum.Counters[n]
}
func (s *resultSummary) SystemUpdates() int {
return s.getCounter(db.SystemUpdates)
}
func (s *resultSummary) ContainsSystemUpdates() bool {
if s.sum.ContainsSystemUpdates != nil {
return *s.sum.ContainsSystemUpdates
}
return s.getCounter(db.SystemUpdates) > 0
}
func (s *resultSummary) NodesCreated() int {
return s.getCounter(db.NodesCreated)
}
func (s *resultSummary) NodesDeleted() int {
return s.getCounter(db.NodesDeleted)
}
func (s *resultSummary) RelationshipsCreated() int {
return s.getCounter(db.RelationshipsCreated)
}
func (s *resultSummary) RelationshipsDeleted() int {
return s.getCounter(db.RelationshipsDeleted)
}
func (s *resultSummary) PropertiesSet() int {
return s.getCounter(db.PropertiesSet)
}
func (s *resultSummary) LabelsAdded() int {
return s.getCounter(db.LabelsAdded)
}
func (s *resultSummary) LabelsRemoved() int {
return s.getCounter(db.LabelsRemoved)
}
func (s *resultSummary) IndexesAdded() int {
return s.getCounter(db.IndexesAdded)
}
func (s *resultSummary) IndexesRemoved() int {
return s.getCounter(db.IndexesRemoved)
}
func (s *resultSummary) ConstraintsAdded() int {
return s.getCounter(db.ConstraintsAdded)
}
func (s *resultSummary) ConstraintsRemoved() int {
return s.getCounter(db.ConstraintsRemoved)
}
func (s *resultSummary) ResultAvailableAfter() time.Duration {
return time.Duration(s.sum.TFirst) * time.Millisecond
}
func (s *resultSummary) ResultConsumedAfter() time.Duration {
return time.Duration(s.sum.TLast) * time.Millisecond
}
func (s *resultSummary) Plan() Plan {
if s.sum.Plan == nil {
return nil
}
return &plan{plan: s.sum.Plan}
}
func (s *resultSummary) Database() DatabaseInfo {
database := s.sum.Database
if database == "" {
return nil
}
return &databaseInfo{name: database}
}
type databaseInfo struct {
name string
}
func (d *databaseInfo) Name() string {
return d.name
}
type plan struct {
plan *db.Plan
}
func (p *plan) Operator() string {
return p.plan.Operator
}
func (p *plan) Arguments() map[string]any {
return p.plan.Arguments
}
func (p *plan) Identifiers() []string {
return p.plan.Identifiers
}
func (p *plan) Children() []Plan {
children := make([]Plan, len(p.plan.Children))
for i, c := range p.plan.Children {
children[i] = &plan{plan: &c}
}
return children
}
func (s *resultSummary) Profile() ProfiledPlan {
if s.sum.ProfiledPlan == nil {
return nil
}
return &profile{profile: s.sum.ProfiledPlan}
}
type profile struct {
profile *db.ProfiledPlan
}
func (p *profile) String() string {
return fmt.Sprintf("%v", *p.profile)
}
func (p *profile) Operator() string {
return p.profile.Operator
}
func (p *profile) Arguments() map[string]any {
return p.profile.Arguments
}
func (p *profile) Identifiers() []string {
return p.profile.Identifiers
}
func (p *profile) DbHits() int64 {
return p.profile.DbHits
}
func (p *profile) Records() int64 {
return p.profile.Records
}
func (p *profile) Children() []ProfiledPlan {
children := make([]ProfiledPlan, len(p.profile.Children))
for i, c := range p.profile.Children {
child := c
children[i] = &profile{profile: &child}
}
return children
}
func (p *profile) PageCacheMisses() int64 {
return p.profile.PageCacheMisses
}
func (p *profile) PageCacheHits() int64 {
return p.profile.PageCacheHits
}
func (p *profile) PageCacheHitRatio() float64 {
return p.profile.PageCacheHitRatio
}
func (p *profile) Time() int64 {
return p.profile.Time
}
func (s *resultSummary) Notifications() []Notification {
if s.sum.Notifications == nil {
return nil
}
notifications := make([]Notification, len(s.sum.Notifications))
for i := range s.sum.Notifications {
notifications[i] = ¬ification{notification: &s.sum.Notifications[i]}
}
return notifications
}
type notification struct {
notification *db.Notification
}
func (n *notification) Code() string {
return n.notification.Code
}
func (n *notification) Title() string {
return n.notification.Title
}
func (n *notification) Description() string {
return n.notification.Description
}
func (n *notification) Severity() string {
return n.RawSeverityLevel()
}
func (n *notification) RawSeverityLevel() string {
return n.notification.Severity
}
func (n *notification) SeverityLevel() NotificationSeverity {
switch n.notification.Severity {
case "WARNING":
return Warning
case "INFORMATION":
return Information
default:
return UnknownSeverity
}
}
func (n *notification) RawCategory() string {
return n.notification.Category
}
func (n *notification) Category() NotificationCategory {
switch n.notification.Category {
case "HINT":
return Hint
case "UNRECOGNIZED":
return Unrecognized
case "UNSUPPORTED":
return Unsupported
case "PERFORMANCE":
return Performance
case "DEPRECATION":
return Deprecation
case "SECURITY":
return Security
case "TOPOLOGY":
return Topology
case "GENERIC":
return Generic
default:
return UnknownCategory
}
}
func (n *notification) Position() InputPosition {
if n.notification.Position == nil {
return nil
}
return n
}
func (n *notification) Offset() int {
return n.notification.Position.Offset
}
func (n *notification) Column() int {
return n.notification.Position.Column
}
func (n *notification) Line() int {
return n.notification.Position.Line
}