From fffef841191409d0d7dc5b9d8ceb78c7309a1d52 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 17 Apr 2023 03:17:15 +0200 Subject: [PATCH] Making system status reporting better --- llamabot.js | 34 +++++++++++++++++++++++----------- package.json | 3 ++- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/llamabot.js b/llamabot.js index 7122265..d148fc5 100644 --- a/llamabot.js +++ b/llamabot.js @@ -3,7 +3,9 @@ import fetch from 'node-fetch'; import { emptyResponses } from './assets/emptyMessages.js'; import { resetResponses, userResetMessages } from './assets/resetMessages.js'; import { errorMessages, busyResponses } from './assets/errorMessages.js'; +import cpuStat from 'cpu-stat'; import os from 'os'; + import { Client, GatewayIntentBits, @@ -181,18 +183,28 @@ async function generateResponse(conversation, message) { // define a function that shows the system load percentage and updates the message const showSystemLoad = async () => { - const systemLoad = os.loadavg()[0] / os.cpus().length * 100; - const freeMemory = os.freemem() / 1024 / 1024; - const totalMemory = os.totalmem() / 1024 / 1024; - const usedMemory = totalMemory - freeMemory; - const messageData = `Please wait, I am thinking...\nSystem Load: ${systemLoad.toFixed(2)}%\nMemory Usage: ${usedMemory.toFixed(2)} MB / ${totalMemory.toFixed(2)} MB`; - // if the message object doesn't exist, create it - if (!botMessage) { - botMessage = await message.channel.send(messageData); - } else { - botMessage.edit(messageData); // otherwise, update the message - } + cpuStat.usagePercent(function(err, percent, seconds) { + if (err) { + return console.log(err); + } + + const systemLoad = percent //the percentage cpu usage over all cores + const freeMemory = os.freemem() / 1024 / 1024 / 1024; + const totalMemory = os.totalmem() / 1024 / 1024 / 1024; + const usedMemory = totalMemory - freeMemory; + const messageData = `Please wait, I am thinking...\nSystem Load Average: ${systemLoad.toFixed(2)}%\nMemory Usage: ${usedMemory.toFixed(2)} GB / ${totalMemory.toFixed(2)} GB`; + + // if the message object doesn't exist, create it + if (!botMessage) { + (async () => { + botMessage = await message.channel.send(messageData); + })() + + } else { + botMessage.edit(messageData); // otherwise, update the message + } + }) }; // call the function initially diff --git a/package.json b/package.json index 0deb1bd..cab05ad 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "discord.js": "^14.9.0", "dotenv": "^16.0.3", "node-fetch": "^3.3.1", - "os": "^0.1.2" + "os": "^0.1.2", + "cpu-stat": "^2.0.1" } }