diff --git a/README.md b/README.md index d1466d3..f88727f 100644 --- a/README.md +++ b/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 +} +``` diff --git a/bin/webserver.mjs b/bin/webserver.mjs index 859fe0b..a242b9d 100644 --- a/bin/webserver.mjs +++ b/bin/webserver.mjs @@ -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 }) } diff --git a/lib/onLedger.mjs b/lib/onLedger.mjs index 5d464e7..d0fcd60 100644 --- a/lib/onLedger.mjs +++ b/lib/onLedger.mjs @@ -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, } diff --git a/lib/onTransaction.mjs b/lib/onTransaction.mjs index 99cea37..60f810c 100644 --- a/lib/onTransaction.mjs +++ b/lib/onTransaction.mjs @@ -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, }