sailfork / t3code-github / .repos/ alchemy-effect/ packages/ gcp/ src/ ContactCenterInsights/ DashboardsChart.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 type { DateRangeConfig } from "./Dashboard.ts";
import {
  encodeOwnership,
  hasOwnershipMarker,
  lastSegment,
  locationOf,
  locationParent,
  ownedByAlchemy,
  parentOf,
  parseOwnership,
  sameJson,
  toResourceId,
} from "./ownership.ts";

export type DashboardsChartProps = {
  /**
   * Parent Dashboard resource name
   * (`projects/{project}/locations/{location}/dashboards/{dashboard}`).
   * Immutable — changing it replaces the chart.
   */
  parent: string;
  /**
   * Chart id (the `{chart}` segment). If omitted, a unique id is generated.
   * Immutable — changing it replaces the chart. 4-64 characters matching
   * `^[a-z]([a-z0-9-]{0,61}[a-z0-9])?

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/gcp/src/ContactCenterInsights/DashboardsChart.ts at main · sailfork/t3code-github · Sailfork
	
		
. */ chartId?: string; /** * User-facing display name. */ displayName?: string; /** * Chart description. Charts have no labels field, so Alchemy ownership * is stored in a `[alchemy …]` prefix and stripped from attributes. */ description?: string; /** * Filter applied to the chart. */ filter?: string; /** * Width in grid units. */ width?: number; /** * Height in grid units. */ height?: number; /** * Visualization (`BAR`, `LINE`, `AREA`, `PIE`, `TABLE`, …). */ chartVisualizationType?: | "CHART_VISUALIZATION_TYPE_UNSPECIFIED" | "BAR" | "LINE" | "AREA" | "PIE" | "SCATTER" | "TABLE" | "SCORE_CARD" | "SUNBURST" | "GAUGE" | "SANKEY"; /** * Date range applied to the chart. */ dateRangeConfig?: DateRangeConfig; /** * Query or generative-insights data source. */ dataSource?: cci.GoogleCloudContactcenterinsightsV1ChartDataSource; /** * Click action (redirect or conversation filter). */ action?: cci.GoogleCloudContactcenterinsightsV1ChartAction; }; export type DashboardsChart = Resource< "GCP.ContactCenterInsights.DashboardsChart", DashboardsChartProps, { /** Full resource name. */ name: string; /** Chart id (last path segment). */ chartId: string; /** Parent dashboard resource name. */ parent: 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; /** Chart filter. */ filter: string | undefined; /** Width in grid units. */ width: number | undefined; /** Height in grid units. */ height: number | undefined; /** Server-assigned chart type. */ chartType: string | undefined; /** Visualization type. */ chartVisualizationType: string | undefined; /** Date range config. */ dateRangeConfig: DateRangeConfig | undefined; /** Data source. */ dataSource: cci.GoogleCloudContactcenterinsightsV1ChartDataSource | undefined; /** Click action. */ action: cci.GoogleCloudContactcenterinsightsV1ChartAction | undefined; /** RFC3339 creation timestamp. */ createTime: string | undefined; /** RFC3339 last-update timestamp. */ updateTime: string | undefined; }, never, Providers >; /** * A chart widget on a Contact Center Insights dashboard. * * Parent dashboard and chart id are immutable. Charts have no labels * field — Alchemy stamps ownership into the description. Display name, * description, filter, size, visualization, and data source update in * place. * * ### Creating a Chart * **Example:** Bar chart on a dashboard * ```typescript * const chart = yield* GCP.ContactCenterInsights.DashboardsChart("Volume", { * parent: dashboard.name, * displayName: "volume", * chartVisualizationType: "BAR", * }); * ``` * * @resource * @category ContactCenterInsights */ export const DashboardsChart = Resource<DashboardsChart>( "GCP.ContactCenterInsights.DashboardsChart", ); export class DashboardsChartNotResolved extends Data.TaggedError( "GCP.ContactCenterInsights.DashboardsChartNotResolved", )<{ resourceName: string; }> {} const resourceName = (parent: string, chartId: string) => `${parent}/charts/${chartId}`; const optionalString = (value: string | undefined): string | undefined => value; 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 = (chart: cci.GoogleCloudContactcenterinsightsV1Chart, project: string) => { const name = chart.name ?? ""; const parsed = parseOwnership(chart.description); return { name, chartId: lastSegment(name), parent: parentOf(name), location: locationOf(name), project, displayName: chart.displayName, description: parsed.text, filter: chart.filter, width: chart.width, height: chart.height, chartType: optionalString(chart.chartType), chartVisualizationType: optionalString(chart.chartVisualizationType), dateRangeConfig: toDateRangeConfig(chart.dateRangeConfig), dataSource: chart.dataSource, action: chart.action, createTime: chart.createTime, updateTime: chart.updateTime, }; }; const getByName = (name: string) => name.length === 0 ? Effect.undefined : cci .getProjectsLocationsDashboardsCharts({ name }) .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); const listDashboards = (parent: string) => cci.listProjectsLocationsDashboards.pages({ parent, pageSize: 100 }).pipe( Stream.flatMap((page) => Stream.fromIterable(page.dashboards ?? [])), Stream.map((dashboard) => dashboard.name ?? ""), Stream.filter((name) => name.length > 0), Stream.runCollect, Effect.map((chunk) => Array.from(chunk)), Effect.catchTag("NotFound", () => Effect.succeed([] as string[])), ); const listAtParent = (parent: string, project: string) => cci.listProjectsLocationsDashboardsCharts({ parent }).pipe( Effect.map((page) => (page.charts ?? []) .filter((chart) => hasOwnershipMarker(chart.description)) .map((chart) => toAttrs(chart, project)), ), Effect.catchTag("NotFound", () => Effect.succeed([])), ); export const DashboardsChartProvider = () => Provider.succeed(DashboardsChart, { stables: ["name", "chartId", "parent", "location", "project", "createTime"], diff: Effect.fn(function* ({ news, olds, output }) { if (!isResolved(news)) return undefined; const previousParent = olds?.parent ?? output?.parent; if (previousParent !== undefined && news.parent !== previousParent) { return { action: "replace" as const, deleteFirst: false }; } const previousId = olds?.chartId ?? output?.chartId; if (previousId !== undefined && news.chartId !== undefined && news.chartId !== previousId) { return { action: "replace" as const, deleteFirst: true }; } return undefined; }), read: Effect.fn(function* ({ id, olds, output, previous }) { const env = yield* GcpEnvironment.current; const chartId = yield* toResourceId(id, olds?.chartId, output?.chartId); const name = output?.name ?? (olds?.parent !== undefined ? resourceName(olds.parent, chartId) : ""); 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; const dashboards = yield* listDashboards(locationParent(env.project, env.region)); const pages = yield* Effect.forEach( dashboards, (parent) => listAtParent(parent, env.project), { concurrency: 4 }, ); return pages.flat(); }), reconcile: Effect.fn(function* ({ id, news, output }) { const env = yield* GcpEnvironment.current; const chartId = yield* toResourceId(id, news.chartId, output?.chartId); const name = resourceName(news.parent, chartId); const ownership = yield* createInternalLabels(id); const description = encodeOwnership(ownership, news.description); const displayName = news.displayName ?? chartId; let current = yield* getByName(output?.name ?? name); if (current === undefined) { const created = yield* cci .createProjectsLocationsDashboardsCharts({ parent: news.parent, chartId, body: { displayName, description, filter: news.filter, width: news.width, height: news.height, chartVisualizationType: news.chartVisualizationType, dateRangeConfig: news.dateRangeConfig, dataSource: news.dataSource, action: news.action, }, }) .pipe(Effect.catchTag("Conflict", () => getByName(name))); current = created ?? undefined; } if (current === undefined) { return yield* new DashboardsChartNotResolved({ resourceName: name }); } const displayChanged = (current.displayName ?? "") !== displayName; const descriptionChanged = (current.description ?? "") !== description; const filterChanged = (current.filter ?? "") !== (news.filter ?? ""); const widthChanged = (current.width ?? 0) !== (news.width ?? 0); const heightChanged = (current.height ?? 0) !== (news.height ?? 0); const vizChanged = (current.chartVisualizationType ?? "") !== (news.chartVisualizationType ?? ""); const dateRangeChanged = !sameJson(current.dateRangeConfig, news.dateRangeConfig); const dataSourceChanged = !sameJson(current.dataSource, news.dataSource); const actionChanged = !sameJson(current.action, news.action); if ( displayChanged || descriptionChanged || filterChanged || widthChanged || heightChanged || vizChanged || dateRangeChanged || dataSourceChanged || actionChanged ) { current = yield* cci.patchProjectsLocationsDashboardsCharts({ name: current.name ?? name, updateMask: "*", body: { name: current.name ?? name, displayName, description, filter: news.filter, width: news.width, height: news.height, chartVisualizationType: news.chartVisualizationType, dateRangeConfig: news.dateRangeConfig, dataSource: news.dataSource, action: news.action, }, }); } return toAttrs(current, env.project); }), delete: Effect.fn(function* ({ output }) { yield* cci .deleteProjectsLocationsDashboardsCharts({ name: output.name }) .pipe(Effect.catchTag("NotFound", () => Effect.void)); }), });
.repos/alchemy-effect/packages/gcp/src/ContactCenterInsights/DashboardsChart.ts at main · sailfork/t3code-github · Sailfork
.\n */\n chartId?: string;\n /**\n * User-facing display name.\n */\n displayName?: string;\n /**\n * Chart description. Charts have no labels field, so Alchemy ownership\n * is stored in a `[alchemy …]` prefix and stripped from attributes.\n */\n description?: string;\n /**\n * Filter applied to the chart.\n */\n filter?: string;\n /**\n * Width in grid units.\n */\n width?: number;\n /**\n * Height in grid units.\n */\n height?: number;\n /**\n * Visualization (`BAR`, `LINE`, `AREA`, `PIE`, `TABLE`, …).\n */\n chartVisualizationType?:\n | \"CHART_VISUALIZATION_TYPE_UNSPECIFIED\"\n | \"BAR\"\n | \"LINE\"\n | \"AREA\"\n | \"PIE\"\n | \"SCATTER\"\n | \"TABLE\"\n | \"SCORE_CARD\"\n | \"SUNBURST\"\n | \"GAUGE\"\n | \"SANKEY\";\n /**\n * Date range applied to the chart.\n */\n dateRangeConfig?: DateRangeConfig;\n /**\n * Query or generative-insights data source.\n */\n dataSource?: cci.GoogleCloudContactcenterinsightsV1ChartDataSource;\n /**\n * Click action (redirect or conversation filter).\n */\n action?: cci.GoogleCloudContactcenterinsightsV1ChartAction;\n};\n\nexport type DashboardsChart = Resource\u003c\n \"GCP.ContactCenterInsights.DashboardsChart\",\n DashboardsChartProps,\n {\n /** Full resource name. */\n name: string;\n /** Chart id (last path segment). */\n chartId: string;\n /** Parent dashboard resource name. */\n parent: 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 /** Chart filter. */\n filter: string | undefined;\n /** Width in grid units. */\n width: number | undefined;\n /** Height in grid units. */\n height: number | undefined;\n /** Server-assigned chart type. */\n chartType: string | undefined;\n /** Visualization type. */\n chartVisualizationType: string | undefined;\n /** Date range config. */\n dateRangeConfig: DateRangeConfig | undefined;\n /** Data source. */\n dataSource: cci.GoogleCloudContactcenterinsightsV1ChartDataSource | undefined;\n /** Click action. */\n action: cci.GoogleCloudContactcenterinsightsV1ChartAction | undefined;\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 chart widget on a Contact Center Insights dashboard.\n *\n * Parent dashboard and chart id are immutable. Charts have no labels\n * field — Alchemy stamps ownership into the description. Display name,\n * description, filter, size, visualization, and data source update in\n * place.\n *\n * ### Creating a Chart\n * **Example:** Bar chart on a dashboard\n * ```typescript\n * const chart = yield* GCP.ContactCenterInsights.DashboardsChart(\"Volume\", {\n * parent: dashboard.name,\n * displayName: \"volume\",\n * chartVisualizationType: \"BAR\",\n * });\n * ```\n *\n * @resource\n * @category ContactCenterInsights\n */\nexport const DashboardsChart = Resource\u003cDashboardsChart>(\n \"GCP.ContactCenterInsights.DashboardsChart\",\n);\n\nexport class DashboardsChartNotResolved extends Data.TaggedError(\n \"GCP.ContactCenterInsights.DashboardsChartNotResolved\",\n)\u003c{\n resourceName: string;\n}> {}\n\nconst resourceName = (parent: string, chartId: string) => `${parent}/charts/${chartId}`;\n\nconst optionalString = (value: string | undefined): string | undefined => value;\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 = (chart: cci.GoogleCloudContactcenterinsightsV1Chart, project: string) => {\n const name = chart.name ?? \"\";\n const parsed = parseOwnership(chart.description);\n return {\n name,\n chartId: lastSegment(name),\n parent: parentOf(name),\n location: locationOf(name),\n project,\n displayName: chart.displayName,\n description: parsed.text,\n filter: chart.filter,\n width: chart.width,\n height: chart.height,\n chartType: optionalString(chart.chartType),\n chartVisualizationType: optionalString(chart.chartVisualizationType),\n dateRangeConfig: toDateRangeConfig(chart.dateRangeConfig),\n dataSource: chart.dataSource,\n action: chart.action,\n createTime: chart.createTime,\n updateTime: chart.updateTime,\n };\n};\n\nconst getByName = (name: string) =>\n name.length === 0\n ? Effect.undefined\n : cci\n .getProjectsLocationsDashboardsCharts({ name })\n .pipe(Effect.catchTag(\"NotFound\", () => Effect.undefined));\n\nconst listDashboards = (parent: string) =>\n cci.listProjectsLocationsDashboards.pages({ parent, pageSize: 100 }).pipe(\n Stream.flatMap((page) => Stream.fromIterable(page.dashboards ?? [])),\n Stream.map((dashboard) => dashboard.name ?? \"\"),\n Stream.filter((name) => name.length > 0),\n Stream.runCollect,\n Effect.map((chunk) => Array.from(chunk)),\n Effect.catchTag(\"NotFound\", () => Effect.succeed([] as string[])),\n );\n\nconst listAtParent = (parent: string, project: string) =>\n cci.listProjectsLocationsDashboardsCharts({ parent }).pipe(\n Effect.map((page) =>\n (page.charts ?? [])\n .filter((chart) => hasOwnershipMarker(chart.description))\n .map((chart) => toAttrs(chart, project)),\n ),\n Effect.catchTag(\"NotFound\", () => Effect.succeed([])),\n );\n\nexport const DashboardsChartProvider = () =>\n Provider.succeed(DashboardsChart, {\n stables: [\"name\", \"chartId\", \"parent\", \"location\", \"project\", \"createTime\"],\n\n diff: Effect.fn(function* ({ news, olds, output }) {\n if (!isResolved(news)) return undefined;\n const previousParent = olds?.parent ?? output?.parent;\n if (previousParent !== undefined && news.parent !== previousParent) {\n return { action: \"replace\" as const, deleteFirst: false };\n }\n const previousId = olds?.chartId ?? output?.chartId;\n if (previousId !== undefined && news.chartId !== undefined && news.chartId !== previousId) {\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 chartId = yield* toResourceId(id, olds?.chartId, output?.chartId);\n const name =\n output?.name ?? (olds?.parent !== undefined ? resourceName(olds.parent, chartId) : \"\");\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 const dashboards = yield* listDashboards(locationParent(env.project, env.region));\n const pages = yield* Effect.forEach(\n dashboards,\n (parent) => listAtParent(parent, env.project),\n { concurrency: 4 },\n );\n return pages.flat();\n }),\n\n reconcile: Effect.fn(function* ({ id, news, output }) {\n const env = yield* GcpEnvironment.current;\n const chartId = yield* toResourceId(id, news.chartId, output?.chartId);\n const name = resourceName(news.parent, chartId);\n const ownership = yield* createInternalLabels(id);\n const description = encodeOwnership(ownership, news.description);\n const displayName = news.displayName ?? chartId;\n\n let current = yield* getByName(output?.name ?? name);\n\n if (current === undefined) {\n const created = yield* cci\n .createProjectsLocationsDashboardsCharts({\n parent: news.parent,\n chartId,\n body: {\n displayName,\n description,\n filter: news.filter,\n width: news.width,\n height: news.height,\n chartVisualizationType: news.chartVisualizationType,\n dateRangeConfig: news.dateRangeConfig,\n dataSource: news.dataSource,\n action: news.action,\n },\n })\n .pipe(Effect.catchTag(\"Conflict\", () => getByName(name)));\n current = created ?? undefined;\n }\n\n if (current === undefined) {\n return yield* new DashboardsChartNotResolved({ 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 widthChanged = (current.width ?? 0) !== (news.width ?? 0);\n const heightChanged = (current.height ?? 0) !== (news.height ?? 0);\n const vizChanged =\n (current.chartVisualizationType ?? \"\") !== (news.chartVisualizationType ?? \"\");\n const dateRangeChanged = !sameJson(current.dateRangeConfig, news.dateRangeConfig);\n const dataSourceChanged = !sameJson(current.dataSource, news.dataSource);\n const actionChanged = !sameJson(current.action, news.action);\n\n if (\n displayChanged ||\n descriptionChanged ||\n filterChanged ||\n widthChanged ||\n heightChanged ||\n vizChanged ||\n dateRangeChanged ||\n dataSourceChanged ||\n actionChanged\n ) {\n current = yield* cci.patchProjectsLocationsDashboardsCharts({\n name: current.name ?? name,\n updateMask: \"*\",\n body: {\n name: current.name ?? name,\n displayName,\n description,\n filter: news.filter,\n width: news.width,\n height: news.height,\n chartVisualizationType: news.chartVisualizationType,\n dateRangeConfig: news.dateRangeConfig,\n dataSource: news.dataSource,\n action: news.action,\n },\n });\n }\n\n return toAttrs(current, env.project);\n }),\n\n delete: Effect.fn(function* ({ output }) {\n yield* cci\n .deleteProjectsLocationsDashboardsCharts({ name: output.name })\n .pipe(Effect.catchTag(\"NotFound\", () => Effect.void));\n }),\n });\n","preview":null,"history":null,"version":"ab425278970fdc57d267734a007cac58ade9052c"},"/_session?shell=1":null}