如何基于CI Tests数据展示仅最后一次构建的测试结果饼图?
问题
我尝试可视化以JUnit XML格式上传的CI测试结果,现有ci_tests和ci_pipelines两个数据源,均包含status变量。需求是基于用户选择的时间窗口,展示仅最后一次构建的测试结果(通过/失败/跳过)分布的饼图,但当前查询显示的是多个构建的累计结果,即使同一天有多个构建运行。
以下是我尝试的JSON查询:
{ "title": "Last Build Status", "requests": [ { "response_format": "scalar", "queries": [ { "name": "query1", "data_source": "ci_tests", "search": { "query": "test_level:test @test.name:* @test.suite:TEMI*" }, "indexes": ["*"], "group_by": [ { "facet": "@test.status", "limit": 1000, "sort": { "aggregation": "count", "order": "desc", "metric": "count" }, "should_exclude_missing": true } ], "compute": { "aggregation": "cardinality", "metric": "@test.name" }, "storage": "hot" } ], "style": { "palette": "semantic" }, "formulas": [ { "formula": "query1" } ], "sort": { "count": 1000, "order_by": [ { "type": "formula", "index": 0, "order": "desc" } ] } } ], "type": "sunburst", "legend": { "type": "automatic" } }
该可视化可运行但展示的是累计结果而非最新构建的结果,请问如何修改Datadog仪表盘查询(或添加过滤器),使饼图仅反映最近一次构建的测试结果而非多个构建的累计数据?
解决方案
要只展示最近一次构建的测试结果,需先从ci_pipelines数据源获取最新构建的标识(如@pipeline.id),再用该标识过滤ci_tests中的数据,具体修改如下:
修改后的完整JSON查询
{ "title": "Last Build Status", "requests": [ { "response_format": "scalar", "queries": [ { "name": "latest_pipeline", "data_source": "ci_pipelines", "search": { "query": "status:*" }, "indexes": ["*"], "group_by": [ { "facet": "@pipeline.id", "limit": 1, "sort": { "aggregation": "max", "order": "desc", "metric": "@pipeline.timestamp" } } ], "compute": { "aggregation": "max", "metric": "@pipeline.timestamp" }, "storage": "hot" }, { "name": "query1", "data_source": "ci_tests", "search": { "query": "test_level:test @test.name:* @test.suite:TEMI* @pipeline.id:{{latest_pipeline.value}}" }, "indexes": ["*"], "group_by": [ { "facet": "@test.status", "limit": 1000, "sort": { "aggregation": "count", "order": "desc", "metric": "count" }, "should_exclude_missing": true } ], "compute": { "aggregation": "cardinality", "metric": "@test.name" }, "storage": "hot" } ], "style": { "palette": "semantic" }, "formulas": [ { "formula": "query1" } ], "sort": { "count": 1000, "order_by": [ { "type": "formula", "index": 0, "order": "desc" } ] } } ], "type": "sunburst", "legend": { "type": "automatic" } }
关键修改说明
- 新增
latest_pipeline查询:从ci_pipelines中按@pipeline.timestamp降序排序,取第一个结果(最新构建)的@pipeline.id。 - 过滤测试数据:在
ci_tests的查询中加入@pipeline.id:{{latest_pipeline.value}},通过模板变量引用最新构建的ID,确保只统计该构建下的测试结果。 - 保留原有
group_by和compute逻辑,维持测试结果的统计维度(通过/失败/跳过)和计算方式(去重测试用例数)不变。
内容的提问来源于stack exchange,提问作者goodman
相关产品推荐
相关产品推荐

