perf: optimize machine token lookup

This commit is contained in:
codex-bot 2025-10-30 16:30:59 -03:00
parent 38b46f32ce
commit 4c848486a6
2 changed files with 17 additions and 14 deletions

View file

@ -5,7 +5,7 @@ import { ConvexError, v } from "convex/values"
import { sha256 } from "@noble/hashes/sha256" import { sha256 } from "@noble/hashes/sha256"
import { randomBytes } from "@noble/hashes/utils" import { randomBytes } from "@noble/hashes/utils"
import type { Doc, Id } from "./_generated/dataModel" import type { Doc, Id } from "./_generated/dataModel"
import type { MutationCtx } from "./_generated/server" import type { MutationCtx, QueryCtx } from "./_generated/server"
import { normalizeStatus } from "./tickets" import { normalizeStatus } from "./tickets"
const DEFAULT_TENANT_ID = "tenant-atlas" const DEFAULT_TENANT_ID = "tenant-atlas"
@ -62,6 +62,17 @@ function normalizeIdentifiers(macAddresses: string[], serialNumbers: string[]):
return { macs, serials } return { macs, serials }
} }
async function findActiveMachineToken(ctx: QueryCtx, machineId: Id<"machines">, now: number) {
const tokens = await ctx.db
.query("machineTokens")
.withIndex("by_machine_revoked_expires", (q) =>
q.eq("machineId", machineId).eq("revoked", false).gt("expiresAt", now),
)
.collect()
return tokens.length > 0 ? tokens[0]! : null
}
function toHex(input: Uint8Array) { function toHex(input: Uint8Array) {
return Array.from(input) return Array.from(input)
.map((byte) => byte.toString(16).padStart(2, "0")) .map((byte) => byte.toString(16).padStart(2, "0"))
@ -762,12 +773,7 @@ export const listByTenant = query({
return Promise.all( return Promise.all(
machines.map(async (machine) => { machines.map(async (machine) => {
const tokens = await ctx.db const activeToken = await findActiveMachineToken(ctx, machine._id, now)
.query("machineTokens")
.withIndex("by_machine", (q) => q.eq("machineId", machine._id))
.collect()
const activeToken = tokens.find((token) => !token.revoked && token.expiresAt > now) ?? null
const offlineThresholdMs = getOfflineThresholdMs() const offlineThresholdMs = getOfflineThresholdMs()
const staleThresholdMs = getStaleThresholdMs(offlineThresholdMs) const staleThresholdMs = getStaleThresholdMs(offlineThresholdMs)
const manualStatus = (machine.status ?? "").toLowerCase() const manualStatus = (machine.status ?? "").toLowerCase()
@ -893,12 +899,7 @@ export const getById = query({
} }
const resolvedCompany = companyFromId ?? companyFromSlug const resolvedCompany = companyFromId ?? companyFromSlug
const tokens = await ctx.db const activeToken = await findActiveMachineToken(ctx, machine._id, now)
.query("machineTokens")
.withIndex("by_machine", (q) => q.eq("machineId", machine._id))
.collect()
const activeToken = tokens.find((token) => !token.revoked && token.expiresAt > now) ?? null
const offlineThresholdMs = getOfflineThresholdMs() const offlineThresholdMs = getOfflineThresholdMs()
const staleThresholdMs = getStaleThresholdMs(offlineThresholdMs) const staleThresholdMs = getStaleThresholdMs(offlineThresholdMs)

View file

@ -379,5 +379,7 @@ export default defineSchema({
}) })
.index("by_token_hash", ["tokenHash"]) .index("by_token_hash", ["tokenHash"])
.index("by_machine", ["machineId"]) .index("by_machine", ["machineId"])
.index("by_tenant_machine", ["tenantId", "machineId"]), .index("by_tenant_machine", ["tenantId", "machineId"])
.index("by_machine_created", ["machineId", "createdAt"])
.index("by_machine_revoked_expires", ["machineId", "revoked", "expiresAt"]),
}); });