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

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-06-22 11:32:51 -05: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-09 23:47:00 -08:00
2022-06-22 11:32:51 -05:00
/**
* Executes when invoked by a remote client
* @param {Object} env - Environment object with references to core, server, socket & payload
2022-06-22 11:32:51 -05:00
* @public
* @return {void}
*/
2020-09-17 00:44:32 -05:00
export async function run({ core, server, socket }) {
2018-06-04 00:07:24 -07:00
// gather connection and channel count
2018-03-09 23:47:00 -08:00
let ips = {};
let channels = {};
2019-11-06 23:35:23 -08:00
// for (const client of server.clients) {
2019-11-07 11:46:55 -06:00
server.clients.forEach((client) => {
2018-03-09 23:47:00 -08:00
if (client.channel) {
channels[client.channel] = true;
2019-11-06 23:35:23 -08:00
ips[client.address] = true;
2018-03-09 23:47:00 -08:00
}
2019-11-06 23:35:23 -08:00
});
2018-03-09 23:47:00 -08:00
2019-11-06 23:35:23 -08:00
const uniqueClientCount = Object.keys(ips).length;
const uniqueChannels = Object.keys(channels).length;
2018-03-09 23:47:00 -08:00
ips = null;
channels = null;
2018-06-04 00:07:24 -07:00
// dispatch info
2018-03-09 23:47:00 -08:00
server.reply({
2024-01-08 11:09:59 -08:00
cmd: 'info', // @todo Add numeric info code as `id`
2019-11-06 23:35:23 -08:00
text: `${uniqueClientCount} unique IPs in ${uniqueChannels} channels`,
2020-10-10 00:34:59 -05:00
channel: socket.channel, // @todo Multichannel
2018-03-09 23:47:00 -08:00
}, socket);
2018-06-04 00:07:24 -07:00
// stats are fun
2019-03-18 23:36:21 -07:00
core.stats.increment('stats-requested');
2019-11-06 23:35:23 -08:00
}
2018-03-09 23:47:00 -08:00
2022-06-22 11:32:51 -05: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-06 23:35:23 -08:00
export const info = {
2018-05-13 16:03:22 +05:30
name: 'stats',
2022-06-22 11:32:51 -05:00
category: 'core',
description: 'Sends back legacy server stats to the calling client',
usage: `
2019-11-06 23:35:23 -08:00
API: { cmd: 'stats' }`,
2018-06-04 00:07:24 -07:00
};