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

main
ts 177 lines 6.15 KB
Raw
1 import * as appsignals from "@distilled.cloud/aws/application-signals";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import type { Providers } from "../Providers.ts";
7
8 export interface GroupingConfigurationProps {
9 /**
10 * The custom grouping attribute definitions for this account. Each
11 * definition names a grouping dimension (`GroupingName`), the telemetry
12 * attribute or AWS tag keys it is sourced from (`GroupingSourceKeys`,
13 * e.g. `"Tag.team"` or an OTel resource attribute key), and an optional
14 * `DefaultGroupingValue` used when none of the source keys are present.
15 */
16 groupingAttributeDefinitions: appsignals.GroupingAttributeDefinition[];
17 }
18
19 export interface GroupingConfiguration extends Resource<
20 "AWS.ApplicationSignals.GroupingConfiguration",
21 GroupingConfigurationProps,
22 {
23 /**
24 * The grouping attribute definitions as returned by the service.
25 */
26 groupingAttributeDefinitions: appsignals.GroupingAttributeDefinition[];
27 /**
28 * When the grouping configuration was last updated (ISO timestamp).
29 */
30 updatedAt: string | undefined;
31 },
32 never,
33 Providers
34 > {}
35
36 /**
37 * The CloudWatch Application Signals grouping configuration for this
38 * account — an account-level singleton that defines custom grouping
39 * attributes (sourced from telemetry attributes or AWS tags) used to
40 * organize and filter discovered services in the Application Signals
41 * console and APIs.
42 *
43 * There is at most ONE grouping configuration per account/region;
44 * `PutGroupingConfiguration` replaces the whole definition list.
45 *
46 * ### Creating a Grouping Configuration
47 * **Example:** Group Services by Team Tag
48 * ```typescript
49 * import * as AWS from "@alchemy.run/aws";
50 *
51 * const grouping = yield* AWS.ApplicationSignals.GroupingConfiguration(
52 * "Grouping",
53 * {
54 * groupingAttributeDefinitions: [
55 * {
56 * GroupingName: "Team",
57 * GroupingSourceKeys: ["Tag.team"],
58 * DefaultGroupingValue: "unassigned",
59 * },
60 * ],
61 * },
62 * );
63 * ```
64 *
65 * **Example:** Multiple Grouping Dimensions
66 * ```typescript
67 * const grouping = yield* AWS.ApplicationSignals.GroupingConfiguration(
68 * "Grouping",
69 * {
70 * groupingAttributeDefinitions: [
71 * { GroupingName: "Team", GroupingSourceKeys: ["Tag.team"] },
72 * {
73 * GroupingName: "CostCenter",
74 * GroupingSourceKeys: ["Tag.cost-center", "business_unit"],
75 * DefaultGroupingValue: "shared",
76 * },
77 * ],
78 * },
79 * );
80 * ```
81 *
82 * @resource
83 */
84 export const GroupingConfiguration = Resource<GroupingConfiguration>(
85 "AWS.ApplicationSignals.GroupingConfiguration",
86 );
87
88 /** Normalize a definition for comparison (drop undefined members). */
89 const normalizeDefinition = (definition: appsignals.GroupingAttributeDefinition) => ({
90 GroupingName: definition.GroupingName,
91 GroupingSourceKeys: definition.GroupingSourceKeys ?? [],
92 DefaultGroupingValue: definition.DefaultGroupingValue,
93 });
94
95 const sameDefinitions = (
96 desired: appsignals.GroupingAttributeDefinition[],
97 observed: appsignals.GroupingAttributeDefinition[],
98 ): boolean =>
99 desired.length === observed.length &&
100 desired.every(
101 (d, i) =>
102 JSON.stringify(normalizeDefinition(d)) === JSON.stringify(normalizeDefinition(observed[i])),
103 );
104
105 export const GroupingConfigurationProvider = () =>
106 Provider.effect(
107 GroupingConfiguration,
108 Effect.gen(function* () {
109 /**
110 * Observe the account's grouping configuration. The API has no
111 * dedicated "get" — an account without a configuration returns an
112 * empty definition list and no `UpdatedAt`.
113 */
114 const observe = appsignals.listGroupingAttributeDefinitions({}).pipe(
115 Effect.map((response) =>
116 response.UpdatedAt === undefined && response.GroupingAttributeDefinitions.length === 0
117 ? undefined
118 : {
119 groupingAttributeDefinitions: [...response.GroupingAttributeDefinitions],
120 updatedAt: response.UpdatedAt?.toISOString(),
121 },
122 ),
123 );
124
125 return {
126 stables: [],
127
128 read: Effect.fn(function* ({ olds, output }) {
129 const observed = yield* observe;
130 if (observed === undefined) return undefined;
131 // The grouping configuration is not taggable, so ownership cannot
132 // be branded. If we have no record of creating it, surface it as
133 // an existing-but-foreign singleton so takeover requires --adopt.
134 return output !== undefined || olds !== undefined ? observed : Unowned(observed);
135 }),
136
137 reconcile: Effect.fn(function* ({ id, news, session }) {
138 const desired = news.groupingAttributeDefinitions;
139
140 // 1. Observe — the live configuration is authoritative.
141 const observed = yield* observe;
142
143 // 2/3. Ensure + sync — PutGroupingConfiguration is a full-replace
144 // upsert; skip the call when the observed definitions already
145 // match the desired ones.
146 const fresh =
147 observed !== undefined &&
148 sameDefinitions(desired, observed.groupingAttributeDefinitions)
149 ? observed
150 : yield* appsignals
151 .putGroupingConfiguration({
152 GroupingAttributeDefinitions: desired,
153 })
154 .pipe(
155 Effect.map((response) => ({
156 groupingAttributeDefinitions: [
157 ...response.GroupingConfiguration.GroupingAttributeDefinitions,
158 ],
159 updatedAt: response.GroupingConfiguration.UpdatedAt?.toISOString(),
160 })),
161 );
162
163 yield* session.note(id);
164 return fresh;
165 }),
166
167 delete: Effect.fn(function* () {
168 // DeleteGroupingConfiguration is idempotent — deleting an absent
169 // configuration succeeds.
170 yield* appsignals.deleteGroupingConfiguration({});
171 }),
172
173 // Account-level singleton; enumeration happens through read.
174 list: () => Effect.succeed([]),
175 };
176 }),
177 );