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 QuickSightDashboardUnexpectedResponse extends Data.TaggedError( |
| 16 | "QuickSightDashboardUnexpectedResponse", |
| 17 | )<{ |
| 18 | readonly message: string; |
| 19 | readonly cause?: unknown; |
| 20 | }> {} |
| 21 | |
| 22 | /** |
| 23 | * Properties for an Amazon QuickSight dashboard — a published, read-only view |
| 24 | * built from a template, an analysis, or an inline definition. |
| 25 | */ |
| 26 | export interface DashboardProps { |
| 27 | /** |
| 28 | * Unique id of the dashboard within the account. Stable — changing it |
| 29 | * replaces the dashboard. If omitted, a unique id is generated. |
| 30 | */ |
| 31 | dashboardId?: string; |
| 32 | |
| 33 | /** |
| 34 | * Display name of the dashboard. |
| 35 | */ |
| 36 | name: string; |
| 37 | |
| 38 | /** |
| 39 | * Source of the dashboard content — a template or an existing analysis to |
| 40 | * clone. Provide either `sourceEntity` or `definition`. |
| 41 | */ |
| 42 | sourceEntity?: quicksight.DashboardSourceEntity; |
| 43 | |
| 44 | /** |
| 45 | * Inline definition of the dashboard content. Provide either `definition` |
| 46 | * or `sourceEntity`. |
| 47 | */ |
| 48 | definition?: quicksight.DashboardVersionDefinition; |
| 49 | |
| 50 | /** |
| 51 | * Parameters passed to the dashboard's datasets. |
| 52 | */ |
| 53 | parameters?: quicksight.Parameters; |
| 54 | |
| 55 | /** |
| 56 | * Resource-level permissions on the dashboard. |
| 57 | */ |
| 58 | permissions?: quicksight.ResourcePermission[]; |
| 59 | |
| 60 | /** |
| 61 | * Publish options (e.g. ad-hoc filtering, export to CSV). |
| 62 | */ |
| 63 | dashboardPublishOptions?: quicksight.DashboardPublishOptions; |
| 64 | |
| 65 | /** |
| 66 | * ARN of the theme applied to the dashboard. |
| 67 | */ |
| 68 | themeArn?: string; |
| 69 | |
| 70 | /** |
| 71 | * Description of the created/updated version. |
| 72 | */ |
| 73 | versionDescription?: string; |
| 74 | |
| 75 | /** |
| 76 | * Tags to apply to the dashboard. |
| 77 | */ |
| 78 | tags?: Record<string, string>; |
| 79 | } |
| 80 | |
| 81 | export interface Dashboard extends Resource< |
| 82 | "AWS.QuickSight.Dashboard", |
| 83 | DashboardProps, |
| 84 | { |
| 85 | /** Unique id of the dashboard within the account. */ |
| 86 | dashboardId: string; |
| 87 | /** ARN of the dashboard. */ |
| 88 | arn: string; |
| 89 | /** Display name of the dashboard. */ |
| 90 | name: string; |
| 91 | /** Current version status (e.g. `CREATION_SUCCESSFUL`). */ |
| 92 | status: string; |
| 93 | }, |
| 94 | never, |
| 95 | Providers |
| 96 | > {} |
| 97 | |
| 98 | /** |
| 99 | * An Amazon QuickSight dashboard — a published, read-only view built from a |
| 100 | * template, an analysis, or an inline definition. |
| 101 | * |
| 102 | * QuickSight requires an active account subscription in the region. Without |
| 103 | * one, create operations fail with the typed `QuickSightSubscriptionRequired` |
| 104 | * error. |
| 105 | * |
| 106 | * ### Creating a Dashboard |
| 107 | * **Example:** Dashboard from a Template |
| 108 | * ```typescript |
| 109 | * const dashboard = yield* Dashboard("sales-overview", { |
| 110 | * name: "Sales Overview", |
| 111 | * sourceEntity: { |
| 112 | * SourceTemplate: { |
| 113 | * Arn: templateArn, |
| 114 | * DataSetReferences: [ |
| 115 | * { |
| 116 | * DataSetPlaceholder: "sales", |
| 117 | * DataSetArn: dataset.arn, |
| 118 | * }, |
| 119 | * ], |
| 120 | * }, |
| 121 | * }, |
| 122 | * }); |
| 123 | * ``` |
| 124 | * |
| 125 | * @resource |
| 126 | */ |
| 127 | export const Dashboard = Resource<Dashboard>("AWS.QuickSight.Dashboard"); |
| 128 | |
| 129 | export const DashboardProvider = () => |
| 130 | Provider.effect( |
| 131 | Dashboard, |
| 132 | Effect.gen(function* () { |
| 133 | const toId = (id: string, props: Partial<DashboardProps>) => |
| 134 | props.dashboardId |
| 135 | ? Effect.succeed(props.dashboardId) |
| 136 | : createPhysicalName({ id, maxLength: 64 }); |
| 137 | |
| 138 | const readDashboard = Effect.fn(function* (accountId: string, dashboardId: string) { |
| 139 | const response = yield* quicksight |
| 140 | .describeDashboard({ |
| 141 | AwsAccountId: accountId, |
| 142 | DashboardId: dashboardId, |
| 143 | }) |
| 144 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 145 | return response?.Dashboard; |
| 146 | }); |
| 147 | |
| 148 | const settle = (accountId: string, dashboardId: string) => |
| 149 | waitForSettled( |
| 150 | dashboardId, |
| 151 | readDashboard(accountId, dashboardId).pipe( |
| 152 | Effect.map((d) => (d === undefined ? undefined : { ...d, status: d.Version?.Status })), |
| 153 | ), |
| 154 | ); |
| 155 | |
| 156 | const toAttrs = (dashboard: quicksight.Dashboard) => ({ |
| 157 | dashboardId: dashboard.DashboardId!, |
| 158 | arn: dashboard.Arn!, |
| 159 | name: dashboard.Name ?? "", |
| 160 | status: dashboard.Version?.Status ?? "", |
| 161 | }); |
| 162 | |
| 163 | return Dashboard.Provider.of({ |
| 164 | stables: ["dashboardId", "arn"], |
| 165 | |
| 166 | diff: Effect.fn(function* ({ id, olds = {}, news }) { |
| 167 | if (!isResolved(news)) return undefined; |
| 168 | if ((yield* toId(id, olds)) !== (yield* toId(id, news))) { |
| 169 | return { action: "replace" } as const; |
| 170 | } |
| 171 | }), |
| 172 | |
| 173 | read: Effect.fn(function* ({ id, olds = {}, output }) { |
| 174 | const { accountId } = yield* AWSEnvironment.current; |
| 175 | const dashboardId = output?.dashboardId ?? (yield* toId(id, olds)); |
| 176 | const dashboard = yield* readDashboard(accountId, dashboardId); |
| 177 | if (dashboard === undefined) return undefined; |
| 178 | const attrs = toAttrs(dashboard); |
| 179 | const tags = yield* readQuickSightTags(attrs.arn); |
| 180 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 181 | }), |
| 182 | |
| 183 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 184 | const { accountId } = yield* AWSEnvironment.current; |
| 185 | const dashboardId = output?.dashboardId ?? (yield* toId(id, news)); |
| 186 | const internalTags = yield* createInternalTags(id); |
| 187 | const desiredTags = { ...internalTags, ...news.tags }; |
| 188 | |
| 189 | // 1. Observe. |
| 190 | let observed = yield* readDashboard(accountId, dashboardId); |
| 191 | |
| 192 | // 2. Ensure — create if missing. |
| 193 | if (observed === undefined) { |
| 194 | yield* quicksight |
| 195 | .createDashboard({ |
| 196 | AwsAccountId: accountId, |
| 197 | DashboardId: dashboardId, |
| 198 | Name: news.name, |
| 199 | SourceEntity: news.sourceEntity, |
| 200 | Definition: news.definition, |
| 201 | Parameters: news.parameters, |
| 202 | Permissions: news.permissions, |
| 203 | DashboardPublishOptions: news.dashboardPublishOptions, |
| 204 | ThemeArn: news.themeArn, |
| 205 | VersionDescription: news.versionDescription, |
| 206 | Tags: toWireTags(desiredTags), |
| 207 | }) |
| 208 | .pipe(Effect.catchTag("ResourceExistsException", () => Effect.void)); |
| 209 | } else { |
| 210 | // 3. Sync — publish a new version. |
| 211 | yield* quicksight.updateDashboard({ |
| 212 | AwsAccountId: accountId, |
| 213 | DashboardId: dashboardId, |
| 214 | Name: news.name, |
| 215 | SourceEntity: news.sourceEntity, |
| 216 | Definition: news.definition, |
| 217 | Parameters: news.parameters, |
| 218 | DashboardPublishOptions: news.dashboardPublishOptions, |
| 219 | ThemeArn: news.themeArn, |
| 220 | VersionDescription: news.versionDescription, |
| 221 | }); |
| 222 | } |
| 223 | |
| 224 | observed = yield* settle(accountId, dashboardId); |
| 225 | if (observed === undefined) { |
| 226 | return yield* new QuickSightDashboardUnexpectedResponse({ |
| 227 | message: `QuickSight dashboard '${dashboardId}' not found after reconcile`, |
| 228 | }); |
| 229 | } |
| 230 | |
| 231 | // 3b. Sync tags. |
| 232 | yield* syncQuickSightTags(observed.Arn!, desiredTags); |
| 233 | |
| 234 | yield* session.note(dashboardId); |
| 235 | return toAttrs(observed); |
| 236 | }), |
| 237 | |
| 238 | delete: Effect.fn(function* ({ output }) { |
| 239 | const { accountId } = yield* AWSEnvironment.current; |
| 240 | yield* quicksight |
| 241 | .deleteDashboard({ |
| 242 | AwsAccountId: accountId, |
| 243 | DashboardId: output.dashboardId, |
| 244 | }) |
| 245 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 246 | }), |
| 247 | |
| 248 | list: () => |
| 249 | Effect.gen(function* () { |
| 250 | const { accountId } = yield* AWSEnvironment.current; |
| 251 | return yield* quicksight.listDashboards.pages({ AwsAccountId: accountId }).pipe( |
| 252 | Stream.runCollect, |
| 253 | Effect.map((chunk) => |
| 254 | Array.from(chunk) |
| 255 | .flatMap((page) => page.DashboardSummaryList ?? []) |
| 256 | .flatMap((s) => |
| 257 | s.DashboardId !== undefined && s.Arn !== undefined |
| 258 | ? [ |
| 259 | { |
| 260 | dashboardId: s.DashboardId, |
| 261 | arn: s.Arn, |
| 262 | name: s.Name ?? "", |
| 263 | status: "", |
| 264 | }, |
| 265 | ] |
| 266 | : [], |
| 267 | ), |
| 268 | ), |
| 269 | ); |
| 270 | }), |
| 271 | }); |
| 272 | }), |
| 273 | ); |