Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

19.7 KB
Raw Blame
import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { In, IsNull, LessThan, MoreThan, Not, Repository } from 'typeorm'
import { validate as isUuid } from 'uuid'
import { Box } from '../../box/entities/box.entity'
import { BoxState } from '../../box/enums/box-state.enum'
import { OrganizationUser } from '../../organization/entities/organization-user.entity'
import { Organization } from '../../organization/entities/organization.entity'
import { BoxUsagePeriodArchive } from '../../usage/entities/box-usage-period-archive.entity'
import { BoxUsagePeriod } from '../../usage/entities/box-usage-period.entity'
import { User } from '../../user/user.entity'
import { cursorFor, cursorValue } from '../utils/pagination'
import { GIB, INACTIVE_BOX_STATES, latestDate } from '../utils/projection'
import { registrationAfter, registrationCursor, type RegistrationQuery } from '../utils/organization-query'
import { AdminOrganizationActivationService } from './organization-activation.service'

type DetailQuery = {
  memberCursor?: string
  boxCursor?: string
  memberLimit: number
  boxLimit: number
  includeUsage?: boolean
  includeActivation?: boolean
}
type ImpactState = 'impacted' | 'not_impacted'
type UsagePeriod = Pick<BoxUsagePeriod, 'startAt' | 'endAt' | 'cpu' | 'disk'>
type Fraction = { numerator: bigint; denominator: bigint }
// One row per (organization, state, desiredState) rather than one row per box.
type BoxGroupRow = { organizationId: string; state: BoxState; desiredState: string; boxCount: string; observedAt: Date }
type MemberCountRow = { organizationId: string; memberCount: string }
type MemberEmailRow = { organizationId: string; email: string }
type OrganizationStats = {
  memberCount: number
  boxCount: number
  impactedBoxCount: number
  observedAt: Date | null
}

const MEMBER_EMAIL_LIMIT = 200
const escapedLike = (value: string): string => `%${value.replace(/[\\%_]/g, '\\$&')}%`
const timestamp = (value: Date): string => value.toISOString()
const impactState = (impacted: boolean): ImpactState => (impacted ? 'impacted' : 'not_impacted')
// state and desiredState are separate enums, so the comparison goes through their text form.
const isImpactedBox = (box: { state: BoxState; desiredState: string }): boolean =>
  box.state === BoxState.ERROR || String(box.state) !== String(box.desiredState)
const count = (value: string | number | null | undefined): number => Number(value ?? 0) || 0
const emptyStats = (): OrganizationStats => ({
  memberCount: 0,
  boxCount: 0,
  impactedBoxCount: 0,
  observedAt: null,
})
const decimalFraction = (value: number): Fraction => {
  const [coefficient, exponentText] = value.toString().toLowerCase().split('e')
  const exponent = Number(exponentText ?? 0)
  const negative = coefficient.startsWith('-')
  const unsigned = negative ? coefficient.slice(1) : coefficient
  const [whole, fraction = ''] = unsigned.split('.')
  const digits = BigInt(`${whole}${fraction}`)
  const decimalPlaces = fraction.length - exponent
  const numerator = (negative ? -digits : digits) * (decimalPlaces < 0 ? 10n ** BigInt(-decimalPlaces) : 1n)
  const denominator = decimalPlaces > 0 ? 10n ** BigInt(decimalPlaces) : 1n
  return { numerator, denominator }
}
const greatestCommonDivisor = (left: bigint, right: bigint): bigint => {
  let a = left < 0n ? -left : left
  let b = right < 0n ? -right : right
  while (b !== 0n) [a, b] = [b, a % b]
  return a
}
const addFraction = (left: Fraction, right: Fraction): Fraction => {
  const denominatorGcd = greatestCommonDivisor(left.denominator, right.denominator)
  const leftMultiplier = right.denominator / denominatorGcd
  const rightMultiplier = left.denominator / denominatorGcd
  const numerator = left.numerator * leftMultiplier + right.numerator * rightMultiplier
  const denominator = left.denominator * leftMultiplier
  const resultGcd = greatestCommonDivisor(numerator, denominator)
  return { numerator: numerator / resultGcd, denominator: denominator / resultGcd }
}
const weightedSeconds = (durationMs: number, resource: number, multiplier = 1n): Fraction => {
  const quantity = decimalFraction(resource)
  return {
    numerator: BigInt(durationMs) * quantity.numerator * multiplier,
    denominator: 1000n * quantity.denominator,
  }
}

