Skip to content
← Public packages

@kentcdodds/fly

Manage Fly.io apps, Machines, logs, and metrics.

src/machine-count.ts

23 lines · 901 B · TypeScript
import { listMachines } from './machines.ts'
import type { FlyMachinesInput } from './types.ts'

/**
 * Count Fly machines and group totals by state and app name.
 * @returns Aggregate counts plus the underlying machine list.
 * @example
 * import machineCount from 'kody:@kentcdodds/fly/machine-count'
 * const result = await machineCount()
 * // => { count: 12, byState: { started: 10 }, byApp: { kcd: 4 }, ... }
 */
export default async function machineCount(params: FlyMachinesInput = {}) {
  const result = await listMachines(params)
  const byState = {}
  const byApp = {}
  for (const machine of result.machines) {
    const state = machine.state || 'unknown'
    const app = machine.app?.name || machine.app?.id || 'unknown'
    byState[state] = (byState[state] || 0) + 1
    byApp[app] = (byApp[app] || 0) + 1
  }
  return { count: result.count, byState, byApp, machines: result.machines }
}