@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 534 lines 19.7 KB
Raw
1 import * as bigqueryreservation from "@distilled.cloud/gcp/bigqueryreservation_v1";
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 { tagRecord } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Stream from "effect/Stream";
11 import { GcpEnvironment } from "../Environment.ts";
12 import {
13 createInternalLabels,
14 diffLabels,
15 hasAlchemyLabels,
16 stripInternalLabels,
17 toLabels,
18 } from "../Labels.ts";
19 import type { Providers } from "../Providers.ts";
20
21 const DEFAULT_EDITION = "ENTERPRISE";
22 const DEFAULT_SLOT_CAPACITY = "0";
23 const MAX_NAME_LENGTH = 64;
24
25 export type ReservationEdition = bigqueryreservation.ReservationEditionEnum | (string & {});
26
27 export type ReservationScalingMode = bigqueryreservation.ReservationScalingModeEnum | (string & {});
28
29 export type ReservationAutoscale = {
30 /**
31 * Maximum extra slots the reservation may add when demand exceeds
32 * baseline `slotCapacity`.
33 */
34 maxSlots?: string;
35 };
36
37 export type ReservationSchedulingPolicy = {
38 /**
39 * Soft per-project slot cap for queries in this reservation. Preview.
40 */
41 maxSlots?: string;
42 /**
43 * Soft per-project job concurrency cap. Preview.
44 */
45 concurrency?: string;
46 };
47
48 export type ReservationProps = {
49 /**
50 * Reservation id (the `{reservation}` segment of
51 * `projects/{project}/locations/{location}/reservations/{reservation}`).
52 * If omitted, a unique name is generated from the stack, stage, and
53 * logical id. Must contain only lowercase letters, digits, or dashes;
54 * start with a letter; not end with a dash; max 64 characters.
55 * Immutable — changing it replaces the reservation.
56 */
57 reservationId?: string;
58 /**
59 * BigQuery location (`us-central1`, `US`, `EU`, …). Immutable —
60 * changing it replaces the reservation. Multi-regions `US` and `EU`
61 * stay uppercase; regional ids are lowercased (`US-CENTRAL1` becomes
62 * `us-central1`).
63 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
64 */
65 location?: string;
66 /**
67 * Baseline slots. `"0"` is valid for edition reservations that rely on
68 * idle slots or autoscale. Immutable identity is unchanged; the value
69 * itself is mutable.
70 * @default "0"
71 */
72 slotCapacity?: string;
73 /**
74 * When true, jobs using this reservation cannot burst into idle slots
75 * from sibling reservations. STANDARD edition requires this to be true.
76 * @default true when edition is STANDARD, otherwise false
77 */
78 ignoreIdleSlots?: boolean;
79 /**
80 * Reservation edition. Immutable — changing it replaces the
81 * reservation. `STANDARD` cannot set a non-zero baseline
82 * `slotCapacity` (use `autoscale.maxSlots` instead).
83 * @default "ENTERPRISE"
84 */
85 edition?: ReservationEdition;
86 /**
87 * User labels. Alchemy ownership labels are merged in automatically.
88 */
89 labels?: Record<string, string>;
90 /**
91 * Autoscale ceiling. Mutually exclusive with `maxSlots` plus
92 * `scalingMode`. `STANDARD` reservations are autoscale-only.
93 */
94 autoscale?: ReservationAutoscale;
95 /**
96 * Soft target for concurrent jobs. `"0"` lets BigQuery pick.
97 */
98 concurrency?: string;
99 /**
100 * Overall slot cap covering baseline, idle, and scaled slots. Must be
101 * set together with `scalingMode`.
102 */
103 maxSlots?: string;
104 /**
105 * How the reservation uses idle and autoscale slots when `maxSlots` is
106 * set (`AUTOSCALE_ONLY`, `IDLE_SLOTS_ONLY`, `ALL_SLOTS`).
107 */
108 scalingMode?: ReservationScalingMode;
109 /**
110 * Reservation group this reservation belongs to, as
111 * `projects/{project}/locations/{location}/reservationGroups/{group}`
112 * or a bare group id.
113 */
114 reservationGroup?: string;
115 /**
116 * Secondary location for managed disaster recovery. Setting this on
117 * create or update converts the reservation into a failover
118 * reservation.
119 */
120 secondaryLocation?: string;
121 /**
122 * Place the reservation in the organization's DR secondary region
123 * (multi-region `US` or `EU` only). Preview; the project must be
124 * allow-listed. Immutable — changing it replaces the reservation.
125 */
126 multiRegionAuxiliary?: boolean;
127 /**
128 * Per-project scheduling caps. Preview.
129 */
130 schedulingPolicy?: ReservationSchedulingPolicy;
131 };
132
133 export type Reservation = Resource<
134 "GCP.BigQueryReservation.Reservation",
135 ReservationProps,
136 {
137 /** Full resource name `projects/{project}/locations/{location}/reservations/{reservation}`. */
138 name: string;
139 /** Reservation id (last path segment). */
140 reservationId: string;
141 /** Project id. */
142 project: string;
143 /** Location id (`us-central1`, `US`, …). */
144 location: string;
145 /** User labels (Alchemy ownership labels stripped). */
146 labels: Record<string, string>;
147 /** Baseline slot capacity. */
148 slotCapacity: string | undefined;
149 /** Whether idle slots from siblings are ignored. */
150 ignoreIdleSlots: boolean;
151 /** Reservation edition (`STANDARD`, `ENTERPRISE`, …). */
152 edition: string | undefined;
153 /** Autoscale settings, including current extra slots. */
154 autoscale: bigqueryreservation.Autoscale | undefined;
155 /** Job concurrency target. */
156 concurrency: string | undefined;
157 /** Overall slot cap, if set. */
158 maxSlots: string | undefined;
159 /** Scaling mode used with `maxSlots`. */
160 scalingMode: string | undefined;
161 /** Reservation group, if any. */
162 reservationGroup: string | undefined;
163 /** Secondary DR location, if any. */
164 secondaryLocation: string | undefined;
165 /** Current primary replica location (failover reservations). */
166 primaryLocation: string | undefined;
167 /** Original primary location used for billing failover reservations. */
168 originalPrimaryLocation: string | undefined;
169 /** Whether this is a multi-region auxiliary reservation. */
170 multiRegionAuxiliary: boolean;
171 /** Per-project scheduling policy. */
172 schedulingPolicy: bigqueryreservation.SchedulingPolicy | undefined;
173 /** DR replication status of the primary replica. */
174 replicationStatus: bigqueryreservation.ReplicationStatus | undefined;
175 /** RFC3339 creation timestamp. */
176 creationTime: string | undefined;
177 /** RFC3339 last-update timestamp. */
178 updateTime: string | undefined;
179 },
180 never,
181 Providers
182 >;
183
184 /**
185 * A BigQuery slot reservation — a named pool of slots that assignments
186 * can consume.
187 *
188 * Changing `reservationId`, `location`, `edition`, or
189 * `multiRegionAuxiliary` replaces the reservation. Baseline capacity,
190 * idle-slot behavior, labels, autoscale, and failover location are
191 * updated in place.
192 *
193 * ### Creating a Reservation
194 * **Example:** Generated name, zero-slot ENTERPRISE
195 * ```typescript
196 * const slots = yield* GCP.BigQueryReservation.Reservation("Slots", {});
197 * ```
198 *
199 * **Example:** Explicit id, labels, and idle-slot isolation
200 * ```typescript
201 * const slots = yield* GCP.BigQueryReservation.Reservation("Slots", {
202 * reservationId: "analytics-prod",
203 * location: "us-central1",
204 * edition: "ENTERPRISE",
205 * slotCapacity: "0",
206 * ignoreIdleSlots: false,
207 * labels: { env: "prod" },
208 * });
209 * ```
210 *
211 * **Example:** STANDARD with autoscale
212 * ```typescript
213 * const slots = yield* GCP.BigQueryReservation.Reservation("Slots", {
214 * location: "US",
215 * edition: "STANDARD",
216 * ignoreIdleSlots: true,
217 * autoscale: { maxSlots: "50" },
218 * });
219 * ```
220 *
221 * @resource
222 * @category BigQueryReservation
223 */
224 export const Reservation = Resource<Reservation>("GCP.BigQueryReservation.Reservation");
225
226 export class ReservationNotResolved extends Data.TaggedError(
227 "GCP.BigQueryReservation.ReservationNotResolved",
228 )<{
229 resourceName: string;
230 }> {}
231
232 const lastSegment = (value: string) => {
233 const trimmed = value.replace(/\/+$/, "");
234 const parts = trimmed.split("/");
235 return parts[parts.length - 1] || trimmed;
236 };
237
238 const normalizeLocation = (location: string) => {
239 const value = lastSegment(location);
240 const upper = value.toUpperCase();
241 if (upper === "US" || upper === "EU") return upper;
242 return value.toLowerCase();
243 };
244
245 const normalizeEdition = (edition: string | undefined) => {
246 const value = (edition ?? DEFAULT_EDITION).toUpperCase();
247 return value === "EDITION_UNSPECIFIED" ? DEFAULT_EDITION : value;
248 };
249
250 const slotCapacityOf = (value: string | undefined) =>
251 value === undefined || value === "" ? DEFAULT_SLOT_CAPACITY : value;
252
253 const ignoreIdleSlotsOf = (value: boolean | undefined, edition: string) =>
254 value ?? edition === "STANDARD";
255
256 const resourceName = (project: string, location: string, reservationId: string) =>
257 `projects/${project}/locations/${location}/reservations/${reservationId}`;
258
259 const parentOf = (project: string, location: string) => `projects/${project}/locations/${location}`;
260
261 const parseName = (name: string) => {
262 const parts = name.split("/").filter((part) => part.length > 0);
263 const reservationsAt = parts.lastIndexOf("reservations");
264 const locationsAt = parts.lastIndexOf("locations");
265 const projectsAt = parts.lastIndexOf("projects");
266 return {
267 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
268 location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "",
269 reservationId:
270 reservationsAt >= 0 && parts[reservationsAt + 1]
271 ? parts[reservationsAt + 1]!
272 : lastSegment(name),
273 };
274 };
275
276 const userLabels = (
277 labels: Record<string, string | undefined> | null | undefined,
278 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
279
280 const toId = (id: string, reservationId: string | undefined, existing?: string) =>
281 Effect.gen(function* () {
282 if (reservationId !== undefined) return reservationId;
283 if (existing !== undefined) return existing;
284 const generated = yield* createPhysicalName({
285 id,
286 maxLength: MAX_NAME_LENGTH,
287 lowercase: true,
288 });
289 const prefixed = /^[a-z]/.test(generated) ? generated : `r${generated}`;
290 return prefixed.replace(/-+$/g, "").slice(0, MAX_NAME_LENGTH);
291 });
292
293 const compact = <T extends Record<string, unknown>>(value: T): T =>
294 Object.fromEntries(Object.entries(value).filter(([, item]) => item !== undefined)) as T;
295
296 const jsonOf = (value: unknown) => JSON.stringify(value ?? null);
297
298 const toAttrs = (current: bigqueryreservation.Reservation, project: string) => {
299 const name = current.name ?? "";
300 const parsed = parseName(name);
301 return {
302 name,
303 reservationId: parsed.reservationId,
304 project: parsed.project || project,
305 location: parsed.location,
306 labels: userLabels(current.labels),
307 slotCapacity: current.slotCapacity,
308 ignoreIdleSlots: current.ignoreIdleSlots === true,
309 edition: current.edition,
310 autoscale: current.autoscale,
311 concurrency: current.concurrency,
312 maxSlots: current.maxSlots,
313 scalingMode: current.scalingMode,
314 reservationGroup: current.reservationGroup,
315 secondaryLocation: current.secondaryLocation,
316 primaryLocation: current.primaryLocation,
317 originalPrimaryLocation: current.originalPrimaryLocation,
318 multiRegionAuxiliary: current.multiRegionAuxiliary === true,
319 schedulingPolicy: current.schedulingPolicy,
320 replicationStatus: current.replicationStatus,
321 creationTime: current.creationTime,
322 updateTime: current.updateTime,
323 };
324 };
325
326 const getByName = (name: string) =>
327 bigqueryreservation
328 .getProjectsLocationsReservations({ name })
329 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
330
331 const listOwnedAt = (project: string, location: string) =>
332 bigqueryreservation.listProjectsLocationsReservations
333 .pages({
334 parent: parentOf(project, location),
335 pageSize: 1000,
336 })
337 .pipe(
338 Stream.flatMap((page) => Stream.fromIterable(page.reservations ?? [])),
339 Stream.filter((item) =>
340 Object.keys(item.labels ?? {}).some((key) => key.startsWith("alchemy-")),
341 ),
342 Stream.map((item) => toAttrs(item, project)),
343 Stream.runCollect,
344 Effect.map((chunk) => Array.from(chunk)),
345 Effect.catchTag("NotFound", () => Effect.succeed([])),
346 );
347
348 const desiredAutoscale = (news: ReservationProps): bigqueryreservation.Autoscale | undefined =>
349 news.autoscale === undefined ? undefined : compact({ maxSlots: news.autoscale.maxSlots });
350
351 const desiredSchedulingPolicy = (
352 news: ReservationProps,
353 ): bigqueryreservation.SchedulingPolicy | undefined =>
354 news.schedulingPolicy === undefined
355 ? undefined
356 : compact({
357 maxSlots: news.schedulingPolicy.maxSlots,
358 concurrency: news.schedulingPolicy.concurrency,
359 });
360
361 const toBody = (
362 news: ReservationProps,
363 labels: Record<string, string>,
364 extras: {
365 edition?: string;
366 slotCapacity: string;
367 ignoreIdleSlots: boolean;
368 },
369 ): bigqueryreservation.Reservation =>
370 compact({
371 slotCapacity: extras.slotCapacity,
372 ignoreIdleSlots: extras.ignoreIdleSlots,
373 edition: extras.edition,
374 labels,
375 autoscale: desiredAutoscale(news),
376 concurrency: news.concurrency,
377 maxSlots: news.maxSlots,
378 scalingMode: news.scalingMode,
379 reservationGroup: news.reservationGroup,
380 secondaryLocation: news.secondaryLocation,
381 multiRegionAuxiliary: news.multiRegionAuxiliary,
382 schedulingPolicy: desiredSchedulingPolicy(news),
383 });
384
385 export const ReservationProvider = () =>
386 Provider.succeed(Reservation, {
387 stables: ["name", "reservationId", "project", "location", "edition", "creationTime"],
388
389 diff: Effect.fn(function* ({ news, olds, output }) {
390 if (!isResolved(news)) return undefined;
391 const env = yield* GcpEnvironment.current;
392
393 const previousId = olds?.reservationId ?? output?.reservationId;
394 const nextId = news.reservationId ?? previousId;
395 const previousLocation = normalizeLocation(olds?.location ?? output?.location ?? env.region);
396 const nextLocation = normalizeLocation(news.location ?? output?.location ?? env.region);
397 const previousEdition = normalizeEdition(olds?.edition ?? output?.edition);
398 const nextEdition = normalizeEdition(news.edition ?? previousEdition);
399 const previousAuxiliary = olds?.multiRegionAuxiliary ?? output?.multiRegionAuxiliary;
400 const nextAuxiliary = news.multiRegionAuxiliary ?? previousAuxiliary ?? false;
401
402 const replace =
403 (previousId !== undefined && nextId !== undefined && nextId !== previousId) ||
404 previousLocation !== nextLocation ||
405 previousEdition !== nextEdition ||
406 (previousAuxiliary === true) !== (nextAuxiliary === true);
407
408 if (!replace) return undefined;
409 return {
410 action: "replace" as const,
411 deleteFirst:
412 previousLocation === nextLocation && previousId !== undefined && nextId === previousId,
413 };
414 }),
415
416 read: Effect.fn(function* ({ id, olds, output }) {
417 const env = yield* GcpEnvironment.current;
418 const reservationId = yield* toId(id, olds?.reservationId, output?.reservationId);
419 const location = normalizeLocation(olds?.location ?? output?.location ?? env.region);
420 const name = output?.name ?? resourceName(env.project, location, reservationId);
421 const existing = yield* getByName(name);
422 if (existing === undefined) return undefined;
423 const attrs = toAttrs(existing, env.project);
424 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
425 }),
426
427 list: () =>
428 Effect.gen(function* () {
429 const env = yield* GcpEnvironment.current;
430 const pages = yield* Effect.forEach(
431 Array.from(new Set([env.region, "US", "EU"])),
432 (location) => listOwnedAt(env.project, location),
433 { concurrency: 4 },
434 );
435 const byName = new Map<string, ReturnType<typeof toAttrs>>();
436 for (const item of pages.flat()) {
437 byName.set(item.name, item);
438 }
439 return Array.from(byName.values());
440 }),
441
442 reconcile: Effect.fn(function* ({ id, news, output }) {
443 const env = yield* GcpEnvironment.current;
444 const reservationId = yield* toId(id, news.reservationId, output?.reservationId);
445 const location = normalizeLocation(news.location ?? output?.location ?? env.region);
446 const name = resourceName(env.project, location, reservationId);
447 const parent = parentOf(env.project, location);
448 const desiredLabels = {
449 ...toLabels(news.labels),
450 ...(yield* createInternalLabels(id)),
451 };
452 const edition = normalizeEdition(news.edition ?? output?.edition);
453 const slotCapacity = slotCapacityOf(news.slotCapacity);
454 const ignoreIdleSlots = ignoreIdleSlotsOf(news.ignoreIdleSlots, edition);
455 const createBody = toBody(news, desiredLabels, {
456 edition,
457 slotCapacity,
458 ignoreIdleSlots,
459 });
460
461 let current = yield* getByName(output?.name ?? name);
462
463 if (current === undefined) {
464 const created = yield* bigqueryreservation
465 .createProjectsLocationsReservations({
466 parent,
467 reservationId,
468 body: createBody,
469 })
470 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
471 current = created ?? undefined;
472 }
473
474 if (current === undefined) {
475 return yield* new ReservationNotResolved({ resourceName: name });
476 }
477
478 const observedLabels = tagRecord(current.labels);
479 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
480 const labelsChanged = upsert.length > 0 || removed.length > 0;
481 const slotCapacityChanged = slotCapacityOf(current.slotCapacity) !== slotCapacity;
482 const ignoreIdleSlotsChanged = (current.ignoreIdleSlots === true) !== ignoreIdleSlots;
483 const autoscaleChanged =
484 news.autoscale !== undefined &&
485 (current.autoscale?.maxSlots ?? "") !== (news.autoscale.maxSlots ?? "");
486 const concurrencyChanged =
487 news.concurrency !== undefined && (current.concurrency ?? "") !== news.concurrency;
488 const maxSlotsChanged =
489 news.maxSlots !== undefined && (current.maxSlots ?? "") !== news.maxSlots;
490 const scalingModeChanged =
491 news.scalingMode !== undefined && (current.scalingMode ?? "") !== news.scalingMode;
492 const reservationGroupChanged =
493 news.reservationGroup !== undefined &&
494 (current.reservationGroup ?? "") !== news.reservationGroup;
495 const secondaryLocationChanged =
496 news.secondaryLocation !== undefined &&
497 (current.secondaryLocation ?? "") !== news.secondaryLocation;
498 const schedulingPolicyChanged =
499 news.schedulingPolicy !== undefined &&
500 jsonOf(current.schedulingPolicy) !== jsonOf(desiredSchedulingPolicy(news));
501
502 const updateMask = [
503 labelsChanged ? "labels" : undefined,
504 slotCapacityChanged ? "slotCapacity" : undefined,
505 ignoreIdleSlotsChanged ? "ignoreIdleSlots" : undefined,
506 autoscaleChanged ? "autoscale" : undefined,
507 concurrencyChanged ? "concurrency" : undefined,
508 maxSlotsChanged ? "maxSlots" : undefined,
509 scalingModeChanged ? "scalingMode" : undefined,
510 reservationGroupChanged ? "reservationGroup" : undefined,
511 secondaryLocationChanged ? "secondaryLocation" : undefined,
512 schedulingPolicyChanged ? "schedulingPolicy" : undefined,
513 ].filter((field): field is string => field !== undefined);
514
515 if (updateMask.length > 0) {
516 current = yield* bigqueryreservation.patchProjectsLocationsReservations({
517 name: current.name ?? name,
518 updateMask: updateMask.join(","),
519 body: toBody(news, desiredLabels, {
520 slotCapacity,
521 ignoreIdleSlots,
522 }),
523 });
524 }
525
526 return toAttrs(current, env.project);
527 }),
528
529 delete: Effect.fn(function* ({ output }) {
530 yield* bigqueryreservation
531 .deleteProjectsLocationsReservations({ name: output.name })
532 .pipe(Effect.catchTag("NotFound", () => Effect.void));
533 }),
534 });