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

main
ts 255 lines 8.38 KB
Raw
1 import * as databrew from "@distilled.cloud/aws/databrew";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
8 import * as Effect from "effect/Effect";
9 import * as Stream from "effect/Stream";
10 import { AWSEnvironment } from "../Environment.ts";
11 import type { Providers } from "../Providers.ts";
12 import { cleanMap, databrewArn, fetchObservedTags, syncTags } from "./internal.ts";
13
14 /** A single data-quality rule (a check expression + optional threshold). */
15 export interface RulesetRule {
16 /** The rule name (unique within the ruleset). */
17 name: string;
18 /** Skip the rule without deleting it. @default false */
19 disabled?: boolean;
20 /**
21 * The check expression, e.g. `:col1 is_between :val1 and :val2` or
22 * `AGG(MISSING_VALUES_PERCENTAGE) < :val1`.
23 */
24 checkExpression: string;
25 /** Variable substitutions for the expression, keyed by `:name`. */
26 substitutionMap?: Record<string, string>;
27 /** Pass/fail threshold for row-level checks. */
28 threshold?: {
29 /** The threshold value. */
30 value: number;
31 /** Comparison type. @default "GREATER_THAN_OR_EQUAL" */
32 type?:
33 | "GREATER_THAN_OR_EQUAL"
34 | "LESS_THAN_OR_EQUAL"
35 | "GREATER_THAN"
36 | "LESS_THAN"
37 | (string & {});
38 /** Whether `value` is a row count or a percentage. @default "COUNT" */
39 unit?: "COUNT" | "PERCENTAGE" | (string & {});
40 };
41 /** Columns the rule applies to (by name or regex). */
42 columnSelectors?: {
43 /** A regular expression matching column names. */
44 regex?: string;
45 /** An exact column name. */
46 name?: string;
47 }[];
48 }
49
50 export interface RulesetProps {
51 /**
52 * Name of the ruleset. If omitted, a unique name is generated. Changing
53 * the name replaces the ruleset.
54 * @default a generated physical name
55 */
56 rulesetName?: string;
57 /**
58 * A description of the ruleset.
59 */
60 description?: string;
61 /**
62 * The ARN of the DataBrew dataset the ruleset validates. Changing it
63 * replaces the ruleset.
64 */
65 targetArn: string;
66 /**
67 * The data-quality rules.
68 */
69 rules: RulesetRule[];
70 /**
71 * Tags to apply to the ruleset. Merged with internal Alchemy tags.
72 */
73 tags?: Record<string, string>;
74 }
75
76 export interface Ruleset extends Resource<
77 "AWS.DataBrew.Ruleset",
78 RulesetProps,
79 {
80 /** Name of the ruleset. */
81 rulesetName: string;
82 /** ARN of the ruleset. */
83 rulesetArn: string;
84 /** ARN of the dataset the ruleset validates. */
85 targetArn: string;
86 },
87 {},
88 Providers
89 > {}
90
91 /**
92 * An AWS Glue DataBrew ruleset — a set of data-quality rules bound to a
93 * dataset. Attach it to a profile job via `validationConfigurations` to
94 * produce pass/fail validation results alongside the data profile.
95 * ### Creating Rulesets
96 * **Example:** Data-Quality Rules for a Dataset
97 * ```typescript
98 * import * as AWS from "@alchemy.run/aws";
99 *
100 * const ruleset = yield* AWS.DataBrew.Ruleset("Quality", {
101 * targetArn: dataset.datasetArn,
102 * rules: [
103 * {
104 * name: "no-missing-ids",
105 * checkExpression: "AGG(MISSING_VALUES_PERCENTAGE) == :val1",
106 * substitutionMap: { ":val1": "0" },
107 * columnSelectors: [{ name: "id" }],
108 * },
109 * ],
110 * });
111 * ```
112 *
113 * **Example:** Validate in a Profile Job
114 * ```typescript
115 * const profile = yield* AWS.DataBrew.Job("Profile", {
116 * type: "PROFILE",
117 * datasetName: dataset.datasetName,
118 * role: role.roleArn,
119 * outputLocation: { bucket: bucket.bucketName, key: "profiles/" },
120 * validationConfigurations: [{ rulesetArn: ruleset.rulesetArn }],
121 * });
122 * ```
123 *
124 * @resource
125 */
126 export const Ruleset = Resource<Ruleset>("AWS.DataBrew.Ruleset");
127
128 const buildRules = (rules: RulesetRule[]) =>
129 rules.map((rule) => ({
130 Name: rule.name,
131 Disabled: rule.disabled,
132 CheckExpression: rule.checkExpression,
133 SubstitutionMap: rule.substitutionMap,
134 Threshold: rule.threshold
135 ? {
136 Value: rule.threshold.value,
137 Type: rule.threshold.type,
138 Unit: rule.threshold.unit,
139 }
140 : undefined,
141 ColumnSelectors: rule.columnSelectors?.map((s) => ({
142 Regex: s.regex,
143 Name: s.name,
144 })),
145 }));
146
147 export const RulesetProvider = () =>
148 Provider.effect(
149 Ruleset,
150 Effect.gen(function* () {
151 const createName = Effect.fn(function* (
152 id: string,
153 props: { rulesetName?: string | undefined },
154 ) {
155 return props.rulesetName ?? (yield* createPhysicalName({ id, maxLength: 255 }));
156 });
157
158 const observe = Effect.fn(function* (name: string) {
159 return yield* databrew
160 .describeRuleset({ Name: name })
161 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
162 });
163
164 return Ruleset.Provider.of({
165 stables: ["rulesetName", "rulesetArn", "targetArn"],
166
167 list: () =>
168 Effect.gen(function* () {
169 const { accountId, region } = yield* AWSEnvironment.current;
170 const pages = yield* databrew.listRulesets.pages({}).pipe(Stream.runCollect);
171 return Array.from(pages)
172 .flatMap((page) => page.Rulesets ?? [])
173 .map((r) => ({
174 rulesetName: r.Name,
175 rulesetArn: r.ResourceArn ?? databrewArn(region, accountId, "ruleset", r.Name),
176 targetArn: r.TargetArn ?? "",
177 }));
178 }),
179
180 read: Effect.fn(function* ({ id, olds, output }) {
181 const { accountId, region } = yield* AWSEnvironment.current;
182 const name = output?.rulesetName ?? (yield* createName(id, olds ?? {}));
183 const ruleset = yield* observe(name);
184 if (ruleset === undefined) return undefined;
185 const arn = ruleset.ResourceArn ?? databrewArn(region, accountId, "ruleset", name);
186 const attrs = {
187 rulesetName: name,
188 rulesetArn: arn,
189 targetArn: ruleset.TargetArn ?? "",
190 };
191 const tags = cleanMap(ruleset.Tags);
192 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
193 }),
194
195 diff: Effect.fn(function* ({ id, news, olds }) {
196 if (!isResolved(news)) return undefined;
197 const oldName = yield* createName(id, olds);
198 const newName = yield* createName(id, news);
199 if (oldName !== newName) return { action: "replace" } as const;
200 // UpdateRuleset only accepts Description + Rules.
201 if (olds.targetArn !== news.targetArn) {
202 return { action: "replace" } as const;
203 }
204 }),
205
206 reconcile: Effect.fn(function* ({ id, news, output, session }) {
207 const { accountId, region } = yield* AWSEnvironment.current;
208 const name = output?.rulesetName ?? (yield* createName(id, news));
209 const internalTags = yield* createInternalTags(id);
210 const desiredTags = { ...news.tags, ...internalTags };
211
212 // 1. OBSERVE
213 const ruleset = yield* observe(name);
214
215 // 2. ENSURE / 3. SYNC — UpdateRuleset is a full PUT of the rules
216 if (ruleset === undefined) {
217 yield* databrew
218 .createRuleset({
219 Name: name,
220 Description: news.description,
221 TargetArn: news.targetArn,
222 Rules: buildRules(news.rules),
223 Tags: desiredTags,
224 })
225 .pipe(Effect.catchTag("ConflictException", () => Effect.void));
226 } else {
227 yield* databrew.updateRuleset({
228 Name: name,
229 Description: news.description,
230 Rules: buildRules(news.rules),
231 });
232 }
233
234 const arn = ruleset?.ResourceArn ?? databrewArn(region, accountId, "ruleset", name);
235
236 // 3b. SYNC TAGS against observed cloud tags
237 const observedTags = yield* fetchObservedTags(arn);
238 yield* syncTags(arn, observedTags, desiredTags);
239
240 yield* session.note(name);
241 return {
242 rulesetName: name,
243 rulesetArn: arn,
244 targetArn: news.targetArn,
245 };
246 }),
247
248 delete: Effect.fn(function* ({ output }) {
249 yield* databrew
250 .deleteRuleset({ Name: output.rulesetName })
251 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
252 }),
253 });
254 }),
255 );