command execution metrics

This commit is contained in:
Erik 2022-05-09 18:18:36 +03:00
parent 6aaa2f4658
commit 0a7a89e62a
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
2 changed files with 28 additions and 1 deletions

View File

@ -129,12 +129,15 @@ class CommandHandler extends Observer {
async _executeCommand(invoker, options) {
let response = null;
const now = Date.now();
try {
response = await invoker.command.execute(invoker, options);
invoker.command.success(now);
} catch (error) {
if (error instanceof CommandError) {
this._generateError(invoker, { error, type: 'command' });
} else {
invoker.command.error(now);
this.logger.error(error.stack || error);
this._generateError(invoker, { type: 'commandHandler' });
}

View File

@ -44,7 +44,9 @@ class Command extends Component {
this._invokes = {
success: 0,
fail: 0
successTime: null,
fail: 0,
failTime: null
};
this.options = [];
@ -79,6 +81,28 @@ class Command extends Component {
throw new Error(`${this.resolveable} is missing an execute function.`);
}
success(when) {
const now = Date.now();
const execTime = now - when;
if (!this._invokes.successTime) {
this._invokes.successTime = execTime;
this._invokes.success++;
} else { // Calculate new average
this._invokes.successTime = (this._invokes.successTime * this._invokes.success + execTime) / ++this._invokes.success;
}
}
error(when) {
const now = Date.now();
const execTime = now - when;
if (!this._invokes.failTime) {
this._invokes.failTime = execTime;
this._invokes.fail++;
} else { // Calculate new average
this._invokes.failTime = (this._invokes.failTime * this._invokes.fail + execTime) / ++this._invokes.fail;
}
}
usageEmbed(invoker, verbose = false) {
const fields = [];