sailfork / t3code-github / .repos/ alchemy-effect/ packages/ gcp/ src/ Healthcare/ internal.ts

import * as healthcare from "@distilled.cloud/gcp/healthcare_v1";
import { createPhysicalName } from "alchemy/PhysicalName";
import { tagRecord } from "alchemy/Tags";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Schedule from "effect/Schedule";
import * as Stream from "effect/Stream";
import { isTransientGcpError } from "../Errors.ts";
import {
  alchemyLabelKeys,
  createInternalLabels,
  hasAlchemyLabels,
  stripInternalLabels,
  toLabels,
} from "../Labels.ts";

export const MAX_ID_LENGTH = 256;

export const lastSegment = (value: string) => {
  const trimmed = value.replace(/\/+$/, "");
  const parts = trimmed.split("/");
  return parts[parts.length - 1] || trimmed;
};

export const parentOf = (name: string) => {
  const parts = name.split("/").filter((part) => part.length > 0);
  return parts.slice(0, -2).join("/");
};

export const locationParent = (project: string, location: string) =>
  `projects/${project}/locations/${location}`;

export const normalizeLocation = (location: string | undefined, defaultLocation: string) =>
  lastSegment(location ?? defaultLocation).toLowerCase();

export const sameText = (left: string | undefined, right: string | undefined) =>
  (left ?? "") === (right ?? "");

export const sameJson = (left: unknown, right: unknown) =>
  JSON.stringify(left ?? null) === JSON.stringify(right ?? null);

export const updateMaskOf = (...fields: Array<string | undefined>) =>
  fields.filter((field): field is string => field !== undefined).join(",");

export const parseResourceName = (name: string, collection: string, defaultLocation: string) => {
  const parts = name.split("/").filter((part) => part.length > 0);
  const collectionAt = parts.lastIndexOf(collection);
  const locationsAt = parts.lastIndexOf("locations");
  const projectsAt = parts.lastIndexOf("projects");
  return {
    project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
    location:
      locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : defaultLocation,
    id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
    parent:
      collectionAt > 0
        ? parts.slice(0, collectionAt).join("/")
        : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
  };
};

export const expandParent = (
  value: string,
  project: string,
  location: string,
  collection: string,
) => {
  if (value.includes("/")) return value.replace(/\/+$/, "");
  return `projects/${project}/locations/${location}/${collection}/${value}`;
};

export const userLabels = (
  labels: Record<string, string | undefined> | null | undefined,
): Record<string, string> => stripInternalLabels(tagRecord(labels));

export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
  Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));

export const withOwnershipMetadata = (
  metadata: Record<string, string> | undefined,
  ownership: Record<string, string>,
) => ({ ...toLabels(metadata), ...ownership });

export const encodeDataId = (labels: Record<string, string>, dataId: string): string =>
  `alc-${labels[alchemyLabelKeys.id] ?? "x"}-${dataId}`;

export const decodeDataId = (dataId: string | undefined): string | undefined => {
  if (dataId === undefined) return undefined;
  if (!dataId.startsWith("alc-")) return dataId;
  const rest = dataId.slice("alc-".length);
  const dash = rest.indexOf("-");
  return dash >= 0 ? rest.slice(dash + 1) : dataId;
};

export const hasOwnedDataId = (dataId: string | undefined) => (dataId ?? "").startsWith("alc-");

export const toPhysicalId = (
  id: string,
  explicit: string | undefined,
  existing: string | undefined,
  maxLength = MAX_ID_LENGTH,
) =>
  Effect.gen(function* () {
    if (explicit !== undefined) return explicit;
    if (existing !== undefined) return existing;
    const generated = yield* createPhysicalName({
      id,
      maxLength,
      lowercase: true,
    });
    const next = /^[a-z]/.test(generated) ? generated : `h${generated}`.slice(0, maxLength);
    return next.length >= 1 ? next : "h";
  });

export const toPhysicalSnake = (
  id: string,
  explicit: string | undefined,
  existing: string | undefined,
  maxLength = MAX_ID_LENGTH,
) =>
  Effect.gen(function* () {
    if (explicit !== undefined) return explicit;
    if (existing !== undefined) return existing;
    const generated = yield* createPhysicalName({
      id,
      maxLength,
      lowercase: true,
      delimiter: "_",
    });
    let next = generated.replace(/[^a-z0-9_]/g, "_").replace(/_+/g, "_");
    if (!/^[a-z]/.test(next)) next = `a${next}`.slice(0, maxLength);
    next = next.slice(0, maxLength).replace(/_+$/g, "");
    if (next.length === 0) next = "attr";
    if (!next.startsWith("attr_")) {
      next = `attr_${next}`.slice(0, maxLength);
    }
    return next;
  });

