sailfork / t3code-github / .repos/ alchemy-effect/ packages/ gcp/ src/ ContactCenterInsights/ Dashboard.ts

import * as cci from "@distilled.cloud/gcp/contactcenterinsights_v1";
import { unownedUnlessPrevious } from "alchemy/AdoptPolicy";
import { isResolved } from "alchemy/Diff";
import * as Provider from "alchemy/Provider";
import { Resource } from "alchemy/Resource";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import { GcpEnvironment } from "../Environment.ts";
import { createInternalLabels } from "../Labels.ts";
import type { Providers } from "../Providers.ts";
import {
  encodeOwnership,
  hasOwnershipMarker,
  lastSegment,
  locationOf,
  locationParent,
  ownedByAlchemy,
  parseOwnership,
  sameJson,
  toResourceId,
} from "./ownership.ts";

export type DateRangeConfig = {
  /** Rolling window measured in `unit`. */
  relativeDateRange?: {
    /** Number of units in the past. */
    quantity?: string;
    /** Calendar unit (`DAY`, `WEEK`, `MONTH`, `QUARTER`, `YEAR`). */
    unit?: "TIME_UNIT_UNSPECIFIED" | "DAY" | "WEEK" | "MONTH" | "QUARTER" | "YEAR" | (string & {});
  };
  /** Inclusive start/end timestamps. */
  absoluteDateRange?: {
    startTime?: string;
    endTime?: string;
  };
};

