@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 156 lines 5.6 KB
Raw
1 import * as amp from "@distilled.cloud/aws/amp";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7 import type { Providers } from "../Providers.ts";
8 import { decodeDefinition, encodeDefinition } from "./internal.ts";
9
10 export interface AlertManagerDefinitionProps {
11 /**
12 * Id of the AMP workspace this alert manager definition belongs to. A
13 * workspace has at most one alert manager definition. Changing the
14 * workspace replaces the definition.
15 */
16 workspaceId: string;
17 /**
18 * The Alertmanager configuration as a YAML document (the `alertmanager.yml`
19 * shape, with `alertmanager_config` and optional `template_files` keys).
20 * Updated in place.
21 */
22 definition: string;
23 }
24
25 export interface AlertManagerDefinition extends Resource<
26 "AWS.AMP.AlertManagerDefinition",
27 AlertManagerDefinitionProps,
28 {
29 workspaceId: string;
30 status: string;
31 },
32 never,
33 Providers
34 > {}
35
36 /**
37 * The Alertmanager definition for an Amazon Managed Service for Prometheus
38 * workspace — configures how firing alerts are grouped, routed, and
39 * dispatched to receivers (SNS, etc.). A workspace has at most one.
40 *
41 * ### Creating an Alert Manager Definition
42 * **Example:** Basic Definition
43 * ```typescript
44 * const workspace = yield* AMP.Workspace("Metrics", {});
45 * const alerts = yield* AMP.AlertManagerDefinition("Alerts", {
46 * workspaceId: workspace.workspaceId,
47 * definition: `alertmanager_config: |
48 * route:
49 * receiver: default
50 * receivers:
51 * - name: default`,
52 * });
53 * ```
54 *
55 * @resource
56 */
57 export const AlertManagerDefinition = Resource<AlertManagerDefinition>(
58 "AWS.AMP.AlertManagerDefinition",
59 );
60
61 export const AlertManagerDefinitionProvider = () =>
62 Provider.effect(
63 AlertManagerDefinition,
64 Effect.gen(function* () {
65 /** Describe the definition; typed not-found → undefined. */
66 const describe = Effect.fn(function* (workspaceId: string) {
67 const response = yield* amp
68 .describeAlertManagerDefinition({ workspaceId })
69 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
70 return response?.alertManagerDefinition;
71 });
72
73 /**
74 * Best-effort poll toward ACTIVE. The workspace's Alertmanager
75 * component can take a few minutes to provision, so we do not block a
76 * deploy on it: we wait a bounded window and return the last observed
77 * status (CREATING converges on a later reconcile). A definition in
78 * CREATING / UPDATING omits its `data` blob from the description.
79 */
80 const waitActive = Effect.fn(function* (workspaceId: string) {
81 return yield* amp.describeAlertManagerDefinition({ workspaceId }).pipe(
82 Effect.map((r) => r.alertManagerDefinition),
83 Effect.repeat({
84 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
85 until: (d) => d.status.statusCode === "ACTIVE",
86 }),
87 );
88 });
89
90 return {
91 stables: ["workspaceId"],
92
93 diff: Effect.fn(function* ({ olds, news }) {
94 if (!isResolved(news)) return undefined;
95 if (olds?.workspaceId !== news.workspaceId) {
96 return { action: "replace" } as const;
97 }
98 }),
99
100 read: Effect.fn(function* ({ olds, output }) {
101 const workspaceId = output?.workspaceId ?? olds?.workspaceId;
102 if (!workspaceId) return undefined;
103 const def = yield* describe(workspaceId);
104 if (def === undefined) return undefined;
105 // AMP alert manager definitions are not taggable — ownership is
106 // implied by the owned parent workspace.
107 return { workspaceId, status: def.status.statusCode };
108 }),
109
110 reconcile: Effect.fn(function* ({ news, session }) {
111 const workspaceId = news!.workspaceId;
112 const desiredData = yield* encodeDefinition(news!.definition);
113
114 // 1. Observe — the live definition is authoritative.
115 const existing = yield* describe(workspaceId);
116
117 // 2/3. Ensure + sync — create when absent, put when the YAML drifts.
118 // `data` is present once ACTIVE (omitted while CREATING).
119 if (existing === undefined) {
120 yield* amp.createAlertManagerDefinition({
121 workspaceId,
122 data: desiredData,
123 });
124 } else {
125 const currentDefinition =
126 existing.data !== undefined ? yield* decodeDefinition(existing.data) : undefined;
127 if (currentDefinition !== news!.definition) {
128 yield* amp.putAlertManagerDefinition({
129 workspaceId,
130 data: desiredData,
131 });
132 }
133 }
134
135 // Wait for ACTIVE so the returned status and data blob are settled.
136 const fresh = yield* waitActive(workspaceId);
137
138 yield* session.note(workspaceId);
139 return { workspaceId, status: fresh.status.statusCode };
140 }),
141
142 delete: Effect.fn(function* ({ output }) {
143 yield* amp.deleteAlertManagerDefinition({ workspaceId: output.workspaceId }).pipe(
144 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
145 Effect.retry({
146 while: (e) => e._tag === "ConflictException",
147 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
148 }),
149 );
150 }),
151
152 // Singleton sub-resource keyed by its parent workspace.
153 list: () => Effect.succeed([]),
154 };
155 }),
156 );