Skip to content

Commit 91eb9ec

Browse files
committed
Add query stats
1 parent 4baef5e commit 91eb9ec

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

src/connection.js

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
165165
: (query = q, query.active = true)
166166

167167
build(q)
168+
q.statistics && (q.statistics.executed = performance.now())
169+
q.handler.onquery && (q.handler.onquery = q.handler.onquery(q))
168170
return write(toBuffer(q))
169171
&& !q.describeFirst
170172
&& !q.cursorFn

src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ function Postgres(a, b) {
8585

8686
function Sql(handler) {
8787
handler.debug = options.debug
88+
handler.stats = options.stats
89+
handler.onquery = options.onquery
8890

8991
Object.entries(options.types).reduce((acc, [name, type]) => {
9092
acc[name] = (x) => new Parameter(x, type.to)
@@ -491,6 +493,8 @@ function parseOptions(a, b) {
491493
onclose : o.onclose,
492494
onparameter : o.onparameter,
493495
socket : o.socket,
496+
stats : o.stats,
497+
onquery : o.onquery,
494498
transform : parseTransform(o.transform || { undefined: undefined }),
495499
parameters : {},
496500
shared : { retries: 0, typeArrayMap: {} },

src/query.js

+39-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export class Query extends Promise {
1313
reject = b
1414
})
1515

16+
this.resolver = resolve
17+
this.rejecter = reject
18+
19+
this.statistics = handler.stats || handler.debug ? { started: -1, executed: -1 } : undefined
1620
this.tagged = Array.isArray(strings.raw)
1721
this.strings = strings
1822
this.args = args
@@ -23,19 +27,30 @@ export class Query extends Promise {
2327
this.state = null
2428
this.statement = null
2529

26-
this.resolve = x => (this.active = false, resolve(x))
27-
this.reject = x => (this.active = false, reject(x))
28-
2930
this.active = false
3031
this.cancelled = null
3132
this.executed = false
3233
this.signature = ''
3334

34-
this[originError] = this.handler.debug
35+
this[originError] = handler.debug
3536
? new Error()
3637
: this.tagged && cachedError(this.strings)
3738
}
3839

40+
resolve(x) {
41+
this.active = false
42+
this.statistics && addStats(this, x)
43+
this.handler.onquery && (this.handler.onquery = this.handler.onquery(x))
44+
this.resolver(x)
45+
}
46+
47+
reject(x) {
48+
this.active = false
49+
this.statistics && addStats(this, x)
50+
this.handler.onquery && (this.handler.onquery = this.handler.onquery(x))
51+
this.rejecter(x)
52+
}
53+
3954
get origin() {
4055
return (this.handler.debug
4156
? this[originError].stack
@@ -131,13 +146,25 @@ export class Query extends Promise {
131146
return this
132147
}
133148

149+
stats() {
150+
this.statistics = { started: -1, executed: -1 }
151+
return this
152+
}
153+
134154
values() {
135155
this.isRaw = 'values'
136156
return this
137157
}
138158

139159
async handle() {
140-
!this.executed && (this.executed = true) && await 1 && this.handler(this)
160+
if (this.executed)
161+
return
162+
163+
this.executed = true
164+
await 1
165+
this.statistics && (this.statistics.started = performance.now())
166+
this.handler.onquery && (this.handler.onquery = this.handler.onquery(this))
167+
this.handler(this)
141168
}
142169

143170
execute() {
@@ -171,3 +198,10 @@ function cachedError(xs) {
171198
Error.stackTraceLimit = x
172199
return originCache.get(xs)
173200
}
201+
202+
203+
function addStats(query, result) {
204+
result.waiting = query.statistics.executed - query.statistics.started
205+
result.duration = performance.now() - query.statistics.started
206+
result.execution = performance.now() - query.statistics.executed
207+
}

0 commit comments

Comments
 (0)