Skip to content

Commit

Permalink
cli: Add support for deleting subgraphs by protocol or syncing network
Browse files Browse the repository at this point in the history
- Can now supply multiple filters to `indexer actions delete`: --status,
--syncing, and --network.
  • Loading branch information
fordN committed Nov 18, 2024
1 parent 6f5083f commit d59ed25
Showing 1 changed file with 52 additions and 15 deletions.
67 changes: 52 additions & 15 deletions packages/indexer-cli/src/commands/indexer/actions/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] [<actionID1> ...]
${chalk.bold('graph indexer actions delete')} [options]
${chalk.dim('Options:')}
-h, --help Show usage information
-n, --network <networkName> Filter by protocol network (mainnet, arbitrum-one, sepolia, arbitrum-sepolia)
-s, --syncing <networkName> 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',
Expand All @@ -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(
Expand All @@ -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')
Expand All @@ -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)

Expand Down

0 comments on commit d59ed25

Please sign in to comment.