2018-03-14 13:26:53 +08:00
|
|
|
/*
|
|
|
|
Description: Outputs more info than the legacy stats command
|
|
|
|
*/
|
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// module support functions
|
2019-11-07 15:35:23 +08:00
|
|
|
const { stripIndents } = require('common-tags');
|
2018-03-14 13:26:53 +08:00
|
|
|
|
|
|
|
const formatTime = (time) => {
|
|
|
|
let seconds = time[0] + time[1] / 1e9;
|
|
|
|
|
|
|
|
let minutes = Math.floor(seconds / 60);
|
2019-11-07 15:35:23 +08:00
|
|
|
seconds %= 60;
|
2018-03-14 13:26:53 +08:00
|
|
|
|
|
|
|
let hours = Math.floor(minutes / 60);
|
2019-11-07 15:35:23 +08:00
|
|
|
minutes %= 60;
|
2018-06-04 15:07:24 +08:00
|
|
|
|
2019-11-07 15:35:23 +08:00
|
|
|
const days = Math.floor(hours / 24);
|
|
|
|
hours %= 24;
|
2018-06-04 15:07:24 +08:00
|
|
|
|
|
|
|
return `${days.toFixed(0)}d ${hours.toFixed(0)}h ${minutes.toFixed(0)}m ${seconds.toFixed(0)}s`;
|
2018-03-14 13:26:53 +08:00
|
|
|
};
|
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// module main
|
2020-09-17 13:44:32 +08:00
|
|
|
export async function run({ core, server, socket }) {
|
2018-06-04 15:07:24 +08:00
|
|
|
// gather connection and channel count
|
2018-03-14 13:26:53 +08:00
|
|
|
let ips = {};
|
|
|
|
let channels = {};
|
2019-11-07 15:35:23 +08:00
|
|
|
// for (const client of server.clients) {
|
2019-11-08 01:46:55 +08:00
|
|
|
server.clients.forEach((client) => {
|
2018-03-14 13:26:53 +08:00
|
|
|
if (client.channel) {
|
|
|
|
channels[client.channel] = true;
|
2019-11-07 15:35:23 +08:00
|
|
|
ips[client.address] = true;
|
2018-03-14 13:26:53 +08:00
|
|
|
}
|
2019-11-07 15:35:23 +08:00
|
|
|
});
|
2018-03-14 13:26:53 +08:00
|
|
|
|
2019-11-07 15:35:23 +08:00
|
|
|
const uniqueClientCount = Object.keys(ips).length;
|
|
|
|
const uniqueChannels = Object.keys(channels).length;
|
2018-03-14 13:26:53 +08:00
|
|
|
|
|
|
|
ips = null;
|
|
|
|
channels = null;
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// dispatch info
|
2018-03-14 13:26:53 +08:00
|
|
|
server.reply({
|
|
|
|
cmd: 'info',
|
|
|
|
text: stripIndents`current-connections: ${uniqueClientCount}
|
|
|
|
current-channels: ${uniqueChannels}
|
2019-03-19 14:36:21 +08:00
|
|
|
users-joined: ${(core.stats.get('users-joined') || 0)}
|
|
|
|
invites-sent: ${(core.stats.get('invites-sent') || 0)}
|
|
|
|
messages-sent: ${(core.stats.get('messages-sent') || 0)}
|
|
|
|
users-banned: ${(core.stats.get('users-banned') || 0)}
|
|
|
|
users-kicked: ${(core.stats.get('users-kicked') || 0)}
|
|
|
|
stats-requested: ${(core.stats.get('stats-requested') || 0)}
|
2019-11-07 15:35:23 +08:00
|
|
|
server-uptime: ${formatTime(process.hrtime(core.stats.get('start-time')))}`,
|
2020-10-10 13:34:59 +08:00
|
|
|
channel: socket.channel, // @todo Multichannel
|
2018-03-14 13:26:53 +08:00
|
|
|
}, socket);
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// stats are fun
|
2019-03-19 14:36:21 +08:00
|
|
|
core.stats.increment('stats-requested');
|
2019-11-07 15:35:23 +08:00
|
|
|
}
|
2018-03-14 13:26:53 +08:00
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// module hook functions
|
2019-11-07 15:35:23 +08:00
|
|
|
export function initHooks(server) {
|
2019-11-08 01:46:55 +08:00
|
|
|
server.registerHook('in', 'chat', this.statsCheck.bind(this), 26);
|
2019-11-07 15:35:23 +08:00
|
|
|
}
|
2018-09-30 14:44:36 +08:00
|
|
|
|
|
|
|
// hooks chat commands checking for /stats
|
2020-09-17 13:44:32 +08:00
|
|
|
export function statsCheck({
|
|
|
|
core, server, socket, payload,
|
|
|
|
}) {
|
2018-09-30 14:44:36 +08:00
|
|
|
if (typeof payload.text !== 'string') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.text.startsWith('/stats')) {
|
2020-09-17 13:44:32 +08:00
|
|
|
this.run({
|
|
|
|
core,
|
|
|
|
server,
|
|
|
|
socket,
|
|
|
|
payload: {
|
|
|
|
cmd: 'morestats',
|
|
|
|
},
|
2018-09-30 14:44:36 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload;
|
2019-11-07 15:35:23 +08:00
|
|
|
}
|
2018-09-30 14:44:36 +08:00
|
|
|
|
2019-11-07 15:35:23 +08:00
|
|
|
export const info = {
|
2018-05-13 18:33:22 +08:00
|
|
|
name: 'morestats',
|
2018-09-30 14:44:36 +08:00
|
|
|
description: 'Sends back current server stats to the calling client',
|
|
|
|
usage: `
|
|
|
|
API: { cmd: 'morestats' }
|
2019-11-07 15:35:23 +08:00
|
|
|
Text: /stats`,
|
2018-06-04 15:07:24 +08:00
|
|
|
};
|