|
| 1 | +import { Flags } from '@oclif/core' |
| 2 | +import cliProgress from 'cli-progress' |
| 3 | + |
| 4 | +import { ScCommand } from '../../../sc-command.js' |
| 5 | +import { EventBrokerListApiResponse, OperationData, OperationResponse } from '../../../types/broker.js' |
| 6 | +import { ScConnection } from '../../../util/sc-connection.js' |
| 7 | +import { camelCaseToTitleCase, renderKeyValueTable, sleep } from '../../../util/internal.js' |
| 8 | + |
| 9 | +export default class MissionctrlBrokerOpstatus extends ScCommand<typeof MissionctrlBrokerOpstatus> { |
| 10 | + static override args = {} |
| 11 | + static override description = `Get the status of an operation that being performed on an event broker service. |
| 12 | + To get the operation, you provide identifier of the operation and the identifier of the event broker service. |
| 13 | +
|
| 14 | + Token Permissions: [ mission_control:access or services:get or services:get:self or services:view or services:view:self ]` |
| 15 | + static override examples = [ |
| 16 | + '<%= config.bin %> <%= command.id %>', |
| 17 | + ] |
| 18 | + static override flags = { |
| 19 | + 'broker-id': Flags.string({ |
| 20 | + char: 'b', |
| 21 | + description: 'Id of the event broker service.', |
| 22 | + exactlyOne: ['broker-id', 'name'], |
| 23 | + }), |
| 24 | + name: Flags.string({ |
| 25 | + char: 'n', |
| 26 | + description: 'Name of the event broker service.', |
| 27 | + exactlyOne: ['broker-id', 'name'], |
| 28 | + }), |
| 29 | + 'operation-id': Flags.string({ |
| 30 | + char: 'o', |
| 31 | + description: 'The identifier of the operation being performed on the event broker service.' |
| 32 | + }), |
| 33 | + 'show-progress': Flags.boolean({ |
| 34 | + char: 'p', |
| 35 | + description: 'Displays a status bar of the in-progress operation. The command will wait for completion of each step of the operation.' |
| 36 | + }), |
| 37 | + 'wait-ms': Flags.integer({ |
| 38 | + char: 'w', |
| 39 | + description: 'The milliseconds to wait between API call in when showing progress. Default is 5000 ms.' |
| 40 | + }), |
| 41 | + } |
| 42 | + |
| 43 | + public async run(): Promise<OperationData> { |
| 44 | + const { flags } = await this.parse(MissionctrlBrokerOpstatus) |
| 45 | + |
| 46 | + const name = flags.name ?? '' |
| 47 | + let brokerId = flags['broker-id'] ?? '' |
| 48 | + const operationId = flags['operation-id'] ?? '' |
| 49 | + const showProgress = flags['show-progress'] ?? false |
| 50 | + const waitMs = flags['wait-ms'] ?? 5000 |
| 51 | + |
| 52 | + const conn = new ScConnection() |
| 53 | + |
| 54 | + // API url |
| 55 | + let apiUrl: string = `/missionControl/eventBrokerServices` |
| 56 | + |
| 57 | + // If broker name provided, retrieve the broker service id first |
| 58 | + // then retrieve the operation status using the id |
| 59 | + if (name) { |
| 60 | + // API call to get broker by name |
| 61 | + apiUrl += `?customAttributes=name=="${name}"` |
| 62 | + const resp = await conn.get<EventBrokerListApiResponse>(apiUrl) |
| 63 | + // TODO FUTURE: show status of multiple brokers operations that match the name |
| 64 | + if (resp.data.length > 1) { |
| 65 | + this.error(`Multiple broker services found with: ${name}. Exactly one broker service must match the provided name.`) |
| 66 | + } else { |
| 67 | + brokerId = resp.data[0]?.id |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // API call to retrieve status of the broker operation |
| 72 | + apiUrl = `/missionControl/eventBrokerServices/${brokerId}/operations/${operationId}` |
| 73 | + if (showProgress) { |
| 74 | + apiUrl += '?expand=progressLogs' |
| 75 | + } |
| 76 | + let resp = await conn.get<OperationResponse>(apiUrl) |
| 77 | + this.print(resp.data) |
| 78 | + |
| 79 | + // Display progress bar for each step included in the progress logs |
| 80 | + // Enable progress bar if set |
| 81 | + if (showProgress && resp.data.progressLogs && resp.data.progressLogs.length > 0) { |
| 82 | + let progressLogs = resp.data.progressLogs |
| 83 | + let numSteps = progressLogs.length |
| 84 | + |
| 85 | + // Create a new progress bar instance and use shades_classic theme |
| 86 | + const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic); |
| 87 | + |
| 88 | + // start the progress bar with a total value of size of the steps and start value of 0 |
| 89 | + progressBar.start(numSteps, 0); |
| 90 | + |
| 91 | + // Update the progress with the steps completed |
| 92 | + let completedNumSteps = 0 |
| 93 | + while (completedNumSteps < numSteps) { |
| 94 | + // Wait before making the next API call |
| 95 | + await sleep(waitMs) |
| 96 | + // Make another API call to get the lastest progress |
| 97 | + resp = await conn.get<OperationResponse>(apiUrl) |
| 98 | + if (resp.data.progressLogs) { |
| 99 | + progressLogs = resp.data.progressLogs |
| 100 | + for (const progressLog of progressLogs) { |
| 101 | + if (progressLog.step === 'success') { |
| 102 | + completedNumSteps++ |
| 103 | + } |
| 104 | + } |
| 105 | + // Update progress bar |
| 106 | + progressBar.update(completedNumSteps) |
| 107 | + } else { |
| 108 | + break |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // stop the progress bar |
| 113 | + progressBar.stop() |
| 114 | + } |
| 115 | + |
| 116 | + return resp.data |
| 117 | + } |
| 118 | + |
| 119 | + private print(environment: OperationData): void { |
| 120 | + const tableRows = [ |
| 121 | + ['Key', 'Value'], |
| 122 | + ...Object.entries(environment).map(([key, value]) => [camelCaseToTitleCase(key), value]), |
| 123 | + ] |
| 124 | + this.log() |
| 125 | + this.log(renderKeyValueTable(tableRows)) |
| 126 | + } |
| 127 | +} |
0 commit comments