1
0
mirror of https://github.com/hack-chat/main.git synced 2024-03-22 13:20:33 +08:00
hack-chat-main/commands/core/morestats.js

97 lines
2.5 KiB
JavaScript
Raw Normal View History

/*
Description: Outputs more info than the legacy stats command
*/
// module support functions
2019-11-07 15:35:23 +08:00
const { stripIndents } = require('common-tags');
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;
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`;
};
// 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
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) => {
if (client.channel) {
channels[client.channel] = true;
2019-11-07 15:35:23 +08:00
ips[client.address] = true;
}
2019-11-07 15:35:23 +08:00
});
2019-11-07 15:35:23 +08:00
const uniqueClientCount = Object.keys(ips).length;
const uniqueChannels = Object.keys(channels).length;
ips = null;
channels = null;
2018-06-04 15:07:24 +08:00
// dispatch info
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
}, 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
}
// 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
}
// hooks chat commands checking for /stats
2020-09-17 13:44:32 +08:00
export function statsCheck({
core, server, socket, payload,
}) {
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',
},
});
return false;
}
return payload;
2019-11-07 15:35:23 +08:00
}
2019-11-07 15:35:23 +08:00
export const info = {
2018-05-13 18:33:22 +08:00
name: 'morestats',
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
};