Skip to content

Commit

Permalink
Add query stats
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Jul 1, 2023
1 parent e546ac0 commit d5044f3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
: (query = q, query.active = true)

build(q)
q.statistics && (q.statistics.executed = performance.now())
q.handler.onquery && (q.handler.onquery = q.handler.onquery(q))
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ function Postgres(a, b) {

function Sql(handler) {
handler.debug = options.debug
handler.stats = options.stats
handler.onquery = options.onquery

Object.entries(options.types).reduce((acc, [name, type]) => {
acc[name] = (x) => new Parameter(x, type.to)
Expand Down Expand Up @@ -479,6 +481,8 @@ function parseOptions(a, b) {
onclose : o.onclose,
onparameter : o.onparameter,
socket : o.socket,
stats : o.stats,
onquery : o.onquery,
transform : parseTransform(o.transform || { undefined: undefined }),
parameters : {},
shared : { retries: 0, typeArrayMap: {} },
Expand Down
44 changes: 39 additions & 5 deletions src/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export class Query extends Promise {
reject = b
})

this.resolver = resolve
this.rejecter = reject

this.statistics = handler.stats || handler.debug ? { started: -1, executed: -1 } : undefined
this.tagged = Array.isArray(strings.raw)
this.strings = strings
this.args = args
Expand All @@ -23,19 +27,30 @@ export class Query extends Promise {
this.state = null
this.statement = null

this.resolve = x => (this.active = false, resolve(x))
this.reject = x => (this.active = false, reject(x))

this.active = false
this.cancelled = null
this.executed = false
this.signature = ''

this[originError] = this.handler.debug
this[originError] = handler.debug
? new Error()
: this.tagged && cachedError(this.strings)
}

resolve(x) {
this.active = false
this.statistics && addStats(this, x)
this.handler.onquery && (this.handler.onquery = this.handler.onquery(x))
this.resolver(x)
}

reject(x) {
this.active = false
this.statistics && addStats(this, x)
this.handler.onquery && (this.handler.onquery = this.handler.onquery(x))
this.rejecter(x)
}

get origin() {
return this.handler.debug
? this[originError].stack
Expand Down Expand Up @@ -132,13 +147,25 @@ export class Query extends Promise {
return this
}

stats() {
this.statistics = { started: -1, executed: -1 }
return this
}

values() {
this.isRaw = 'values'
return this
}

async handle() {
!this.executed && (this.executed = true) && await 1 && this.handler(this)
if (this.executed)
return

this.executed = true
await 1
this.statistics && (this.statistics.started = performance.now())
this.handler.onquery && (this.handler.onquery = this.handler.onquery(this))
this.handler(this)
}

execute() {
Expand Down Expand Up @@ -172,3 +199,10 @@ function cachedError(xs) {
Error.stackTraceLimit = x
return originCache.get(xs)
}


function addStats(query, result) {
result.waiting = query.statistics.executed - query.statistics.started
result.duration = performance.now() - query.statistics.started
result.execution = performance.now() - query.statistics.executed
}

0 comments on commit d5044f3

Please sign in to comment.