export type DashboardProps = {
  /**
   * Region (`us-central1`, …). Immutable — changing it replaces the
   * dashboard.
   * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
   */
  location?: string;
  /**
   * Dashboard id (the `{dashboard}` segment). If omitted, a unique id is
   * generated from the stack, stage, and logical id. Immutable — changing
   * it replaces the dashboard. 4-64 characters matching
   * `^[a-z]([a-z0-9-]{0,61}[a-z0-9])?

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/gcp/src/ContactCenterInsights/Dashboard.ts at main · sailfork/t3code-github · Sailfork
	
		
. */ dashboardId?: string; /** * User-facing display name. */ displayName?: string; /** * Human-readable description. Dashboards have no labels field, so * Alchemy ownership is stored in a `[alchemy …]` prefix and stripped * from attributes. */ description?: string; /** * Filter applied to every chart on the dashboard. */ filter?: string; /** * Date range applied to every chart on the dashboard. */ dateRangeConfig?: DateRangeConfig; /** * Root widget container describing the dashboard layout. */ rootContainer?: cci.GoogleCloudContactcenterinsightsV1Container; }; export type Dashboard = Resource< "GCP.ContactCenterInsights.Dashboard", DashboardProps, { /** Full resource name. */ name: string; /** Dashboard id (last path segment). */ dashboardId: string; /** Region id. */ location: string; /** Project id. */ project: string; /** User-facing display name. */ displayName: string | undefined; /** User description with the Alchemy ownership prefix stripped. */ description: string | undefined; /** Dashboard-wide filter. */ filter: string | undefined; /** Dashboard-wide date range. */ dateRangeConfig: DateRangeConfig | undefined; /** Root layout container. */ rootContainer: cci.GoogleCloudContactcenterinsightsV1Container | undefined; /** Whether the dashboard is a predefined read-only dashboard. */ readOnly: boolean; /** RFC3339 creation timestamp. */ createTime: string | undefined; /** RFC3339 last-update timestamp. */ updateTime: string | undefined; }, never, Providers >; /** * A Contact Center Insights dashboard of conversation charts. * * Dashboards have no labels field — Alchemy stamps ownership into the * description. Location and dashboard id are immutable. Display name, * description, filter, date range, and root container update in place. * * ### Creating a Dashboard * **Example:** Generated id * ```typescript * const dashboard = yield* GCP.ContactCenterInsights.Dashboard("Overview", { * displayName: "overview", * description: "call quality", * }); * ``` * * **Example:** Named dashboard * ```typescript * const dashboard = yield* GCP.ContactCenterInsights.Dashboard("Overview", { * dashboardId: "quality-overview", * displayName: "quality", * }); * ``` * * @resource * @category ContactCenterInsights */ export const Dashboard = Resource<Dashboard>("GCP.ContactCenterInsights.Dashboard"); export class DashboardNotResolved extends Data.TaggedError( "GCP.ContactCenterInsights.DashboardNotResolved", )<{ resourceName: string; }> {} const resourceName = (parent: string, dashboardId: string) => `${parent}/dashboards/${dashboardId}`; const toDateRangeConfig = ( config: cci.GoogleCloudContactcenterinsightsV1DateRangeConfig | undefined, ): DateRangeConfig | undefined => { if (config === undefined) return undefined; return { relativeDateRange: config.relativeDateRange === undefined ? undefined : { quantity: config.relativeDateRange.quantity, unit: config.relativeDateRange.unit, }, absoluteDateRange: config.absoluteDateRange === undefined ? undefined : { startTime: config.absoluteDateRange.startTime, endTime: config.absoluteDateRange.endTime, }, }; }; const toAttrs = (dashboard: cci.GoogleCloudContactcenterinsightsV1Dashboard, project: string) => { const name = dashboard.name ?? ""; const parsed = parseOwnership(dashboard.description); return { name, dashboardId: lastSegment(name), location: locationOf(name), project, displayName: dashboard.displayName, description: parsed.text, filter: dashboard.filter, dateRangeConfig: toDateRangeConfig(dashboard.dateRangeConfig), rootContainer: dashboard.rootContainer, readOnly: dashboard.readOnly === true, createTime: dashboard.createTime, updateTime: dashboard.updateTime, }; }; const getByName = (name: string) => name.length === 0 ? Effect.undefined : cci .getProjectsLocationsDashboards({ name }) .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); const listAt = (parent: string, project: string) => cci.listProjectsLocationsDashboards.pages({ parent, pageSize: 100 }).pipe( Stream.flatMap((page) => Stream.fromIterable(page.dashboards ?? [])), Stream.filter((dashboard) => hasOwnershipMarker(dashboard.description)), Stream.map((dashboard) => toAttrs(dashboard, project)), Stream.runCollect, Effect.map((chunk) => Array.from(chunk)), Effect.catchTag("NotFound", () => Effect.succeed([])), ); export const DashboardProvider = () => Provider.succeed(Dashboard, { stables: ["name", "dashboardId", "location", "project", "createTime"], diff: Effect.fn(function* ({ news, olds, output }) { if (!isResolved(news)) return undefined; const previousLocation = olds?.location ?? output?.location; const nextLocation = news.location; if ( previousLocation !== undefined && nextLocation !== undefined && previousLocation !== nextLocation ) { return { action: "replace" as const, deleteFirst: false }; } const previousId = olds?.dashboardId ?? output?.dashboardId; if ( previousId !== undefined && news.dashboardId !== undefined && news.dashboardId !== previousId ) { return { action: "replace" as const, deleteFirst: true }; } return undefined; }), read: Effect.fn(function* ({ id, olds, output, previous }) { const env = yield* GcpEnvironment.current; const dashboardId = yield* toResourceId(id, olds?.dashboardId, output?.dashboardId); const location = olds?.location ?? output?.location ?? env.region; const name = output?.name ?? resourceName(locationParent(env.project, location), dashboardId); const existing = yield* getByName(name); if (existing === undefined) return undefined; const attrs = toAttrs(existing, env.project); return (yield* ownedByAlchemy(id, existing.description)) ? attrs : unownedUnlessPrevious(attrs, previous, "name"); }), list: () => Effect.gen(function* () { const env = yield* GcpEnvironment.current; return yield* listAt(locationParent(env.project, env.region), env.project); }), reconcile: Effect.fn(function* ({ id, news, output }) { const env = yield* GcpEnvironment.current; const location = news.location ?? output?.location ?? env.region; const parent = locationParent(env.project, location); const dashboardId = yield* toResourceId(id, news.dashboardId, output?.dashboardId); const name = resourceName(parent, dashboardId); const ownership = yield* createInternalLabels(id); const description = encodeOwnership(ownership, news.description); const displayName = news.displayName ?? dashboardId; let current = yield* getByName(output?.name ?? name); if (current === undefined) { const created = yield* cci .createProjectsLocationsDashboards({ parent, dashboardId, body: { displayName, description, filter: news.filter, dateRangeConfig: news.dateRangeConfig, rootContainer: news.rootContainer, }, }) .pipe(Effect.catchTag("Conflict", () => getByName(name))); current = created ?? undefined; } if (current === undefined) { return yield* new DashboardNotResolved({ resourceName: name }); } const displayChanged = (current.displayName ?? "") !== displayName; const descriptionChanged = (current.description ?? "") !== description; const filterChanged = (current.filter ?? "") !== (news.filter ?? ""); const dateRangeChanged = !sameJson(current.dateRangeConfig, news.dateRangeConfig); const rootChanged = !sameJson(current.rootContainer, news.rootContainer); if ( displayChanged || descriptionChanged || filterChanged || dateRangeChanged || rootChanged ) { current = yield* cci.patchProjectsLocationsDashboards({ name: current.name ?? name, updateMask: [ displayChanged ? "display_name" : undefined, descriptionChanged ? "description" : undefined, filterChanged ? "filter" : undefined, dateRangeChanged ? "date_range_config" : undefined, rootChanged ? "root_container" : undefined, ] .filter((field): field is string => field !== undefined) .join(","), body: { name: current.name ?? name, displayName, description, filter: news.filter, dateRangeConfig: news.dateRangeConfig, rootContainer: news.rootContainer, }, }); } return toAttrs(current, env.project); }), delete: Effect.fn(function* ({ output }) { yield* cci .deleteProjectsLocationsDashboards({ name: output.name }) .pipe(Effect.catchTag("NotFound", () => Effect.void)); }), });
.repos/alchemy-effect/packages/gcp/src/ContactCenterInsights/Dashboard.ts at main · sailfork/t3code-github · Sailfork
.\n */\n dashboardId?: string;\n /**\n * User-facing display name.\n */\n displayName?: string;\n /**\n * Human-readable description. Dashboards have no labels field, so\n * Alchemy ownership is stored in a `[alchemy …]` prefix and stripped\n * from attributes.\n */\n description?: string;\n /**\n * Filter applied to every chart on the dashboard.\n */\n filter?: string;\n /**\n * Date range applied to every chart on the dashboard.\n */\n dateRangeConfig?: DateRangeConfig;\n /**\n * Root widget container describing the dashboard layout.\n */\n rootContainer?: cci.GoogleCloudContactcenterinsightsV1Container;\n};\n\nexport type Dashboard = Resource\u003c\n \"GCP.ContactCenterInsights.Dashboard\",\n DashboardProps,\n {\n /** Full resource name. */\n name: string;\n /** Dashboard id (last path segment). */\n dashboardId: string;\n /** Region id. */\n location: string;\n /** Project id. */\n project: string;\n /** User-facing display name. */\n displayName: string | undefined;\n /** User description with the Alchemy ownership prefix stripped. */\n description: string | undefined;\n /** Dashboard-wide filter. */\n filter: string | undefined;\n /** Dashboard-wide date range. */\n dateRangeConfig: DateRangeConfig | undefined;\n /** Root layout container. */\n rootContainer: cci.GoogleCloudContactcenterinsightsV1Container | undefined;\n /** Whether the dashboard is a predefined read-only dashboard. */\n readOnly: boolean;\n /** RFC3339 creation timestamp. */\n createTime: string | undefined;\n /** RFC3339 last-update timestamp. */\n updateTime: string | undefined;\n },\n never,\n Providers\n>;\n\n/**\n * A Contact Center Insights dashboard of conversation charts.\n *\n * Dashboards have no labels field — Alchemy stamps ownership into the\n * description. Location and dashboard id are immutable. Display name,\n * description, filter, date range, and root container update in place.\n *\n * ### Creating a Dashboard\n * **Example:** Generated id\n * ```typescript\n * const dashboard = yield* GCP.ContactCenterInsights.Dashboard(\"Overview\", {\n * displayName: \"overview\",\n * description: \"call quality\",\n * });\n * ```\n *\n * **Example:** Named dashboard\n * ```typescript\n * const dashboard = yield* GCP.ContactCenterInsights.Dashboard(\"Overview\", {\n * dashboardId: \"quality-overview\",\n * displayName: \"quality\",\n * });\n * ```\n *\n * @resource\n * @category ContactCenterInsights\n */\nexport const Dashboard = Resource\u003cDashboard>(\"GCP.ContactCenterInsights.Dashboard\");\n\nexport class DashboardNotResolved extends Data.TaggedError(\n \"GCP.ContactCenterInsights.DashboardNotResolved\",\n)\u003c{\n resourceName: string;\n}> {}\n\nconst resourceName = (parent: string, dashboardId: string) => `${parent}/dashboards/${dashboardId}`;\n\nconst toDateRangeConfig = (\n config: cci.GoogleCloudContactcenterinsightsV1DateRangeConfig | undefined,\n): DateRangeConfig | undefined => {\n if (config === undefined) return undefined;\n return {\n relativeDateRange:\n config.relativeDateRange === undefined\n ? undefined\n : {\n quantity: config.relativeDateRange.quantity,\n unit: config.relativeDateRange.unit,\n },\n absoluteDateRange:\n config.absoluteDateRange === undefined\n ? undefined\n : {\n startTime: config.absoluteDateRange.startTime,\n endTime: config.absoluteDateRange.endTime,\n },\n };\n};\n\nconst toAttrs = (dashboard: cci.GoogleCloudContactcenterinsightsV1Dashboard, project: string) => {\n const name = dashboard.name ?? \"\";\n const parsed = parseOwnership(dashboard.description);\n return {\n name,\n dashboardId: lastSegment(name),\n location: locationOf(name),\n project,\n displayName: dashboard.displayName,\n description: parsed.text,\n filter: dashboard.filter,\n dateRangeConfig: toDateRangeConfig(dashboard.dateRangeConfig),\n rootContainer: dashboard.rootContainer,\n readOnly: dashboard.readOnly === true,\n createTime: dashboard.createTime,\n updateTime: dashboard.updateTime,\n };\n};\n\nconst getByName = (name: string) =>\n name.length === 0\n ? Effect.undefined\n : cci\n .getProjectsLocationsDashboards({ name })\n .pipe(Effect.catchTag(\"NotFound\", () => Effect.undefined));\n\nconst listAt = (parent: string, project: string) =>\n cci.listProjectsLocationsDashboards.pages({ parent, pageSize: 100 }).pipe(\n Stream.flatMap((page) => Stream.fromIterable(page.dashboards ?? [])),\n Stream.filter((dashboard) => hasOwnershipMarker(dashboard.description)),\n Stream.map((dashboard) => toAttrs(dashboard, project)),\n Stream.runCollect,\n Effect.map((chunk) => Array.from(chunk)),\n Effect.catchTag(\"NotFound\", () => Effect.succeed([])),\n );\n\nexport const DashboardProvider = () =>\n Provider.succeed(Dashboard, {\n stables: [\"name\", \"dashboardId\", \"location\", \"project\", \"createTime\"],\n\n diff: Effect.fn(function* ({ news, olds, output }) {\n if (!isResolved(news)) return undefined;\n const previousLocation = olds?.location ?? output?.location;\n const nextLocation = news.location;\n if (\n previousLocation !== undefined &&\n nextLocation !== undefined &&\n previousLocation !== nextLocation\n ) {\n return { action: \"replace\" as const, deleteFirst: false };\n }\n const previousId = olds?.dashboardId ?? output?.dashboardId;\n if (\n previousId !== undefined &&\n news.dashboardId !== undefined &&\n news.dashboardId !== previousId\n ) {\n return { action: \"replace\" as const, deleteFirst: true };\n }\n return undefined;\n }),\n\n read: Effect.fn(function* ({ id, olds, output, previous }) {\n const env = yield* GcpEnvironment.current;\n const dashboardId = yield* toResourceId(id, olds?.dashboardId, output?.dashboardId);\n const location = olds?.location ?? output?.location ?? env.region;\n const name = output?.name ?? resourceName(locationParent(env.project, location), dashboardId);\n const existing = yield* getByName(name);\n if (existing === undefined) return undefined;\n const attrs = toAttrs(existing, env.project);\n return (yield* ownedByAlchemy(id, existing.description))\n ? attrs\n : unownedUnlessPrevious(attrs, previous, \"name\");\n }),\n\n list: () =>\n Effect.gen(function* () {\n const env = yield* GcpEnvironment.current;\n return yield* listAt(locationParent(env.project, env.region), env.project);\n }),\n\n reconcile: Effect.fn(function* ({ id, news, output }) {\n const env = yield* GcpEnvironment.current;\n const location = news.location ?? output?.location ?? env.region;\n const parent = locationParent(env.project, location);\n const dashboardId = yield* toResourceId(id, news.dashboardId, output?.dashboardId);\n const name = resourceName(parent, dashboardId);\n const ownership = yield* createInternalLabels(id);\n const description = encodeOwnership(ownership, news.description);\n const displayName = news.displayName ?? dashboardId;\n\n let current = yield* getByName(output?.name ?? name);\n\n if (current === undefined) {\n const created = yield* cci\n .createProjectsLocationsDashboards({\n parent,\n dashboardId,\n body: {\n displayName,\n description,\n filter: news.filter,\n dateRangeConfig: news.dateRangeConfig,\n rootContainer: news.rootContainer,\n },\n })\n .pipe(Effect.catchTag(\"Conflict\", () => getByName(name)));\n current = created ?? undefined;\n }\n\n if (current === undefined) {\n return yield* new DashboardNotResolved({ resourceName: name });\n }\n\n const displayChanged = (current.displayName ?? \"\") !== displayName;\n const descriptionChanged = (current.description ?? \"\") !== description;\n const filterChanged = (current.filter ?? \"\") !== (news.filter ?? \"\");\n const dateRangeChanged = !sameJson(current.dateRangeConfig, news.dateRangeConfig);\n const rootChanged = !sameJson(current.rootContainer, news.rootContainer);\n\n if (\n displayChanged ||\n descriptionChanged ||\n filterChanged ||\n dateRangeChanged ||\n rootChanged\n ) {\n current = yield* cci.patchProjectsLocationsDashboards({\n name: current.name ?? name,\n updateMask: [\n displayChanged ? \"display_name\" : undefined,\n descriptionChanged ? \"description\" : undefined,\n filterChanged ? \"filter\" : undefined,\n dateRangeChanged ? \"date_range_config\" : undefined,\n rootChanged ? \"root_container\" : undefined,\n ]\n .filter((field): field is string => field !== undefined)\n .join(\",\"),\n body: {\n name: current.name ?? name,\n displayName,\n description,\n filter: news.filter,\n dateRangeConfig: news.dateRangeConfig,\n rootContainer: news.rootContainer,\n },\n });\n }\n\n return toAttrs(current, env.project);\n }),\n\n delete: Effect.fn(function* ({ output }) {\n yield* cci\n .deleteProjectsLocationsDashboards({ name: output.name })\n .pipe(Effect.catchTag(\"NotFound\", () => Effect.void));\n }),\n });\n","preview":null,"history":null,"version":"195bd42334501d710bca3185d3b3fc17baa2f164"},"/_session?shell=1":null}