@Injectable()
export class AdminOrganizationOverviewService {
  constructor(
    @InjectRepository(Organization) private readonly organizationRepository: Repository<Organization>,
    @InjectRepository(OrganizationUser)
    private readonly organizationUserRepository: Repository<OrganizationUser>,
    @InjectRepository(User) private readonly userRepository: Repository<User>,
    @InjectRepository(Box) private readonly boxRepository: Repository<Box>,
    @InjectRepository(BoxUsagePeriod)
    private readonly usagePeriodRepository: Repository<BoxUsagePeriod>,
    private readonly activation: AdminOrganizationActivationService,
  ) {}

  async list(input: RegistrationQuery) {
    const organizations = await this.registrationRows(input)
    const page = organizations.slice(0, input.limit)
    const nextOrganization = organizations.length > input.limit ? page.at(-1) : undefined
    const organizationIds = page.map((organization) => organization.id)
    const [stats, memberEmails] = await Promise.all([
      this.organizationStats(organizationIds, input.boxCountScope),
      input.includeMemberEmails ? this.memberEmails(organizationIds) : undefined,
    ])
    const creators = page.length
      ? await this.userRepository.find({
          where: { id: In([...new Set(page.map((row) => row.createdBy))]) },
          select: { id: true, email: true, name: true, emailVerified: true },
        })
      : []
    const creatorsById = new Map(creators.map((user) => [user.id, user]))
    const summaries = page.map((organization) => {
      const metadata = this.registration(organization, creatorsById)
      const emailVerified = creatorsById.get(organization.createdBy)?.emailVerified
      const registration = {
        organizationId: organization.id,
        name: organization.name,
        ...metadata,
        creator: metadata.creator
          ? { ...metadata.creator, ...(typeof emailVerified === 'boolean' ? { emailVerified } : {}) }
          : null,
        observedAt: timestamp(organization.updatedAt),
      }
      const organizationStats = stats.get(organization.id) ?? emptyStats()
      return {
        ...registration,
        memberCount: organizationStats.memberCount,
        boxCount: organizationStats.boxCount,
        impactState: impactState(organizationStats.impactedBoxCount > 0),
        observedAt: timestamp(
          latestDate([organization.updatedAt, organizationStats.observedAt]) ?? organization.updatedAt,
        ),
        ...(memberEmails ? { memberEmails: memberEmails.get(organization.id) ?? [] } : {}),
      }
    })
    return {
      items: summaries,
      nextCursor: nextOrganization ? registrationCursor(input, nextOrganization) : null,
      limit: input.limit,
      observedAt: latestDate(summaries.map((summary) => new Date(summary.observedAt)))?.toISOString() ?? null,
    }
  }

  private registrationRows(input: RegistrationQuery): Promise<Organization[]> {
    const after = registrationAfter(input)
    const created = `date_trunc('milliseconds', organization."createdAt")`
    const rows = this.organizationRepository
      .createQueryBuilder('organization')
      .select([
        'organization.id',
        'organization.name',
        'organization.createdAt',
        'organization.createdBy',
        'organization.updatedAt',
      ])
      .orderBy(created, 'ASC')
      .addOrderBy('organization.id', 'ASC')
      .take(input.limit + 1)
    if (input.query)
      rows.andWhere(
        isUuid(input.query)
          ? '(organization.id = :searchId OR organization.name ILIKE :search)'
          : 'organization.name ILIKE :search',
        { searchId: input.query, search: escapedLike(input.query) },
      )
    if (input.createdFrom) rows.andWhere('organization."createdAt" >= :from', { from: input.createdFrom })
    if (input.organizationId)
      rows.andWhere('organization.id = :organizationId', { organizationId: input.organizationId })
    if (input.createdTo) rows.andWhere('organization."createdAt" < :to', { to: input.createdTo })
    if (after)
      rows.andWhere(`(${created}, organization.id) > (CAST(:createdAt AS timestamptz), CAST(:id AS uuid))`, {
        createdAt: after.createdAt,
        id: after.id,
      })
    return rows.getMany()
  }

