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
| 1 | import * as amp from "@distilled.cloud/aws/amp"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { createInternalTags, hasAlchemyTags } from "alchemy/Tags"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { decodeDefinition, encodeDefinition, syncAmpTags, toTagRecord } from "./internal.ts"; |
| 12 | |
| 13 | export class RuleGroupsNamespaceNotReady extends Data.TaggedError("RuleGroupsNamespaceNotReady")<{ |
| 14 | readonly message: string; |
| 15 | readonly cause?: unknown; |
| 16 | }> {} |
| 17 | |
| 18 | export interface RuleGroupsNamespaceProps { |
| 19 | /** |
| 20 | * Id of the AMP workspace this rule groups namespace belongs to. Changing |
| 21 | * the workspace replaces the namespace. |
| 22 | */ |
| 23 | workspaceId: string; |
| 24 | /** |
| 25 | * Name of the rule groups namespace. Changing the name replaces the |
| 26 | * namespace. |
| 27 | */ |
| 28 | name: string; |
| 29 | /** |
| 30 | * The rules definition as a Prometheus-format YAML document (the same |
| 31 | * shape as a `prometheus.yml` `groups:` file). Updated in place. |
| 32 | */ |
| 33 | definition: string; |
| 34 | /** |
| 35 | * User-defined tags for the namespace. |
| 36 | */ |
| 37 | tags?: Record<string, string>; |
| 38 | } |
| 39 | |
| 40 | export interface RuleGroupsNamespace extends Resource< |
| 41 | "AWS.AMP.RuleGroupsNamespace", |
| 42 | RuleGroupsNamespaceProps, |
| 43 | { |
| 44 | workspaceId: string; |
| 45 | name: string; |
| 46 | ruleGroupsNamespaceArn: string; |
| 47 | status: string; |
| 48 | }, |
| 49 | never, |
| 50 | Providers |
| 51 | > {} |
| 52 | |
| 53 | /** |
| 54 | * A rule groups namespace inside an Amazon Managed Service for Prometheus |
| 55 | * workspace — a container of Prometheus recording and alerting rules, |
| 56 | * supplied as a YAML definition. |
| 57 | * |
| 58 | * ### Creating a Rule Groups Namespace |
| 59 | * **Example:** Basic Recording Rule |
| 60 | * ```typescript |
| 61 | * const workspace = yield* AMP.Workspace("Metrics", {}); |
| 62 | * const rules = yield* AMP.RuleGroupsNamespace("Rules", { |
| 63 | * workspaceId: workspace.workspaceId, |
| 64 | * name: "default", |
| 65 | * definition: `groups: |
| 66 | * - name: example |
| 67 | * rules: |
| 68 | * - record: metric:requests:rate5m |
| 69 | * expr: rate(http_requests_total[5m])`, |
| 70 | * }); |
| 71 | * ``` |
| 72 | * |
| 73 | * @resource |
| 74 | */ |
| 75 | export const RuleGroupsNamespace = Resource<RuleGroupsNamespace>("AWS.AMP.RuleGroupsNamespace"); |
| 76 | |
| 77 | export const RuleGroupsNamespaceProvider = () => |
| 78 | Provider.effect( |
| 79 | RuleGroupsNamespace, |
| 80 | Effect.gen(function* () { |
| 81 | const toAttrs = (ns: amp.RuleGroupsNamespaceDescription) => ({ |
| 82 | workspaceId: "", |
| 83 | name: ns.name, |
| 84 | ruleGroupsNamespaceArn: ns.arn, |
| 85 | status: ns.status.statusCode, |
| 86 | }); |
| 87 | |
| 88 | /** Describe the namespace; typed not-found → undefined. */ |
| 89 | const describe = Effect.fn(function* (workspaceId: string, name: string) { |
| 90 | const response = yield* amp |
| 91 | .describeRuleGroupsNamespace({ workspaceId, name }) |
| 92 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 93 | return response?.ruleGroupsNamespace; |
| 94 | }); |
| 95 | |
| 96 | /** |
| 97 | * Poll until the namespace reaches ACTIVE. A namespace in CREATING / |
| 98 | * UPDATING omits its `data` blob from the description, so callers that |
| 99 | * need the definition must wait for ACTIVE first. Activation is |
| 100 | * usually seconds but has been observed to remain CREATING beyond 90s |
| 101 | * under account load. Poll less frequently for a bounded ~2 minute |
| 102 | * window so deployments tolerate that documented asynchronous |
| 103 | * lifecycle without creating an unbounded wait. |
| 104 | */ |
| 105 | const waitActive = Effect.fn(function* (workspaceId: string, name: string) { |
| 106 | const ns = yield* amp.describeRuleGroupsNamespace({ workspaceId, name }).pipe( |
| 107 | Effect.map((r) => r.ruleGroupsNamespace), |
| 108 | Effect.repeat({ |
| 109 | schedule: Schedule.max([Schedule.fixed("12 seconds"), Schedule.recurs(10)]), |
| 110 | until: (n) => n.status.statusCode === "ACTIVE", |
| 111 | }), |
| 112 | ); |
| 113 | if (ns.status.statusCode !== "ACTIVE") { |
| 114 | return yield* new RuleGroupsNamespaceNotReady({ |
| 115 | message: `AMP rule groups namespace ${name} did not become ACTIVE (status: ${ns.status.statusCode})`, |
| 116 | }); |
| 117 | } |
| 118 | return ns; |
| 119 | }); |
| 120 | |
| 121 | return { |
| 122 | stables: ["workspaceId", "name", "ruleGroupsNamespaceArn"], |
| 123 | |
| 124 | diff: Effect.fn(function* ({ olds, news }) { |
| 125 | if (!isResolved(news)) return undefined; |
| 126 | if (olds?.workspaceId !== news.workspaceId || olds?.name !== news.name) { |
| 127 | return { action: "replace" } as const; |
| 128 | } |
| 129 | }), |
| 130 | |
| 131 | read: Effect.fn(function* ({ id, olds, output }) { |
| 132 | const workspaceId = output?.workspaceId ?? olds?.workspaceId; |
| 133 | const name = output?.name ?? olds?.name; |
| 134 | if (!workspaceId || !name) return undefined; |
| 135 | const ns = yield* describe(workspaceId, name); |
| 136 | if (ns === undefined) return undefined; |
| 137 | const attrs = { ...toAttrs(ns), workspaceId }; |
| 138 | const tags = toTagRecord(ns.tags); |
| 139 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 140 | }), |
| 141 | |
| 142 | reconcile: Effect.fn(function* ({ id, news, session }) { |
| 143 | const workspaceId = news!.workspaceId; |
| 144 | const name = news!.name; |
| 145 | const internalTags = yield* createInternalTags(id); |
| 146 | const desiredTags = { ...internalTags, ...news!.tags }; |
| 147 | const desiredData = yield* encodeDefinition(news!.definition); |
| 148 | |
| 149 | // 1. Observe — the live namespace is authoritative. |
| 150 | const ns = yield* describe(workspaceId, name); |
| 151 | |
| 152 | // 2. Ensure — create if missing. |
| 153 | if (ns === undefined) { |
| 154 | yield* amp.createRuleGroupsNamespace({ |
| 155 | workspaceId, |
| 156 | name, |
| 157 | data: desiredData, |
| 158 | tags: desiredTags, |
| 159 | }); |
| 160 | } else { |
| 161 | // 3. Sync definition — put only when the YAML drifts. `data` is |
| 162 | // present once the namespace is ACTIVE (omitted while CREATING). |
| 163 | const currentDefinition = |
| 164 | ns.data !== undefined ? yield* decodeDefinition(ns.data) : undefined; |
| 165 | if (currentDefinition !== news!.definition) { |
| 166 | yield* amp.putRuleGroupsNamespace({ |
| 167 | workspaceId, |
| 168 | name, |
| 169 | data: desiredData, |
| 170 | }); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // Wait for ACTIVE so the ARN, status, and data blob are settled. |
| 175 | const fresh = yield* waitActive(workspaceId, name); |
| 176 | |
| 177 | // 3b. Sync tags — diff against OBSERVED cloud tags. |
| 178 | yield* syncAmpTags(fresh.arn, desiredTags); |
| 179 | |
| 180 | yield* session.note(`${workspaceId}/${name}`); |
| 181 | return { ...toAttrs(fresh), workspaceId }; |
| 182 | }), |
| 183 | |
| 184 | delete: Effect.fn(function* ({ output }) { |
| 185 | yield* amp |
| 186 | .deleteRuleGroupsNamespace({ |
| 187 | workspaceId: output.workspaceId, |
| 188 | name: output.name, |
| 189 | }) |
| 190 | .pipe( |
| 191 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 192 | Effect.retry({ |
| 193 | while: (e) => e._tag === "ConflictException", |
| 194 | schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]), |
| 195 | }), |
| 196 | ); |
| 197 | }), |
| 198 | |
| 199 | // Sub-resource keyed by its parent workspace — not independently |
| 200 | // enumerable across the account. |
| 201 | list: () => Effect.succeed([]), |
| 202 | }; |
| 203 | }), |
| 204 | ); |