获取过滤HPALM测试运行数据重复项的Neo4j Cypher查询
解决Neo4j中重复test-id记录的统计问题
针对你从HPALM导入Neo4j后,同一个test-id对应多条不同时间和状态的测试记录,需要过滤出最新记录再做统计的需求,我整理了几个实用的Cypher查询方案,适配Grafana仪表盘的统计需求:
1. 获取每个test-id的最新测试记录
首先我们需要筛选出每个test-id对应的最新一条记录(基于last_modified时间),这个查询可以作为后续统计的基础:
// 匹配所有测试记录节点(请根据实际节点标签替换:Test) MATCH (t:Test) // 按test-id分组,计算每组的最新修改时间 WITH t.test_id, MAX(t.last_modified) AS latest_run_time // 匹配回对应的最新记录 MATCH (latest_test:Test) WHERE latest_test.test_id = t.test_id AND latest_test.last_modified = latest_run_time // 返回最新记录的关键信息 RETURN latest_test.test_id AS test_id, latest_test.status AS test_status, latest_test.last_modified AS last_run_time
如果你的业务场景中,同一个test-id可能在同一时间存在多条记录(比如并行运行),可以用COLLECT+排序的方式确保取到最新的单条记录:
MATCH (t:Test) // 按test-id分组,收集该id的所有记录 WITH t.test_id, COLLECT(t) AS test_records // 按修改时间降序排序,取第一条作为最新记录 WITH test_id, HEAD(ORDER BY test_records.last_modified DESC) AS latest_test RETURN latest_test.test_id AS test_id, latest_test.status AS test_status, latest_test.last_modified AS last_run_time
2. 基于最新记录生成统计汇总
拿到最新记录后,就可以直接统计不同状态的测试数量,这个查询可以直接用于Grafana生成饼图、条形图等:
MATCH (t:Test) WITH t.test_id, MAX(t.last_modified) AS latest_run_time MATCH (latest_test:Test) WHERE latest_test.test_id = t.test_id AND latest_test.last_modified = latest_run_time // 按状态分组统计数量 WITH latest_test.status AS test_status, COUNT(*) AS status_count // 返回统计结果,方便Grafana解析 RETURN test_status, status_count ORDER BY test_status
注意事项
- 请根据你的实际数据模型替换节点标签(比如
:Test可能是:HPALM_TestRun)和属性名(比如last_modified可能是execution_time); - 如果需要统计更多维度(比如按测试套件、模块),可以在分组阶段加入对应的属性,比如
WITH t.test_id, t.test_suite, MAX(t.last_modified) AS latest_run_time; - Grafana中使用Neo4j数据源时,请确保查询返回的字段名清晰,方便图表映射。
内容的提问来源于stack exchange,提问作者Meghal
相关产品推荐
相关产品推荐

