cli: allow DB stats tracking via environment variable

This commit is contained in:
Florian Dold 2023-09-15 17:14:37 +02:00
parent 0ff189d229
commit 40d2aa0c11
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 28 additions and 15 deletions

View File

@ -53,6 +53,7 @@ import {
import { createPlatformHttpLib } from "@gnu-taler/taler-util/http"; import { createPlatformHttpLib } from "@gnu-taler/taler-util/http";
import { JsonMessage, runRpcServer } from "@gnu-taler/taler-util/twrpc"; import { JsonMessage, runRpcServer } from "@gnu-taler/taler-util/twrpc";
import { import {
AccessStats,
createNativeWalletHost, createNativeWalletHost,
createNativeWalletHost2, createNativeWalletHost2,
Wallet, Wallet,
@ -237,16 +238,21 @@ export interface WalletContext {
): Promise<T>; ): Promise<T>;
} }
interface CreateWalletResult {
wallet: Wallet;
getStats: () => AccessStats;
}
async function createLocalWallet( async function createLocalWallet(
walletCliArgs: WalletCliArgsType, walletCliArgs: WalletCliArgsType,
notificationHandler?: (n: WalletNotification) => void, notificationHandler?: (n: WalletNotification) => void,
): Promise<Wallet> { ): Promise<CreateWalletResult> {
const dbPath = walletCliArgs.wallet.walletDbFile ?? defaultWalletDbPath; const dbPath = walletCliArgs.wallet.walletDbFile ?? defaultWalletDbPath;
const myHttpLib = createPlatformHttpLib({ const myHttpLib = createPlatformHttpLib({
enableThrottling: walletCliArgs.wallet.noThrottle ? false : true, enableThrottling: walletCliArgs.wallet.noThrottle ? false : true,
requireTls: walletCliArgs.wallet.noHttp, requireTls: walletCliArgs.wallet.noHttp,
}); });
const wallet = await createNativeWalletHost({ const wh = await createNativeWalletHost2({
persistentStoragePath: dbPath !== ":memory:" ? dbPath : undefined, persistentStoragePath: dbPath !== ":memory:" ? dbPath : undefined,
httpLib: myHttpLib, httpLib: myHttpLib,
notifyHandler: (n) => { notifyHandler: (n) => {
@ -269,10 +275,10 @@ async function createLocalWallet(
applyVerbose(walletCliArgs.wallet.verbose); applyVerbose(walletCliArgs.wallet.verbose);
try { try {
await wallet.handleCoreApiRequest("initWallet", "native-init", { await wh.wallet.handleCoreApiRequest("initWallet", "native-init", {
skipDefaults: walletCliArgs.wallet.skipDefaults, skipDefaults: walletCliArgs.wallet.skipDefaults,
}); });
return wallet; return { wallet: wh.wallet, getStats: wh.getDbStats };
} catch (e) { } catch (e) {
const ed = getErrorDetailFromException(e); const ed = getErrorDetailFromException(e);
console.error("Operation failed: " + summarizeTalerErrorDetail(ed)); console.error("Operation failed: " + summarizeTalerErrorDetail(ed));
@ -307,16 +313,20 @@ async function withWallet<T>(
w.close(); w.close();
return res; return res;
} else { } else {
const w = await createLocalWallet(walletCliArgs, waiter.notify); const wh = await createLocalWallet(walletCliArgs, waiter.notify);
const ctx: WalletContext = { const ctx: WalletContext = {
client: w.client, client: wh.wallet.client,
waitForNotificationCond: waiter.waitForNotificationCond, waitForNotificationCond: waiter.waitForNotificationCond,
makeCoreApiRequest(operation, payload) { makeCoreApiRequest(operation, payload) {
return w.handleCoreApiRequest(operation, "my-req", payload); return wh.wallet.handleCoreApiRequest(operation, "my-req", payload);
}, },
}; };
const result = await f(ctx); const result = await f(ctx);
w.stop(); wh.wallet.stop();
if (process.env.TALER_WALLET_DBSTATS) {
console.log("database stats:");
console.log(j2s(wh.getStats()));
}
return result; return result;
} }
} }
@ -330,7 +340,8 @@ async function withLocalWallet<T>(
walletCliArgs: WalletCliArgsType, walletCliArgs: WalletCliArgsType,
f: (w: { client: WalletCoreApiClient; ws: Wallet }) => Promise<T>, f: (w: { client: WalletCoreApiClient; ws: Wallet }) => Promise<T>,
): Promise<T> { ): Promise<T> {
const w = await createLocalWallet(walletCliArgs); const wh = await createLocalWallet(walletCliArgs);
const w = wh.wallet;
const res = await f({ client: w.client, ws: w }); const res = await f({ client: w.client, ws: w });
w.stop(); w.stop();
return res; return res;
@ -1030,8 +1041,7 @@ peerCli
const resp = await wallet.client.call( const resp = await wallet.client.call(
WalletApiOperation.ConfirmPeerPullDebit, WalletApiOperation.ConfirmPeerPullDebit,
{ {
peerPullDebitId: peerPullDebitId: args.confirmIncomingPayPull.peerPullDebitId,
args.confirmIncomingPayPull.peerPullDebitId,
}, },
); );
console.log(JSON.stringify(resp, undefined, 2)); console.log(JSON.stringify(resp, undefined, 2));
@ -1046,8 +1056,7 @@ peerCli
const resp = await wallet.client.call( const resp = await wallet.client.call(
WalletApiOperation.ConfirmPeerPushCredit, WalletApiOperation.ConfirmPeerPushCredit,
{ {
peerPushCreditId: peerPushCreditId: args.confirmIncomingPayPush.peerPushCreditId,
args.confirmIncomingPayPush.peerPushCreditId,
}, },
); );
console.log(JSON.stringify(resp, undefined, 2)); console.log(JSON.stringify(resp, undefined, 2));
@ -1174,7 +1183,8 @@ advancedCli
}) })
.action(async (args) => { .action(async (args) => {
logger.info(`serving at ${args.serve.unixPath}`); logger.info(`serving at ${args.serve.unixPath}`);
const w = await createLocalWallet(args); const wh = await createLocalWallet(args);
const w = wh.wallet;
w.runTaskLoop() w.runTaskLoop()
.then((res) => { .then((res) => {
logger.warn("task loop exited unexpectedly"); logger.warn("task loop exited unexpectedly");

View File

@ -108,10 +108,13 @@ async function makeSqliteDb(
filename: args.persistentStoragePath ?? ":memory:", filename: args.persistentStoragePath ?? ":memory:",
}); });
myBackend.enableTracing = false; myBackend.enableTracing = false;
if (process.env.TALER_WALLET_DBSTATS) {
myBackend.trackStats = true;
}
const myBridgeIdbFactory = new BridgeIDBFactory(myBackend); const myBridgeIdbFactory = new BridgeIDBFactory(myBackend);
return { return {
getStats() { getStats() {
throw Error("not implemented"); return myBackend.accessStats;
}, },
idbFactory: myBridgeIdbFactory, idbFactory: myBridgeIdbFactory,
}; };