如何在 Foundry Functions 中拥有灵活的分组列?
How to have flexible grouping column in Foundry Functions?
在我的 Workshop 应用程序中,我想要一个带有可变 x 轴的条形图。下拉小部件将用于 select 所需的 x 轴。
为此,我正在编写一个 TypeScript 函数,它将 return 提供给图表小部件的数据。
我写了下面的函数:
@Function()
public async flexibleCounter2DimensionTest(
objects: ObjectSet<Data>,
xaxis : string ): Promise<TwoDimensionalAggregation<string>> {
return objects
.groupBy(val => val[xaxis].topValues())
.count()
}
xaxis
参数将定义要对其执行分组的列名。
我在 Foundry Functions 中很难做到这一点,我总是在编译时收到以下错误:
{
"stdout": "src/hospital_provider_analysis.ts(170,9): error TS2322: Type 'TwoDimensionalAggregation<BucketKey, number>' is not assignable to type 'TwoDimensionalAggregation<string, number>'.\n Type 'BucketKey' is not assignable to type 'string'.\n Type 'false' is not assignable to type 'string'.\nsrc/hospital_provider_analysis.ts(171,33): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'BucketableProperties'.\n No index signature with a parameter of type 'string' was found on type 'BucketableProperties'.\n",
"stderr": ""
}
有解决此问题的想法吗?
Typescript 的一些更强大的部分 type-system 允许我们指定此聚合函数的 type-safe 版本。我在下面的 第 1 部分 中详细介绍了一种方法。
公开此 type-safe 聚合函数时,我们需要执行一些额外的检查,因为 Functions 当前不支持 union types(请参阅 Palantir 文档中的 'Functions API: Input and output types')。我在下面的 第 2 部分 中列出了我的代码。
第 1 部分:type-safe 聚合
首先,我们列出所有 属性 API 我们想要 group-by 作为新 union type of literal types 的名字。这些应该是您通常用来在函数中访问这些属性的 API 名称。
type AggregatableStringProperties = "propertyA" | "propertyB" | ...;
现在,我们可以定义一个 type-safe 聚合函数,它接受一个对象集,属性 由(AggregatableStringProperties
中的)聚合:
private async flexibleCounter2DimensionTestImpl(
objects: ObjectSet<Data>,
xaxis: AggregatableStringProperties,
) : Promise<TwoDimensionalAggregation<string>> {
return objects
.groupBy(val => val[xaxis].topValues())
.count();
}
第 2 部分:将其公开为 @Function()
使用我们在 第 1 部分 中定义的 type-safe 聚合函数,我们现在可以使用 type assertions 公开它。在下面的代码中,我添加了一些可选逻辑来为该函数的用户提供有用的调试信息。
@Function()
public async flexibleCounter2DimensionTest(
objects: ObjectSet<Data>,
xaxis : string
): Promise<TwoDimensionalAggregation<string>> {
const xaxis_t = xaxis as AggregatableStringProperties;
if (Data.properties[xaxis_t] === undefined || Data.properties[xaxis_t].baseType.type !== "string") {
throw new UserFacingError("xaxis argument ('" + xaxis + "') is not a string property of the Data object");
}
return this.flexibleCounter2DimensionTestImpl(objects, xaxis_t);
}
在我的 Workshop 应用程序中,我想要一个带有可变 x 轴的条形图。下拉小部件将用于 select 所需的 x 轴。
为此,我正在编写一个 TypeScript 函数,它将 return 提供给图表小部件的数据。
我写了下面的函数:
@Function()
public async flexibleCounter2DimensionTest(
objects: ObjectSet<Data>,
xaxis : string ): Promise<TwoDimensionalAggregation<string>> {
return objects
.groupBy(val => val[xaxis].topValues())
.count()
}
xaxis
参数将定义要对其执行分组的列名。
我在 Foundry Functions 中很难做到这一点,我总是在编译时收到以下错误:
{ "stdout": "src/hospital_provider_analysis.ts(170,9): error TS2322: Type 'TwoDimensionalAggregation<BucketKey, number>' is not assignable to type 'TwoDimensionalAggregation<string, number>'.\n Type 'BucketKey' is not assignable to type 'string'.\n Type 'false' is not assignable to type 'string'.\nsrc/hospital_provider_analysis.ts(171,33): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'BucketableProperties'.\n No index signature with a parameter of type 'string' was found on type 'BucketableProperties'.\n", "stderr": "" }
有解决此问题的想法吗?
Typescript 的一些更强大的部分 type-system 允许我们指定此聚合函数的 type-safe 版本。我在下面的 第 1 部分 中详细介绍了一种方法。
公开此 type-safe 聚合函数时,我们需要执行一些额外的检查,因为 Functions 当前不支持 union types(请参阅 Palantir 文档中的 'Functions API: Input and output types')。我在下面的 第 2 部分 中列出了我的代码。
第 1 部分:type-safe 聚合
首先,我们列出所有 属性 API 我们想要 group-by 作为新 union type of literal types 的名字。这些应该是您通常用来在函数中访问这些属性的 API 名称。
type AggregatableStringProperties = "propertyA" | "propertyB" | ...;
现在,我们可以定义一个 type-safe 聚合函数,它接受一个对象集,属性 由(AggregatableStringProperties
中的)聚合:
private async flexibleCounter2DimensionTestImpl(
objects: ObjectSet<Data>,
xaxis: AggregatableStringProperties,
) : Promise<TwoDimensionalAggregation<string>> {
return objects
.groupBy(val => val[xaxis].topValues())
.count();
}
第 2 部分:将其公开为 @Function()
使用我们在 第 1 部分 中定义的 type-safe 聚合函数,我们现在可以使用 type assertions 公开它。在下面的代码中,我添加了一些可选逻辑来为该函数的用户提供有用的调试信息。
@Function()
public async flexibleCounter2DimensionTest(
objects: ObjectSet<Data>,
xaxis : string
): Promise<TwoDimensionalAggregation<string>> {
const xaxis_t = xaxis as AggregatableStringProperties;
if (Data.properties[xaxis_t] === undefined || Data.properties[xaxis_t].baseType.type !== "string") {
throw new UserFacingError("xaxis argument ('" + xaxis + "') is not a string property of the Data object");
}
return this.flexibleCounter2DimensionTestImpl(objects, xaxis_t);
}