Skip to content

Commit ef2f89e

Browse files
kathiekiwiTrigger.dev RepoOps
authored andcommitted
fix(webapp): DA success-rate alerts as query alerts, list alerts and channels across the org
The dashboard assistant now builds success-rate alerts as query alerts instead of failure-rate thresholds, can list alerts across environments and projects, and can list alert channels and legacy alerts. Mono-RevId: e092e56ebe0e67ab451469e16369f349f4013448
1 parent de018d5 commit ef2f89e

19 files changed

Lines changed: 1261 additions & 52 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
area: webapp
3+
type: improvement
4+
---
5+
6+
The assistant can now list alerts across environments and projects, and show which alert channels are set up.

‎apps/webapp/app/components/alerts/legacyAlertChannels.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export type LegacyAlertType =
2929
| "DEPLOYMENT_FAILURE"
3030
| "DEPLOYMENT_SUCCESS";
3131

32-
const LEGACY_ALERT_TYPE_LABEL: Record<LegacyAlertType, string> = {
32+
export const LEGACY_ALERT_TYPE_LABEL: Record<LegacyAlertType, string> = {
3333
TASK_RUN: "Task run failure",
3434
TASK_RUN_ATTEMPT: "Task attempt failure",
3535
DEPLOYMENT_FAILURE: "Deployment failure",

‎apps/webapp/app/components/dashboard-agent/tool-labels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const TOOL_LABELS: Record<string, string> = {
2828
analyze_query_alert: "Checking the query for an alert",
2929
update_alert_draft: "Updating the alert draft",
3030
list_monitoring_alerts: "Listing alerts",
31+
list_alert_channels: "Listing alert channels",
3132
resolve_monitoring_alert: "Resolving an alert",
3233
locate: "Locating the id",
3334
submit_feedback: "Sending feedback",
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { json, type LoaderFunctionArgs } from "@remix-run/server-runtime";
2+
import { z } from "zod";
3+
import { prisma } from "~/db.server";
4+
import { resolveAgentAlertContext } from "~/services/dashboardAgentAlertContext.server";
5+
import {
6+
alertReadAbility,
7+
authenticateDashboardAgent,
8+
authorizeAlerts,
9+
gateMonitoringAlerts,
10+
} from "~/services/dashboardAgentMonitoringAlerts.server";
11+
import {
12+
AGENT_ALERT_CHANNEL_SCOPES,
13+
listScopedAlertChannels,
14+
} from "~/v3/services/alerts/agentAlertScope.server";
15+
16+
const QuerySchema = z.object({
17+
chatId: z.string().min(1),
18+
scope: z.enum(AGENT_ALERT_CHANNEL_SCOPES).default("project"),
19+
type: z.enum(["EMAIL", "SLACK", "WEBHOOK"]).optional(),
20+
legacy: z
21+
.enum(["true", "false"])
22+
.optional()
23+
.transform((v) => v === "true"),
24+
});
25+
26+
export async function loader({ request }: LoaderFunctionArgs) {
27+
const auth = await authenticateDashboardAgent(request);
28+
if ("error" in auth) return auth.error;
29+
30+
const query = QuerySchema.safeParse(
31+
Object.fromEntries(new URL(request.url).searchParams.entries())
32+
);
33+
if (!query.success) {
34+
return json({ error: "Invalid request" }, { status: 400 });
35+
}
36+
37+
const context = await resolveAgentAlertContext({
38+
userId: auth.userId,
39+
environmentId: auth.environmentId,
40+
chatId: query.data.chatId,
41+
});
42+
if (!context.ok) {
43+
return json(
44+
{ error: context.error, code: context.code },
45+
{ status: context.code === "environment_mismatch" ? 400 : 404 }
46+
);
47+
}
48+
const { environment } = context;
49+
50+
if (!(await gateMonitoringAlerts(environment, auth.userId))) {
51+
return json({ error: "Monitoring alerts are not enabled for this project" }, { status: 403 });
52+
}
53+
54+
const denied = await authorizeAlerts(request, environment, "read");
55+
if (denied) return denied;
56+
57+
const { channels, truncated } = await listScopedAlertChannels(
58+
prisma,
59+
{
60+
origin: {
61+
userId: auth.userId,
62+
organizationId: environment.organizationId,
63+
projectId: environment.project.id,
64+
environmentId: environment.id,
65+
},
66+
scope: query.data.scope,
67+
type: query.data.type,
68+
legacy: query.data.legacy,
69+
},
70+
alertReadAbility(request, environment.organizationId)
71+
);
72+
73+
return json({ scope: query.data.scope, channels, truncated });
74+
}

‎apps/webapp/app/routes/api.v1.dashboard-agent.monitoring-alerts.ts‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { json, type ActionFunctionArgs, type LoaderFunctionArgs } from "@remix-run/server-runtime";
22
import { z } from "zod";
3-
import {
4-
createAlert,
5-
getAlert,
6-
listAlerts,
7-
type Alert,
8-
} from "~/v3/services/alerts/storage/alertStore.server";
3+
import { createAlert, getAlert, type Alert } from "~/v3/services/alerts/storage/alertStore.server";
94
import {
105
alertParamsSchema,
116
behaviorSchema,
@@ -24,9 +19,15 @@ import { $transaction, prisma } from "~/db.server";
2419
import { logger } from "~/services/logger.server";
2520
import {
2621
authenticateDashboardAgent,
22+
alertReadAbility,
2723
authorizeAlerts,
2824
gateMonitoringAlerts,
2925
} from "~/services/dashboardAgentMonitoringAlerts.server";
26+
import {
27+
AGENT_ALERT_SCOPES,
28+
listScopedAlerts,
29+
type ScopedAlert,
30+
} from "~/v3/services/alerts/agentAlertScope.server";
3031
import { scheduleCreatedAlert } from "~/v3/services/alerts/alertLifecycle.server";
3132
import {
3233
buildAlertFromDraft,
@@ -54,13 +55,18 @@ function alertView(a: Alert) {
5455
};
5556
}
5657

58+
function scopedAlertView({ alert, project, environment, channelIds }: ScopedAlert) {
59+
return { ...alertView(alert), project, environment, channelIds };
60+
}
61+
5762
function contextStatus(code: AgentAlertContextError) {
5863
return code === "environment_mismatch" ? 400 : 404;
5964
}
6065

6166
const ListQuerySchema = z.object({
6267
chatId: z.string().min(1),
6368
alertId: z.string().min(1).optional(),
69+
scope: z.enum(AGENT_ALERT_SCOPES).default("environment"),
6470
attention: z.enum(["clear", "needs_attention"]).optional(),
6571
lifecycle: z.enum(["active", "completed", "expired", "stopped"]).optional(),
6672
});
@@ -217,14 +223,23 @@ export async function loader({ request }: LoaderFunctionArgs) {
217223
return json({ alert: alertView(alert) });
218224
}
219225

220-
const alerts = await listAlerts(prisma, {
221-
projectId: context.environment.project.id,
222-
environmentId: context.environment.id,
223-
attention: query.data.attention,
224-
lifecycle: query.data.lifecycle,
225-
});
226+
const { alerts, truncated } = await listScopedAlerts(
227+
prisma,
228+
{
229+
origin: {
230+
userId: auth.userId,
231+
organizationId: context.environment.organizationId,
232+
projectId: context.environment.project.id,
233+
environmentId: context.environment.id,
234+
},
235+
scope: query.data.scope,
236+
attention: query.data.attention,
237+
lifecycle: query.data.lifecycle,
238+
},
239+
alertReadAbility(request, context.environment.organizationId)
240+
);
226241

227-
return json({ alerts: alerts.map(alertView) });
242+
return json({ scope: query.data.scope, alerts: alerts.map(scopedAlertView), truncated });
228243
}
229244

230245
export async function action({ request }: ActionFunctionArgs) {

‎apps/webapp/app/services/dashboardAgentMonitoringAlerts.server.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { AuthenticatedEnvironment } from "~/services/apiAuth.server";
33
import { canUseMonitoringAlerts } from "~/services/monitoringAlertsApi.server";
44
import { rbac } from "~/services/rbac.server";
55
import { authenticateUatOrApiRequest } from "~/services/uatRoutePreamble.server";
6+
import type { AlertReadAbility } from "~/v3/services/alerts/agentAlertScope.server";
67

78
export async function authenticateDashboardAgent(
89
request: Request
@@ -56,3 +57,12 @@ export async function authorizeAlerts(
5657
);
5758
}
5859
}
60+
61+
export function alertReadAbility(request: Request, organizationId: string): AlertReadAbility {
62+
return async (projectId) => {
63+
const auth = await rbac.authenticateUserActor(request, { organizationId, projectId });
64+
if (!auth.ok) return () => false;
65+
return (envType) =>
66+
auth.ability.can("read", envType ? { type: "alerts", envType } : { type: "alerts" });
67+
};
68+
}

0 commit comments

Comments
 (0)