@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 133 lines 5.18 KB
Raw
1 import * as athena from "@distilled.cloud/aws/athena";
2 import * as Output from "alchemy/Output";
3 import type { Output as OutputType } from "alchemy/Output";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Schedule from "effect/Schedule";
7 import * as AWSBindings from "../AWSBindings.ts";
8 import type { Bucket } from "../S3/Bucket.ts";
9 import { AthenaQueryFailed, Query, type QueryResult, type RunQueryRequest } from "./Query.ts";
10 import type { WorkGroup } from "./WorkGroup.ts";
11
12 // The workgroup ARN is `arn:aws:athena:{region}:{account}:workgroup/{name}` —
13 // the Glue Data Catalog ARNs share that partition/region/account.
14 const glueBase = (workGroupArn: OutputType<string>) =>
15 workGroupArn.pipe(
16 Output.map((arn) => {
17 const [, partition, , region, account] = arn.split(":");
18 return `arn:${partition}:glue:${region}:${account}`;
19 }),
20 );
21 const glueCatalogArn = (workGroupArn: OutputType<string>) =>
22 glueBase(workGroupArn).pipe(Output.map((base) => `${base}:catalog`));
23 const glueDatabaseArn = (workGroupArn: OutputType<string>) =>
24 glueBase(workGroupArn).pipe(Output.map((base) => `${base}:database/*`));
25 const glueTableArn = (workGroupArn: OutputType<string>) =>
26 glueBase(workGroupArn).pipe(Output.map((base) => `${base}:table/*/*`));
27
28 export const QueryHttp = Layer.effect(
29 Query,
30 Effect.gen(function* () {
31 const bind = yield* AWSBindings.Bind;
32 const startQueryExecution = yield* AWSBindings.withCredentials(athena.startQueryExecution);
33 const getQueryExecution = yield* AWSBindings.withCredentials(athena.getQueryExecution);
34 const getQueryResults = yield* AWSBindings.withCredentials(athena.getQueryResults);
35
36 return Effect.fn(function* (workGroup: WorkGroup, resultsBucket: Bucket) {
37 const WorkGroupName = yield* workGroup.workGroupName;
38 if (!globalThis.__ALCHEMY_RUNTIME__) {
39 yield* bind`AWS.Athena.Query(${workGroup}, ${resultsBucket})`({
40 policyStatements: [
41 {
42 Effect: "Allow",
43 Action: [
44 "athena:StartQueryExecution",
45 "athena:GetQueryExecution",
46 "athena:GetQueryResults",
47 "athena:StopQueryExecution",
48 ],
49 Resource: [workGroup.workGroupArn],
50 },
51 {
52 Effect: "Allow",
53 Action: ["s3:GetBucketLocation", "s3:ListBucket"],
54 Resource: [resultsBucket.bucketArn],
55 },
56 {
57 Effect: "Allow",
58 Action: ["s3:GetObject", "s3:PutObject", "s3:AbortMultipartUpload"],
59 Resource: [Output.interpolate`${resultsBucket.bucketArn}/*`],
60 },
61 {
62 // Athena resolves table metadata through the Glue Data
63 // Catalog on the caller's behalf — reads are required for any
64 // query over a Glue-backed table. Scoped to the workgroup's
65 // account/region catalog (derived from its ARN).
66 Effect: "Allow",
67 Action: [
68 "glue:GetDatabase",
69 "glue:GetDatabases",
70 "glue:GetTable",
71 "glue:GetTables",
72 "glue:GetPartition",
73 "glue:GetPartitions",
74 ],
75 Resource: [
76 glueCatalogArn(workGroup.workGroupArn),
77 glueDatabaseArn(workGroup.workGroupArn),
78 glueTableArn(workGroup.workGroupArn),
79 ],
80 },
81 ],
82 });
83 }
84 return Effect.fn(`AWS.Athena.Query(${workGroup.LogicalId})`)(function* (
85 request: RunQueryRequest,
86 ) {
87 const workGroupName = yield* WorkGroupName;
88 const started = yield* startQueryExecution({
89 ...request,
90 WorkGroup: workGroupName,
91 });
92 const queryExecutionId = started.QueryExecutionId!;
93
94 // Poll GetQueryExecution until terminal — bounded to ~90s so a hung
95 // query fails fast instead of hitting the caller's timeout.
96 const exec = yield* getQueryExecution({
97 QueryExecutionId: queryExecutionId,
98 }).pipe(
99 Effect.map((r) => r.QueryExecution),
100 Effect.repeat({
101 schedule: Schedule.spaced("1 second"),
102 until: (qe) => {
103 const s = qe?.Status?.State;
104 return s === "SUCCEEDED" || s === "FAILED" || s === "CANCELLED";
105 },
106 times: 90,
107 }),
108 );
109
110 const state = exec?.Status?.State ?? "FAILED";
111 const reason = exec?.Status?.StateChangeReason;
112 if (state !== "SUCCEEDED") {
113 return yield* new AthenaQueryFailed({ queryExecutionId, state, reason });
114 }
115
116 const results = yield* getQueryResults({
117 QueryExecutionId: queryExecutionId,
118 });
119 const columns = (results.ResultSet?.ResultSetMetadata?.ColumnInfo ?? []).map((c) => c.Name);
120 const rows = (results.ResultSet?.Rows ?? []).map((row) =>
121 (row.Data ?? []).map((d) => d.VarCharValue ?? ""),
122 );
123 return {
124 queryExecutionId,
125 state,
126 stateChangeReason: reason,
127 columns,
128 rows,
129 } satisfies QueryResult;
130 });
131 });
132 }),
133 );