Files
server-pages/server-status.js
Kevin Mok bc0e55187a Create status JSON
Compare fetched titles with ones in CSV to get status. Include update
time and server uptime.
2019-09-22 23:34:54 -04:00

50 lines
1.3 KiB
JavaScript

const urlMetadata = require('url-metadata')
const csv = require('csvtojson')
const log = console.log
const { spawnSync } = require( 'child_process' ),
// uptime = spawnSync( 'uptime', [ '-p' ] )
uptime = spawnSync( 'uptime', [ '-s' ] )
const csvFilePath = 'server-pages.csv'
// const csvFilePath = 'server-pages-test.csv'
const checkIfTitleMatches = obj => {
return urlMetadata(obj['url']).then(metadata => { // success handler
if (metadata.title == obj['siteTitle']) {
// log(obj['shortUrl'])
obj['status'] = 'up'
} else {
// console.log(metadata)
obj['status'] = 'down'
}
return true
},
error => { // failure handler
console.log(error)
})
}
const updateSiteStatus = json => {
const promiseArray = []
json.forEach(async function (obj) {
if (obj['siteTitle'] != '') {
promiseArray.push(checkIfTitleMatches(obj))
} else{
obj['status'] = 'up'
}
})
return promiseArray
}
csv().fromFile(csvFilePath).then(async function (json) {
Promise.all(updateSiteStatus(json)).then(results => {
const statusJson = {
"sites": json,
"updateTime": Date.now(),
"upSince": `${uptime.stdout.toString()}`.replace(/\n/g, ''),
}
// console.log(JSON.stringify(statusJson))
console.log(JSON.stringify(statusJson, null, 2))
})
})