  private registration(organization: Organization, users: Map<string, User>) {
    const creator = users.get(organization.createdBy)
    return {
      createdAt: timestamp(organization.createdAt),
      createdBy: organization.createdBy,
      creator: creator ? { userId: creator.id, email: creator.email || null, displayName: creator.name } : null,
    }
  }

  /**
   * Counts each organization's members and boxes in the database. The page above is
   * bounded, so the work behind it must be too: grouping by the box state enums keeps the
   * result set sized by the enum cross-product rather than by how many boxes exist.
   */
  private async organizationStats(
    organizationIds: string[],
    boxCountScope?: 'all',
  ): Promise<Map<string, OrganizationStats>> {
    const stats = new Map<string, OrganizationStats>()
    if (organizationIds.length === 0) return stats

    const [memberCounts, boxGroups] = await Promise.all([
      this.organizationUserRepository
        .createQueryBuilder('member')
        .select('member."organizationId"', 'organizationId')
        .addSelect('COUNT(*)', 'memberCount')
        .where('member."organizationId" IN (:...organizationIds)', { organizationIds })
        .groupBy('member."organizationId"')
        .getRawMany<MemberCountRow>(),
      this.boxRepository
        .createQueryBuilder('box')
        .select('box."organizationId"', 'organizationId')
        .addSelect('box.state', 'state')
        .addSelect('box."desiredState"', 'desiredState')
        .addSelect('COUNT(*)', 'boxCount')
        .addSelect('MAX(box."updatedAt")', 'observedAt')
        .where('box."organizationId" IN (:...organizationIds)', { organizationIds })
        .groupBy('box."organizationId"')
        .addGroupBy('box.state')
        .addGroupBy('box."desiredState"')
        .getRawMany<BoxGroupRow>(),
    ])

    const statsFor = (organizationId: string): OrganizationStats => {
      const existing = stats.get(organizationId)
      if (existing) return existing
      const created = emptyStats()
      stats.set(organizationId, created)
      return created
    }
    for (const group of memberCounts) statsFor(group.organizationId).memberCount = count(group.memberCount)
    for (const group of boxGroups) {
      const organization = statsFor(group.organizationId)
      organization.observedAt = latestDate([organization.observedAt, group.observedAt])
      const boxes = count(group.boxCount)
      const inactive = INACTIVE_BOX_STATES.includes(group.state)
      if (!inactive || boxCountScope === 'all') organization.boxCount += boxes
      if (inactive) continue
      if (isImpactedBox(group)) organization.impactedBoxCount += boxes
    }
    return stats
  }

