|
| 1 | +import { json } from "@remix-run/server-runtime"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { prisma } from "~/db.server"; |
| 4 | +import { createLoaderApiRoute } from "~/services/routeBuilders/apiBuilder.server"; |
| 5 | +import { parseEventRateLimitConfig } from "~/v3/services/events/eventRateLimiter.server"; |
| 6 | + |
| 7 | +const ParamsSchema = z.object({ |
| 8 | + eventId: z.string(), |
| 9 | +}); |
| 10 | + |
| 11 | +export const loader = createLoaderApiRoute( |
| 12 | + { |
| 13 | + params: ParamsSchema, |
| 14 | + corsStrategy: "all", |
| 15 | + authorization: { |
| 16 | + action: "read", |
| 17 | + resource: (_resource, params) => ({ tasks: params.eventId }), |
| 18 | + superScopes: ["read:runs", "read:all", "admin"], |
| 19 | + }, |
| 20 | + findResource: async () => 1 as const, |
| 21 | + }, |
| 22 | + async ({ params, authentication }) => { |
| 23 | + const environment = authentication.environment; |
| 24 | + |
| 25 | + // Find event definition |
| 26 | + const eventDef = await prisma.eventDefinition.findFirst({ |
| 27 | + where: { |
| 28 | + slug: params.eventId, |
| 29 | + projectId: environment.projectId, |
| 30 | + }, |
| 31 | + orderBy: { createdAt: "desc" }, |
| 32 | + }); |
| 33 | + |
| 34 | + if (!eventDef) { |
| 35 | + return json({ error: `Event "${params.eventId}" not found` }, { status: 404 }); |
| 36 | + } |
| 37 | + |
| 38 | + // Get subscribers |
| 39 | + const subscriptions = await prisma.eventSubscription.findMany({ |
| 40 | + where: { |
| 41 | + eventDefinitionId: eventDef.id, |
| 42 | + environmentId: environment.id, |
| 43 | + }, |
| 44 | + select: { |
| 45 | + taskSlug: true, |
| 46 | + enabled: true, |
| 47 | + rateLimit: true, |
| 48 | + filter: true, |
| 49 | + consumerGroup: true, |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + const activeCount = subscriptions.filter((s) => s.enabled).length; |
| 54 | + const disabledCount = subscriptions.length - activeCount; |
| 55 | + |
| 56 | + // Get DLQ counts |
| 57 | + const [pendingCount, retriedCount, discardedCount] = await Promise.all([ |
| 58 | + prisma.deadLetterEvent.count({ |
| 59 | + where: { |
| 60 | + eventType: params.eventId, |
| 61 | + projectId: environment.projectId, |
| 62 | + environmentId: environment.id, |
| 63 | + status: "PENDING", |
| 64 | + }, |
| 65 | + }), |
| 66 | + prisma.deadLetterEvent.count({ |
| 67 | + where: { |
| 68 | + eventType: params.eventId, |
| 69 | + projectId: environment.projectId, |
| 70 | + environmentId: environment.id, |
| 71 | + status: "RETRIED", |
| 72 | + }, |
| 73 | + }), |
| 74 | + prisma.deadLetterEvent.count({ |
| 75 | + where: { |
| 76 | + eventType: params.eventId, |
| 77 | + projectId: environment.projectId, |
| 78 | + environmentId: environment.id, |
| 79 | + status: "DISCARDED", |
| 80 | + }, |
| 81 | + }), |
| 82 | + ]); |
| 83 | + |
| 84 | + // Parse rate limit config |
| 85 | + const rateLimitConfig = parseEventRateLimitConfig(eventDef.rateLimit); |
| 86 | + |
| 87 | + return json({ |
| 88 | + eventType: params.eventId, |
| 89 | + subscribers: { |
| 90 | + total: subscriptions.length, |
| 91 | + active: activeCount, |
| 92 | + disabled: disabledCount, |
| 93 | + list: subscriptions.map((s) => ({ |
| 94 | + taskSlug: s.taskSlug, |
| 95 | + enabled: s.enabled, |
| 96 | + hasRateLimit: !!s.rateLimit, |
| 97 | + hasFilter: !!s.filter, |
| 98 | + consumerGroup: s.consumerGroup, |
| 99 | + })), |
| 100 | + }, |
| 101 | + dlq: { |
| 102 | + pending: pendingCount, |
| 103 | + retried: retriedCount, |
| 104 | + discarded: discardedCount, |
| 105 | + }, |
| 106 | + rateLimit: rateLimitConfig |
| 107 | + ? { limit: rateLimitConfig.limit, window: rateLimitConfig.window } |
| 108 | + : null, |
| 109 | + }); |
| 110 | + } |
| 111 | +); |
0 commit comments