From 4dae273cd61ba2a46e809e62d11d77146c2b9444 Mon Sep 17 00:00:00 2001 From: Dan Janosik Date: Sun, 5 Jul 2020 21:03:15 -0400 Subject: [PATCH] electrum connect/disconnect stats --- app/api/electrumAddressApi.js | 42 +++++++++++++++++++++++++++++------ views/admin.pug | 31 ++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/app/api/electrumAddressApi.js b/app/api/electrumAddressApi.js index 5e9bdeb..d55e5c8 100644 --- a/app/api/electrumAddressApi.js +++ b/app/api/electrumAddressApi.js @@ -16,7 +16,14 @@ const ElectrumClient = require('electrum-client'); var electrumClients = []; -global.electrumStats = {}; +global.electrumStats = { + base: { + connect: { count: 0, firstSeenAt: null, lastSeenAt: null }, + disconnect: { count: 0, firstSeenAt: null, lastSeenAt: null }, + error: { count: 0, firstSeenAt: null, lastSeenAt: null } + }, + rpc: {} +}; var noConnectionsErrorText = "No ElectrumX connection available. This could mean that the connection was lost or that ElectrumX is processing transactions and therefore not accepting requests. This tool will try to reconnect. If you manage your own ElectrumX server you may want to check your ElectrumX logs."; @@ -53,6 +60,13 @@ function connectToServer(host, port, protocol) { var onConnect = function(client, versionInfo) { debugLog(`Connected to ElectrumX @ ${host}:${port} (${JSON.stringify(versionInfo)})`); + global.electrumStats.base.connect.count++; + global.electrumStats.base.connect.lastSeenAt = new Date(); + + if (global.electrumStats.base.connect.firstSeenAt == null) { + global.electrumStats.base.connect.firstSeenAt = new Date(); + } + electrumClients.push(client); resolve(); @@ -61,6 +75,13 @@ function connectToServer(host, port, protocol) { var onClose = function(client) { debugLog(`Disconnected from ElectrumX @ ${host}:${port}`); + global.electrumStats.base.disconnect.count++; + global.electrumStats.base.disconnect.lastSeenAt = new Date(); + + if (global.electrumStats.base.disconnect.firstSeenAt == null) { + global.electrumStats.base.disconnect.firstSeenAt = new Date(); + } + var index = electrumClients.indexOf(client); if (index > -1) { @@ -71,6 +92,13 @@ function connectToServer(host, port, protocol) { var onError = function(err) { debugLog(`Electrum error: ${JSON.stringify(err)}`); + global.electrumStats.base.error.count++; + global.electrumStats.base.error.lastSeenAt = new Date(); + + if (global.electrumStats.base.error.firstSeenAt == null) { + global.electrumStats.base.error.firstSeenAt = new Date(); + } + utils.logError("937gf47dsyde", err, {host:host, port:port, protocol:protocol}); }; @@ -296,18 +324,18 @@ function getAddressBalance(addrScripthash) { } function logStats(cmd, dt, success) { - if (!global.electrumStats[cmd]) { - global.electrumStats[cmd] = {count:0, time:0, successes:0, failures:0}; + if (!global.electrumStats.rpc[cmd]) { + global.electrumStats.rpc[cmd] = {count:0, time:0, successes:0, failures:0}; } - global.electrumStats[cmd].count++; - global.electrumStats[cmd].time += dt; + global.electrumStats.rpc[cmd].count++; + global.electrumStats.rpc[cmd].time += dt; if (success) { - global.electrumStats[cmd].successes++; + global.electrumStats.rpc[cmd].successes++; } else { - global.electrumStats[cmd].failures++; + global.electrumStats.rpc[cmd].failures++; } } diff --git a/views/admin.pug b/views/admin.pug index fe16aec..af700f6 100644 --- a/views/admin.pug +++ b/views/admin.pug @@ -140,7 +140,34 @@ block content table.table.table-hover.table-striped thead tr - th Method + th Action + th.text-right Count + th.text-right First Seen + th.text-right Last Seen + + tbody + each item, itemName in electrumStats.base + tr.text-monospace + td #{itemName} + td.text-right #{item.count.toLocaleString()} + + if (item.firstSeenAt) + td.text-right #{moment.duration(new Date().getTime() - item.firstSeenAt.getTime()).format()} ago + else + td.text-right - + + if (item.lastSeenAt) + td.text-right #{moment.duration(new Date().getTime() - item.lastSeenAt.getTime()).format()} ago + else + td.text-right - + + hr + + div.table-responsive + table.table.table-hover.table-striped + thead + tr + th RPC th.text-right Count th.text-right Time small (s) @@ -150,7 +177,7 @@ block content th.text-right Success Rate tbody - each item, itemName in electrumStats + each item, itemName in electrumStats.rpc tr.text-monospace td #{itemName} td.text-right #{item.count.toLocaleString()}