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/stats.js

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-06-23 00:32:51 +08:00
/**
* @author Marzavec ( https://github.com/marzavec )
* @summary Simple stats
* @version 1.0.0
* @description Sends back legacy server stats to the calling client
* @module stats
*/
2018-03-10 15:47:00 +08:00
2022-06-23 00:32:51 +08:00
/**
* Executes when invoked by a remote client
* @param {Object} env - Environment object with references to core, server, socket & payload
2022-06-23 00:32:51 +08:00
* @public
* @return {void}
*/
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-10 15:47:00 +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-10 15:47:00 +08:00
if (client.channel) {
channels[client.channel] = true;
2019-11-07 15:35:23 +08:00
ips[client.address] = true;
2018-03-10 15:47:00 +08:00
}
2019-11-07 15:35:23 +08:00
});
2018-03-10 15:47:00 +08:00
2019-11-07 15:35:23 +08:00
const uniqueClientCount = Object.keys(ips).length;
const uniqueChannels = Object.keys(channels).length;
2018-03-10 15:47:00 +08:00
ips = null;
channels = null;
2018-06-04 15:07:24 +08:00
// dispatch info
2018-03-10 15:47:00 +08:00
server.reply({
cmd: 'info',
2019-11-07 15:35:23 +08:00
text: `${uniqueClientCount} unique IPs in ${uniqueChannels} channels`,
2020-10-10 13:34:59 +08:00
channel: socket.channel, // @todo Multichannel
2018-03-10 15:47:00 +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-10 15:47:00 +08:00
2022-06-23 00:32:51 +08:00
/**
* Module meta information
* @public
* @typedef {Object} stats/info
* @property {string} name - Module command name
* @property {string} category - Module category name
* @property {string} description - Information about module
* @property {string} usage - Information about module usage
*/
2019-11-07 15:35:23 +08:00
export const info = {
2018-05-13 18:33:22 +08:00
name: 'stats',
2022-06-23 00:32:51 +08:00
category: 'core',
description: 'Sends back legacy server stats to the calling client',
usage: `
2019-11-07 15:35:23 +08:00
API: { cmd: 'stats' }`,
2018-06-04 15:07:24 +08:00
};