The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | import { decodeOrderEvent, OrderEvents, OrderEventsTable } from "./resources.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Records every order event, of every type, as a row in BigQuery. |
| 9 | * |
| 10 | * No filter: this subscription receives everything published to the |
| 11 | * topic, including the events the email consumer never sees and the |
| 12 | * ones it dead-letters. |
| 13 | */ |
| 14 | export default class Analytics extends GCP.Function<Analytics>()( |
| 15 | "Analytics", |
| 16 | { |
| 17 | main: import.meta.url, |
| 18 | }, |
| 19 | Effect.gen(function* () { |
| 20 | const table = yield* GCP.BigQuery.WriteTable(OrderEventsTable); |
| 21 | |
| 22 | yield* GCP.PubSub.consumeTopicMessages(OrderEvents, (messages) => |
| 23 | messages.pipe( |
| 24 | Stream.runForEach(({ message }) => |
| 25 | Effect.gen(function* () { |
| 26 | const event = yield* decodeOrderEvent(message.data); |
| 27 | yield* table.insert( |
| 28 | [ |
| 29 | { |
| 30 | eventId: event.eventId, |
| 31 | type: event.type, |
| 32 | orderId: event.orderId, |
| 33 | email: event.email, |
| 34 | total: event.total, |
| 35 | occurredAt: new Date(event.occurredAt), |
| 36 | receivedAt: new Date(), |
| 37 | }, |
| 38 | ], |
| 39 | // BigQuery drops a repeated insertId within its dedup |
| 40 | // window, so a redelivered event does not add a second row. |
| 41 | { insertIds: [event.eventId] }, |
| 42 | ); |
| 43 | yield* Effect.log(`analytics: ${event.type} ${event.orderId}`); |
| 44 | }), |
| 45 | ), |
| 46 | Effect.orDie, |
| 47 | ), |
| 48 | ); |
| 49 | |
| 50 | return { |
| 51 | fetch: Effect.succeed(HttpServerResponse.text("ok")), |
| 52 | }; |
| 53 | }).pipe(Effect.provide([GCP.Run.TopicEventSource, GCP.BigQuery.WriteTableHttp])), |
| 54 | ) {} |