Skip to content

Instantly share code, notes, and snippets.

@bemnet4u
Created June 1, 2021 01:01
Show Gist options
  • Save bemnet4u/0829ce170645a1c0a421ea2c20f54a2a to your computer and use it in GitHub Desktop.
Save bemnet4u/0829ce170645a1c0a421ea2c20f54a2a to your computer and use it in GitHub Desktop.
var request = require('https');
var url = require('url');
var util = require('util');
const p = require('phin');
exports.handler = async (event) => {
// TODO implement
try{
var next = "https://pool.vet/api/check_pool?q="+event.pool;
const resultMap = [];
let pass = false;
var status = "The stake pool passed all tests";
while(next != null) {
const result = await getBody(next);
next = null;
for(r of result) {
if(r.next != null) {
next = `https://pool.vet${r.next}`
} else{
console.log(`Test: ${r.text} => Result: ${r.type}`);
pass = r.text == "The stake pool passed all tests.";
resultMap.push(r);
}
if(r.type != null && r.type == "todo") {
status = r.text;
}
}
}
let response = { statusCode: 200, body: resultMap};
if(pass == false) {
response = { statusCode: 400, body: resultMap};
}
await notifySlack(pass, status);
return response;
} catch (error)
{
console.error(error)
await notifySlack(false, "Unexpected error when checking pool status. Detail: "+error)
return {statusCode: 500, body: error}
}
};
const reqURL = `https://hooks.slack.com/services/<ID>;
async function notifySlack(pass, status) {
var text = pass? `✔️ ${status}` : `❌ ${status}. Check logs https://console.aws.amazon.com/lambda/home`
const message = {
'channel': 'stock_trading',
'username': 'PLOT Monitor',
'text': text,
'icon_emoji': ':aws:',
};
return p({
url: reqURL,
method: 'POST',
data: message
});
}
async function getBody(url) {
console.log("Request: " + url);
// Return new promise
return new Promise(function(resolve, reject) {
try {
request.get(url, function(resp) {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
try{
console.log("body: ", data)
resolve(JSON.parse(data));
} catch(e) {
console.error(e);
reject(e + " " + data);
}
});
})
.on("error", (err) => {
console.error(err)
reject(err);
})
} catch(error) {
console.error(error);
reject(error)
}
})
}
exports.handler({pool: "PLOT"}).then(r => console.log(r))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment