The best way to control Claude Code, Codex and more from any computer or phone
| 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, waitForSettled } from "./internal.ts"; |
| 14 | |
| 15 | export class AnalysisUnexpectedResponse extends Data.TaggedError("AnalysisUnexpectedResponse")<{ |
| 16 | readonly message: string; |
| 17 | readonly cause?: unknown; |
| 18 | }> {} |
| 19 | |
| 20 | /** |
| 21 | * Properties for an Amazon QuickSight analysis — an editable workspace built |
| 22 | * from a template or an inline definition, which can be published as a |
| 23 | * dashboard. |
| 24 | */ |
| 25 | export interface AnalysisProps { |
| 26 | /** |
| 27 | * Unique id of the analysis within the account. Stable — changing it |
| 28 | * replaces the analysis. If omitted, a unique id is generated. |
| 29 | */ |
| 30 | analysisId?: string; |
| 31 | |
| 32 | /** |
| 33 | * Display name of the analysis. |
| 34 | */ |
| 35 | name: string; |
| 36 | |
| 37 | /** |
| 38 | * Source of the analysis content — a template to clone. Provide either |
| 39 | * `sourceEntity` or `definition`. |
| 40 | */ |
| 41 | sourceEntity?: quicksight.AnalysisSourceEntity; |
| 42 | |
| 43 | /** |
| 44 | * Inline definition of the analysis content. Provide either `definition` |
| 45 | * or `sourceEntity`. |
| 46 | */ |
| 47 | definition?: quicksight.AnalysisDefinition; |
| 48 | |
| 49 | /** |
| 50 | * Parameters passed to the analysis's datasets. |
| 51 | */ |
| 52 | parameters?: quicksight.Parameters; |
| 53 | |
| 54 | /** |
| 55 | * Resource-level permissions on the analysis. |
| 56 | */ |
| 57 | permissions?: quicksight.ResourcePermission[]; |
| 58 | |
| 59 | /** |
| 60 | * ARN of the theme applied to the analysis. |
| 61 | */ |
| 62 | themeArn?: string; |
| 63 | |
| 64 | /** |
| 65 | * When deleting, whether to skip the recovery window and delete |
| 66 | * immediately. Recommended for ephemeral/test analyses so they don't |
| 67 | * linger in a recoverable state. |
| 68 | * @default true |
| 69 | */ |
| 70 | forceDeleteWithoutRecovery?: boolean; |
| 71 | |
| 72 | /** |
| 73 | * Tags to apply to the analysis. |
| 74 | */ |
| 75 | tags?: Record<string, string>; |
| 76 | } |
| 77 | |
| 78 | export interface Analysis extends Resource< |
| 79 | "AWS.QuickSight.Analysis", |
| 80 | AnalysisProps, |
| 81 | { |
| 82 | /** Unique id of the analysis within the account. */ |
| 83 | analysisId: string; |
| 84 | /** ARN of the analysis. */ |
| 85 | arn: string; |
| 86 | /** Display name of the analysis. */ |
| 87 | name: string; |
| 88 | /** Current lifecycle status (e.g. `CREATION_SUCCESSFUL`). */ |
| 89 | status: string; |
| 90 | }, |
| 91 | never, |
| 92 | Providers |
| 93 | > {} |
| 94 | |
| 95 | /** |
| 96 | * An Amazon QuickSight analysis — an editable workspace built from a template |
| 97 | * or an inline definition, which can be published as a dashboard. |
| 98 | * |
| 99 | * QuickSight requires an active account subscription in the region. Without |
| 100 | * one, create operations fail with the typed `QuickSightSubscriptionRequired` |
| 101 | * error. |
| 102 | * |
| 103 | * ### Creating an Analysis |
| 104 | * **Example:** Analysis from a Template |
| 105 | * ```typescript |
| 106 | * const analysis = yield* Analysis("explore-sales", { |
| 107 | * name: "Explore Sales", |
| 108 | * sourceEntity: { |
| 109 | * SourceTemplate: { |
| 110 | * Arn: templateArn, |
| 111 | * DataSetReferences: [ |
| 112 | * { DataSetPlaceholder: "sales", DataSetArn: dataset.arn }, |
| 113 | * ], |
| 114 | * }, |
| 115 | * }, |
| 116 | * }); |
| 117 | * ``` |
| 118 | * |
| 119 | * @resource |
| 120 | */ |
| 121 | export const Analysis = Resource<Analysis>("AWS.QuickSight.Analysis"); |
| 122 | |
| 123 | export const AnalysisProvider = () => |
| 124 | Provider.effect( |
| 125 | Analysis, |
| 126 | Effect.gen(function* () { |
| 127 | const toId = (id: string, props: Partial<AnalysisProps>) => |
| 128 | props.analysisId |
| 129 | ? Effect.succeed(props.analysisId) |
| 130 | : createPhysicalName({ id, maxLength: 64 }); |
| 131 | |
| 132 | const readAnalysis = Effect.fn(function* (accountId: string, analysisId: string) { |
| 133 | const response = yield* quicksight |
| 134 | .describeAnalysis({ AwsAccountId: accountId, AnalysisId: analysisId }) |
| 135 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 136 | const analysis = response?.Analysis; |
| 137 | if (analysis === undefined || analysis.Status === "DELETED") { |
| 138 | return undefined; |
| 139 | } |
| 140 | return analysis; |
| 141 | }); |
| 142 | |
| 143 | const settle = (accountId: string, analysisId: string) => |
| 144 | waitForSettled( |
| 145 | analysisId, |
| 146 | readAnalysis(accountId, analysisId).pipe( |
| 147 | Effect.map((a) => (a === undefined ? undefined : { ...a, status: a.Status })), |
| 148 | ), |
| 149 | ); |
| 150 | |
| 151 | const toAttrs = (analysis: quicksight.Analysis) => ({ |
| 152 | analysisId: analysis.AnalysisId!, |
| 153 | arn: analysis.Arn!, |
| 154 | name: analysis.Name ?? "", |
| 155 | status: analysis.Status ?? "", |
| 156 | }); |
| 157 | |
| 158 | return Analysis.Provider.of({ |
| 159 | stables: ["analysisId", "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 analysisId = output?.analysisId ?? (yield* toId(id, olds)); |
| 171 | const analysis = yield* readAnalysis(accountId, analysisId); |
| 172 | if (analysis === undefined) return undefined; |
| 173 | const attrs = toAttrs(analysis); |
| 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 analysisId = output?.analysisId ?? (yield* toId(id, news)); |
| 181 | const internalTags = yield* createInternalTags(id); |
| 182 | const desiredTags = { ...internalTags, ...news.tags }; |
| 183 | |
| 184 | // 1. Observe. |
| 185 | let observed = yield* readAnalysis(accountId, analysisId); |
| 186 | |
| 187 | // 2. Ensure — create if missing. |
| 188 | if (observed === undefined) { |
| 189 | yield* quicksight |
| 190 | .createAnalysis({ |
| 191 | AwsAccountId: accountId, |
| 192 | AnalysisId: analysisId, |
| 193 | Name: news.name, |
| 194 | SourceEntity: news.sourceEntity, |
| 195 | Definition: news.definition, |
| 196 | Parameters: news.parameters, |
| 197 | Permissions: news.permissions, |
| 198 | ThemeArn: news.themeArn, |
| 199 | Tags: toWireTags(desiredTags), |
| 200 | }) |
| 201 | .pipe(Effect.catchTag("ResourceExistsException", () => Effect.void)); |
| 202 | } else { |
| 203 | // 3. Sync. |
| 204 | yield* quicksight.updateAnalysis({ |
| 205 | AwsAccountId: accountId, |
| 206 | AnalysisId: analysisId, |
| 207 | Name: news.name, |
| 208 | SourceEntity: news.sourceEntity, |
| 209 | Definition: news.definition, |
| 210 | Parameters: news.parameters, |
| 211 | ThemeArn: news.themeArn, |
| 212 | }); |
| 213 | } |
| 214 | |
| 215 | observed = yield* settle(accountId, analysisId); |
| 216 | if (observed === undefined) { |
| 217 | return yield* new AnalysisUnexpectedResponse({ |
| 218 | message: `QuickSight analysis '${analysisId}' not found after reconcile`, |
| 219 | }); |
| 220 | } |
| 221 | |
| 222 | // 3b. Sync tags. |
| 223 | yield* syncQuickSightTags(observed.Arn!, desiredTags); |
| 224 | |
| 225 | yield* session.note(analysisId); |
| 226 | return toAttrs(observed); |
| 227 | }), |
| 228 | |
| 229 | delete: Effect.fn(function* ({ olds, output }) { |
| 230 | const { accountId } = yield* AWSEnvironment.current; |
| 231 | yield* quicksight |
| 232 | .deleteAnalysis({ |
| 233 | AwsAccountId: accountId, |
| 234 | AnalysisId: output.analysisId, |
| 235 | ForceDeleteWithoutRecovery: olds?.forceDeleteWithoutRecovery ?? true, |
| 236 | }) |
| 237 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 238 | }), |
| 239 | |
| 240 | list: () => |
| 241 | Effect.gen(function* () { |
| 242 | const { accountId } = yield* AWSEnvironment.current; |
| 243 | return yield* quicksight.listAnalyses.pages({ AwsAccountId: accountId }).pipe( |
| 244 | Stream.runCollect, |
| 245 | Effect.map((chunk) => |
| 246 | Array.from(chunk) |
| 247 | .flatMap((page) => page.AnalysisSummaryList ?? []) |
| 248 | .flatMap((s) => |
| 249 | s.AnalysisId !== undefined && s.Arn !== undefined && s.Status !== "DELETED" |
| 250 | ? [ |
| 251 | { |
| 252 | analysisId: s.AnalysisId, |
| 253 | arn: s.Arn, |
| 254 | name: s.Name ?? "", |
| 255 | status: s.Status ?? "", |
| 256 | }, |
| 257 | ] |
| 258 | : [], |
| 259 | ), |
| 260 | ), |
| 261 | ); |
| 262 | }), |
| 263 | }); |
| 264 | }), |
| 265 | ); |