  /**
   * Reads up to MEMBER_EMAIL_LIMIT non-empty emails per organization. The cap is applied
   * in the database, after empty emails are filtered, so large organizations stay bounded.
   */
  private async memberEmails(organizationIds: string[]): Promise<Map<string, string[]>> {
    const emails = new Map<string, string[]>()
    if (organizationIds.length === 0) return emails

    // `user` is reserved in Postgres, so the joined table gets its own alias.
    const ranked = this.organizationUserRepository
      .createQueryBuilder('member')
      .innerJoin(User, 'member_user', 'member_user.id = member."userId"')
      .select('member."organizationId"', 'organizationId')
      .addSelect('member_user.email', 'email')
      .addSelect('ROW_NUMBER() OVER (PARTITION BY member."organizationId" ORDER BY member."userId" ASC)', 'position')
      .where('member."organizationId" IN (:...organizationIds)', { organizationIds })
      .andWhere(`member_user.email <> ''`)
    const rows = await this.organizationUserRepository.manager
      .createQueryBuilder()
      .addCommonTableExpression(ranked, 'ranked')
      .select('ranked."organizationId"', 'organizationId')
      .addSelect('ranked.email', 'email')
      .from('ranked', 'ranked')
      .where('ranked.position <= :memberEmailLimit', { memberEmailLimit: MEMBER_EMAIL_LIMIT })
      .orderBy('ranked."organizationId"', 'ASC')
      .addOrderBy('ranked.position', 'ASC')
      .getRawMany<MemberEmailRow>()

    for (const row of rows) {
      const organizationEmails = emails.get(row.organizationId) ?? []
      organizationEmails.push(row.email)
      emails.set(row.organizationId, organizationEmails)
    }
    return emails
  }

  async detail(organizationId: string, input: DetailQuery) {
    const memberAfter = cursorValue(input.memberCursor)
    const boxAfter = cursorValue(input.boxCursor)
    const organization = await this.organizationRepository.findOne({
      where: { id: organizationId },
      select: {
        id: true,
        name: true,
        updatedAt: true,
        createdAt: true,
        createdBy: true,
        suspended: true,
      },
    })
    if (!organization) return null

    const [memberships, organizationBoxes, usage, impactedBoxes, activation] = await Promise.all([
      this.organizationUserRepository.find({
        where: { organizationId, ...(memberAfter ? { userId: MoreThan(memberAfter) } : {}) },
        select: { userId: true, role: true, createdAt: true },
        order: { userId: 'ASC' },
        take: input.memberLimit + 1,
      }),
      this.boxRepository.find({
        where: {
          organizationId,
          state: Not(In(INACTIVE_BOX_STATES)),
          ...(boxAfter ? { id: MoreThan(boxAfter) } : {}),
        },
        select: {
          id: true,
          organizationId: true,
          name: true,
          state: true,
          desiredState: true,
          runnerId: true,
          region: true,
          updatedAt: true,
        },
        order: { id: 'ASC' },
        take: input.boxLimit + 1,
      }),
      input.includeUsage ? this.currentMonthUsage(organizationId) : undefined,
      // Impact is a property of the whole organization, not of the requested page, so it
      // gets its own bounded query instead of a scan the page happens to have loaded.
      // The row count is bounded by `take`, and the row width by this projection: an
      // unprojected getMany() would also read `secrets`, `env`, and `authToken`, none of
      // which the evidence below renders.
      // state and desiredState are separate Postgres enum types; ::text makes them comparable.
      this.boxRepository
        .createQueryBuilder('box')
        .select(['box.id', 'box.name', 'box.state', 'box.desiredState', 'box.updatedAt'])
        .where('box."organizationId" = :organizationId', { organizationId })
        .andWhere('box.state NOT IN (:...inactive)', { inactive: INACTIVE_BOX_STATES })
        .andWhere('(box.state = :error OR box.state::text <> box."desiredState"::text)', { error: BoxState.ERROR })
        .orderBy('box.id', 'ASC')
        .take(input.boxLimit)
        .getMany(),
      input.includeActivation ? this.activation.get(organizationId) : undefined,
    ])
    const userIds = [...new Set([...memberships.map((membership) => membership.userId), organization.createdBy])]
    const users = userIds.length
      ? await this.userRepository.find({
          where: { id: In(userIds) },
          select: { id: true, email: true, name: true },
        })
      : []
    const usersById = new Map(users.map((user) => [user.id, user]))
    const memberPage = memberships.slice(0, input.memberLimit)
    const boxPage = organizationBoxes.slice(0, input.boxLimit)
    const nextMember = memberships.length > input.memberLimit ? memberPage.at(-1) : undefined
    const nextBox = organizationBoxes.length > input.boxLimit ? boxPage.at(-1) : undefined

    return {
      organizationId,
      name: organization.name,
      suspended: organization.suspended,
      ...this.registration(organization, usersById),
      members: {
        items: memberPage.flatMap((membership) => {
          const user = usersById.get(membership.userId)
          return user
            ? [
                {
                  userId: membership.userId,
                  email: user.email,
                  displayName: user.name,
                  organizationRole: membership.role,
                  joinedAt: timestamp(membership.createdAt),
                },
              ]
            : []
        }),
        nextCursor: nextMember ? cursorFor(nextMember.userId) : null,
        limit: input.memberLimit,
      },
      boxes: {
        items: boxPage.map((box) => ({
          id: box.id,
          name: box.name,
          observedState: box.state,
          desiredState: box.desiredState,
          runnerId: box.runnerId ?? null,
          region: box.region,
          observedAt: timestamp(box.updatedAt),
        })),
        nextCursor: nextBox ? cursorFor(nextBox.id) : null,
        limit: input.boxLimit,
      },
      impact: {
        state: impactState(impactedBoxes.length > 0),
        evidence: impactedBoxes.map((box) => ({
          boxId: box.id,
          observedAt: timestamp(box.updatedAt),
          summary: `${box.name}: ${box.state} (desired ${box.desiredState})`,
        })),
      },
      ...(usage ? { usage } : {}),
      ...(input.includeActivation ? { activation } : {}),
      observedAt: timestamp(organization.updatedAt),
    }
  }

