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

main
ts 341 lines 11.4 KB
Raw
1 import * as bigtable from "@distilled.cloud/gcp/bigtableadmin_v2";
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 * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 instanceIdOf,
13 instanceNameOf,
14 listAlchemyInstances,
15 materializedViewName,
16 MAX_MATERIALIZED_VIEW_ID_LENGTH,
17 parentOwned,
18 parseResourceName,
19 toPhysicalId,
20 waitForOperation,
21 collectPages,
22 } from "./operations.ts";
23
24 export type InstancesMaterializedViewProps = {
25 /**
26 * Parent instance. Full name `projects/{project}/instances/{instance}`
27 * or the instance id. Immutable — changing it replaces the view.
28 */
29 instance: string;
30 /**
31 * Materialized view id (the `{materialized_view}` segment of
32 * `.../instances/{instance}/materializedViews/{materialized_view}`).
33 * If omitted, a unique name is generated from the stack, stage, and
34 * logical id. Must be unique among table ids and view ids in the
35 * instance. Immutable — changing it replaces the view.
36 */
37 materializedViewId?: string;
38 /**
39 * GoogleSQL SELECT that defines the continuous materialized view.
40 * Must include `GROUP BY` or `ORDER BY` and cannot use `SELECT *`.
41 * Immutable — changing it replaces the view.
42 */
43 query: string;
44 /**
45 * Protect the view from Admin API deletes. Disabled automatically on
46 * destroy.
47 * @default false
48 */
49 deletionProtection?: boolean;
50 };
51
52 export type InstancesMaterializedView = Resource<
53 "GCP.Bigtable.InstancesMaterializedView",
54 InstancesMaterializedViewProps,
55 {
56 /** Full resource name `.../instances/{instance}/materializedViews/{materialized_view}`. */
57 name: string;
58 /** Materialized view id (last path segment). */
59 materializedViewId: string;
60 /** Parent instance resource name. */
61 instance: string;
62 /** Parent instance id. */
63 instanceId: string;
64 /** Project id. */
65 project: string;
66 /** Defining SELECT query. */
67 query: string | undefined;
68 /** Whether Admin API deletes are blocked. */
69 deletionProtection: boolean;
70 /** Per-cluster replication state. */
71 clusterStates: bigtable.GoogleBigtableAdminV2MaterializedViewClusterStateMap | undefined;
72 /** Server etag. */
73 etag: string | undefined;
74 },
75 never,
76 Providers
77 >;
78
79 /**
80 * A Cloud Bigtable continuous materialized view — a SQL SELECT whose
81 * results are precomputed and kept current.
82 *
83 * The parent instance must already exist. The query must use `GROUP BY`
84 * or `ORDER BY` and cannot use `SELECT *`. Changing `materializedViewId`,
85 * `instance`, or `query` replaces the view. `deletionProtection` updates
86 * in place.
87 *
88 * Materialized views have no labels field. Alchemy treats a view as
89 * owned when its parent instance carries Alchemy labels, so `list` /
90 * `pnpm nuke:gcp` can find it.
91 *
92 * ### Creating a Materialized View
93 * **Example:** Count of rows grouped by a constant key
94 * ```typescript
95 * const events = yield* GCP.Bigtable.Table("Events", {
96 * instance: instance.name,
97 * columnFamilies: { cf: {} },
98 * });
99 * const view = yield* GCP.Bigtable.InstancesMaterializedView("Counts", {
100 * instance: instance.name,
101 * query: "SELECT '*' AS _key, COUNT(*) AS row_count FROM events GROUP BY _key",
102 * });
103 * ```
104 *
105 * @resource
106 * @category Bigtable
107 */
108 export const InstancesMaterializedView = Resource<InstancesMaterializedView>(
109 "GCP.Bigtable.InstancesMaterializedView",
110 );
111
112 export class MaterializedViewNotResolved extends Data.TaggedError(
113 "GCP.Bigtable.MaterializedViewNotResolved",
114 )<{
115 resourceName: string;
116 }> {}
117
118 export class MaterializedViewStillExists extends Data.TaggedError(
119 "GCP.Bigtable.MaterializedViewStillExists",
120 )<{
121 resourceName: string;
122 }> {}
123
124 const toId = (id: string, materializedViewId: string | undefined, existing?: string) =>
125 toPhysicalId(id, materializedViewId, existing, MAX_MATERIALIZED_VIEW_ID_LENGTH);
126
127 const queryOf = (value: string | undefined) => (value ?? "").trim();
128
129 const toAttrs = (view: bigtable.MaterializedView, project: string) => {
130 const name = view.name ?? "";
131 const parsed = parseResourceName(name);
132 return {
133 name,
134 materializedViewId: parsed.materializedViewId,
135 instance: parsed.instance,
136 instanceId: parsed.instanceId,
137 project: parsed.project || project,
138 query: view.query,
139 deletionProtection: view.deletionProtection === true,
140 clusterStates: view.clusterStates,
141 etag: view.etag,
142 };
143 };
144
145 const getByName = (name: string) =>
146 bigtable
147 .getProjectsInstancesMaterializedViews({ name, view: "FULL" })
148 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
149
150 const waitUntilExists = (name: string) =>
151 getByName(name).pipe(
152 Effect.flatMap((view) =>
153 view
154 ? Effect.succeed(view)
155 : Effect.fail(new MaterializedViewNotResolved({ resourceName: name })),
156 ),
157 Effect.retry({
158 while: (error) => error._tag === "GCP.Bigtable.MaterializedViewNotResolved",
159 times: 8,
160 schedule: Schedule.spaced("1 second"),
161 }),
162 );
163
164 const waitUntilGone = (name: string) =>
165 getByName(name).pipe(
166 Effect.flatMap((view) =>
167 view === undefined
168 ? Effect.void
169 : Effect.fail(new MaterializedViewStillExists({ resourceName: name })),
170 ),
171 Effect.retry({
172 while: (error) => error._tag === "GCP.Bigtable.MaterializedViewStillExists",
173 times: 8,
174 schedule: Schedule.spaced("2 seconds"),
175 }),
176 );
177
178 const unprotect = (name: string, etag: string | undefined) =>
179 Effect.gen(function* () {
180 const patched = yield* bigtable.patchProjectsInstancesMaterializedViews({
181 name,
182 updateMask: "deletion_protection",
183 body: { deletionProtection: false, etag },
184 });
185 yield* waitForOperation(patched);
186 });
187
188 export const InstancesMaterializedViewProvider = () =>
189 Provider.succeed(InstancesMaterializedView, {
190 stables: ["name", "materializedViewId", "instance", "instanceId", "project"],
191
192 diff: Effect.fn(function* ({ news, olds, output }) {
193 if (!isResolved(news)) return undefined;
194 const previousId = olds?.materializedViewId ?? output?.materializedViewId;
195 const nextId = news.materializedViewId ?? previousId;
196 const previousInstance = instanceIdOf(
197 olds?.instance ?? output?.instance ?? output?.instanceId ?? "",
198 );
199 const nextInstance = instanceIdOf(news.instance);
200 const previousQuery = queryOf(olds?.query ?? output?.query);
201 const nextQuery = queryOf(news.query);
202 if (
203 (previousId !== undefined && nextId !== undefined && previousId !== nextId) ||
204 (previousInstance.length > 0 && previousInstance !== nextInstance) ||
205 (previousQuery.length > 0 && previousQuery !== nextQuery)
206 ) {
207 return { action: "replace" as const };
208 }
209 return undefined;
210 }),
211
212 read: Effect.fn(function* ({ id, olds, output }) {
213 const env = yield* GcpEnvironment.current;
214 const materializedViewId = yield* toId(
215 id,
216 olds?.materializedViewId,
217 output?.materializedViewId,
218 );
219 const instanceRef = olds?.instance ?? output?.instance;
220 const name =
221 output?.name ??
222 (instanceRef
223 ? materializedViewName(env.project, instanceIdOf(instanceRef), materializedViewId)
224 : undefined);
225 if (name === undefined) return undefined;
226 const existing = yield* getByName(name);
227 if (existing === undefined) return undefined;
228 const attrs = toAttrs(existing, env.project);
229 return (yield* parentOwned(attrs.instance)) ? attrs : Unowned(attrs);
230 }),
231
232 list: () =>
233 Effect.gen(function* () {
234 const env = yield* GcpEnvironment.current;
235 const instances = (yield* listAlchemyInstances(env.project)).filter(
236 (instance): instance is bigtable.Instance & { name: string } =>
237 typeof instance.name === "string" && instance.name.length > 0,
238 );
239 const pages = yield* Effect.forEach(
240 instances,
241 (instance) =>
242 collectPages(
243 bigtable.listProjectsInstancesMaterializedViews.pages({
244 parent: instance.name,
245 pageSize: 1000,
246 }),
247 (page) => page.materializedViews,
248 ).pipe(
249 Effect.catchTag("NotFound", () => Effect.succeed([] as bigtable.MaterializedView[])),
250 ),
251 { concurrency: 4 },
252 );
253 return pages.flat().map((view) => toAttrs(view, env.project));
254 }),
255
256 reconcile: Effect.fn(function* ({ id, news, output }) {
257 const env = yield* GcpEnvironment.current;
258 const materializedViewId = yield* toId(
259 id,
260 news.materializedViewId,
261 output?.materializedViewId,
262 );
263 const parent = instanceNameOf(env.project, news.instance);
264 const name = `${parent}/materializedViews/${materializedViewId}`;
265 const desiredProtection = news.deletionProtection === true;
266
267 let current = yield* getByName(output?.name ?? name);
268
269 if (current === undefined) {
270 const created = yield* bigtable
271 .createProjectsInstancesMaterializedViews({
272 parent,
273 materializedViewId,
274 body: {
275 query: news.query,
276 deletionProtection: desiredProtection,
277 },
278 })
279 .pipe(
280 // A just-created source table rejects views for a short while.
281 Effect.retry({
282 while: (error) => error._tag === "SourceTableNotReady",
283 times: 8,
284 schedule: Schedule.spaced("5 seconds"),
285 }),
286 Effect.catchTag("Conflict", () => Effect.undefined),
287 );
288 if (created !== undefined) {
289 yield* waitForOperation(created, { alreadyExistsOk: true });
290 }
291 current = yield* waitUntilExists(name);
292 }
293
294 if ((current.deletionProtection === true) !== desiredProtection) {
295 const patched = yield* bigtable
296 .patchProjectsInstancesMaterializedViews({
297 name,
298 updateMask: "deletion_protection",
299 body: {
300 deletionProtection: desiredProtection,
301 etag: current.etag,
302 },
303 })
304 .pipe(
305 Effect.retry({
306 while: (error) => error._tag === "Conflict",
307 times: 8,
308 schedule: Schedule.spaced("1 second"),
309 }),
310 );
311 yield* waitForOperation(patched);
312 current = yield* getByName(name);
313 if (current === undefined) {
314 return yield* new MaterializedViewNotResolved({ resourceName: name });
315 }
316 }
317
318 return toAttrs(current, env.project);
319 }),
320
321 delete: Effect.fn(function* ({ output }) {
322 const current = yield* getByName(output.name);
323 if (current === undefined) return;
324 if (current.deletionProtection === true) {
325 yield* unprotect(output.name, current.etag);
326 }
327 yield* bigtable
328 .deleteProjectsInstancesMaterializedViews({
329 name: output.name,
330 })
331 .pipe(
332 Effect.catchTag("NotFound", () => Effect.void),
333 Effect.retry({
334 while: (error) => error._tag === "Conflict",
335 times: 8,
336 schedule: Schedule.spaced("2 seconds"),
337 }),
338 );
339 yield* waitUntilGone(output.name);
340 }),
341 });