From d59ed259020848d827c9d2fc1635355d7c0fff26 Mon Sep 17 00:00:00 2001 From: Ford Date: Mon, 18 Nov 2024 15:36:08 -0800 Subject: [PATCH] cli: Add support for deleting subgraphs by protocol or syncing network - Can now supply multiple filters to `indexer actions delete`: --status, --syncing, and --network. --- .../src/commands/indexer/actions/delete.ts | 67 ++++++++++++++----- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/packages/indexer-cli/src/commands/indexer/actions/delete.ts b/packages/indexer-cli/src/commands/indexer/actions/delete.ts index 84940bf0e..f93d1e316 100644 --- a/packages/indexer-cli/src/commands/indexer/actions/delete.ts +++ b/packages/indexer-cli/src/commands/indexer/actions/delete.ts @@ -3,19 +3,29 @@ import chalk from 'chalk' import { loadValidatedConfig } from '../../../config' import { createIndexerManagementClient } from '../../../client' -import { fixParameters } from '../../../command-helpers' +import { + extractProtocolNetworkOption, + extractSyncingNetworkOption, + fixParameters, +} from '../../../command-helpers' import { deleteActions, fetchActions } from '../../../actions' const HELP = ` ${chalk.bold('graph indexer actions delete')} [options] all ${chalk.bold('graph indexer actions delete')} [options] [ ...] +${chalk.bold('graph indexer actions delete')} [options] ${chalk.dim('Options:')} -h, --help Show usage information + -n, --network Filter by protocol network (mainnet, arbitrum-one, sepolia, arbitrum-sepolia) + -s, --syncing Filter by the syncing network (see https://thegraph.com/networks/ for supported networks) --status queued|approved|pending|success|failed|canceled Filter by status -o, --output table|json|yaml Choose the output format: table (default), JSON, or YAML ` +function isNumber(value?: string | number): boolean { + return value != null && value !== '' && !isNaN(Number(value.toString())) +} module.exports = { name: 'delete', @@ -32,18 +42,37 @@ module.exports = { const outputFormat = o || output || 'table' const toHelp = help || h || undefined + let protocolNetwork: string | undefined = undefined + let syncingNetwork: string | undefined = undefined + let deleteType: 'ids' | 'all' | 'filter' = 'filter' + if (toHelp) { inputSpinner.stopAndPersist({ symbol: '💁', text: HELP }) return } try { + protocolNetwork = extractProtocolNetworkOption(parameters.options) + + syncingNetwork = extractSyncingNetworkOption(parameters.options) + if (!['json', 'yaml', 'table'].includes(outputFormat)) { throw Error( `Invalid output format "${outputFormat}", must be one of ['json', 'yaml', 'table']`, ) } + if ( + !status && + !syncingNetwork && + !protocolNetwork && + (!actionIDs || actionIDs.length === 0) + ) { + throw Error( + `Required at least one argument: actionID(s), 'all', '--status' filter, '--network' filter, or '--syncing' filter`, + ) + } + if ( status && !['queued', 'approved', 'pending', 'success', 'failed', 'canceled'].includes( @@ -55,18 +84,22 @@ module.exports = { ) } - if (actionIDs[0] == 'all') { - if (status || actionIDs.length > 1) { + if (actionIDs && actionIDs[0] == 'all') { + deleteType = 'all' + if (status || protocolNetwork || syncingNetwork || actionIDs.length > 1) { throw Error( - `Invalid query, cannot specify '--status' filter or multiple ids in addition to 'action = all'`, + `Invalid query, cannot specify '--status'|'--network'|'--syncing' filters or action ids in addition to 'action = all'`, ) } } - if (!status && (!actionIDs || actionIDs.length === 0)) { - throw Error( - `Required at least one argument: actionID(s), 'all', or '--status' filter`, - ) + if (actionIDs && isNumber(actionIDs[0])) { + deleteType = 'ids' + if (status || protocolNetwork || syncingNetwork || actionIDs.length > 1) { + throw Error( + `Invalid query, cannot specify '--status'|'--network'|'--syncing' filters or action ids in addition to 'action = all'`, + ) + } } inputSpinner.succeed('Processed input parameters') @@ -81,13 +114,17 @@ module.exports = { try { const config = loadValidatedConfig() const client = await createIndexerManagementClient({ url: config.api }) - - const numericActionIDs: number[] = - actionIDs[0] == 'all' - ? (await fetchActions(client, {})).map(action => action.id) - : status - ? (await fetchActions(client, { status })).map(action => action.id) - : actionIDs.map(action => +action) + let numericActionIDs: number[] = [] + + if (deleteType === 'all') { + numericActionIDs = (await fetchActions(client, {})).map(action => action.id) + } else if (deleteType === 'filter') { + numericActionIDs = ( + await fetchActions(client, { status, protocolNetwork, syncingNetwork }) + ).map(action => action.id) + } else if (deleteType === 'ids') { + numericActionIDs = actionIDs.map(action => +action) + } const numDeleted = await deleteActions(client, numericActionIDs)