export const replaceOnIdentity = (input: {
  previousId?: string;
  nextId?: string;
  previousParent?: string;
  nextParent?: string;
  extra?: boolean;
}) => {
  if (input.extra === true) {
    return { action: "replace" as const, deleteFirst: false };
  }
  if (
    input.previousId !== undefined &&
    input.nextId !== undefined &&
    input.previousId !== input.nextId
  ) {
    return { action: "replace" as const, deleteFirst: false };
  }
  if (
    input.previousParent !== undefined &&
    input.nextParent !== undefined &&
    input.previousParent !== input.nextParent
  ) {
    return { action: "replace" as const, deleteFirst: false };
  }
  return undefined;
};

export const retryTransient = <A, E extends { readonly _tag: string }, R>(
  effect: Effect.Effect<A, E, R>,
) =>
  effect.pipe(
    Effect.retry({
      while: (error) => isTransientGcpError(error) || error._tag === "NotFound",
      times: 8,
      schedule: Schedule.exponential("250 millis"),
    }),
  );

export class DeleteNotConfirmed extends Data.TaggedError("GCP.Healthcare.DeleteNotConfirmed")<{}> {}

/** Poll until the resource is gone; fails if it is still readable after ~60s. */
export const waitUntilGone = <A, E, R>(get: Effect.Effect<A | undefined, E, R>, _name: string) =>
  get.pipe(
    Effect.repeat({
      schedule: Schedule.spaced("2 seconds"),
      until: (value) => value === undefined,
      times: 30,
    }),
    Effect.flatMap((value): Effect.Effect<void, DeleteNotConfirmed> =>
      value === undefined ? Effect.void : Effect.fail(new DeleteNotConfirmed()),
    ),
  );

const emptyList = <A>() => Effect.succeed<A[]>([]);

