|
1 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public
|
2 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
|
3 | 3 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
4 |
| -import chalk from 'chalk' |
| 4 | +import { MathUtils, TimeUtils } from '@ironfish/sdk' |
5 | 5 | import { IronfishCommand } from '../../command'
|
6 |
| -import { RemoteFlags } from '../../flags' |
7 |
| -import { table, TableFlags } from '../../ui' |
| 6 | +import { JsonFlags, RemoteFlags } from '../../flags' |
| 7 | +import * as ui from '../../ui' |
8 | 8 |
|
9 | 9 | export class StatusCommand extends IronfishCommand {
|
10 | 10 | static description = `Get status of all accounts`
|
| 11 | + static enableJsonFlag = true |
11 | 12 |
|
12 | 13 | static flags = {
|
13 | 14 | ...RemoteFlags,
|
14 |
| - ...TableFlags, |
| 15 | + ...JsonFlags, |
15 | 16 | }
|
16 | 17 |
|
17 |
| - async start(): Promise<void> { |
18 |
| - const { flags } = await this.parse(StatusCommand) |
| 18 | + async start(): Promise<unknown> { |
| 19 | + await this.parse(StatusCommand) |
19 | 20 |
|
20 | 21 | const client = await this.connectRpc()
|
21 | 22 |
|
22 |
| - const response = await client.wallet.getAccountsStatus() |
23 |
| - |
24 |
| - table( |
25 |
| - response.content.accounts, |
26 |
| - { |
27 |
| - name: { |
28 |
| - get: (row) => row.name, |
29 |
| - header: 'Account Name', |
30 |
| - minWidth: 11, |
31 |
| - }, |
32 |
| - id: { |
33 |
| - get: (row) => row.id, |
34 |
| - header: 'Account ID', |
35 |
| - }, |
36 |
| - viewOnly: { |
37 |
| - get: (row) => row.viewOnly, |
38 |
| - header: 'View Only', |
39 |
| - }, |
40 |
| - headHash: { |
41 |
| - get: (row) => row.head?.hash ?? 'NULL', |
42 |
| - header: 'Head Hash', |
43 |
| - }, |
44 |
| - headInChain: { |
45 |
| - get: (row) => row.head?.inChain ?? 'NULL', |
46 |
| - header: 'Head In Chain', |
47 |
| - }, |
48 |
| - sequence: { |
49 |
| - get: (row) => row.head?.sequence ?? 'NULL', |
50 |
| - header: 'Head Sequence', |
51 |
| - }, |
52 |
| - scanningEnabled: { |
53 |
| - get: (row) => (row.scanningEnabled ? chalk.green('✓') : ''), |
54 |
| - header: 'Scanning Enabled', |
55 |
| - }, |
56 |
| - }, |
57 |
| - { |
58 |
| - printLine: this.log.bind(this), |
59 |
| - ...flags, |
60 |
| - }, |
61 |
| - ) |
| 23 | + const [nodeStatus, walletStatus] = await Promise.all([ |
| 24 | + client.node.getStatus(), |
| 25 | + client.wallet.getAccounts(), |
| 26 | + ]) |
| 27 | + |
| 28 | + const status: Record<string, unknown> = { |
| 29 | + Wallet: nodeStatus.content.accounts.enabled ? 'ENABLED' : 'DISABLED', |
| 30 | + Accounts: walletStatus.content.accounts.length, |
| 31 | + Head: nodeStatus.content.accounts.head.hash, |
| 32 | + Sequence: nodeStatus.content.accounts.head.sequence, |
| 33 | + Scanner: 'IDLE', |
| 34 | + } |
| 35 | + |
| 36 | + if (nodeStatus.content.accounts.scanning) { |
| 37 | + const progress = MathUtils.round( |
| 38 | + (nodeStatus.content.accounts.scanning.sequence / |
| 39 | + nodeStatus.content.accounts.scanning.endSequence) * |
| 40 | + 100, |
| 41 | + 2, |
| 42 | + ) |
| 43 | + |
| 44 | + const duration = Date.now() - nodeStatus.content.accounts.scanning.startedAt |
| 45 | + const speed = MathUtils.round(nodeStatus.content.accounts.scanning.speed, 2) |
| 46 | + |
| 47 | + status['Scanner'] = 'SCANNING' |
| 48 | + status['Scan Progress'] = progress + '%' |
| 49 | + status['Scan Speed'] = `${speed} B/s` |
| 50 | + status['Scan Duration'] = TimeUtils.renderSpan(duration, { |
| 51 | + hideMilliseconds: true, |
| 52 | + forceSecond: true, |
| 53 | + }) |
| 54 | + status[ |
| 55 | + 'Scan Block' |
| 56 | + ] = `${nodeStatus.content.accounts.scanning.sequence} -> ${nodeStatus.content.accounts.scanning.endSequence}` |
| 57 | + } |
| 58 | + |
| 59 | + this.log(ui.card(status)) |
| 60 | + |
| 61 | + return { |
| 62 | + ...nodeStatus.content.accounts, |
| 63 | + accountsCount: walletStatus.content.accounts.length, |
| 64 | + } |
62 | 65 | }
|
63 | 66 | }
|
0 commit comments