@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, 06:00 UTC

main
ts 374 lines 13.1 KB
Raw
1 import * as analyticshub from "@distilled.cloud/gcp/analyticshub_v1";
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 Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 AnalyticshubNotResolved,
12 deleteRetry,
13 displayNameOf,
14 encodeDescription,
15 hasOwnershipMarker,
16 listExchangesInProject,
17 listListings,
18 listQueryTemplates,
19 locationParent,
20 missingGet,
21 normalizeLocation,
22 ownedById,
23 parseDescription,
24 parseResourceName,
25 replaceOnIdentity,
26 retryTransient,
27 sameBool,
28 sameText,
29 sharingKind,
30 toPhysicalId,
31 updateMaskOf,
32 ownershipLabels,
33 } from "./internal.ts";
34
35 export type SharingEnvironmentConfig = analyticshub.SharingEnvironmentConfig;
36 export type DataExchangeDiscoveryType = analyticshub.DataExchangeDiscoveryTypeEnum | (string & {});
37
38 export type DataExchangeProps = {
39 /**
40 * Data exchange id (the `{dataExchange}` segment of
41 * `projects/{project}/locations/{location}/dataExchanges/{dataExchange}`).
42 * If omitted, a unique id is generated. Must contain only letters,
43 * numbers, and underscores; max 100 bytes. Immutable — changing it
44 * replaces the exchange.
45 */
46 dataExchangeId?: string;
47 /**
48 * BigQuery location (`us-central1`, `US`, `EU`, …). Immutable —
49 * changing it replaces the exchange.
50 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
51 */
52 location?: string;
53 /**
54 * Human-readable display name. Letters, numbers, underscores, dashes,
55 * spaces, and ampersands. Max 63 bytes. Defaults to the exchange id.
56 */
57 displayName?: string;
58 /**
59 * Human-readable description (max 2000 bytes). Analytics Hub data
60 * exchanges have no labels field, so Alchemy ownership is stored in a
61 * `[alchemy …]` prefix and stripped from attributes.
62 */
63 description?: string;
64 /**
65 * Documentation describing the data exchange.
66 */
67 documentation?: string;
68 /**
69 * Email or URL of the primary point of contact. Max 1000 bytes.
70 */
71 primaryContact?: string;
72 /**
73 * Base64-encoded icon. Max 3.0 MiB encoded.
74 */
75 icon?: string;
76 /**
77 * When true, query jobs against linked datasets record the subscriber
78 * email in the logs.
79 * @default false
80 */
81 logLinkedDatasetQueryUserEmail?: boolean;
82 /**
83 * Discovery visibility for listings under this exchange
84 * (`DISCOVERY_TYPE_PRIVATE`, `DISCOVERY_TYPE_PUBLIC`). Updating this
85 * also overwrites `discoveryType` on every listing in the exchange.
86 */
87 discoveryType?: DataExchangeDiscoveryType;
88 /**
89 * Sharing environment. Set `dcrExchangeConfig` for a data clean room,
90 * or `defaultExchangeConfig` for a standard exchange. Switching kinds
91 * replaces the exchange.
92 */
93 sharingEnvironmentConfig?: SharingEnvironmentConfig;
94 };
95
96 export type DataExchange = Resource<
97 "GCP.AnalyticsHub.DataExchange",
98 DataExchangeProps,
99 {
100 /** Full resource name. */
101 name: string;
102 /** Data exchange id (last path segment). */
103 dataExchangeId: string;
104 /** Project id. */
105 project: string;
106 /** Location id. */
107 location: string;
108 /** Display name. */
109 displayName: string | undefined;
110 /** User description (Alchemy ownership marker stripped). */
111 description: string | undefined;
112 /** Documentation. */
113 documentation: string | undefined;
114 /** Primary contact. */
115 primaryContact: string | undefined;
116 /** Base64-encoded icon. */
117 icon: string | undefined;
118 /** Whether linked-dataset query emails are logged. */
119 logLinkedDatasetQueryUserEmail: boolean;
120 /** Discovery type. */
121 discoveryType: string | undefined;
122 /** Sharing environment. */
123 sharingEnvironmentConfig: SharingEnvironmentConfig | undefined;
124 /** Number of listings in the exchange. */
125 listingCount: number | undefined;
126 },
127 never,
128 Providers
129 >;
130
131 /**
132 * A BigQuery Analytics Hub data exchange — a container for listings
133 * that share datasets or Pub/Sub topics with subscribers.
134 *
135 * Location and id are immutable. Display name, description,
136 * documentation, contact, discovery type, and email logging update in
137 * place. Switching between a default exchange and a data clean room
138 * replaces the exchange.
139 *
140 * Data exchanges have no labels. Alchemy stamps
141 * `alchemy-stack` / `alchemy-stage` / `alchemy-id` into the description
142 * so `list` and `pnpm nuke:gcp` can identify owned exchanges.
143 *
144 * ### Creating a Data Exchange
145 * **Example:** Generated id
146 * ```typescript
147 * const exchange = yield* GCP.AnalyticsHub.DataExchange("Marketplace", {
148 * displayName: "Marketplace",
149 * description: "shared datasets",
150 * });
151 * ```
152 *
153 * **Example:** Named exchange with a contact
154 * ```typescript
155 * const exchange = yield* GCP.AnalyticsHub.DataExchange("Marketplace", {
156 * dataExchangeId: "marketplace",
157 * location: "us-central1",
158 * displayName: "Marketplace",
159 * primaryContact: "data@example.com",
160 * });
161 * ```
162 *
163 * ### Data Clean Rooms
164 * **Example:** Create a data clean room exchange
165 * ```typescript
166 * const dcr = yield* GCP.AnalyticsHub.DataExchange("CleanRoom", {
167 * displayName: "Clean Room",
168 * sharingEnvironmentConfig: { dcrExchangeConfig: {} },
169 * });
170 * ```
171 *
172 * @resource
173 * @category AnalyticsHub
174 */
175 export const DataExchange = Resource<DataExchange>("GCP.AnalyticsHub.DataExchange");
176
177 const resourceName = (project: string, location: string, dataExchangeId: string) =>
178 `${locationParent(project, location)}/dataExchanges/${dataExchangeId}`;
179
180 const toAttrs = (exchange: analyticshub.DataExchange, project: string) => {
181 const name = exchange.name ?? "";
182 const parsed = parseResourceName(name, "dataExchanges");
183 const { description } = parseDescription(exchange.description);
184 return {
185 name,
186 dataExchangeId: parsed.id,
187 project: parsed.project || project,
188 location: parsed.location,
189 displayName: exchange.displayName,
190 description,
191 documentation: exchange.documentation,
192 primaryContact: exchange.primaryContact,
193 icon: exchange.icon,
194 logLinkedDatasetQueryUserEmail: exchange.logLinkedDatasetQueryUserEmail === true,
195 discoveryType: exchange.discoveryType,
196 sharingEnvironmentConfig: exchange.sharingEnvironmentConfig,
197 listingCount: exchange.listingCount,
198 };
199 };
200
201 const getByName = missingGet;
202
203 export const DataExchangeProvider = () =>
204 Provider.succeed(DataExchange, {
205 nuke: {
206 dependsOn: ["GCP.BigQuery.Dataset"],
207 },
208 stables: ["name", "dataExchangeId", "project", "location"],
209
210 diff: Effect.fn(function* ({ news, olds, output }) {
211 if (!isResolved(news)) return undefined;
212 const env = yield* GcpEnvironment.current;
213 const previousKind = sharingKind(
214 olds?.sharingEnvironmentConfig ?? output?.sharingEnvironmentConfig,
215 );
216 const nextKind =
217 news.sharingEnvironmentConfig !== undefined
218 ? sharingKind(news.sharingEnvironmentConfig)
219 : previousKind;
220 return replaceOnIdentity({
221 previousId: olds?.dataExchangeId ?? output?.dataExchangeId,
222 nextId: news.dataExchangeId,
223 previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region),
224 nextLocation: normalizeLocation(
225 news.location ?? olds?.location ?? output?.location,
226 env.region,
227 ),
228 extra: news.sharingEnvironmentConfig !== undefined && nextKind !== previousKind,
229 });
230 }),
231
232 read: Effect.fn(function* ({ id, olds, output }) {
233 const env = yield* GcpEnvironment.current;
234 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
235 const dataExchangeId = yield* toPhysicalId(id, olds?.dataExchangeId, output?.dataExchangeId);
236 const name = output?.name ?? resourceName(env.project, location, dataExchangeId);
237 const existing = yield* getByName(name);
238 if (existing === undefined) return undefined;
239 const attrs = toAttrs(existing, env.project);
240 return (yield* ownedById(id, existing.description)) ? attrs : Unowned(attrs);
241 }),
242
243 list: () =>
244 Effect.gen(function* () {
245 const env = yield* GcpEnvironment.current;
246 const items = yield* listExchangesInProject(env.project, env.region);
247 return items
248 .filter((item) => hasOwnershipMarker(item.description))
249 .map((item) => toAttrs(item, env.project));
250 }),
251
252 reconcile: Effect.fn(function* ({ id, news, output }) {
253 const env = yield* GcpEnvironment.current;
254 const location = normalizeLocation(news.location ?? output?.location, env.region);
255 const parent = locationParent(env.project, location);
256 const dataExchangeId = yield* toPhysicalId(id, news.dataExchangeId, output?.dataExchangeId);
257 const name = output?.name ?? resourceName(env.project, location, dataExchangeId);
258 const ownership = yield* ownershipLabels(id);
259 const desiredDescription = encodeDescription(ownership, news.description);
260 const displayName = displayNameOf(news.displayName, dataExchangeId);
261 const body: analyticshub.DataExchange = {
262 displayName,
263 description: desiredDescription,
264 documentation: news.documentation,
265 primaryContact: news.primaryContact,
266 icon: news.icon,
267 logLinkedDatasetQueryUserEmail:
268 news.logLinkedDatasetQueryUserEmail === true ? true : undefined,
269 discoveryType: news.discoveryType,
270 sharingEnvironmentConfig: news.sharingEnvironmentConfig,
271 };
272
273 let current = yield* getByName(name);
274
275 if (current === undefined) {
276 const created = yield* retryTransient(
277 analyticshub.createProjectsLocationsDataExchanges({
278 parent,
279 dataExchangeId,
280 body,
281 }),
282 ).pipe(Effect.catchTag("Conflict", () => getByName(name)));
283 current = created ?? undefined;
284 }
285
286 if (current === undefined) {
287 return yield* new AnalyticshubNotResolved({ resourceName: name });
288 }
289
290 const currentName = current.name ?? name;
291 const displayChanged = !sameText(current.displayName, displayName);
292 const descriptionChanged = !sameText(current.description, desiredDescription);
293 const documentationChanged = !sameText(current.documentation, news.documentation);
294 const contactChanged = !sameText(current.primaryContact, news.primaryContact);
295 const iconChanged = !sameText(current.icon, news.icon);
296 const emailChanged =
297 news.logLinkedDatasetQueryUserEmail !== undefined &&
298 !sameBool(current.logLinkedDatasetQueryUserEmail, news.logLinkedDatasetQueryUserEmail);
299 const discoveryChanged =
300 news.discoveryType !== undefined && !sameText(current.discoveryType, news.discoveryType);
301 const sharingChanged =
302 news.sharingEnvironmentConfig !== undefined &&
303 sharingKind(current.sharingEnvironmentConfig) !==
304 sharingKind(news.sharingEnvironmentConfig);
305
306 if (
307 displayChanged ||
308 descriptionChanged ||
309 documentationChanged ||
310 contactChanged ||
311 iconChanged ||
312 emailChanged ||
313 discoveryChanged ||
314 sharingChanged
315 ) {
316 current = yield* retryTransient(
317 analyticshub.patchProjectsLocationsDataExchanges({
318 name: currentName,
319 updateMask: updateMaskOf(
320 displayChanged ? "displayName" : undefined,
321 descriptionChanged ? "description" : undefined,
322 documentationChanged ? "documentation" : undefined,
323 contactChanged ? "primaryContact" : undefined,
324 iconChanged ? "icon" : undefined,
325 emailChanged ? "logLinkedDatasetQueryUserEmail" : undefined,
326 discoveryChanged ? "discoveryType" : undefined,
327 sharingChanged ? "sharingEnvironmentConfig" : undefined,
328 ),
329 body: { name: currentName, ...body },
330 }),
331 );
332 }
333
334 return toAttrs(current, env.project);
335 }),
336
337 delete: Effect.fn(function* ({ output }) {
338 const listings = yield* listListings(output.name);
339 yield* Effect.forEach(
340 listings.filter((listing) => (listing.name ?? "").length > 0),
341 (listing) =>
342 deleteRetry(
343 analyticshub.deleteProjectsLocationsDataExchangesListings({
344 name: listing.name!,
345 deleteCommercial: true,
346 }),
347 ),
348 { concurrency: 2 },
349 );
350 yield* listListings(output.name).pipe(
351 Effect.repeat({
352 schedule: Schedule.spaced("2 seconds"),
353 until: (items) => items.length === 0,
354 times: 12,
355 }),
356 );
357 const templates = yield* listQueryTemplates(output.name);
358 yield* Effect.forEach(
359 templates.filter((template) => (template.name ?? "").length > 0),
360 (template) =>
361 deleteRetry(
362 analyticshub.deleteProjectsLocationsDataExchangesQueryTemplates({
363 name: template.name!,
364 }),
365 ),
366 { concurrency: 2 },
367 );
368 yield* deleteRetry(
369 analyticshub.deleteProjectsLocationsDataExchanges({
370 name: output.name,
371 }),
372 );
373 }),
374 });