@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, 04:45 UTC

main
ts 161 lines 5.62 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 { normalizeAmpLogGroupArn } from "./internal.ts";
9
10 export interface LoggingConfigurationProps {
11 /**
12 * Id of the AMP workspace whose rules/alerting logs are shipped. A
13 * workspace has at most one logging configuration. Changing the workspace
14 * replaces the configuration.
15 */
16 workspaceId: string;
17 /**
18 * ARN of the CloudWatch Logs log group that receives the workspace's rule
19 * evaluation and alerting (vended) logs. AMP expects the ARN with a
20 * trailing `:*` — one is appended automatically when missing.
21 */
22 logGroupArn: string;
23 }
24
25 export interface LoggingConfiguration extends Resource<
26 "AWS.AMP.LoggingConfiguration",
27 LoggingConfigurationProps,
28 {
29 workspaceId: string;
30 logGroupArn: string;
31 status: string;
32 },
33 never,
34 Providers
35 > {}
36
37 /**
38 * The rules/alerting logging configuration of an Amazon Managed Service for
39 * Prometheus workspace — ships rule evaluation failures and Alertmanager
40 * delivery errors to a CloudWatch Logs log group. A workspace has at most
41 * one.
42 *
43 * ### Creating a Logging Configuration
44 * **Example:** Ship Rule and Alerting Logs to CloudWatch Logs
45 * ```typescript
46 * const workspace = yield* AMP.Workspace("Metrics", {});
47 * const logs = yield* Logs.LogGroup("AmpLogs", {
48 * logGroupName: "/aws/vendedlogs/prometheus/metrics",
49 * });
50 * const logging = yield* AMP.LoggingConfiguration("Logging", {
51 * workspaceId: workspace.workspaceId,
52 * logGroupArn: logs.logGroupArn,
53 * });
54 * ```
55 *
56 * @resource
57 */
58 export const LoggingConfiguration = Resource<LoggingConfiguration>("AWS.AMP.LoggingConfiguration");
59
60 export const LoggingConfigurationProvider = () =>
61 Provider.effect(
62 LoggingConfiguration,
63 Effect.gen(function* () {
64 /** Describe the configuration; typed not-found → undefined. */
65 const describe = Effect.fn(function* (workspaceId: string) {
66 const response = yield* amp
67 .describeLoggingConfiguration({ workspaceId })
68 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
69 return response?.loggingConfiguration;
70 });
71
72 /**
73 * Bounded wait toward ACTIVE. Returns the last observed metadata —
74 * a still-transitioning configuration converges on a later reconcile.
75 */
76 const waitActive = Effect.fn(function* (workspaceId: string) {
77 return yield* amp.describeLoggingConfiguration({ workspaceId }).pipe(
78 Effect.map((r) => r.loggingConfiguration),
79 Effect.repeat({
80 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(20)]),
81 until: (c) => c.status.statusCode === "ACTIVE",
82 }),
83 );
84 });
85
86 const toAttrs = (meta: amp.LoggingConfigurationMetadata) => ({
87 workspaceId: meta.workspace,
88 logGroupArn: meta.logGroupArn,
89 status: meta.status.statusCode,
90 });
91
92 return {
93 stables: ["workspaceId"],
94
95 diff: Effect.fn(function* ({ olds, news }) {
96 if (!isResolved(news)) return undefined;
97 if (olds?.workspaceId !== news.workspaceId) {
98 return { action: "replace" } as const;
99 }
100 }),
101
102 read: Effect.fn(function* ({ olds, output }) {
103 const workspaceId = output?.workspaceId ?? olds?.workspaceId;
104 if (!workspaceId) return undefined;
105 const meta = yield* describe(workspaceId);
106 if (meta === undefined) return undefined;
107 // Logging configurations are not taggable — ownership is implied
108 // by the owned parent workspace.
109 return toAttrs(meta);
110 }),
111
112 reconcile: Effect.fn(function* ({ news, session }) {
113 const workspaceId = news!.workspaceId;
114 const desiredArn = normalizeAmpLogGroupArn(news!.logGroupArn);
115
116 // 1. Observe — the live configuration is authoritative.
117 const existing = yield* describe(workspaceId);
118
119 // 2/3. Ensure + sync — create when absent (tolerating a create
120 // race), update when the log group drifts.
121 if (existing === undefined) {
122 yield* amp
123 .createLoggingConfiguration({
124 workspaceId,
125 logGroupArn: desiredArn,
126 })
127 .pipe(
128 Effect.catchTag("ConflictException", () =>
129 amp.updateLoggingConfiguration({
130 workspaceId,
131 logGroupArn: desiredArn,
132 }),
133 ),
134 );
135 } else if (existing.logGroupArn !== desiredArn) {
136 yield* amp.updateLoggingConfiguration({
137 workspaceId,
138 logGroupArn: desiredArn,
139 });
140 }
141
142 const fresh = yield* waitActive(workspaceId);
143 yield* session.note(workspaceId);
144 return toAttrs(fresh);
145 }),
146
147 delete: Effect.fn(function* ({ output }) {
148 yield* amp.deleteLoggingConfiguration({ workspaceId: output.workspaceId }).pipe(
149 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
150 Effect.retry({
151 while: (e) => e._tag === "ConflictException",
152 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
153 }),
154 );
155 }),
156
157 // Singleton sub-resource keyed by its parent workspace.
158 list: () => Effect.succeed([]),
159 };
160 }),
161 );