mirror of
https://github.com/Xahau/Validation-Ledger-Tx-Store-to-xPOP.git
synced 2025-11-20 11:45:51 +00:00
Add health + stats endpoint
This commit is contained in:
13
README.md
13
README.md
@@ -71,3 +71,16 @@ for automation.
|
||||
|
||||
This file browser is for development and test purposes only, for production, put a static webserver
|
||||
in front of this application & reverse proxy only the WebSocket (HTTP Upgrade) server.
|
||||
|
||||
#### Monitoring
|
||||
|
||||
A health check endpoint lives on `/health`, and returns e.g.:
|
||||
|
||||
```json
|
||||
{
|
||||
"uptime": 44023,
|
||||
"lastLedger": 41800244,
|
||||
"lastLedgerTx": 41800238,
|
||||
"txCount": 276
|
||||
}
|
||||
```
|
||||
|
||||
@@ -5,6 +5,12 @@ import expressWs from 'express-ws'
|
||||
import autoindex from 'express-autoindex/dist/index.cjs.js'
|
||||
import 'dotenv/config'
|
||||
|
||||
import { lastLedger } from '../lib/onLedger.mjs'
|
||||
import { txCount } from '../lib/onTransaction.mjs'
|
||||
|
||||
const startDate = new Date()
|
||||
let lastLedgerTx
|
||||
|
||||
let wss // WebSocket Server
|
||||
|
||||
if (!wss) {
|
||||
@@ -27,6 +33,15 @@ if (!wss) {
|
||||
|
||||
app.use('/', express.static('./store/'))
|
||||
|
||||
app.use('/health', (req, res) => {
|
||||
res.json({
|
||||
uptime: new Date() - startDate,
|
||||
lastLedger: lastLedger ?? null,
|
||||
lastLedgerTx: lastLedgerTx ?? null,
|
||||
txCount: txCount ?? null,
|
||||
})
|
||||
})
|
||||
|
||||
app.use('/:networkId([0-9]{1,})', (req, res, next) => {
|
||||
return autoindex('./store/' + req.params.networkId + '/', { json: /application\/json/.test(req.get('accept')) })(req, res, next)
|
||||
})
|
||||
@@ -78,6 +93,8 @@ const emit = _data => {
|
||||
data.xpop.blob = undefined
|
||||
}
|
||||
|
||||
lastLedgerTx = data.origin.ledgerIndex
|
||||
|
||||
if (account === '' || data.account === account) {
|
||||
client.send(JSON.stringify(data), { binary: false })
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import 'dotenv/config'
|
||||
const obtainedHumanReadableLedgers = []
|
||||
const obtainedBinaryTxLedgers = []
|
||||
|
||||
let lastLedger
|
||||
|
||||
const onLedger = async ({
|
||||
networkId,
|
||||
ledger,
|
||||
@@ -19,6 +21,8 @@ const onLedger = async ({
|
||||
const relativeStoreDir = 'store/' + networkId + '/' + ledgerIndexToFolders(ledger.ledger_index)
|
||||
const storeDir = new URL('../' + relativeStoreDir, import.meta.url).pathname
|
||||
|
||||
lastLedger = ledger.ledger_index
|
||||
|
||||
if (await dirExists(storeDir)) {
|
||||
const ledgerData = [
|
||||
...(
|
||||
@@ -148,4 +152,5 @@ const onLedger = async ({
|
||||
|
||||
export {
|
||||
onLedger,
|
||||
lastLedger,
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ import { waitForLedgerReady } from './events/ledgerReady.mjs'
|
||||
import { emit } from '../bin/webserver.mjs'
|
||||
import 'dotenv/config'
|
||||
|
||||
let txCount = 0
|
||||
|
||||
const xpopBinaryDir = new URL('../store/xpop', import.meta.url).pathname
|
||||
const lastSeenTransactions = []
|
||||
|
||||
@@ -81,6 +83,8 @@ const onTransaction = async ({
|
||||
if (xpopWritten) {
|
||||
console.log(' ### EMIT XPOP READY FOR', tx?.Account, Number(tx.Sequence), tx.hash)
|
||||
|
||||
txCount++
|
||||
|
||||
return await emit({
|
||||
account: tx?.Account,
|
||||
sequence: tx.Sequence,
|
||||
@@ -116,4 +120,5 @@ const onTransaction = async ({
|
||||
|
||||
export {
|
||||
onTransaction,
|
||||
txCount,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user