|
| 1 | +import { Args } from '@oclif/core'; |
| 2 | +import type { Task } from 'apify-client'; |
| 3 | +import chalk from 'chalk'; |
| 4 | + |
| 5 | +import { ApifyCommand } from '../../lib/apify_command.js'; |
| 6 | +import { prettyPrintBytes } from '../../lib/commands/pretty-print-bytes.js'; |
| 7 | +import { CompactMode, ResponsiveTable } from '../../lib/commands/responsive-table.js'; |
| 8 | +import { getUserPlanPricing } from '../../lib/commands/storage-size.js'; |
| 9 | +import { tryToGetKeyValueStore } from '../../lib/commands/storages.js'; |
| 10 | +import { error, simpleLog } from '../../lib/outputs.js'; |
| 11 | +import { getLoggedClientOrThrow, TimestampFormatter } from '../../lib/utils.js'; |
| 12 | + |
| 13 | +const consoleLikeTable = new ResponsiveTable({ |
| 14 | + allColumns: ['Row1', 'Row2'], |
| 15 | + mandatoryColumns: ['Row1', 'Row2'], |
| 16 | +}); |
| 17 | + |
| 18 | +export class KeyValueStoresInfoCommand extends ApifyCommand<typeof KeyValueStoresInfoCommand> { |
| 19 | + static override description = 'Shows information about a key-value store.'; |
| 20 | + |
| 21 | + static override hiddenAliases = ['kvs:info']; |
| 22 | + |
| 23 | + static override args = { |
| 24 | + storeId: Args.string({ |
| 25 | + description: 'The key-value store ID to print information about.', |
| 26 | + required: true, |
| 27 | + }), |
| 28 | + }; |
| 29 | + |
| 30 | + static override enableJsonFlag = true; |
| 31 | + |
| 32 | + async run() { |
| 33 | + const { storeId } = this.args; |
| 34 | + |
| 35 | + const apifyClient = await getLoggedClientOrThrow(); |
| 36 | + const maybeStore = await tryToGetKeyValueStore(apifyClient, storeId); |
| 37 | + |
| 38 | + if (!maybeStore) { |
| 39 | + error({ |
| 40 | + message: `Key-value store with ID or name "${storeId}" not found.`, |
| 41 | + }); |
| 42 | + |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const { keyValueStore: info } = maybeStore; |
| 47 | + |
| 48 | + const [user, actor, run] = await Promise.all([ |
| 49 | + apifyClient |
| 50 | + .user(info.userId) |
| 51 | + .get() |
| 52 | + .then((u) => u!), |
| 53 | + info.actId ? apifyClient.actor(info.actId).get() : Promise.resolve(undefined), |
| 54 | + info.actRunId ? apifyClient.run(info.actRunId).get() : Promise.resolve(undefined), |
| 55 | + ]); |
| 56 | + |
| 57 | + let task: Task | undefined; |
| 58 | + |
| 59 | + if (run?.actorTaskId) { |
| 60 | + task = await apifyClient |
| 61 | + .task(run.actorTaskId) |
| 62 | + .get() |
| 63 | + .catch(() => undefined); |
| 64 | + } |
| 65 | + |
| 66 | + if (this.flags.json) { |
| 67 | + return { |
| 68 | + ...info, |
| 69 | + user, |
| 70 | + actor: actor || null, |
| 71 | + run: run || null, |
| 72 | + task: task || null, |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + const fullSizeInBytes = info.stats?.storageBytes || 0; |
| 77 | + const readCount = info.stats?.readCount || 0; |
| 78 | + const writeCount = info.stats?.writeCount || 0; |
| 79 | + const deleteCount = info.stats?.deleteCount || 0; |
| 80 | + const listCount = info.stats?.listCount || 0; |
| 81 | + |
| 82 | + const operationsParts = [ |
| 83 | + `${chalk.bold(readCount.toLocaleString('en-US'))} ${chalk.gray(this.pluralString(readCount, 'read', 'reads'))}`, |
| 84 | + `${chalk.bold(writeCount.toLocaleString('en-US'))} ${chalk.gray(this.pluralString(writeCount, 'write', 'writes'))}`, |
| 85 | + `${chalk.bold(deleteCount.toLocaleString('en-US'))} ${chalk.gray(this.pluralString(deleteCount, 'delete', 'deletes'))}`, |
| 86 | + `${chalk.bold(listCount.toLocaleString('en-US'))} ${chalk.gray(this.pluralString(listCount, 'list', 'lists'))}`, |
| 87 | + ]; |
| 88 | + |
| 89 | + let row3 = `Operations: ${operationsParts.join(' / ')}`; |
| 90 | + |
| 91 | + if (user.plan) { |
| 92 | + const pricing = getUserPlanPricing(user.plan); |
| 93 | + |
| 94 | + if (pricing) { |
| 95 | + const storeCostPerHour = |
| 96 | + pricing.KEY_VALUE_STORE_TIMED_STORAGE_GBYTE_HOURS * (fullSizeInBytes / 1000 ** 3); |
| 97 | + const storeCostPerMonth = storeCostPerHour * 24 * 30; |
| 98 | + |
| 99 | + const usdAmountString = |
| 100 | + storeCostPerMonth > 1 ? `$${storeCostPerMonth.toFixed(2)}` : `$${storeCostPerHour.toFixed(3)}`; |
| 101 | + |
| 102 | + row3 += `\nStorage size: ${prettyPrintBytes({ bytes: fullSizeInBytes, shortBytes: true, precision: 1 })} / ${chalk.gray(`${usdAmountString} per month`)}`; |
| 103 | + } |
| 104 | + } else { |
| 105 | + row3 += `\nStorage size: ${prettyPrintBytes({ bytes: fullSizeInBytes, shortBytes: true, precision: 1 })} / ${chalk.gray('$unknown per month')}`; |
| 106 | + } |
| 107 | + |
| 108 | + const row1 = [ |
| 109 | + `Store ID: ${chalk.bgGray(info.id)}`, |
| 110 | + `Name: ${info.name ? chalk.bgGray(info.name) : chalk.bold(chalk.italic('Unnamed'))}`, |
| 111 | + `Created: ${chalk.bold(TimestampFormatter.display(info.createdAt))}`, |
| 112 | + `Modified: ${chalk.bold(TimestampFormatter.display(info.modifiedAt))}`, |
| 113 | + ].join('\n'); |
| 114 | + |
| 115 | + let runInfo = chalk.bold('—'); |
| 116 | + |
| 117 | + if (info.actRunId) { |
| 118 | + if (run) { |
| 119 | + runInfo = chalk.bgBlue(run.id); |
| 120 | + } else { |
| 121 | + runInfo = chalk.italic(chalk.gray('Run removed')); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + let actorInfo = chalk.bold('—'); |
| 126 | + |
| 127 | + if (actor) { |
| 128 | + actorInfo = chalk.blue(actor.title || actor.name); |
| 129 | + } |
| 130 | + |
| 131 | + let taskInfo = chalk.bold('—'); |
| 132 | + |
| 133 | + if (task) { |
| 134 | + taskInfo = chalk.blue(task.title || task.name); |
| 135 | + } |
| 136 | + |
| 137 | + const row2 = [`Run: ${runInfo}`, `Actor: ${actorInfo}`, `Task: ${taskInfo}`].join('\n'); |
| 138 | + |
| 139 | + consoleLikeTable.pushRow({ |
| 140 | + Row1: row1, |
| 141 | + Row2: row2, |
| 142 | + }); |
| 143 | + |
| 144 | + const rendered = consoleLikeTable.render(CompactMode.NoLines); |
| 145 | + |
| 146 | + const rows = rendered.split('\n').map((row) => row.trim()); |
| 147 | + |
| 148 | + // Remove the first row |
| 149 | + rows.shift(); |
| 150 | + |
| 151 | + const message = [ |
| 152 | + `${chalk.bold(info.name || chalk.italic('Unnamed'))}`, |
| 153 | + `${chalk.gray(info.name ? `${user.username}/${info.name}` : info.id)} ${chalk.gray('Owned by')} ${chalk.blue(user.username)}`, |
| 154 | + '', |
| 155 | + rows.join('\n'), |
| 156 | + '', |
| 157 | + row3, |
| 158 | + ].join('\n'); |
| 159 | + |
| 160 | + simpleLog({ message, stdout: true }); |
| 161 | + |
| 162 | + return undefined; |
| 163 | + } |
| 164 | +} |
0 commit comments