UNPKG

@datastax/astra-db-ts

Version:
154 lines (153 loc) 5.1 kB
// Copyright Datastax, Inc // SPDX-License-Identifier: Apache-2.0 import { BaseClientEvent } from '../lib/logging/base-event.js'; import { DataAPIError } from '../documents/errors.js'; const mkCommandEventTarget = (info) => { const target = { url: info.url }; if (info.keyspace) { target.keyspace = info.keyspace; } if (info.tOrCType) { target[info.tOrCType] = info.tOrC; } return target; }; export class CommandEvent extends BaseClientEvent { constructor(name, requestId, info, extra) { super(name, requestId, extra); Object.defineProperty(this, "command", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "target", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.command = info.command; this.target = mkCommandEventTarget(info); } get commandName() { return Object.keys(this.command)[0]; } getMessagePrefix() { const source = this.target.collection || this.target.table; return (source === undefined) ? `${this.target.keyspace ?? '<no_keyspace>'}::${this.commandName}` : `${source}::${this.commandName}`; } _extraLogInfoAsString() { return this.extraLogInfo ? `{${Object.entries(this.extraLogInfo).map(([k, v]) => `${k}=${v}`).join(',')}} ` : ''; } /** * @internal */ _modifyEventForFormatVerbose(event) { event.target = event.target.url; } } /** * Emitted when a command is started, before the initial HTTP request is made. * * **Note that these emit *real* commands, not any abstracted commands like "insertMany" or "updateMany", * which may be split into multiple of those commands under the hood.** * * See {@link CommandEvent} for more information about all the common properties available on this event. * * @public */ export class CommandStartedEvent extends CommandEvent { /** * Should not be instantiated by the user. * * @internal */ constructor(requestId, info, extra) { super('CommandStarted', requestId, info, extra); Object.defineProperty(this, "timeout", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.timeout = info.timeoutManager.initial(); } getMessage() { return this._extraLogInfoAsString(); } } export class CommandSucceededEvent extends CommandEvent { constructor(requestId, info, extra, reply, started) { super('CommandSucceeded', requestId, info, extra); Object.defineProperty(this, "duration", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "response", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.duration = performance.now() - started; this.response = reply; } getMessage() { return `${this._extraLogInfoAsString()}(${~~this.duration}ms)`; } } export class CommandFailedEvent extends CommandEvent { constructor(requestId, info, extra, reply, error, started) { super('CommandFailed', requestId, info, extra); Object.defineProperty(this, "duration", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "error", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "response", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.duration = performance.now() - started; this.response = reply; this.error = error; } getMessage() { return `${this._extraLogInfoAsString()}(${~~this.duration}ms) ERROR: ${JSON.stringify(this.error.message)}`; } trimDuplicateFields() { if (this.error instanceof DataAPIError) { return { ...this, error: this.error.withTransientDupesForEvents() }; } return this; } } export class CommandWarningsEvent extends CommandEvent { constructor(requestId, info, extra, warnings) { super('CommandWarnings', requestId, info, extra); Object.defineProperty(this, "warnings", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.warnings = warnings; } getMessage() { return `${this._extraLogInfoAsString()}WARNINGS: ${this.warnings.map(w => JSON.stringify(w.message)).join(', ')}`; } }