如何在 Google Data Studio 中使用带 ID(键值)的过滤器控件
how to use filter control with IDs (key-value) in Google Data Studio
我正在为 Google Datastudio 开发自定义连接器,它连接到 Google Data Studio Community Connectors documentation and the very nice article on Building a custom Google Data Studio 之后的特定 API 服务。
数据架构是:
function getFields(request) {
var fields = cc.getFields();
var types = cc.FieldType;
var aggregations = cc.AggregationType;
fields.newDimension().setId('id').setName('ID').setType(types.NUMBER);
fields.newDimension().setId('date').setName('Date').setType(types.YEAR_MONTH_DAY);
fields.newDimension().setId('user_id').setName('User ID').setType(types.NUMBER);
fields.newDimension().setId('user').setName('User').setType(types.TEXT);
//fields.newMetric().setId('time').setName('Time').setType(types.DURATION).setAggregation(aggregations.SUM);
fields.newMetric().setId('time').setName('Time').setType(types.NUMBER).setAggregation(aggregations.SUM);
fields.newMetric().setId('revenue').setName('Revenue').setType(types.CURRENCY_EUR).setAggregation(aggregations.SUM);
fields.setDefaultDimension('user');
fields.setDefaultMetric('time');
return fields;
}
由于从 API 检索的数据可能非常大,我想直接在 API 级别应用 Google Data Studio 提供的过滤器。问题是,工作室中的过滤器控件通常会显示用户友好的人类可读名称,例如用户名,而不是用户 ID。但是,API 级别需要用户 ID 进行过滤。我得到的请求:
function getData(request) {
// retrieve the data
}
在没有 ID 的情况下获取此过滤器。而且我不太热衷于在面向用户的过滤器控件中显示用户 ID。
{
scriptParams={lastRefresh=1588507514238},
fields=[{name=time}, {forFilterOnly=true, name=user}],
dateRange={endDate=2020-05-02, startDate=2020-04-05},
dimensionsFilters=[[{fieldName=user, type=INCLUDE, values=[John Doe], operator=IN_LIST}]]
}
我看到工作室中的过滤器控件不支持键值。知道如何以任何其他方式实现这一目标吗?我唯一的想法是将已知的 user-user_id 组合存储在缓存中,并在发送 API 请求之前将用户名转换为用户 ID。但希望有更简单的方法吗?
正确的过滤器应该是这样的:
{
scriptParams={lastRefresh=1588507514238},
fields=[{name=time}, {forFilterOnly=true, name=user_id}],
dateRange={endDate=2020-05-02, startDate=2020-04-05},
dimensionsFilters=[[{fieldName=user_id, type=INCLUDE, values=[1234], operator=IN_LIST}]]
}
实际上 Google Data Studio 没有像 explained here. This totally makes sense. Why? If the designer of the report has added a filter control (e.g. user names) then that filter control needs to know all possible users. Otherwise, it can not display all options / selections to the user. Therefore, it must ask the connector for all non-filtered data at least once. The Data Studio will automatically take care of caching 这样的支持。
不过,建议在连接器中缓存 HTTP 结果,以防 UI 决定再次请求数据。这就是你需要 CacheService for which there is a good demo implementation here 的时候了。在这种情况下,第一个 HTTP 调用会花费一些时间,但所有后续调用都会快得多。假设您的初始结果有 1.000 行,您仍然可能需要在连接器内对其进行过滤以获得最终结果,比如 6 行,这可能会很慢。由 UI 完成的过滤甚至更慢。
如果您仍然需要在用户名和用户 ID 或类似关系之间进行转换,那么您可能需要在检索数据时缓存这些关系并将 UI 过滤器(用于用户名)转换为通过解析与缓存数据的关系来获取 ID(例如用户 ID)。在这种情况下,您将忽略最初缓存的 1.000 行结果(仅作为示例),使用 id(例如用户 id)调整 HTTP GET 查询,从而仅检索 6 行。这是一个 API 调用,但在连接器和 UI 中都没有过滤。连接器实现的过滤是 explained here as well as in the filter API reference.
您需要自己决定是否需要在 API 级别、在连接器内部或由 UI 自动进行过滤。在我的例子中,API 级别是更快的级别,但它会调用 API 调用,而某些 API 的后续(频率)或每日(配额)调用数量非常有限。
我正在为 Google Datastudio 开发自定义连接器,它连接到 Google Data Studio Community Connectors documentation and the very nice article on Building a custom Google Data Studio 之后的特定 API 服务。
数据架构是:
function getFields(request) {
var fields = cc.getFields();
var types = cc.FieldType;
var aggregations = cc.AggregationType;
fields.newDimension().setId('id').setName('ID').setType(types.NUMBER);
fields.newDimension().setId('date').setName('Date').setType(types.YEAR_MONTH_DAY);
fields.newDimension().setId('user_id').setName('User ID').setType(types.NUMBER);
fields.newDimension().setId('user').setName('User').setType(types.TEXT);
//fields.newMetric().setId('time').setName('Time').setType(types.DURATION).setAggregation(aggregations.SUM);
fields.newMetric().setId('time').setName('Time').setType(types.NUMBER).setAggregation(aggregations.SUM);
fields.newMetric().setId('revenue').setName('Revenue').setType(types.CURRENCY_EUR).setAggregation(aggregations.SUM);
fields.setDefaultDimension('user');
fields.setDefaultMetric('time');
return fields;
}
由于从 API 检索的数据可能非常大,我想直接在 API 级别应用 Google Data Studio 提供的过滤器。问题是,工作室中的过滤器控件通常会显示用户友好的人类可读名称,例如用户名,而不是用户 ID。但是,API 级别需要用户 ID 进行过滤。我得到的请求:
function getData(request) {
// retrieve the data
}
在没有 ID 的情况下获取此过滤器。而且我不太热衷于在面向用户的过滤器控件中显示用户 ID。
{
scriptParams={lastRefresh=1588507514238},
fields=[{name=time}, {forFilterOnly=true, name=user}],
dateRange={endDate=2020-05-02, startDate=2020-04-05},
dimensionsFilters=[[{fieldName=user, type=INCLUDE, values=[John Doe], operator=IN_LIST}]]
}
我看到工作室中的过滤器控件不支持键值。知道如何以任何其他方式实现这一目标吗?我唯一的想法是将已知的 user-user_id 组合存储在缓存中,并在发送 API 请求之前将用户名转换为用户 ID。但希望有更简单的方法吗?
正确的过滤器应该是这样的:
{
scriptParams={lastRefresh=1588507514238},
fields=[{name=time}, {forFilterOnly=true, name=user_id}],
dateRange={endDate=2020-05-02, startDate=2020-04-05},
dimensionsFilters=[[{fieldName=user_id, type=INCLUDE, values=[1234], operator=IN_LIST}]]
}
实际上 Google Data Studio 没有像 explained here. This totally makes sense. Why? If the designer of the report has added a filter control (e.g. user names) then that filter control needs to know all possible users. Otherwise, it can not display all options / selections to the user. Therefore, it must ask the connector for all non-filtered data at least once. The Data Studio will automatically take care of caching 这样的支持。
不过,建议在连接器中缓存 HTTP 结果,以防 UI 决定再次请求数据。这就是你需要 CacheService for which there is a good demo implementation here 的时候了。在这种情况下,第一个 HTTP 调用会花费一些时间,但所有后续调用都会快得多。假设您的初始结果有 1.000 行,您仍然可能需要在连接器内对其进行过滤以获得最终结果,比如 6 行,这可能会很慢。由 UI 完成的过滤甚至更慢。
如果您仍然需要在用户名和用户 ID 或类似关系之间进行转换,那么您可能需要在检索数据时缓存这些关系并将 UI 过滤器(用于用户名)转换为通过解析与缓存数据的关系来获取 ID(例如用户 ID)。在这种情况下,您将忽略最初缓存的 1.000 行结果(仅作为示例),使用 id(例如用户 id)调整 HTTP GET 查询,从而仅检索 6 行。这是一个 API 调用,但在连接器和 UI 中都没有过滤。连接器实现的过滤是 explained here as well as in the filter API reference.
您需要自己决定是否需要在 API 级别、在连接器内部或由 UI 自动进行过滤。在我的例子中,API 级别是更快的级别,但它会调用 API 调用,而某些 API 的后续(频率)或每日(配额)调用数量非常有限。