Vertex 파생 속성 함수
함수로 계산한 속성을 객체 보기, 확장 노드 레이블과 노드 색상에 사용할 수 있습니다.
사용 조건
- 호출 가능한 함수로 내보냅니다. TypeScript v1은 public 메서드의
@Function()을, TypeScript v2는export default를 사용합니다. - 반환값은 객체를 키로 하는 맵입니다. v1은
FunctionsMap<ObjectType, CustomType>, v2는Record<ObjectSpecifier<ObjectType>, CustomType>입니다. - 모든 키는 입력의 객체를 나타냅니다.
- 값은 primitive 또는 각 필드가 primitive인 사용자 정의 타입입니다.
- 릴리스 태그로 게시되어 있어야 합니다.
- 객체 하나가 아니라 객체 배열 또는 객체 집합을 입력으로 받습니다. 예를 들면
ExampleDataRoute[]또는ObjectSet<ExampleDataRoute>입니다. - 그 객체 집합 외의 추가 입력은 없습니다.
객체마다 함수를 따로 호출하는 대신 여러 객체를 함께 계산하여 큰 그래프의 호출 비용을 줄이는 구조입니다.
노선 취소율 예시
생성 SDK에 ExampleDataRoute, flights 링크와 항공편의 Boolean cancelled 속성이 있는 구성을 전제로 합니다. 취소 항공편 수를 전체 항공편 수로 나누어 백분율을 반환합니다.
연결 항공편이 없으면 비율을 정의할 수 없으므로 오류를 반환합니다. 취소 여부가 비어 있는 경우도 0%로 대신하지 않습니다.
TypeScript v1
import { Double, Function, FunctionsMap, UserFacingError } from "@foundry/functions-api";
import { ExampleDataRoute } from "@foundry/ontology-api";
export class VertexDerivedPropertyFunctions {
@Function()
public async flightCancellationPercentage(
routes: ExampleDataRoute[],
): Promise<FunctionsMap<ExampleDataRoute, Double>> {
const result = new FunctionsMap<ExampleDataRoute, Double>();
await Promise.all(routes.map(async route => {
const flights = await route.flights.allAsync();
if (flights.length === 0) {
throw new UserFacingError("항공편이 없는 노선의 취소율은 계산할 수 없습니다.");
}
let cancelledCount = 0;
for (const flight of flights) {
if (flight.cancelled == null) {
throw new UserFacingError("항공편의 취소 여부가 필요합니다.");
}
if (flight.cancelled === true) {
cancelledCount += 1;
}
}
result.set(route, (cancelledCount / flights.length) * 100);
}));
return result;
}
}
TypeScript v2
import type { ObjectSet, ObjectSpecifier } from "@osdk/client";
import type { Double } from "@osdk/functions";
import type { ExampleDataRoute } from "@ontology/sdk";
export default async function flightCancellationPercentage(
routes: ObjectSet<ExampleDataRoute>,
): Promise<Record<ObjectSpecifier<ExampleDataRoute>, Double>> {
const result: Record<ObjectSpecifier<ExampleDataRoute>, Double> = {};
for await (const route of routes.asyncIter()) {
let flightCount = 0;
let cancelledCount = 0;
for await (const flight of route.$link.flights.asyncIter({ $select: ["cancelled"] })) {
if (flight.cancelled == null) {
throw new Error("항공편의 취소 여부가 필요합니다.");
}
flightCount += 1;
if (flight.cancelled === true) {
cancelledCount += 1;
}
}
if (flightCount === 0) {
throw new Error("항공편이 없는 노선의 취소율은 계산할 수 없습니다.");
}
result[route.$objectSpecifier] = (cancelledCount / flightCount) * 100;
}
return result;
}
v2 예시는 객체 집합과 연결 항공편을 반복자로 읽으므로 항공편 배열 전체와 필터 결과 배열을 따로 만들지 않습니다.