export const collectPages = <Page, Item, E extends { readonly _tag: string }, R>(
  pages: Stream.Stream<Page, E, R>,
  items: (page: Page) => readonly Item[] | null | undefined,
) =>
  pages.pipe(
    Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
    Stream.runCollect,
    Effect.map((chunk) => Array.from(chunk) as Item[]),
    Effect.catchIf(
      (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound",
      () => emptyList<Item>(),
    ),
  );

export const encodeHl7 = (value: string) =>
  Effect.sync(() => Buffer.from(value, "utf8").toString("base64"));

export const decodeHl7 = (value: string | undefined) =>
  Effect.sync(() => {
    if (value === undefined || value.length === 0) return "";
    return Buffer.from(value, "base64").toString("utf8");
  });

export const listDatasets = (parent: string) =>
  parent.length === 0
    ? emptyList<healthcare.Dataset>()
    : healthcare.listProjectsLocationsDatasets.pages({ parent, pageSize: 100 }).pipe(
        Stream.flatMap((page) => Stream.fromIterable(page.datasets ?? [])),
        Stream.runCollect,
        Effect.map((chunk) => Array.from(chunk)),
        Effect.catchTag("NotFound", () => emptyList<healthcare.Dataset>()),
      );

export const listHl7V2Stores = (parent: string) =>
  parent.length === 0
    ? emptyList<healthcare.Hl7V2Store>()
    : healthcare.listProjectsLocationsDatasetsHl7V2Stores.pages({ parent, pageSize: 100 }).pipe(
        Stream.flatMap((page) => Stream.fromIterable(page.hl7V2Stores ?? [])),
        Stream.runCollect,
        Effect.map((chunk) => Array.from(chunk)),
        Effect.catchTag("NotFound", () => emptyList<healthcare.Hl7V2Store>()),
      );

export const listMessages = (parent: string) =>
  parent.length === 0
    ? emptyList<healthcare.Message>()
    : healthcare.listProjectsLocationsDatasetsHl7V2StoresMessages
        .pages({ parent, pageSize: 100, view: "FULL" })
        .pipe(
          Stream.flatMap((page) => Stream.fromIterable(page.hl7V2Messages ?? [])),
          Stream.runCollect,
          Effect.map((chunk) => Array.from(chunk)),
          Effect.catchTag("NotFound", () => emptyList<healthcare.Message>()),
        );

// `us-central1` was the fixed default before `GCP.Region`; list paths keep
// it alongside the stack region so datasets created under it are found.
export const listRegionDatasets = (project: string, region: string) =>
  Effect.forEach([...new Set([region, "us-central1"])], (location) =>
    listDatasets(locationParent(project, location)),
  ).pipe(Effect.map((groups) => groups.flat()));

export const forEachDataset = <A, E, R>(
  project: string,
  region: string,
  list: (parent: string) => Effect.Effect<A[], E, R>,
) =>
  Effect.gen(function* () {
    const datasets = yield* listRegionDatasets(project, region);
    const named = datasets.filter((dataset) => (dataset.name ?? "").length > 0);
    const groups = yield* Effect.forEach(named, (dataset) => list(dataset.name!), {
      concurrency: 4,
    });
    return groups.flat();
  });

export const listAlchemyConsentStores = (project: string, region: string) =>
  forEachDataset(project, region, (parent) =>
    collectPages(
      healthcare.listProjectsLocationsDatasetsConsentStores.pages({
        parent,
        pageSize: 1000,
      }),
      (page) => page.consentStores,
    ),
  ).pipe(Effect.map((stores) => stores.filter((store) => hasAlchemyLabelMap(store.labels))));

const markerOf = (labels: Record<string, string>, stack: string, stage: string, id: string) =>
  `[alchemy ${alchemyLabelKeys.stack}=${stack} ${alchemyLabelKeys.stage}=${stage} ${alchemyLabelKeys.id}=${id}]`;

const fitMarker = (labels: Record<string, string>, maxLength: number) => {
  let stack = labels[alchemyLabelKeys.stack] ?? "x";
  let stage = labels[alchemyLabelKeys.stage] ?? "x";
  let id = labels[alchemyLabelKeys.id] ?? "x";
  let marker = markerOf(labels, stack, stage, id);
  while (marker.length > maxLength && (stack.length > 1 || stage.length > 1 || id.length > 1)) {
    if (stack.length >= stage.length && stack.length >= id.length) {
      stack = stack.slice(0, -1);
    } else if (stage.length >= id.length) {
      stage = stage.slice(0, -1);
    } else {
      id = id.slice(0, -1);
    }
    marker = markerOf(labels, stack, stage, id);
  }
  return marker.slice(0, maxLength);
};

export const encodeOwnership = (
  labels: Record<string, string>,
  text: string | undefined,
): string => {
  const marker = fitMarker(labels, 8000);
  const trimmed = text?.trim();
  return trimmed && trimmed.length > 0 ? `${marker}\n${trimmed}` : marker;
};

export const parseOwnership = (
  text: string | undefined,
): {
  labels: Record<string, string>;
  text: string | undefined;
} => {
  if (!text?.startsWith("[alchemy ")) {
    return { labels: {}, text };
  }
  const end = text.indexOf("]");
  if (end < 0) return { labels: {}, text };
  const labels: Record<string, string> = {};
  for (const part of text.slice("[alchemy ".length, end).split(/\s+/)) {
    const eq = part.indexOf("=");
    if (eq > 0) {
      labels[part.slice(0, eq)] = part.slice(eq + 1);
    }
  }
  const rest = text.slice(end + 1).replace(/^[\s\n]+/, "");
  return { labels, text: rest.length > 0 ? rest : undefined };
};

export const hasOwnershipMarker = (text: string | undefined) =>
  Object.keys(parseOwnership(text).labels).some((key) => key.startsWith("alchemy-"));

const prefixMatch = (expected: string, observed: string) =>
  expected === observed || expected.startsWith(observed) || observed.startsWith(expected);

export const ownedByAlchemy = (id: string, text: string | undefined) =>
  Effect.gen(function* () {
    const expected = yield* createInternalLabels(id);
    const { labels } = parseOwnership(text);
    if (!hasOwnershipMarker(text)) return false;
    const exact = yield* hasAlchemyLabels(id, labels);
    if (exact) return true;
    return (
      prefixMatch(expected[alchemyLabelKeys.stack] ?? "", labels[alchemyLabelKeys.stack] ?? "") &&
      prefixMatch(expected[alchemyLabelKeys.stage] ?? "", labels[alchemyLabelKeys.stage] ?? "") &&
      prefixMatch(expected[alchemyLabelKeys.id] ?? "", labels[alchemyLabelKeys.id] ?? "")
    );
  });