Follow-up to83837adaaddressing the critical-review feedback: - Duplicate ConditionKind type consolidated: the one in api/queries/alertRules.ts (which was nullable — wrong) is gone; single source of truth lives in this module. - Module moved out of api/ into pages/Alerts/ where it belongs. api/ is the data layer; labels + hide lists are view-layer concerns. - Hidden values formalised: Comparator.EQ and JvmAggregation.LATEST are intentionally not surfaced in dropdowns (noisy / wrong feature boundary, see in-file comments). They remain in the type unions so rules that carry those values save/load correctly — we just don't advertise them in the UI. - JvmAggregation declaration order restored to MAX/AVG/MIN (matches what users saw before83837ada). LATEST declared last; hidden. - Snapshot tests for every visible *_OPTIONS array — reviewer signal in future PRs when a backend enum change or hide-list edit silently reshapes the dropdown. - `toOptions` gains a JSDoc noting that label-map declaration order is load-bearing (ES2015 Object.keys insertion-order guarantee). - **Honest about the springdoc schema quirk**: the generated polymorphic condition types resolve to `never` at the TypeScript level (two conflicting `kind` discriminators — the class-name literal and the Jackson enum — intersect to never), which silently defeated `Record<T, string>` exhaustiveness. The previous commit's "schema-derived enums" claim was accurate only for the flat-field enums (ConditionKind, Severity, TargetKind); condition-specific enums (RouteMetric, Comparator, JvmAggregation, ExchangeFireMode) were silently `never`. Those are now declared as hand-written string-literal unions with a top-of-file comment spelling out the issue and the regen-and-compare workflow. Real upstream fix is a backend-side adjustment to how springdoc emits polymorphic `@JsonSubTypes` — out of scope for this phase. Verified: ui build green, 56/56 vitest pass (49 pre-existing + 7 new enum snapshots). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
189 lines
6.3 KiB
TypeScript
189 lines
6.3 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import type { components } from '../schema';
|
|
import { apiClient, useSelectedEnv } from './alertMeta';
|
|
|
|
export type AlertRuleResponse = components['schemas']['AlertRuleResponse'];
|
|
export type AlertRuleRequest = components['schemas']['AlertRuleRequest'];
|
|
export type RenderPreviewRequest = components['schemas']['RenderPreviewRequest'];
|
|
export type RenderPreviewResponse = components['schemas']['RenderPreviewResponse'];
|
|
export type TestEvaluateRequest = components['schemas']['TestEvaluateRequest'];
|
|
export type TestEvaluateResponse = components['schemas']['TestEvaluateResponse'];
|
|
export type AlertCondition = AlertRuleResponse['condition'];
|
|
// `ConditionKind` lives in `../../pages/Alerts/enums` alongside its label map
|
|
// and option array — single source of truth to avoid duplicate-type drift.
|
|
|
|
// NOTE ON TYPES: the generated OpenAPI schema for env-scoped alert-rule endpoints
|
|
// emits `path?: never` plus a `query.env: Environment` parameter because the
|
|
// server resolves the env via the `@EnvPath` argument resolver, which the
|
|
// OpenAPI scanner does not recognise as a path variable. At runtime the URL
|
|
// template `{envSlug}` is substituted from `params.path.envSlug` by
|
|
// openapi-fetch regardless of what the TS types say; we therefore cast the
|
|
// call options to `any` on each call to bypass the generated type oddity.
|
|
|
|
/** List alert rules in the current env. */
|
|
export function useAlertRules() {
|
|
const env = useSelectedEnv();
|
|
return useQuery({
|
|
queryKey: ['alertRules', env],
|
|
enabled: !!env,
|
|
queryFn: async () => {
|
|
if (!env) throw new Error('no env');
|
|
const { data, error } = await apiClient.GET(
|
|
'/environments/{envSlug}/alerts/rules',
|
|
{
|
|
params: { path: { envSlug: env } },
|
|
} as any,
|
|
);
|
|
if (error) throw error;
|
|
return data as AlertRuleResponse[];
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Fetch a single alert rule by id. */
|
|
export function useAlertRule(id: string | undefined) {
|
|
const env = useSelectedEnv();
|
|
return useQuery({
|
|
queryKey: ['alertRules', env, id],
|
|
enabled: !!env && !!id,
|
|
queryFn: async () => {
|
|
if (!env || !id) throw new Error('no env/id');
|
|
const { data, error } = await apiClient.GET(
|
|
'/environments/{envSlug}/alerts/rules/{id}',
|
|
{
|
|
params: { path: { envSlug: env, id } },
|
|
} as any,
|
|
);
|
|
if (error) throw error;
|
|
return data as AlertRuleResponse;
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Create a new alert rule in the current env. */
|
|
export function useCreateAlertRule() {
|
|
const qc = useQueryClient();
|
|
const env = useSelectedEnv();
|
|
return useMutation({
|
|
mutationFn: async (req: AlertRuleRequest) => {
|
|
if (!env) throw new Error('no env');
|
|
const { data, error } = await apiClient.POST(
|
|
'/environments/{envSlug}/alerts/rules',
|
|
{
|
|
params: { path: { envSlug: env } },
|
|
body: req,
|
|
} as any,
|
|
);
|
|
if (error) throw error;
|
|
return data as AlertRuleResponse;
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['alertRules', env] });
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Update an existing alert rule. */
|
|
export function useUpdateAlertRule(id: string) {
|
|
const qc = useQueryClient();
|
|
const env = useSelectedEnv();
|
|
return useMutation({
|
|
mutationFn: async (req: AlertRuleRequest) => {
|
|
if (!env) throw new Error('no env');
|
|
const { data, error } = await apiClient.PUT(
|
|
'/environments/{envSlug}/alerts/rules/{id}',
|
|
{
|
|
params: { path: { envSlug: env, id } },
|
|
body: req,
|
|
} as any,
|
|
);
|
|
if (error) throw error;
|
|
return data as AlertRuleResponse;
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['alertRules', env] });
|
|
qc.invalidateQueries({ queryKey: ['alertRules', env, id] });
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Delete an alert rule. */
|
|
export function useDeleteAlertRule() {
|
|
const qc = useQueryClient();
|
|
const env = useSelectedEnv();
|
|
return useMutation({
|
|
mutationFn: async (id: string) => {
|
|
if (!env) throw new Error('no env');
|
|
const { error } = await apiClient.DELETE(
|
|
'/environments/{envSlug}/alerts/rules/{id}',
|
|
{
|
|
params: { path: { envSlug: env, id } },
|
|
} as any,
|
|
);
|
|
if (error) throw error;
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['alertRules', env] });
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Enable or disable an alert rule. Routes to /enable or /disable based on the flag. */
|
|
export function useSetAlertRuleEnabled() {
|
|
const qc = useQueryClient();
|
|
const env = useSelectedEnv();
|
|
return useMutation({
|
|
mutationFn: async ({ id, enabled }: { id: string; enabled: boolean }) => {
|
|
if (!env) throw new Error('no env');
|
|
const path = enabled
|
|
? '/environments/{envSlug}/alerts/rules/{id}/enable'
|
|
: '/environments/{envSlug}/alerts/rules/{id}/disable';
|
|
const { error } = await apiClient.POST(path, {
|
|
params: { path: { envSlug: env, id } },
|
|
} as any);
|
|
if (error) throw error;
|
|
},
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['alertRules', env] });
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Render a preview of the notification title + message for a rule using the provided context. */
|
|
export function useRenderPreview() {
|
|
const env = useSelectedEnv();
|
|
return useMutation({
|
|
mutationFn: async ({ id, req }: { id: string; req: RenderPreviewRequest }) => {
|
|
if (!env) throw new Error('no env');
|
|
const { data, error } = await apiClient.POST(
|
|
'/environments/{envSlug}/alerts/rules/{id}/render-preview',
|
|
{
|
|
params: { path: { envSlug: env, id } },
|
|
body: req,
|
|
} as any,
|
|
);
|
|
if (error) throw error;
|
|
return data as RenderPreviewResponse;
|
|
},
|
|
});
|
|
}
|
|
|
|
/** Test-evaluate a rule (dry-run) without persisting an alert instance. */
|
|
export function useTestEvaluate() {
|
|
const env = useSelectedEnv();
|
|
return useMutation({
|
|
mutationFn: async ({ id, req }: { id: string; req: TestEvaluateRequest }) => {
|
|
if (!env) throw new Error('no env');
|
|
const { data, error } = await apiClient.POST(
|
|
'/environments/{envSlug}/alerts/rules/{id}/test-evaluate',
|
|
{
|
|
params: { path: { envSlug: env, id } },
|
|
body: req,
|
|
} as any,
|
|
);
|
|
if (error) throw error;
|
|
return data as TestEvaluateResponse;
|
|
},
|
|
});
|
|
}
|