-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1612 from orbs-network/feature/metrics-json
Metrics update names + structure (merge pending)
- Loading branch information
Showing
44 changed files
with
586 additions
and
454 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const request = require("request-promise"); | ||
|
||
class Metrics { | ||
constructor(endpoint) { | ||
this._endpoint = endpoint; | ||
this._metrics = {}; | ||
} | ||
|
||
async refresh() { | ||
try { | ||
const body = await request(`http://${this._endpoint}/metrics`, { | ||
timeout: 1000, | ||
}); | ||
this._metrics = JSON.parse(body); | ||
} catch (e) { | ||
// Suppressed errors | ||
console.error(`${e.message}: ${this._endpoint}`); | ||
} | ||
} | ||
|
||
_get(names){ | ||
let curr = this._metrics.Payload; | ||
for (let i = 0;i < names.length; i++) { | ||
if (curr[names[i]]) { | ||
curr = curr[names[i]]; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
return curr; | ||
} | ||
|
||
getBlockHeight() { return this._get(["BlockStorage", "LastCommitted", "BlockHeight"]) || 0} | ||
} | ||
|
||
async function eventuallyClosingBlocks(endpoint, pollingIntervalSeconds) { | ||
let metrics = new Metrics(endpoint); | ||
await metrics.refresh(); | ||
pollingIntervalSeconds = pollingIntervalSeconds || 60; | ||
if (pollingIntervalSeconds <= 0) { | ||
pollingIntervalSeconds = 60; | ||
} | ||
|
||
// First let's get the current blockheight and wait for it to close 5 more blocks | ||
const startBlockHeight = metrics.getBlockHeight(); | ||
console.log('Fetching current blockheight of the network: ', startBlockHeight); | ||
|
||
for (let counter = 0; counter < 60; counter++) { | ||
await sleep(pollingIntervalSeconds * 1000); | ||
await metrics.refresh(); | ||
let currentBlockHeight = metrics.getBlockHeight(); | ||
if (currentBlockHeight >= startBlockHeight + 5) { | ||
console.log(`got to ${currentBlockHeight} - yay!`); | ||
return true; | ||
} | ||
if (counter % 5 === 0) { | ||
console.log(`try #${counter + 1}: network blockheight: ${currentBlockHeight}`); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
// general | ||
function sleep(ms) { | ||
return new Promise(resolve => { | ||
setTimeout(resolve, ms) | ||
}) | ||
} | ||
|
||
module.exports = { | ||
Metrics, eventuallyClosingBlocks | ||
}; |
This file was deleted.
Oops, something went wrong.
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
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
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
Oops, something went wrong.