跳到主要内容

指标查询

网关前缀:${API_BASE}/data/metric/...

参数说明(MetricQueryParams)

  • dimensions 字符串数组,可选,维度列,如 channelcountry
  • filter 对象,可选,Predicate(同 Query 的 where)
  • time 对象,可选,时间配置(同 QueryRequest.time)
  • limit 数字,可选
  • offset 数字,可选

维度与聚合说明

  • 指标定义中通常包含默认聚合(如 SUM(amount)COUNT(*))。
  • 当传入 dimensions 时,返回数据按维度进行分组聚合。
  • 维度字段需存在于底层实体/视图或可派生;否则返回字段错误。

过滤操作符列表

  • eqnegtgteltlteinnot_inlikeilikebetweenis_nullis_not_null

单指标查询

  • POST /metric/{metricName}/query
  • 示例(带维度与过滤):
curl -X POST \
"${API_BASE}/data/metric/revenue/query" \
-H "Content-Type: application/json" \
-H "X-Tenant-Id: tenant-abc123" \
-d '{
"dimensions": ["channel"],
"filter": { "type":"logical", "op":"and", "conditions":[
{ "type":"comparison", "field":"country", "op":"eq", "value":"US" },
{ "type":"comparison", "field":"status", "op":"eq", "value":"PAID" }
]},
"time": { "time_field":"order_date", "time_range": { "preset":"last_30_days" }, "grain":"day" },
"limit": 50
}'
  • 响应示例:
{"success": true, "data": {"columns": ["channel", "revenue"], "rows": [["web", 5000], ["store", 3000]]}}

批量指标查询

  • POST /metric/query/batch
  • 请求体:
    • metrics: string[] 必填,指标名列表
    • params: MetricQueryParams 可选,统一参数
  • 示例:
curl -X POST \
"${API_BASE}/data/metric/query/batch" \
-H "Content-Type: application/json" \
-H "X-Tenant-Id: tenant-abc123" \
-d '{
"metrics": ["revenue", "orders_count"],
"params": {"dimensions":["channel"], "limit": 10}
}'
  • 响应示例:
{
"success": true,
"data": {
"revenue": {"columns": ["channel","revenue"], "rows": [["web",5000]]},
"orders_count": {"columns": ["channel","orders_count"], "rows": [["web",120]]}
}
}

常见错误示例

  • 未知指标:
{"success": false, "message":"指标不存在: gmv", "errorCode":"METRIC_NOT_FOUND"}
  • 非法维度:
{"success": false, "message":"Dimension 'region' not available for metric 'revenue'", "errorCode":"METRIC_QUERY_FAILED"}
  • 非法过滤:
{"success": false, "message":"Operator 'BETWEEN' requires array or {from,to}", "errorCode":"METRIC_QUERY_FAILED"}