Skip to content

Commit

Permalink
useLoopReq: added useTestnet for looping
Browse files Browse the repository at this point in the history
- the notion of updating stop to --stop

Signed-off-by: Salman Wahib <[email protected]>
  • Loading branch information
sxlmnwb committed Aug 12, 2023
1 parent f3a691f commit 0d0a45f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 10 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require('dotenv').config();
const { useMainnet, useTestnet, usePing, useCommand, startWhatsapp } = require('./src');
const { startLoop, stopLoop } = require('./src/useLoopReq');
const { startMainnetLoop, startTestnetLoop, stopLoop } = require('./src/useLoopReq');

const start = async () => {
const client = await startWhatsapp();
Expand All @@ -28,7 +28,7 @@ client.on('message', async msg => {
if (intervalString === '--stop') {
stopLoop(networks, chat);
} else {
startLoop(client, networks, intervalString, chat);
startMainnetLoop(client, networks, intervalString, chat);
}
} else if (commandParts.length === 2) {
const network = commandParts[1];
Expand All @@ -52,7 +52,7 @@ client.on('message', async msg => {
if (intervalString === '--stop') {
stopLoop(networks, chat);
} else {
startLoop(client, networks, intervalString, chat);
startTestnetLoop(client, networks, intervalString, chat);
}
} else if (commandParts.length === 2) {
const network = commandParts[1];
Expand Down
4 changes: 2 additions & 2 deletions src/useCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ module.exports = function useCommand(msg) {
return '```/mainnet <network>```\n```/mainnet <network> <interval>```\n```/mainnet --help```';

case '/mainnet --help':
return 'This tool was created to simplify validator monitoring with cosmos based node, with .env COSMOS_* variables that you can customize.\n\nFor use ```/testnet <network> <interval | stop>```, where ```<interval>``` is a number followed by "m" (for minutes) or "h" (for hours).\n\nFor example :\n```/mainnet cosmos```\n```/mainnet cosmos 1m```\n```/mainnet cosmos 12h```\n```/mainnet cosmos stop```';
return 'This tool was created to simplify validator monitoring with cosmos based node, with .env COSMOS_* variables that you can customize.\n\nFor use ```/testnet <network> <interval | --stop>```, where ```<interval>``` is a number followed by "m" (for minutes) or "h" (for hours).\n\nFor example :\n```/mainnet cosmos```\n```/mainnet cosmos 1m```\n```/mainnet cosmos 12h```\n```/mainnet cosmos --stop```';

case '/testnet':
return '```/testnet <network>```\n```/testnet <network> <interval>```\n```/testnet --help```';

case '/testnet --help':
return 'This tool was created to very simplify validator monitoring with cosmos based node, with .env COSMOS_* variables that you can customize.\n\nFor use ```/testnet <network> <interval | stop>```, where ```<interval>``` is a number followed by "m" (for minutes) or "h" (for hours).\n\nFor example :\n```/testnet cosmost```\n```/testnet cosmost 1m```\n```/testnet cosmost 12h```\n```/testnet cosmost stop```';
return 'This tool was created to very simplify validator monitoring with cosmos based node, with .env COSMOS_* variables that you can customize.\n\nFor use ```/testnet <network> <interval | --stop>```, where ```<interval>``` is a number followed by "m" (for minutes) or "h" (for hours).\n\nFor example :\n```/testnet cosmost```\n```/testnet cosmost 1m```\n```/testnet cosmost 12h```\n```/testnet cosmost --stop```';

case '/ping':
return '```/ping <destination>```\n```/ping --help```';
Expand Down
89 changes: 84 additions & 5 deletions src/useLoopReq.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const useMainnet = require('./useMainnet');
// const useTestnet = require('./useTestnet');
const useTestnet = require('./useTestnet');

let networks = {};

Expand All @@ -23,7 +23,7 @@ const stopLoop = async (networkInputs, chat) => {
};

module.exports = {
startLoop: async (client, networkInputs, intervalString, chat) => {
startMainnetLoop: async (client, networkInputs, intervalString, chat) => {
const stopIndex = networkInputs.indexOf('--stop');
if (stopIndex !== -1) {
networkInputs.splice(stopIndex, 1);
Expand All @@ -34,7 +34,7 @@ module.exports = {
const intervalNumber = parseInt(intervalString.slice(0, -1));
const intervalUnit = intervalString.slice(-1);
if (isNaN(intervalNumber) || (intervalUnit !== 'm' && intervalUnit !== 'h')) {
chat.sendMessage('Invalid command format. Use ```/status <network> <interval | --stop>```, where ```<interval>``` is a number followed by "m" (for minutes) or "h" (for hours).');
chat.sendMessage('Invalid command format. Use ```/mainnet <network> <interval | --stop>```, where ```<interval>``` is a number followed by "m" (for minutes) or "h" (for hours).');
return;
}
const intervalMillis = intervalUnit === 'm' ? intervalNumber * 60 * 1000 : intervalNumber * 60 * 60 * 1000;
Expand Down Expand Up @@ -62,7 +62,7 @@ module.exports = {
intervalId: setInterval(updateStatus, intervalMillis)
};
});

const messageIntervalId = setInterval(async () => {
let statusMessage = '';
for (const networkInput of networkInputs) {
Expand Down Expand Up @@ -99,7 +99,86 @@ module.exports = {
}
});

chat.sendMessage(`Started sending ${networkInputs.join(', ')} status updates every ${intervalNumber} ${intervalUnitWord}.`);
chat.sendMessage(`Started sending mainnet ${networkInputs.join(', ')} status updates every ${intervalNumber} ${intervalUnitWord}.`);
},

startTestnetLoop: async (client, networkInputs, intervalString, chat) => {
const stopIndex = networkInputs.indexOf('--stop');
if (stopIndex !== -1) {
networkInputs.splice(stopIndex, 1);
stopLoop(networkInputs, chat);
return;
}

const intervalNumber = parseInt(intervalString.slice(0, -1));
const intervalUnit = intervalString.slice(-1);
if (isNaN(intervalNumber) || (intervalUnit !== 'm' && intervalUnit !== 'h')) {
chat.sendMessage('Invalid command format. Use ```/testnet <network> <interval | --stop>```, where ```<interval>``` is a number followed by "m" (for minutes) or "h" (for hours).');
return;
}
const intervalMillis = intervalUnit === 'm' ? intervalNumber * 60 * 1000 : intervalNumber * 60 * 60 * 1000;

let intervalUnitWord;
if (intervalUnit === 'm') {
intervalUnitWord = intervalNumber > 1 ? 'minutes' : 'minute';
} else if (intervalUnit === 'h') {
intervalUnitWord = intervalNumber > 1 ? 'hours' : 'hour';
}

networkInputs.forEach(networkInput => {
if (networks[networkInput] && networks[networkInput].intervalId) {
clearInterval(networks[networkInput].intervalId);
}

const updateStatus = async () => {
const statusTestnet = await useTestnet(networkInput);
networks[networkInput].lastStatusTestnet = statusTestnet;
};

updateStatus();

networks[networkInput] = {
intervalId: setInterval(updateStatus, intervalMillis)
};
});

const messageIntervalId = setInterval(async () => {
let statusMessage = '';
for (const networkInput of networkInputs) {
if (networks[networkInput] && networks[networkInput].lastStatusTestnet) {
statusMessage += networks[networkInput].lastStatusTestnet + `\n------------------------------------------------------------------\n`;
}
}

networkInputs.forEach(async networkInput => {
if (networks[networkInput] && networks[networkInput].lastStatusMessage) {
try {
await networks[networkInput].lastStatusMessage.delete(true);
} catch (error) {
console.error(`WARN [${networkInput}] not delete`);
}
networks[networkInput].lastStatusMessage = null;
}
});

if (statusMessage.trim() !== '') {
const newStatusMessage = await chat.sendMessage(statusMessage.trim());

networkInputs.forEach(networkInput => {
if (networks[networkInput]) {
networks[networkInput].lastStatusMessage = newStatusMessage;
}
});
}
}, intervalMillis);

networkInputs.forEach(networkInput => {
if (networks[networkInput]) {
networks[networkInput].messageIntervalId = messageIntervalId;
}
});

chat.sendMessage(`Started sending testnet ${networkInputs.join(', ')} status updates every ${intervalNumber} ${intervalUnitWord}.`);
},

stopLoop
Expand Down

0 comments on commit 0d0a45f

Please sign in to comment.