  private async currentMonthUsage(organizationId: string) {
    const periodEnd = new Date()
    const periodStart = new Date(Date.UTC(periodEnd.getUTCFullYear(), periodEnd.getUTCMonth(), 1))
    // Rollover deletes the live row and inserts a fresh-id archive copy in one transaction
    // (usage.service.ts). Two reads on two pool connections straddle that commit and count
    // the period twice or lose it, and the archive row's new id rules out de-duplication.
    // One repeatable-read snapshot puts both reads on the same side of the rollover.
    const periods = await this.usagePeriodRepository.manager.transaction(
      'REPEATABLE READ',
      async (snapshot): Promise<UsagePeriod[]> => {
        const [currentPeriods, archivedPeriods] = await Promise.all([
          snapshot.find(BoxUsagePeriod, {
            where: [
              { organizationId, startAt: LessThan(periodEnd), endAt: IsNull() },
              { organizationId, startAt: LessThan(periodEnd), endAt: MoreThan(periodStart) },
            ],
            select: { startAt: true, endAt: true, cpu: true, disk: true },
          }),
          snapshot.find(BoxUsagePeriodArchive, {
            where: { organizationId, startAt: LessThan(periodEnd), endAt: MoreThan(periodStart) },
            select: { startAt: true, endAt: true, cpu: true, disk: true },
          }),
        ])
        return [...currentPeriods, ...archivedPeriods]
      },
    )
    const totals = periods.reduce(
      (sum, period: UsagePeriod) => {
        const overlapStart = Math.max(period.startAt.getTime(), periodStart.getTime())
        const overlapEnd = Math.min((period.endAt ?? periodEnd).getTime(), periodEnd.getTime())
        const durationMs = Math.max(0, overlapEnd - overlapStart)
        return {
          compute: addFraction(sum.compute, weightedSeconds(durationMs, period.cpu)),
          storage: addFraction(sum.storage, weightedSeconds(durationMs, period.disk, BigInt(GIB))),
        }
      },
      {
        compute: { numerator: 0n, denominator: 1n },
        storage: { numerator: 0n, denominator: 1n },
      },
    )
    return {
      periodStart: timestamp(periodStart),
      periodEnd: timestamp(periodEnd),
      computeSeconds: String(totals.compute.numerator / totals.compute.denominator),
      storageByteSeconds: String(totals.storage.numerator / totals.storage.denominator),
    }
  }
}