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
| 1 | import * as quicksight from "@distilled.cloud/aws/quicksight"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { createInternalTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { AWSEnvironment } from "../Environment.ts"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { readQuickSightTags, syncQuickSightTags, toWireTags } from "./internal.ts"; |
| 14 | |
| 15 | export class QuickSightDataSetUnexpectedResponse extends Data.TaggedError( |
| 16 | "QuickSightDataSetUnexpectedResponse", |
| 17 | )<{ |
| 18 | readonly message: string; |
| 19 | readonly cause?: unknown; |
| 20 | }> {} |
| 21 | |
| 22 | /** |
| 23 | * Properties for an Amazon QuickSight dataset — a prepared, queryable model |
| 24 | * built on top of one or more data sources. |
| 25 | */ |
| 26 | export interface DataSetProps { |
| 27 | /** |
| 28 | * Unique id of the dataset within the account. Stable — changing it |
| 29 | * replaces the dataset. If omitted, a unique id is generated. |
| 30 | */ |
| 31 | dataSetId?: string; |
| 32 | |
| 33 | /** |
| 34 | * Display name of the dataset. |
| 35 | */ |
| 36 | name: string; |
| 37 | |
| 38 | /** |
| 39 | * Declares the physical tables in the dataset, keyed by a physical table |
| 40 | * id. Each maps to a relational table, custom SQL, or an S3 source. |
| 41 | */ |
| 42 | physicalTableMap: { [key: string]: quicksight.PhysicalTable | undefined }; |
| 43 | |
| 44 | /** |
| 45 | * Whether the data is imported into SPICE or queried directly. |
| 46 | */ |
| 47 | importMode: quicksight.DataSetImportMode; |
| 48 | |
| 49 | /** |
| 50 | * Declares logical tables (joins, transforms) keyed by logical table id. |
| 51 | */ |
| 52 | logicalTableMap?: { [key: string]: quicksight.LogicalTable | undefined }; |
| 53 | |
| 54 | /** |
| 55 | * Column groupings (e.g. geospatial hierarchies). |
| 56 | */ |
| 57 | columnGroups?: quicksight.ColumnGroup[]; |
| 58 | |
| 59 | /** |
| 60 | * Field folders that organize the dataset's fields. |
| 61 | */ |
| 62 | fieldFolders?: { [key: string]: quicksight.FieldFolder | undefined }; |
| 63 | |
| 64 | /** |
| 65 | * Resource-level permissions on the dataset. |
| 66 | */ |
| 67 | permissions?: quicksight.ResourcePermission[]; |
| 68 | |
| 69 | /** |
| 70 | * Row-level permission configuration backed by a permissions dataset. |
| 71 | */ |
| 72 | rowLevelPermissionDataSet?: quicksight.RowLevelPermissionDataSet; |
| 73 | |
| 74 | /** |
| 75 | * Usage configuration controlling how the dataset can be used |
| 76 | * (e.g. as a source for other datasets). |
| 77 | */ |
| 78 | dataSetUsageConfiguration?: quicksight.DataSetUsageConfiguration; |
| 79 | |
| 80 | /** |
| 81 | * Parameters exposed by the dataset. |
| 82 | */ |
| 83 | datasetParameters?: quicksight.DatasetParameter[]; |
| 84 | |
| 85 | /** |
| 86 | * Tags to apply to the dataset. |
| 87 | */ |
| 88 | tags?: Record<string, string>; |
| 89 | } |
| 90 | |
| 91 | export interface DataSet extends Resource< |
| 92 | "AWS.QuickSight.DataSet", |
| 93 | DataSetProps, |
| 94 | { |
| 95 | /** Unique id of the dataset within the account. */ |
| 96 | dataSetId: string; |
| 97 | /** ARN of the dataset. */ |
| 98 | arn: string; |
| 99 | /** Display name of the dataset. */ |
| 100 | name: string; |
| 101 | }, |
| 102 | never, |
| 103 | Providers |
| 104 | > {} |
| 105 | |
| 106 | /** |
| 107 | * An Amazon QuickSight dataset — a prepared, queryable model built on top of |
| 108 | * one or more data sources. |
| 109 | * |
| 110 | * QuickSight requires an active account subscription in the region. Without |
| 111 | * one, create operations fail with the typed `QuickSightSubscriptionRequired` |
| 112 | * error. |
| 113 | * |
| 114 | * ### Creating a Dataset |
| 115 | * **Example:** SPICE Dataset from a Relational Table |
| 116 | * ```typescript |
| 117 | * const dataset = yield* DataSet("sales", { |
| 118 | * name: "Sales", |
| 119 | * importMode: "SPICE", |
| 120 | * physicalTableMap: { |
| 121 | * sales: { |
| 122 | * RelationalTable: { |
| 123 | * DataSourceArn: source.arn, |
| 124 | * Name: "sales", |
| 125 | * InputColumns: [{ Name: "amount", Type: "DECIMAL" }], |
| 126 | * }, |
| 127 | * }, |
| 128 | * }, |
| 129 | * }); |
| 130 | * ``` |
| 131 | * |
| 132 | * @resource |
| 133 | */ |
| 134 | export const DataSet = Resource<DataSet>("AWS.QuickSight.DataSet"); |
| 135 | |
| 136 | export const DataSetProvider = () => |
| 137 | Provider.effect( |
| 138 | DataSet, |
| 139 | Effect.gen(function* () { |
| 140 | const toId = (id: string, props: Partial<DataSetProps>) => |
| 141 | props.dataSetId |
| 142 | ? Effect.succeed(props.dataSetId) |
| 143 | : createPhysicalName({ id, maxLength: 64 }); |
| 144 | |
| 145 | const readSet = Effect.fn(function* (accountId: string, dataSetId: string) { |
| 146 | const response = yield* quicksight |
| 147 | .describeDataSet({ AwsAccountId: accountId, DataSetId: dataSetId }) |
| 148 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 149 | return response?.DataSet; |
| 150 | }); |
| 151 | |
| 152 | const toAttrs = (set: quicksight.DataSet) => ({ |
| 153 | dataSetId: set.DataSetId!, |
| 154 | arn: set.Arn!, |
| 155 | name: set.Name ?? "", |
| 156 | }); |
| 157 | |
| 158 | return DataSet.Provider.of({ |
| 159 | stables: ["dataSetId", "arn"], |
| 160 | |
| 161 | diff: Effect.fn(function* ({ id, olds = {}, news }) { |
| 162 | if (!isResolved(news)) return undefined; |
| 163 | if ((yield* toId(id, olds)) !== (yield* toId(id, news))) { |
| 164 | return { action: "replace" } as const; |
| 165 | } |
| 166 | }), |
| 167 | |
| 168 | read: Effect.fn(function* ({ id, olds = {}, output }) { |
| 169 | const { accountId } = yield* AWSEnvironment.current; |
| 170 | const dataSetId = output?.dataSetId ?? (yield* toId(id, olds)); |
| 171 | const set = yield* readSet(accountId, dataSetId); |
| 172 | if (set === undefined) return undefined; |
| 173 | const attrs = toAttrs(set); |
| 174 | const tags = yield* readQuickSightTags(attrs.arn); |
| 175 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 176 | }), |
| 177 | |
| 178 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 179 | const { accountId } = yield* AWSEnvironment.current; |
| 180 | const dataSetId = output?.dataSetId ?? (yield* toId(id, news)); |
| 181 | const internalTags = yield* createInternalTags(id); |
| 182 | const desiredTags = { ...internalTags, ...news.tags }; |
| 183 | |
| 184 | // 1. Observe. |
| 185 | let observed = yield* readSet(accountId, dataSetId); |
| 186 | |
| 187 | // 2. Ensure — create if missing (tolerate AlreadyExists race). |
| 188 | if (observed === undefined) { |
| 189 | yield* quicksight |
| 190 | .createDataSet({ |
| 191 | AwsAccountId: accountId, |
| 192 | DataSetId: dataSetId, |
| 193 | Name: news.name, |
| 194 | PhysicalTableMap: news.physicalTableMap, |
| 195 | ImportMode: news.importMode, |
| 196 | LogicalTableMap: news.logicalTableMap, |
| 197 | ColumnGroups: news.columnGroups, |
| 198 | FieldFolders: news.fieldFolders, |
| 199 | Permissions: news.permissions, |
| 200 | RowLevelPermissionDataSet: news.rowLevelPermissionDataSet, |
| 201 | DataSetUsageConfiguration: news.dataSetUsageConfiguration, |
| 202 | DatasetParameters: news.datasetParameters, |
| 203 | Tags: toWireTags(desiredTags), |
| 204 | }) |
| 205 | .pipe(Effect.catchTag("ResourceExistsException", () => Effect.void)); |
| 206 | } else { |
| 207 | // 3. Sync — idempotent update of the dataset definition. |
| 208 | yield* quicksight.updateDataSet({ |
| 209 | AwsAccountId: accountId, |
| 210 | DataSetId: dataSetId, |
| 211 | Name: news.name, |
| 212 | PhysicalTableMap: news.physicalTableMap, |
| 213 | ImportMode: news.importMode, |
| 214 | LogicalTableMap: news.logicalTableMap, |
| 215 | ColumnGroups: news.columnGroups, |
| 216 | FieldFolders: news.fieldFolders, |
| 217 | RowLevelPermissionDataSet: news.rowLevelPermissionDataSet, |
| 218 | DataSetUsageConfiguration: news.dataSetUsageConfiguration, |
| 219 | DatasetParameters: news.datasetParameters, |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | observed = yield* readSet(accountId, dataSetId); |
| 224 | if (observed === undefined) { |
| 225 | return yield* new QuickSightDataSetUnexpectedResponse({ |
| 226 | message: `QuickSight dataset '${dataSetId}' not found after reconcile`, |
| 227 | }); |
| 228 | } |
| 229 | |
| 230 | // 3b. Sync tags. |
| 231 | yield* syncQuickSightTags(observed.Arn!, desiredTags); |
| 232 | |
| 233 | yield* session.note(dataSetId); |
| 234 | return toAttrs(observed); |
| 235 | }), |
| 236 | |
| 237 | delete: Effect.fn(function* ({ output }) { |
| 238 | const { accountId } = yield* AWSEnvironment.current; |
| 239 | yield* quicksight |
| 240 | .deleteDataSet({ |
| 241 | AwsAccountId: accountId, |
| 242 | DataSetId: output.dataSetId, |
| 243 | }) |
| 244 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 245 | }), |
| 246 | |
| 247 | list: () => |
| 248 | Effect.gen(function* () { |
| 249 | const { accountId } = yield* AWSEnvironment.current; |
| 250 | return yield* quicksight.listDataSets.pages({ AwsAccountId: accountId }).pipe( |
| 251 | Stream.runCollect, |
| 252 | Effect.map((chunk) => |
| 253 | Array.from(chunk) |
| 254 | .flatMap((page) => page.DataSetSummaries ?? []) |
| 255 | .flatMap((s) => |
| 256 | s.DataSetId !== undefined && s.Arn !== undefined |
| 257 | ? [ |
| 258 | { |
| 259 | dataSetId: s.DataSetId, |
| 260 | arn: s.Arn, |
| 261 | name: s.Name ?? "", |
| 262 | }, |
| 263 | ] |
| 264 | : [], |
| 265 | ), |
| 266 | ), |
| 267 | ); |
| 268 | }), |
| 269 | }); |
| 270 | }), |
| 271 | ); |