| 1 | import * as Lambda from "@/AWS/Lambda"; |
| 2 | import * as Timestream from "@/AWS/Timestream"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 7 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 8 | import path from "pathe"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 11 | |
| 12 | export class TimestreamTestFunction extends Lambda.Function<Lambda.Function>()( |
| 13 | "TimestreamTestFunction", |
| 14 | ) {} |
| 15 | |
| 16 | export default TimestreamTestFunction.make( |
| 17 | { |
| 18 | main, |
| 19 | functionUrl: true, |
| 20 | timeout: Duration.seconds(30), |
| 21 | }, |
| 22 | Effect.gen(function* () { |
| 23 | const database = yield* Timestream.Database("Metrics"); |
| 24 | const table = yield* Timestream.Table("Cpu", { |
| 25 | databaseName: database.databaseName, |
| 26 | }); |
| 27 | const DatabaseName = yield* database.databaseName; |
| 28 | const TableName = yield* table.tableName; |
| 29 | |
| 30 | const writeRecords = yield* Timestream.WriteRecords(table); |
| 31 | const query = yield* Timestream.Query(table); |
| 32 | const prepareQuery = yield* Timestream.PrepareQuery(table); |
| 33 | |
| 34 | return { |
| 35 | fetch: Effect.gen(function* () { |
| 36 | const request = yield* HttpServerRequest; |
| 37 | const pathname = new URL(request.originalUrl).pathname; |
| 38 | |
| 39 | if (request.method === "POST" && pathname === "/write") { |
| 40 | const body = (yield* request.json) as unknown as { |
| 41 | host: string; |
| 42 | value: string; |
| 43 | }; |
| 44 | const result = yield* writeRecords({ |
| 45 | Records: [ |
| 46 | { |
| 47 | Dimensions: [{ Name: "host", Value: body.host }], |
| 48 | MeasureName: "cpu", |
| 49 | MeasureValue: body.value, |
| 50 | MeasureValueType: "DOUBLE", |
| 51 | Time: `${Date.now()}`, |
| 52 | TimeUnit: "MILLISECONDS", |
| 53 | }, |
| 54 | ], |
| 55 | }); |
| 56 | return yield* HttpServerResponse.json({ |
| 57 | recordsIngested: result.RecordsIngested, |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | if (request.method === "GET" && pathname === "/prepare") { |
| 62 | const result = yield* prepareQuery({ |
| 63 | QueryString: `SELECT COUNT(*) AS c FROM "${DatabaseName}"."${TableName}"`, |
| 64 | ValidateOnly: true, |
| 65 | }); |
| 66 | return yield* HttpServerResponse.json({ |
| 67 | columns: result.Columns, |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | if (request.method === "GET" && pathname === "/query") { |
| 72 | const result = yield* query({ |
| 73 | QueryString: `SELECT COUNT(*) AS c FROM "${DatabaseName}"."${TableName}"`, |
| 74 | }); |
| 75 | return yield* HttpServerResponse.json({ |
| 76 | rows: result.Rows, |
| 77 | columns: result.ColumnInfo, |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | return yield* HttpServerResponse.json( |
| 82 | { error: "Not found", method: request.method, pathname }, |
| 83 | { status: 404 }, |
| 84 | ); |
| 85 | }).pipe(Effect.orDie), |
| 86 | }; |
| 87 | }).pipe( |
| 88 | Effect.provide( |
| 89 | Layer.mergeAll( |
| 90 | Timestream.WriteRecordsHttp, |
| 91 | Timestream.QueryHttp, |
| 92 | Timestream.PrepareQueryHttp, |
| 93 | ), |
| 94 | ), |
| 95 | ), |
| 96 | ); |