@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 122 lines 3.98 KB
Raw
1 import type * as Effect from "effect/Effect";
2 import type * as Stream from "effect/Stream";
3 import {
4 consumeBusEvents,
5 type EventRecord,
6 type EventRouteProps,
7 } from "../EventBridge/EventSource.ts";
8
9 /**
10 * A query execution state, as reported in the EventBridge event detail.
11 */
12 export type QueryState = "QUEUED" | "RUNNING" | "SUCCEEDED" | "FAILED" | "CANCELLED";
13
14 /**
15 * The `detail` payload Athena delivers to EventBridge on every query
16 * execution state transition (`Athena Query State Change`).
17 */
18 export interface QueryStateChangeDetail {
19 /** The state the query transitioned into. */
20 currentState: QueryState;
21 /** The state the query transitioned out of (absent on the first event). */
22 previousState?: QueryState;
23 /** Id of the query execution. */
24 queryExecutionId: string;
25 /** Statement type: `DDL`, `DML`, or `UTILITY`. */
26 statementType?: string;
27 /** Name of the workgroup the query ran in. */
28 workgroupName?: string;
29 /** Monotonic sequence number of this transition within the execution. */
30 sequenceNumber?: string;
31 /** Version of the event schema. */
32 versionId?: string;
33 /** Error details when `currentState` is `FAILED`. */
34 athenaError?: {
35 errorCategory?: number;
36 errorType?: number;
37 errorMessage?: string;
38 retryable?: boolean;
39 };
40 /** Additional event fields (the schema grows over time). */
41 [key: string]: unknown;
42 }
43
44 /** An Athena query state change EventBridge event delivered to the handler. */
45 export type QueryStateChangeEvent = EventRecord<QueryStateChangeDetail>;
46
47 export interface QueryStateChangesProps extends EventRouteProps {
48 /**
49 * Logical id for the backing EventBridge rule.
50 * @default "AthenaQueryStateChanges"
51 */
52 id?: string;
53 /**
54 * Only deliver transitions into these states (e.g. `["SUCCEEDED",
55 * "FAILED"]` for terminal outcomes).
56 * @default all states
57 */
58 states?: QueryState[];
59 /**
60 * Only deliver events for queries that ran in these workgroups (by name).
61 * @default all workgroups
62 */
63 workGroups?: string[];
64 }
65
66 /**
67 * Event source connecting Athena query state changes to the hosting compute.
68 * Athena publishes every query execution state transition to the account's
69 * default EventBridge bus (source `aws.athena`, detail-type `Athena Query
70 * State Change`); this subscribes the host Function to those events so it can
71 * react to completed or failed queries without polling.
72 *
73 * Athena publishes to EventBridge automatically — no additional resource is
74 * created besides the EventBridge rule targeting the host. Provide the
75 * host-specific implementation layer (e.g. `AWS.Lambda.EventSource`) on the
76 * Function effect.
77 *
78 * ### Consuming Query State Changes
79 * **Example:** React to Terminal Query Outcomes
80 * ```typescript
81 * import * as AWS from "@alchemy.run/aws";
82 *
83 * export default EtlFunction.make(
84 * { main: import.meta.url },
85 * Effect.gen(function* () {
86 * yield* AWS.Athena.consumeQueryStateChanges(
87 * { states: ["SUCCEEDED", "FAILED"] },
88 * (events) =>
89 * Stream.runForEach(events, (event) =>
90 * Effect.log(
91 * `query ${event.detail.queryExecutionId} → ${event.detail.currentState}`,
92 * ),
93 * ),
94 * );
95 * return {};
96 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
97 * );
98 * ```
99 */
100 export const consumeQueryStateChanges = <StreamReq = never, Req = never>(
101 props: QueryStateChangesProps,
102 process: (
103 events: Stream.Stream<QueryStateChangeEvent, never, StreamReq>,
104 ) => Effect.Effect<void, never, Req>,
105 ) =>
106 consumeBusEvents(
107 props.id ?? "AthenaQueryStateChanges",
108 {
109 source: ["aws.athena"],
110 "detail-type": ["Athena Query State Change"],
111 ...(props.states || props.workGroups
112 ? {
113 detail: {
114 ...(props.states ? { currentState: [...props.states] } : {}),
115 ...(props.workGroups ? { workgroupName: [...props.workGroups] } : {}),
116 },
117 }
118 : {}),
119 },
120 { description: props.description, state: props.state },
121 process,
122 );