import machineCount from './machine-count.ts' import { listOrganizations } from './organizations.ts' import type { FlyAuthOptions } from './fly-core.ts' export type ScheduledStatusInput = FlyAuthOptions & { organizationSlug?: string orgSlug?: string } /** * Read-only machine counts. With no params, uses the only org visible to the * token (the same contract the scheduled job uses). Never mutates Machines. * * @example * import status from 'kody:@kody/fly/scheduled-status' * const result = await status() */ export async function runMachineStatus(input: ScheduledStatusInput = {}) { const explicit = String(input.organizationSlug || input.orgSlug || '').trim() if (explicit) { const counted = await machineCount({ ...input, organizationSlug: explicit }) return { ok: true, organizationSlug: explicit, count: counted.count, byState: counted.byState, byApp: counted.byApp, } } const listed = await listOrganizations({ ...input, first: 100 }) const organizations = listed.organizations if (organizations.length === 0) { return { ok: true, organizationSlug: null, count: 0, byState: {}, byApp: {}, organizations: [] } } if (organizations.length > 1) { return { ok: true, skipped: true, reason: 'Multiple organizations visible; pass organizationSlug to count machines.', organizations: organizations.map((org: any) => ({ slug: org.slug, name: org.name })), } } const organizationSlug = organizations[0].slug const counted = await machineCount({ ...input, organizationSlug }) return { ok: true, organizationSlug, count: counted.count, byState: counted.byState, byApp: counted.byApp, } } export default async function scheduledStatus(input: ScheduledStatusInput = {}) { return await runMachineStatus(input) }