@ledgerhq/coin-modules-monitoring
Version:
Push monitoring metrics to Datadog
116 lines • 5.2 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const store = __importStar(require("@ledgerhq/cryptoassets/tokens"));
const impl_1 = require("@ledgerhq/live-common/bridge/impl");
const index_1 = require("@ledgerhq/live-common/currencies/index");
const rxjs_1 = require("rxjs");
const accountId_1 = require("@ledgerhq/coin-framework/account/accountId");
const LiveConfig_1 = require("@ledgerhq/live-config/LiveConfig");
const sharedConfig_1 = require("@ledgerhq/live-common/config/sharedConfig");
const currencies_1 = __importDefault(require("./currencies"));
const datadog_1 = require("./datadog");
function toEmptyAccount(currency, info) {
const id = (0, accountId_1.encodeAccountId)({
type: "js",
version: "2",
currencyId: currency.id,
xpubOrAddress: info.xpub ?? info.address,
derivationMode: info.derivationMode ?? "",
});
return {
id,
currency,
freshAddress: info.address,
xpub: info.xpub,
derivationMode: info.derivationMode ?? "",
operations: [],
pendingOperations: [],
balanceHistoryCache: {},
};
}
function getSync(currency) {
const bridge = (0, impl_1.getAccountBridgeByFamily)(currency.family);
return (account) => (0, rxjs_1.firstValueFrom)(bridge
.sync(account, { paginationConfig: {} })
.pipe((0, rxjs_1.reduce)((a, f) => f(a), account)));
}
async function default_1(currencyIds) {
LiveConfig_1.LiveConfig.setConfig(sharedConfig_1.liveConfig);
(0, impl_1.setup)(store);
const entries = [];
const nbOfAccounts = currencyIds
.map(currencyId => Object.keys(currencies_1.default[currencyId].accounts).length)
.reduce((previous, current) => previous + current, 0);
let i = 0;
console.log(`Monitoring ${nbOfAccounts} account(s) within ${currencyIds.join(", ")}`);
for (const currencyId of currencyIds) {
const monitored = currencies_1.default[currencyId];
const currency = (0, index_1.getCryptoCurrencyById)(currencyId);
const sync = getSync(currency);
for (const [accountType, info] of Object.entries(monitored.accounts)) {
console.log(`\n[${++i} / ${nbOfAccounts}] Start (currency = "${currencyId}" account = "${accountType}")`);
try {
const startScan = Date.now();
const initialAccount = await sync(toEmptyAccount(currency, info));
const endScan = Date.now();
const startSync = Date.now();
await sync(initialAccount);
const endSync = Date.now();
const scanDuration = endScan - startScan;
const syncDuration = endSync - startSync;
const { xpubOrAddress } = (0, accountId_1.decodeAccountId)(initialAccount.id);
console.log(`[${i} / ${nbOfAccounts}] Completed in ${scanDuration + syncDuration} ms`);
entries.push({
duration: scanDuration,
currencyName: currency.id,
coinModuleName: currency.family,
operationType: "scan",
accountType: accountType,
transactions: initialAccount.operationsCount,
accountAddressOrXpub: xpubOrAddress,
}, {
duration: syncDuration,
currencyName: currency.id,
coinModuleName: currency.family,
operationType: "sync",
accountType: accountType,
transactions: initialAccount.operationsCount,
accountAddressOrXpub: xpubOrAddress,
});
}
catch (err) {
console.error(`Skipping failing run. Error: ${err instanceof Error ? err.message : err}`);
}
}
}
await (0, datadog_1.submitLogs)(entries);
return entries;
}
exports.default = default_1;
//# sourceMappingURL=run.js.map