The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as sasportal from "@distilled.cloud/gcp/sasportal_v1alpha1"; |
| 2 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Schedule from "effect/Schedule"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 8 | |
| 9 | export const MAX_DISPLAY_NAME_LENGTH = 256; |
| 10 | export const DIRECT_CHILDREN = "DIRECT_CHILDREN"; |
| 11 | |
| 12 | export const lastSegment = (value: string) => { |
| 13 | const trimmed = value.replace(/\/+$/, ""); |
| 14 | const parts = trimmed.split("/"); |
| 15 | return parts[parts.length - 1] || trimmed; |
| 16 | }; |
| 17 | |
| 18 | export const parentOf = (name: string) => { |
| 19 | const parts = name.split("/").filter((part) => part.length > 0); |
| 20 | if (parts.length >= 4) return parts.slice(0, -2).join("/"); |
| 21 | return ""; |
| 22 | }; |
| 23 | |
| 24 | export const expandName = (value: string, collection: string) => { |
| 25 | const trimmed = value.replace(/\/+$/, "").trim(); |
| 26 | if (trimmed.includes("/")) return trimmed; |
| 27 | return `${collection}/${trimmed}`; |
| 28 | }; |
| 29 | |
| 30 | export const expandCustomer = (value: string) => expandName(value, "customers"); |
| 31 | |
| 32 | export const expandNode = (value: string) => expandName(value, "nodes"); |
| 33 | |
| 34 | export const expandPath = (value: string) => value.replace(/\/+$/, "").trim(); |
| 35 | |
| 36 | export const sameText = (left: string | undefined, right: string | undefined) => |
| 37 | (left ?? "") === (right ?? ""); |
| 38 | |
| 39 | export const sameJson = (left: unknown, right: unknown) => |
| 40 | JSON.stringify(left ?? null) === JSON.stringify(right ?? null); |
| 41 | |
| 42 | export const sameStringList = ( |
| 43 | left: readonly string[] | undefined, |
| 44 | right: readonly string[] | undefined, |
| 45 | ) => sameJson([...(left ?? [])].slice().sort(), [...(right ?? [])].slice().sort()); |
| 46 | |
| 47 | export const updateMaskOf = (...fields: Array<string | undefined>) => |
| 48 | fields.filter((field): field is string => field !== undefined).join(","); |
| 49 | |
| 50 | export const replaceOnIdentity = (input: { |
| 51 | previousId?: string; |
| 52 | nextId?: string; |
| 53 | previousParent?: string; |
| 54 | nextParent?: string; |
| 55 | extra?: boolean; |
| 56 | }) => { |
| 57 | if (input.extra === true) { |
| 58 | return { action: "replace" as const, deleteFirst: false }; |
| 59 | } |
| 60 | if ( |
| 61 | input.previousId !== undefined && |
| 62 | input.nextId !== undefined && |
| 63 | input.previousId !== input.nextId |
| 64 | ) { |
| 65 | return { action: "replace" as const, deleteFirst: false }; |
| 66 | } |
| 67 | if ( |
| 68 | input.previousParent !== undefined && |
| 69 | input.nextParent !== undefined && |
| 70 | input.previousParent !== input.nextParent |
| 71 | ) { |
| 72 | return { action: "replace" as const, deleteFirst: false }; |
| 73 | } |
| 74 | return undefined; |
| 75 | }; |
| 76 | |
| 77 | const markerOf = (_labels: Record<string, string>, stack: string, stage: string, id: string) => |
| 78 | `[alchemy ${alchemyLabelKeys.stack}=${stack} ${alchemyLabelKeys.stage}=${stage} ${alchemyLabelKeys.id}=${id}]`; |
| 79 | |
| 80 | const fitMarker = (labels: Record<string, string>, maxLength: number) => { |
| 81 | let stack = labels[alchemyLabelKeys.stack] ?? "x"; |
| 82 | let stage = labels[alchemyLabelKeys.stage] ?? "x"; |
| 83 | let id = labels[alchemyLabelKeys.id] ?? "x"; |
| 84 | let marker = markerOf(labels, stack, stage, id); |
| 85 | while (marker.length > maxLength && (stack.length > 1 || stage.length > 1 || id.length > 1)) { |
| 86 | if (stack.length >= stage.length && stack.length >= id.length) { |
| 87 | stack = stack.slice(0, -1); |
| 88 | } else if (stage.length >= id.length) { |
| 89 | stage = stage.slice(0, -1); |
| 90 | } else { |
| 91 | id = id.slice(0, -1); |
| 92 | } |
| 93 | marker = markerOf(labels, stack, stage, id); |
| 94 | } |
| 95 | return marker.slice(0, maxLength); |
| 96 | }; |
| 97 | |
| 98 | export const encodeOwnershipLine = ( |
| 99 | labels: Record<string, string>, |
| 100 | text: string | undefined, |
| 101 | maxLength = MAX_DISPLAY_NAME_LENGTH, |
| 102 | ): string => { |
| 103 | const trimmed = text?.replace(/[\r\n]+/g, " ").trim(); |
| 104 | if (!trimmed) return fitMarker(labels, maxLength); |
| 105 | const minMarker = 24; |
| 106 | const reserved = Math.min(trimmed.length + 1, Math.max(0, maxLength - minMarker)); |
| 107 | const marker = fitMarker(labels, maxLength - reserved); |
| 108 | return `${marker} ${trimmed}`.slice(0, maxLength); |
| 109 | }; |
| 110 | |
| 111 | export const parseOwnership = ( |
| 112 | text: string | undefined, |
| 113 | ): { |
| 114 | labels: Record<string, string>; |
| 115 | text: string | undefined; |
| 116 | } => { |
| 117 | if (!text?.startsWith("[alchemy ")) { |
| 118 | return { labels: {}, text }; |
| 119 | } |
| 120 | const end = text.indexOf("]"); |
| 121 | if (end < 0) return { labels: {}, text }; |
| 122 | const labels: Record<string, string> = {}; |
| 123 | for (const part of text.slice("[alchemy ".length, end).split(/\s+/)) { |
| 124 | const eq = part.indexOf("="); |
| 125 | if (eq > 0) { |
| 126 | labels[part.slice(0, eq)] = part.slice(eq + 1); |
| 127 | } |
| 128 | } |
| 129 | const rest = text.slice(end + 1).replace(/^[\s\n]+/, ""); |
| 130 | return { labels, text: rest.length > 0 ? rest : undefined }; |
| 131 | }; |
| 132 | |
| 133 | export const hasOwnershipMarker = (text: string | undefined) => |
| 134 | Object.keys(parseOwnership(text).labels).some((key) => key.startsWith("alchemy-")); |
| 135 | |
| 136 | const prefixMatch = (expected: string, observed: string) => |
| 137 | expected === observed || expected.startsWith(observed) || observed.startsWith(expected); |
| 138 | |
| 139 | export const ownedByAlchemy = (id: string, text: string | undefined) => |
| 140 | Effect.gen(function* () { |
| 141 | const expected = yield* createInternalLabels(id); |
| 142 | const { labels } = parseOwnership(text); |
| 143 | if (!hasOwnershipMarker(text)) return false; |
| 144 | const exact = yield* hasAlchemyLabels(id, labels); |
| 145 | if (exact) return true; |
| 146 | return ( |
| 147 | prefixMatch(expected[alchemyLabelKeys.stack] ?? "", labels[alchemyLabelKeys.stack] ?? "") && |
| 148 | prefixMatch(expected[alchemyLabelKeys.stage] ?? "", labels[alchemyLabelKeys.stage] ?? "") && |
| 149 | prefixMatch(expected[alchemyLabelKeys.id] ?? "", labels[alchemyLabelKeys.id] ?? "") |
| 150 | ); |
| 151 | }); |
| 152 | |
| 153 | export const ownershipLabels = (id: string) => createInternalLabels(id); |
| 154 | |
| 155 | export const toDisplayName = ( |
| 156 | id: string, |
| 157 | requested: string | undefined, |
| 158 | existing: string | undefined, |
| 159 | maxLength = 40, |
| 160 | ) => |
| 161 | Effect.gen(function* () { |
| 162 | if (requested !== undefined && requested.length > 0) return requested; |
| 163 | if (existing !== undefined && existing.length > 0) return existing; |
| 164 | const generated = yield* createPhysicalName({ |
| 165 | id, |
| 166 | maxLength, |
| 167 | lowercase: true, |
| 168 | }); |
| 169 | const next = /^[a-z]/.test(generated) ? generated : `s${generated}`.slice(0, maxLength); |
| 170 | return next.length > 0 ? next : "s"; |
| 171 | }); |
| 172 | |
| 173 | export const toSerialNumber = ( |
| 174 | id: string, |
| 175 | requested: string | undefined, |
| 176 | existing: string | undefined, |
| 177 | ) => |
| 178 | Effect.gen(function* () { |
| 179 | if (requested !== undefined && requested.length > 0) return requested; |
| 180 | if (existing !== undefined && existing.length > 0) return existing; |
| 181 | const generated = yield* createPhysicalName({ |
| 182 | id, |
| 183 | maxLength: 16, |
| 184 | lowercase: true, |
| 185 | }); |
| 186 | const serial = generated.replace(/[^a-z0-9]/g, "").slice(0, 16); |
| 187 | return serial.length > 0 ? serial.toUpperCase() : "SN1"; |
| 188 | }); |
| 189 | |
| 190 | export class DeleteNotConfirmed extends Data.TaggedError("GCP.SasPortal.DeleteNotConfirmed")<{}> {} |
| 191 | |
| 192 | /** Poll until the resource is gone; fails if it is still readable after ~60s. */ |
| 193 | export const waitUntilGone = <A, E, R>(get: Effect.Effect<A | undefined, E, R>) => |
| 194 | get.pipe( |
| 195 | Effect.repeat({ |
| 196 | schedule: Schedule.spaced("2 seconds"), |
| 197 | until: (value) => value === undefined, |
| 198 | times: 30, |
| 199 | }), |
| 200 | Effect.flatMap((value): Effect.Effect<void, DeleteNotConfirmed> => |
| 201 | value === undefined ? Effect.void : Effect.fail(new DeleteNotConfirmed()), |
| 202 | ), |
| 203 | ); |
| 204 | |
| 205 | export const retryDelete = <A, E extends { readonly _tag: string }, R>( |
| 206 | effect: Effect.Effect<A, E, R>, |
| 207 | ) => |
| 208 | effect.pipe( |
| 209 | Effect.retry({ |
| 210 | while: (error) => error._tag === "Conflict", |
| 211 | times: 8, |
| 212 | schedule: Schedule.exponential("250 millis"), |
| 213 | }), |
| 214 | ); |
| 215 | |
| 216 | const emptyList = <A>() => Effect.succeed([] as A[]); |
| 217 | |
| 218 | const isMissing = <E extends { readonly _tag: string }>( |
| 219 | error: E, |
| 220 | ): error is Extract<E, { readonly _tag: "NotFound" }> => error._tag === "NotFound"; |
| 221 | |
| 222 | export const collectPages = <Page, Item, E extends { readonly _tag: string }, R>( |
| 223 | pages: Stream.Stream<Page, E, R>, |
| 224 | items: (page: Page) => readonly Item[] | null | undefined, |
| 225 | ) => |
| 226 | pages.pipe( |
| 227 | Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])), |
| 228 | Stream.runCollect, |
| 229 | Effect.map((chunk) => Array.from(chunk) as Item[]), |
| 230 | Effect.catchIf(isMissing, () => emptyList<Item>()), |
| 231 | ); |
| 232 | |
| 233 | export const listCustomers = () => |
| 234 | collectPages(sasportal.listCustomers.pages({ pageSize: 100 }), (page) => page.customers); |
| 235 | |
| 236 | export const listCustomerDeployments = (parent: string) => |
| 237 | parent.length === 0 |
| 238 | ? emptyList<sasportal.SasPortalDeployment>() |
| 239 | : collectPages( |
| 240 | sasportal.listCustomersDeployments.pages({ |
| 241 | parent, |
| 242 | pageSize: 100, |
| 243 | filter: DIRECT_CHILDREN, |
| 244 | }), |
| 245 | (page) => page.deployments, |
| 246 | ); |
| 247 | |
| 248 | export const listCustomerDevices = (parent: string) => |
| 249 | parent.length === 0 |
| 250 | ? emptyList<sasportal.SasPortalDevice>() |
| 251 | : collectPages( |
| 252 | sasportal.listCustomersDevices.pages({ |
| 253 | parent, |
| 254 | pageSize: 1000, |
| 255 | }), |
| 256 | (page) => page.devices, |
| 257 | ); |
| 258 | |
| 259 | export const listCustomerNodes = (parent: string) => |
| 260 | parent.length === 0 |
| 261 | ? emptyList<sasportal.SasPortalNode>() |
| 262 | : collectPages( |
| 263 | sasportal.listCustomersNodes.pages({ |
| 264 | parent, |
| 265 | pageSize: 100, |
| 266 | filter: DIRECT_CHILDREN, |
| 267 | }), |
| 268 | (page) => page.nodes, |
| 269 | ); |
| 270 | |
| 271 | export const listNodeDevices = (parent: string) => |
| 272 | parent.length === 0 |
| 273 | ? emptyList<sasportal.SasPortalDevice>() |
| 274 | : collectPages( |
| 275 | sasportal.listNodesDevices.pages({ |
| 276 | parent, |
| 277 | pageSize: 1000, |
| 278 | }), |
| 279 | (page) => page.devices, |
| 280 | ); |
| 281 | |
| 282 | export const listNodeNodes = (parent: string) => |
| 283 | parent.length === 0 |
| 284 | ? emptyList<sasportal.SasPortalNode>() |
| 285 | : collectPages( |
| 286 | sasportal.listNodesNodes.pages({ |
| 287 | parent, |
| 288 | pageSize: 100, |
| 289 | filter: DIRECT_CHILDREN, |
| 290 | }), |
| 291 | (page) => page.nodes, |
| 292 | ); |
| 293 | |
| 294 | export const listCustomerDeploymentDevices = (parent: string) => |
| 295 | parent.length === 0 |
| 296 | ? emptyList<sasportal.SasPortalDevice>() |
| 297 | : collectPages( |
| 298 | sasportal.listCustomersDeploymentsDevices.pages({ |
| 299 | parent, |
| 300 | pageSize: 1000, |
| 301 | }), |
| 302 | (page) => page.devices, |
| 303 | ); |
| 304 | |
| 305 | export const listCustomerNodeDeployments = (parent: string) => |
| 306 | parent.length === 0 |
| 307 | ? emptyList<sasportal.SasPortalDeployment>() |
| 308 | : collectPages( |
| 309 | sasportal.listCustomersNodesDeployments.pages({ |
| 310 | parent, |
| 311 | pageSize: 100, |
| 312 | filter: DIRECT_CHILDREN, |
| 313 | }), |
| 314 | (page) => page.deployments, |
| 315 | ); |
| 316 | |
| 317 | export const listCustomerNodeDevices = (parent: string) => |
| 318 | parent.length === 0 |
| 319 | ? emptyList<sasportal.SasPortalDevice>() |
| 320 | : collectPages( |
| 321 | sasportal.listCustomersNodesDevices.pages({ |
| 322 | parent, |
| 323 | pageSize: 1000, |
| 324 | }), |
| 325 | (page) => page.devices, |
| 326 | ); |
| 327 | |
| 328 | export const listCustomerNodeNodes = (parent: string) => |
| 329 | parent.length === 0 |
| 330 | ? emptyList<sasportal.SasPortalNode>() |
| 331 | : collectPages( |
| 332 | sasportal.listCustomersNodesNodes.pages({ |
| 333 | parent, |
| 334 | pageSize: 100, |
| 335 | filter: DIRECT_CHILDREN, |
| 336 | }), |
| 337 | (page) => page.nodes, |
| 338 | ); |
| 339 | |
| 340 | export const listNodeDeployments = (parent: string) => |
| 341 | parent.length === 0 |
| 342 | ? emptyList<sasportal.SasPortalDeployment>() |
| 343 | : collectPages( |
| 344 | sasportal.listNodesDeployments.pages({ |
| 345 | parent, |
| 346 | pageSize: 100, |
| 347 | filter: DIRECT_CHILDREN, |
| 348 | }), |
| 349 | (page) => page.deployments, |
| 350 | ); |
| 351 | |
| 352 | export const listNodeDeploymentDevices = (parent: string) => |
| 353 | parent.length === 0 |
| 354 | ? emptyList<sasportal.SasPortalDevice>() |
| 355 | : collectPages( |
| 356 | sasportal.listNodesDeploymentsDevices.pages({ |
| 357 | parent, |
| 358 | pageSize: 1000, |
| 359 | }), |
| 360 | (page) => page.devices, |
| 361 | ); |
| 362 | |
| 363 | export const listNodeNodeDeployments = (parent: string) => |
| 364 | parent.length === 0 |
| 365 | ? emptyList<sasportal.SasPortalDeployment>() |
| 366 | : collectPages( |
| 367 | sasportal.listNodesNodesDeployments.pages({ |
| 368 | parent, |
| 369 | pageSize: 100, |
| 370 | filter: DIRECT_CHILDREN, |
| 371 | }), |
| 372 | (page) => page.deployments, |
| 373 | ); |
| 374 | |
| 375 | export const listNodeNodeDevices = (parent: string) => |
| 376 | parent.length === 0 |
| 377 | ? emptyList<sasportal.SasPortalDevice>() |
| 378 | : collectPages( |
| 379 | sasportal.listNodesNodesDevices.pages({ |
| 380 | parent, |
| 381 | pageSize: 1000, |
| 382 | }), |
| 383 | (page) => page.devices, |
| 384 | ); |
| 385 | |
| 386 | export const listNodeNodeNodes = (parent: string) => |
| 387 | parent.length === 0 |
| 388 | ? emptyList<sasportal.SasPortalNode>() |
| 389 | : collectPages( |
| 390 | sasportal.listNodesNodesNodes.pages({ |
| 391 | parent, |
| 392 | pageSize: 100, |
| 393 | filter: DIRECT_CHILDREN, |
| 394 | }), |
| 395 | (page) => page.nodes, |
| 396 | ); |
| 397 | |
| 398 | export type LocatedNode = { |
| 399 | node: sasportal.SasPortalNode; |
| 400 | parent: string; |
| 401 | }; |
| 402 | |
| 403 | export const walkNodes = () => |
| 404 | Effect.gen(function* () { |
| 405 | const customers = yield* listCustomers(); |
| 406 | const located: LocatedNode[] = []; |
| 407 | const seen = new Set<string>(); |
| 408 | const queue: Array<{ parent: string; underCustomer: boolean }> = customers.flatMap( |
| 409 | (customer) => (customer.name ? [{ parent: customer.name, underCustomer: true }] : []), |
| 410 | ); |
| 411 | while (queue.length > 0) { |
| 412 | const batch = queue.splice(0, queue.length); |
| 413 | const pages = yield* Effect.forEach( |
| 414 | batch, |
| 415 | (item) => |
| 416 | (item.underCustomer ? listCustomerNodes(item.parent) : listNodeNodes(item.parent)).pipe( |
| 417 | Effect.map((nodes) => nodes.map((node) => ({ node, parent: item.parent }))), |
| 418 | ), |
| 419 | { concurrency: 4 }, |
| 420 | ); |
| 421 | for (const entry of pages.flat()) { |
| 422 | const name = entry.node.name ?? ""; |
| 423 | if (name.length === 0 || seen.has(name)) continue; |
| 424 | seen.add(name); |
| 425 | located.push(entry); |
| 426 | queue.push({ parent: name, underCustomer: false }); |
| 427 | } |
| 428 | } |
| 429 | return located; |
| 430 | }); |
| 431 | |
| 432 | const catchMissing = <A, E extends { readonly _tag: string }, R>(effect: Effect.Effect<A, E, R>) => |
| 433 | effect.pipe(Effect.catchIf(isMissing, () => Effect.undefined)); |
| 434 | |
| 435 | export const getCustomerDeployment = (name: string) => |
| 436 | name.length === 0 ? Effect.undefined : catchMissing(sasportal.getCustomersDeployments({ name })); |
| 437 | |
| 438 | export const getCustomerDevice = (name: string) => |
| 439 | name.length === 0 ? Effect.undefined : catchMissing(sasportal.getCustomersDevices({ name })); |
| 440 | |
| 441 | export const getCustomerNode = (name: string) => |
| 442 | name.length === 0 ? Effect.undefined : catchMissing(sasportal.getCustomersNodes({ name })); |
| 443 | |
| 444 | export const getNodeDevice = (name: string) => |
| 445 | name.length === 0 ? Effect.undefined : catchMissing(sasportal.getNodesDevices({ name })); |
| 446 | |
| 447 | export const getNodeNode = (name: string) => |
| 448 | name.length === 0 ? Effect.undefined : catchMissing(sasportal.getNodesNodes({ name })); |
| 449 | |
| 450 | export const getDeploymentDevice = (name: string) => |
| 451 | name.length === 0 ? Effect.undefined : catchMissing(sasportal.getDeploymentsDevices({ name })); |
| 452 | |
| 453 | export const getNodeDeployment = (name: string) => |
| 454 | name.length === 0 ? Effect.undefined : catchMissing(sasportal.getNodesDeployments({ name })); |
| 455 | |
| 456 | export const findOwned = <A>( |
| 457 | id: string, |
| 458 | rows: readonly A[], |
| 459 | displayName: (row: A) => string | undefined, |
| 460 | ) => |
| 461 | Effect.gen(function* () { |
| 462 | for (const row of rows) { |
| 463 | if (yield* ownedByAlchemy(id, displayName(row))) { |
| 464 | return row; |
| 465 | } |
| 466 | } |
| 467 | return undefined; |
| 468 | }); |
| 469 | |
| 470 | export const findOwnedCustomerDeployment = (id: string, parent: string) => |
| 471 | Effect.gen(function* () { |
| 472 | const rows = yield* listCustomerDeployments(parent); |
| 473 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 474 | return match === undefined ? undefined : { row: match, parent }; |
| 475 | }); |
| 476 | |
| 477 | export const findOwnedCustomerDevice = (id: string, parent: string) => |
| 478 | Effect.gen(function* () { |
| 479 | const rows = yield* listCustomerDevices(parent); |
| 480 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 481 | return match === undefined ? undefined : { row: match, parent }; |
| 482 | }); |
| 483 | |
| 484 | export const findOwnedCustomerNode = (id: string, parent: string) => |
| 485 | Effect.gen(function* () { |
| 486 | const rows = yield* listCustomerNodes(parent); |
| 487 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 488 | return match === undefined ? undefined : { row: match, parent }; |
| 489 | }); |
| 490 | |
| 491 | export const findOwnedNodeDevice = (id: string, parent: string) => |
| 492 | Effect.gen(function* () { |
| 493 | const rows = yield* listNodeDevices(parent); |
| 494 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 495 | return match === undefined ? undefined : { row: match, parent }; |
| 496 | }); |
| 497 | |
| 498 | export const findOwnedNodeNode = (id: string, parent: string) => |
| 499 | Effect.gen(function* () { |
| 500 | const rows = yield* listNodeNodes(parent); |
| 501 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 502 | return match === undefined ? undefined : { row: match, parent }; |
| 503 | }); |
| 504 | |
| 505 | export const findOwnedCustomerDeploymentDevice = (id: string, parent: string) => |
| 506 | Effect.gen(function* () { |
| 507 | const rows = yield* listCustomerDeploymentDevices(parent); |
| 508 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 509 | return match === undefined ? undefined : { row: match, parent }; |
| 510 | }); |
| 511 | |
| 512 | export const findOwnedCustomerNodeDeployment = (id: string, parent: string) => |
| 513 | Effect.gen(function* () { |
| 514 | const rows = yield* listCustomerNodeDeployments(parent); |
| 515 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 516 | return match === undefined ? undefined : { row: match, parent }; |
| 517 | }); |
| 518 | |
| 519 | export const findOwnedCustomerNodeDevice = (id: string, parent: string) => |
| 520 | Effect.gen(function* () { |
| 521 | const rows = yield* listCustomerNodeDevices(parent); |
| 522 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 523 | return match === undefined ? undefined : { row: match, parent }; |
| 524 | }); |
| 525 | |
| 526 | export const findOwnedCustomerNodeNode = (id: string, parent: string) => |
| 527 | Effect.gen(function* () { |
| 528 | const rows = yield* listCustomerNodeNodes(parent); |
| 529 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 530 | return match === undefined ? undefined : { row: match, parent }; |
| 531 | }); |
| 532 | |
| 533 | export const findOwnedNodeDeployment = (id: string, parent: string) => |
| 534 | Effect.gen(function* () { |
| 535 | const rows = yield* listNodeDeployments(parent); |
| 536 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 537 | return match === undefined ? undefined : { row: match, parent }; |
| 538 | }); |
| 539 | |
| 540 | export const findOwnedNodeDeploymentDevice = (id: string, parent: string) => |
| 541 | Effect.gen(function* () { |
| 542 | const rows = yield* listNodeDeploymentDevices(parent); |
| 543 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 544 | return match === undefined ? undefined : { row: match, parent }; |
| 545 | }); |
| 546 | |
| 547 | export const findOwnedNodeNodeDeployment = (id: string, parent: string) => |
| 548 | Effect.gen(function* () { |
| 549 | const rows = yield* listNodeNodeDeployments(parent); |
| 550 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 551 | return match === undefined ? undefined : { row: match, parent }; |
| 552 | }); |
| 553 | |
| 554 | export const findOwnedNodeNodeDevice = (id: string, parent: string) => |
| 555 | Effect.gen(function* () { |
| 556 | const rows = yield* listNodeNodeDevices(parent); |
| 557 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 558 | return match === undefined ? undefined : { row: match, parent }; |
| 559 | }); |
| 560 | |
| 561 | export const findOwnedNodeNodeNode = (id: string, parent: string) => |
| 562 | Effect.gen(function* () { |
| 563 | const rows = yield* listNodeNodeNodes(parent); |
| 564 | const match = yield* findOwned(id, rows, (row) => row.displayName); |
| 565 | return match === undefined ? undefined : { row: match, parent }; |
| 566 | }); |
| 567 | |
| 568 | export const findDeviceBySerial = ( |
| 569 | rows: readonly sasportal.SasPortalDevice[], |
| 570 | serialNumber: string | undefined, |
| 571 | ) => { |
| 572 | if (serialNumber === undefined || serialNumber.length === 0) { |
| 573 | return undefined; |
| 574 | } |
| 575 | return rows.find((row) => row.serialNumber === serialNumber); |
| 576 | }; |
| 577 | |
| 578 | export const scanOwnedCustomerDeployment = (id: string, parent?: string) => |
| 579 | Effect.gen(function* () { |
| 580 | if (parent !== undefined && parent.length > 0) { |
| 581 | const local = yield* findOwnedCustomerDeployment(id, parent); |
| 582 | if (local !== undefined) return local; |
| 583 | } |
| 584 | const customers = yield* listCustomers(); |
| 585 | for (const customer of customers) { |
| 586 | if (!customer.name) continue; |
| 587 | const found = yield* findOwnedCustomerDeployment(id, customer.name); |
| 588 | if (found !== undefined) return found; |
| 589 | } |
| 590 | return undefined; |
| 591 | }); |
| 592 | |
| 593 | export const scanOwnedCustomerDevice = (id: string, parent?: string) => |
| 594 | Effect.gen(function* () { |
| 595 | if (parent !== undefined && parent.length > 0) { |
| 596 | const local = yield* findOwnedCustomerDevice(id, parent); |
| 597 | if (local !== undefined) return local; |
| 598 | } |
| 599 | const customers = yield* listCustomers(); |
| 600 | for (const customer of customers) { |
| 601 | if (!customer.name) continue; |
| 602 | const found = yield* findOwnedCustomerDevice(id, customer.name); |
| 603 | if (found !== undefined) return found; |
| 604 | } |
| 605 | return undefined; |
| 606 | }); |
| 607 | |
| 608 | export const scanOwnedCustomerNode = (id: string, parent?: string) => |
| 609 | Effect.gen(function* () { |
| 610 | if (parent !== undefined && parent.length > 0) { |
| 611 | const local = yield* findOwnedCustomerNode(id, parent); |
| 612 | if (local !== undefined) return local; |
| 613 | } |
| 614 | const customers = yield* listCustomers(); |
| 615 | for (const customer of customers) { |
| 616 | if (!customer.name) continue; |
| 617 | const found = yield* findOwnedCustomerNode(id, customer.name); |
| 618 | if (found !== undefined) return found; |
| 619 | } |
| 620 | return undefined; |
| 621 | }); |
| 622 | |
| 623 | export const scanOwnedNodeDevice = (id: string, parent?: string) => |
| 624 | Effect.gen(function* () { |
| 625 | if (parent !== undefined && parent.length > 0) { |
| 626 | const local = yield* findOwnedNodeDevice(id, parent); |
| 627 | if (local !== undefined) return local; |
| 628 | } |
| 629 | const nodes = yield* walkNodes(); |
| 630 | for (const entry of nodes) { |
| 631 | const name = entry.node.name ?? ""; |
| 632 | if (name.length === 0) continue; |
| 633 | const found = yield* findOwnedNodeDevice(id, name); |
| 634 | if (found !== undefined) return found; |
| 635 | } |
| 636 | return undefined; |
| 637 | }); |
| 638 | |
| 639 | export const scanOwnedNodeNode = (id: string, parent?: string) => |
| 640 | Effect.gen(function* () { |
| 641 | if (parent !== undefined && parent.length > 0) { |
| 642 | const local = yield* findOwnedNodeNode(id, parent); |
| 643 | if (local !== undefined) return local; |
| 644 | } |
| 645 | const nodes = yield* walkNodes(); |
| 646 | for (const entry of nodes) { |
| 647 | const name = entry.node.name ?? ""; |
| 648 | if (name.length === 0) continue; |
| 649 | const found = yield* findOwnedNodeNode(id, name); |
| 650 | if (found !== undefined) return found; |
| 651 | } |
| 652 | return undefined; |
| 653 | }); |
| 654 | |
| 655 | export type LocatedDeployment = { |
| 656 | deployment: sasportal.SasPortalDeployment; |
| 657 | parent: string; |
| 658 | }; |
| 659 | |
| 660 | export const walkCustomerDeployments = () => |
| 661 | Effect.gen(function* () { |
| 662 | const customers = yield* listCustomers(); |
| 663 | const pages = yield* Effect.forEach( |
| 664 | customers, |
| 665 | (customer) => { |
| 666 | const parent = customer.name ?? ""; |
| 667 | return parent.length === 0 |
| 668 | ? Effect.succeed([] as LocatedDeployment[]) |
| 669 | : listCustomerDeployments(parent).pipe( |
| 670 | Effect.map((rows) => rows.map((deployment) => ({ deployment, parent }))), |
| 671 | ); |
| 672 | }, |
| 673 | { concurrency: 4 }, |
| 674 | ); |
| 675 | return pages.flat(); |
| 676 | }); |
| 677 | |
| 678 | export const walkCustomerNodes = () => |
| 679 | Effect.gen(function* () { |
| 680 | const customers = yield* listCustomers(); |
| 681 | const pages = yield* Effect.forEach( |
| 682 | customers, |
| 683 | (customer) => { |
| 684 | const parent = customer.name ?? ""; |
| 685 | return parent.length === 0 |
| 686 | ? Effect.succeed([] as LocatedNode[]) |
| 687 | : listCustomerNodes(parent).pipe( |
| 688 | Effect.map((rows) => rows.map((node) => ({ node, parent }))), |
| 689 | ); |
| 690 | }, |
| 691 | { concurrency: 4 }, |
| 692 | ); |
| 693 | return pages.flat(); |
| 694 | }); |
| 695 | |
| 696 | export const walkNodeDeployments = () => |
| 697 | Effect.gen(function* () { |
| 698 | const nodes = yield* walkNodes(); |
| 699 | const pages = yield* Effect.forEach( |
| 700 | nodes, |
| 701 | (entry) => { |
| 702 | const parent = entry.node.name ?? ""; |
| 703 | return parent.length === 0 |
| 704 | ? Effect.succeed([] as LocatedDeployment[]) |
| 705 | : listNodeDeployments(parent).pipe( |
| 706 | Effect.map((rows) => rows.map((deployment) => ({ deployment, parent }))), |
| 707 | ); |
| 708 | }, |
| 709 | { concurrency: 4 }, |
| 710 | ); |
| 711 | return pages.flat(); |
| 712 | }); |
| 713 | |
| 714 | export const walkNodeChildren = () => |
| 715 | Effect.gen(function* () { |
| 716 | const nodes = yield* walkNodes(); |
| 717 | const pages = yield* Effect.forEach( |
| 718 | nodes, |
| 719 | (entry) => { |
| 720 | const parent = entry.node.name ?? ""; |
| 721 | return parent.length === 0 |
| 722 | ? Effect.succeed([] as LocatedNode[]) |
| 723 | : listNodeNodes(parent).pipe( |
| 724 | Effect.map((rows) => rows.map((node) => ({ node, parent }))), |
| 725 | ); |
| 726 | }, |
| 727 | { concurrency: 4 }, |
| 728 | ); |
| 729 | return pages.flat(); |
| 730 | }); |
| 731 | |
| 732 | const scanFirst = <A, E, R>( |
| 733 | parent: string | undefined, |
| 734 | local: (parent: string) => Effect.Effect<A | undefined, E, R>, |
| 735 | walk: Effect.Effect<ReadonlyArray<{ name: string }>, E, R>, |
| 736 | ) => |
| 737 | Effect.gen(function* () { |
| 738 | if (parent !== undefined && parent.length > 0) { |
| 739 | const found = yield* local(parent); |
| 740 | if (found !== undefined) return found; |
| 741 | } |
| 742 | const rows = yield* walk; |
| 743 | for (const row of rows) { |
| 744 | if (row.name.length === 0) continue; |
| 745 | const found = yield* local(row.name); |
| 746 | if (found !== undefined) return found; |
| 747 | } |
| 748 | return undefined; |
| 749 | }); |
| 750 | |
| 751 | export const scanOwnedCustomerDeploymentDevice = (id: string, parent?: string) => |
| 752 | scanFirst( |
| 753 | parent, |
| 754 | (next) => findOwnedCustomerDeploymentDevice(id, next), |
| 755 | walkCustomerDeployments().pipe( |
| 756 | Effect.map((rows) => rows.map((entry) => ({ name: entry.deployment.name ?? "" }))), |
| 757 | ), |
| 758 | ); |
| 759 | |
| 760 | export const scanOwnedCustomerNodeDeployment = (id: string, parent?: string) => |
| 761 | scanFirst( |
| 762 | parent, |
| 763 | (next) => findOwnedCustomerNodeDeployment(id, next), |
| 764 | walkCustomerNodes().pipe( |
| 765 | Effect.map((rows) => rows.map((entry) => ({ name: entry.node.name ?? "" }))), |
| 766 | ), |
| 767 | ); |
| 768 | |
| 769 | export const scanOwnedCustomerNodeDevice = (id: string, parent?: string) => |
| 770 | scanFirst( |
| 771 | parent, |
| 772 | (next) => findOwnedCustomerNodeDevice(id, next), |
| 773 | walkCustomerNodes().pipe( |
| 774 | Effect.map((rows) => rows.map((entry) => ({ name: entry.node.name ?? "" }))), |
| 775 | ), |
| 776 | ); |
| 777 | |
| 778 | export const scanOwnedCustomerNodeNode = (id: string, parent?: string) => |
| 779 | scanFirst( |
| 780 | parent, |
| 781 | (next) => findOwnedCustomerNodeNode(id, next), |
| 782 | walkCustomerNodes().pipe( |
| 783 | Effect.map((rows) => rows.map((entry) => ({ name: entry.node.name ?? "" }))), |
| 784 | ), |
| 785 | ); |
| 786 | |
| 787 | export const scanOwnedNodeDeployment = (id: string, parent?: string) => |
| 788 | scanFirst( |
| 789 | parent, |
| 790 | (next) => findOwnedNodeDeployment(id, next), |
| 791 | walkNodes().pipe(Effect.map((rows) => rows.map((entry) => ({ name: entry.node.name ?? "" })))), |
| 792 | ); |
| 793 | |
| 794 | export const scanOwnedNodeDeploymentDevice = (id: string, parent?: string) => |
| 795 | scanFirst( |
| 796 | parent, |
| 797 | (next) => findOwnedNodeDeploymentDevice(id, next), |
| 798 | walkNodeDeployments().pipe( |
| 799 | Effect.map((rows) => rows.map((entry) => ({ name: entry.deployment.name ?? "" }))), |
| 800 | ), |
| 801 | ); |
| 802 | |
| 803 | export const scanOwnedNodeNodeDeployment = (id: string, parent?: string) => |
| 804 | scanFirst( |
| 805 | parent, |
| 806 | (next) => findOwnedNodeNodeDeployment(id, next), |
| 807 | walkNodeChildren().pipe( |
| 808 | Effect.map((rows) => rows.map((entry) => ({ name: entry.node.name ?? "" }))), |
| 809 | ), |
| 810 | ); |
| 811 | |
| 812 | export const scanOwnedNodeNodeDevice = (id: string, parent?: string) => |
| 813 | scanFirst( |
| 814 | parent, |
| 815 | (next) => findOwnedNodeNodeDevice(id, next), |
| 816 | walkNodeChildren().pipe( |
| 817 | Effect.map((rows) => rows.map((entry) => ({ name: entry.node.name ?? "" }))), |
| 818 | ), |
| 819 | ); |
| 820 | |
| 821 | export const scanOwnedNodeNodeNode = (id: string, parent?: string) => |
| 822 | scanFirst( |
| 823 | parent, |
| 824 | (next) => findOwnedNodeNodeNode(id, next), |
| 825 | walkNodeChildren().pipe( |
| 826 | Effect.map((rows) => rows.map((entry) => ({ name: entry.node.name ?? "" }))), |
| 827 | ), |
| 828 | ); |
| 829 | |
| 830 | export type FrequencyRange = { |
| 831 | /** Lowest frequency of the range in MHz. */ |
| 832 | lowFrequencyMhz?: number; |
| 833 | /** Highest frequency of the range in MHz. */ |
| 834 | highFrequencyMhz?: number; |
| 835 | }; |
| 836 | |
| 837 | export type InstallationParams = { |
| 838 | height?: number; |
| 839 | horizontalAccuracy?: number; |
| 840 | antennaGain?: number; |
| 841 | antennaAzimuth?: number; |
| 842 | verticalAccuracy?: number; |
| 843 | latitude?: number; |
| 844 | heightType?: string; |
| 845 | indoorDeployment?: boolean; |
| 846 | eirpCapability?: number; |
| 847 | antennaModel?: string; |
| 848 | longitude?: number; |
| 849 | antennaDowntilt?: number; |
| 850 | antennaBeamwidth?: number; |
| 851 | cpeCbsdIndication?: boolean; |
| 852 | }; |
| 853 | |
| 854 | export type DeviceModel = { |
| 855 | name?: string; |
| 856 | softwareVersion?: string; |
| 857 | firmwareVersion?: string; |
| 858 | hardwareVersion?: string; |
| 859 | vendor?: string; |
| 860 | }; |
| 861 | |
| 862 | export type DeviceAirInterface = { |
| 863 | supportedSpec?: string; |
| 864 | radioTechnology?: string; |
| 865 | }; |
| 866 | |
| 867 | export type DeviceConfig = { |
| 868 | state?: string; |
| 869 | installationParams?: InstallationParams; |
| 870 | measurementCapabilities?: string[]; |
| 871 | model?: DeviceModel; |
| 872 | airInterface?: DeviceAirInterface; |
| 873 | callSign?: string; |
| 874 | category?: string; |
| 875 | userId?: string; |
| 876 | }; |
| 877 | |
| 878 | export type DeviceMetadata = { |
| 879 | interferenceCoordinationGroup?: string; |
| 880 | commonChannelGroup?: string; |
| 881 | antennaModel?: string; |
| 882 | }; |
| 883 | |
| 884 | export type DeviceGrant = { |
| 885 | grantId?: string; |
| 886 | maxEirp?: number; |
| 887 | channelType?: string; |
| 888 | expireTime?: string; |
| 889 | lastHeartbeatTransmitExpireTime?: string; |
| 890 | suspensionReason?: string[]; |
| 891 | frequencyRange?: FrequencyRange; |
| 892 | state?: string; |
| 893 | }; |
| 894 | |
| 895 | export const frequencyRangesOf = ( |
| 896 | ranges: sasportal.SasPortalFrequencyRangeList | undefined, |
| 897 | ): FrequencyRange[] | undefined => { |
| 898 | if (ranges === undefined) return undefined; |
| 899 | return ranges.map((range) => ({ |
| 900 | lowFrequencyMhz: range.lowFrequencyMhz, |
| 901 | highFrequencyMhz: range.highFrequencyMhz, |
| 902 | })); |
| 903 | }; |
| 904 | |
| 905 | export const deviceConfigOf = ( |
| 906 | config: sasportal.SasPortalDeviceConfig | undefined, |
| 907 | ): DeviceConfig | undefined => { |
| 908 | if (config === undefined) return undefined; |
| 909 | return { |
| 910 | state: config.state, |
| 911 | installationParams: config.installationParams, |
| 912 | measurementCapabilities: config.measurementCapabilities, |
| 913 | model: config.model, |
| 914 | airInterface: config.airInterface, |
| 915 | callSign: config.callSign, |
| 916 | category: config.category, |
| 917 | userId: config.userId, |
| 918 | }; |
| 919 | }; |
| 920 | |
| 921 | export const deviceMetadataOf = ( |
| 922 | metadata: sasportal.SasPortalDeviceMetadata | undefined, |
| 923 | ): DeviceMetadata | undefined => { |
| 924 | if (metadata === undefined) return undefined; |
| 925 | return { |
| 926 | interferenceCoordinationGroup: metadata.interferenceCoordinationGroup, |
| 927 | commonChannelGroup: metadata.commonChannelGroup, |
| 928 | antennaModel: metadata.antennaModel, |
| 929 | }; |
| 930 | }; |
| 931 | |
| 932 | export const deviceGrantsOf = ( |
| 933 | grants: sasportal.SasPortalDeviceGrantList | undefined, |
| 934 | ): DeviceGrant[] | undefined => { |
| 935 | if (grants === undefined) return undefined; |
| 936 | return grants.map((grant) => ({ |
| 937 | grantId: grant.grantId, |
| 938 | maxEirp: grant.maxEirp, |
| 939 | channelType: grant.channelType, |
| 940 | expireTime: grant.expireTime, |
| 941 | lastHeartbeatTransmitExpireTime: grant.lastHeartbeatTransmitExpireTime, |
| 942 | suspensionReason: grant.suspensionReason, |
| 943 | frequencyRange: grant.frequencyRange, |
| 944 | state: grant.state, |
| 945 | })); |
| 946 | }; |
| 947 | |
| 948 | export const deviceBody = (input: { |
| 949 | displayName: string; |
| 950 | serialNumber?: string; |
| 951 | fccId?: string; |
| 952 | grantRangeAllowlists?: FrequencyRange[]; |
| 953 | preloadedConfig?: DeviceConfig; |
| 954 | deviceMetadata?: DeviceMetadata; |
| 955 | }): sasportal.SasPortalDevice => ({ |
| 956 | displayName: input.displayName, |
| 957 | serialNumber: input.serialNumber, |
| 958 | fccId: input.fccId, |
| 959 | grantRangeAllowlists: input.grantRangeAllowlists, |
| 960 | preloadedConfig: input.preloadedConfig, |
| 961 | deviceMetadata: input.deviceMetadata, |
| 962 | }); |
| 963 | |
| 964 | export const signedDeviceBody = (input: { |
| 965 | encodedDevice: string; |
| 966 | installerId: string; |
| 967 | }): sasportal.SasPortalCreateSignedDeviceRequest => ({ |
| 968 | encodedDevice: input.encodedDevice, |
| 969 | installerId: input.installerId, |
| 970 | }); |
| 971 | |
| 972 | export const decodeSignedDevice = (encodedDevice: string) => |
| 973 | Effect.sync(() => { |
| 974 | const parts = encodedDevice.split("."); |
| 975 | if (parts.length < 2) return undefined; |
| 976 | try { |
| 977 | const json = Buffer.from(parts[1]!, "base64url").toString("utf8"); |
| 978 | return JSON.parse(json) as sasportal.SasPortalDevice; |
| 979 | } catch { |
| 980 | return undefined; |
| 981 | } |
| 982 | }); |