Created
May 16, 2021 01:11
-
-
Save azarus/aea439556b4c369844be4d40ce79cff0 to your computer and use it in GitHub Desktop.
UI Backend for Chia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var child_process = require("child_process"); | |
const http = require('http'); | |
function getFarmingSummary() | |
{ | |
let data = child_process.execSync("cd /root/chia && . ./activate && chia farm summary").toString(); | |
let farmingStatus = data.match(/Farming status\: (?<Data>(.*))/); | |
let totalChiaFarmed = data.match(/Total chia farmed\: (?<Data>(.*))/); | |
let userTransactionFees = data.match(/User transaction fees\: (?<Data>(.*))/); | |
let blockRewards = data.match(/Block rewards\: (?<Data>(.*))/); | |
let plotCount = data.match(/Plot count\: (?<Data>(.*))/); | |
let totalSizeOfPlots = data.match(/Total size of plots\: (?<Data>(.*))/); | |
let estimatedNetworkSpace = data.match(/Estimated network space\: (?<Data>(.*))/); | |
let expectedTimeToWin = data.match(/Expected time to win\: (?<Data>(.*))/); | |
return { | |
farmingStatus: farmingStatus.groups.Data, | |
totalChiaFarmed: parseFloat(totalChiaFarmed.groups.Data), | |
userTransactionFees: parseFloat(userTransactionFees.groups.Data), | |
blockRewards: parseFloat(blockRewards.groups.Data), | |
plotCount: parseInt(plotCount.groups.Data), | |
totalSizeOfPlots: totalSizeOfPlots.groups.Data, | |
estimatedNetworkSpace: estimatedNetworkSpace.groups.Data, | |
expectedTimeToWin: expectedTimeToWin.groups.Data, | |
} | |
} | |
function getNetworkStatus() | |
{ | |
let data = child_process.execSync("cd /root/chia && . ./activate && chia show -s").toString(); | |
let currentBlockchainStatus = data.match(/Current Blockchain Status\: (?<Data>(.*))/); | |
let currentlySyntedToBlock = data.match(/Currently synced to block\: (?<Data>(.*))/); | |
if(!currentlySyntedToBlock) | |
{ | |
currentlySyntedToBlock = data.match(/Height\:(?<Data>(.*))/); | |
} | |
let estiatedNetworkSpace = data.match(/Estimated network space\: (?<Data>(.*))/); | |
let currentDifficulty = data.match(/Current difficulty\: (?<Data>(.*))/); | |
let currentVDFIterations = data.match(/Current VDF sub_slot_iters\: (?<Data>(.*))/); | |
let totalIterations = data.match(/Total iterations since the start of the blockchain\: (?<Data>(.*))/); | |
let index = data.indexOf("Height: | Hash:"); | |
let it = data.substring(index, data.length).matchAll(/ (?<Height>(\d+)) \| (?<Hash>(.*))/g); | |
let hashes = []; | |
let v = it.next(); | |
let hash = v.value; | |
do | |
{ | |
hashes.push({ | |
height: hash.groups.Height, | |
hash: hash.groups.Hash, | |
}); | |
let v = it.next(); | |
if (!v) | |
break; | |
hash = v.value; | |
} while (hash); | |
return { | |
currentBlockchainStatus: currentBlockchainStatus.groups.Data, | |
currentlySyntedToBlock: parseFloat(currentlySyntedToBlock.groups.Data), | |
estiatedNetworkSpace: estiatedNetworkSpace.groups.Data, | |
currentDifficulty: parseInt(currentDifficulty.groups.Data), | |
currentVDFIterations: parseInt(currentVDFIterations.groups.Data), | |
totalIterations: parseInt(totalIterations.groups.Data), | |
hashes: hashes, | |
} | |
} | |
function getPeers() | |
{ | |
let data = child_process.execSync("cd /root/chia && . ./activate && chia show -c").toString(); | |
let farmingStatus = data.match(/Farming status: (?<Data>(.*))/); | |
let totalChiaFarmed = data.match(/Total chia farmed\: (?<Data>(.*))/); | |
let userTransactionFees = data.match(/User transaction fees\: (?<Data>(.*))/); | |
let blockRewards = data.match(/Block rewards\: (?<Data>(.*))/); | |
let plotCount = data.match(/Plot count\: (?<Data>(.*))/); | |
let totalSizeOfPlots = data.match(/Total size of plots\: (?<Data>(.*))/); | |
let estimatedNetworkSpace = data.match(/Estimated network space\: (?<Data>(.*))/); | |
let expectedTimeToWin = data.match(/Expected time to win\: (?<Data>(.*))/); | |
return { | |
farmingStatus: farmingStatus.groups.Data, | |
totalChiaFarmed: parseFloat(totalChiaFarmed.groups.Data), | |
userTransactionFees: parseFloat(userTransactionFees.groups.Data), | |
blockRewards: parseFloat(blockRewards.groups.Data), | |
plotCount: parseInt(plotCount.groups.Data), | |
totalSizeOfPlots: totalSizeOfPlots.groups.Data, | |
estimatedNetworkSpace: estimatedNetworkSpace.groups.Data, | |
expectedTimeToWin: expectedTimeToWin.groups.Data, | |
} | |
} | |
function getIOStat() | |
{ | |
let data = child_process.execSync("iostat -x").toString(); | |
// let data = ` | |
// Linux 5.4.0-72-generic (playdome-master) 05/13/2021 _x86_64_ (8 CPU) | |
// avg-cpu: %user %nice %system %iowait %steal %idle | |
// 3.45 0.00 0.88 2.03 0.00 93.65 | |
// Device r/s rkB/s rrqm/s %rrqm r_await rareq-sz w/s wkB/s wrqm/s %wrqm w_await wareq-sz d/s dkB/s drqm/s %drqm d_await dareq-sz aqu-sz %util | |
// loop0 0.00 0.02 0.00 0.00 10.68 3.42 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 | |
// loop1 0.02 0.03 0.00 0.00 18.85 1.57 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.01 | |
// loop2 0.00 0.03 0.00 0.00 47.55 22.14 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 | |
// loop3 0.00 0.03 0.00 0.00 51.79 18.44 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 | |
// loop4 0.00 0.01 0.00 0.00 54.96 11.19 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 | |
// loop5 0.27 0.28 0.00 0.00 8.81 1.03 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.01 | |
// loop6 0.23 0.23 0.00 0.00 1.98 1.03 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 | |
// loop7 0.00 0.00 0.00 0.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 | |
// sda 3.23 385.73 0.30 8.41 3.55 119.52 6.13 4955.44 1.61 20.86 14.40 808.65 0.00 0.00 0.00 0.00 0.00 0.00 0.09 5.78 | |
// sdb 6.37 96.65 0.44 6.40 9.95 15.17 34.17 2093.07 5.00 12.77 4.21 61.25 0.00 0.00 0.00 0.00 0.00 0.00 0.14 11.89 | |
// sdc 1.46 82.63 0.20 12.26 6.95 56.77 19.54 2220.76 0.68 3.38 5.77 113.64 0.00 0.00 0.00 0.00 0.00 0.00 0.08 6.32 | |
// `; | |
let it = data.matchAll(/(?<device>(\w+))( +)(?<rps>(.+?))( +)(?<rkbps>(.+?))( +)(?<rrqmps>(.+?))( +)(?<rrqmpercent>(.+?))( +)(?<r_await>(.+?))( +)(?<rareq_sz>(.+?))( +)(?<wps>(.+?))( +)(?<wkbps>(.+?))( +)(?<wrqmps>(.+?))( +)(?<wrqmpercent>(.+?))( +)(?<w_await>(.+?))( +)(?<wareq_sz>(.+?))( +)(?<dps>(.+?))( +)(?<dkbps>(.+?))( +)(?<drqmps>(.+?))( +)(?<drqmpercent>(.+?))( +)(?<d_await>(.+?))( +)(?<dreq_sz>(.+?))( +)(?<aku_sz>(.+?))( +)(?<util>(.+))/g); | |
let drives = []; | |
for(let i of it) | |
{ | |
drives.push(i.groups); | |
} | |
return { | |
drives: drives, | |
} | |
} | |
function getDiskSpace() | |
{ | |
let data = child_process.execSync("df -BG").toString(); | |
// let data = | |
// ` | |
// Filesystem 1G-blocks Used Available Use% Mounted on | |
// udev 8G 0G 8G 0% /dev | |
// tmpfs 2G 1G 2G 1% /run | |
// /dev/sdb3 246G 19G 214G 9% / | |
// tmpfs 8G 1G 8G 1% /dev/shm | |
// tmpfs 1G 0G 1G 0% /run/lock | |
// tmpfs 8G 0G 8G 0% /sys/fs/cgroup | |
// /dev/loop0 1G 1G 0G 100% /snap/core18/1944 | |
// /dev/loop1 1G 1G 0G 100% /snap/core18/1997 | |
// /dev/loop5 1G 1G 0G 100% /snap/snapd/11588 | |
// /dev/loop2 1G 1G 0G 100% /snap/lxd/19188 | |
// /dev/loop3 1G 1G 0G 100% /snap/lxd/20326 | |
// /dev/sdb2 246G 1G 233G 1% /boot | |
// /dev/sdb4 3175G 522G 2492G 18% /storage/disk1 | |
// tmpfs 2G 0G 2G 0% /run/user/0 | |
// /dev/sda 3667G 713G 2768G 21% /storage/disk2 | |
// /dev/sdc 3667G 503G 2978G 15% /storage/disk3 | |
// /dev/loop6 1G 1G 0G 100% /snap/snapd/11841 | |
// `; | |
let it = data.matchAll(/(?<device>(.+?))( +)(?<size>(.+?))(K|G|M|T)( +)(?<used>(.+?))(K|G|M|T)( +)(?<available>(.+?))(K|G|M|T)( +)(?<useage>(.+?))\%( +)(?<mount_point>(.*))/g); | |
let drives = []; | |
for(let i of it) | |
{ | |
drives.push({ | |
device: i.groups.device, | |
size: parseFloat(i.groups.size), | |
used: parseFloat(i.groups.used), | |
free: parseFloat(i.groups.available), | |
useage: parseFloat(i.groups.useage), | |
mountPoint: i.groups.mount_point, | |
}); | |
} | |
return { | |
drives: drives, | |
totalSpace: drives.reduce((prev, current) => { | |
return prev + current.size; | |
}, 0), | |
totalFree: drives.reduce((prev, current) => { | |
return prev + current.free; | |
}, 0), | |
totalUsed: drives.reduce((prev, current) => { | |
return prev + current.used; | |
}, 0), | |
} | |
} | |
const server = http.createServer((req, res) => | |
{ | |
res.statusCode = 200 | |
res.setHeader('Content-Type', 'application/json'); | |
res.setHeader('Access-Control-Allow-Headers', '*'); | |
res.setHeader('Allow', 'OPTIONS, GET, HEAD, POST'); | |
res.setHeader('Access-Control-Allow-Origin', '*'); | |
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, HEAD, POST'); | |
res.end(JSON.stringify({ | |
success: true, | |
farmingSummary: getFarmingSummary(), | |
networkStatus: getNetworkStatus(), | |
iostat: getIOStat(), | |
diskSpace: getDiskSpace(), | |
})); | |
}) | |
server.listen(1880, "0.0.0.0", () => | |
{ | |
console.log(`Server running`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment