Query jobs by conditions such as group id or suite, and count the number
of passed cases and failed cases.
output is like:
{
"kezhiming" : {
"all" : $all,
"nr_pass" : $nr_pass,
"nr_fail" : $nr_fali
},
"chenqun" : {
"all" : $all,
"nr_pass" : $nr_pass,
"nr_fail" : $nr_fali
},
...
}
Signed-off-by: Li Yuanchao <lyc163mail(a)163.com>
---
src/lib/web_backend.rb | 66 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 66 insertions(+)
diff --git a/src/lib/web_backend.rb b/src/lib/web_backend.rb
index 89c0f3c..4a5a51f 100644
--- a/src/lib/web_backend.rb
+++ b/src/lib/web_backend.rb
@@ -548,3 +548,69 @@ def new_refs_statistics(params)
end
[200, headers.merge('Access-Control-Allow-Origin' => '*'), body]
end
+
+def single_count(stats)
+ fail_count = 0
+ pass_count = 0
+ single_nr_fail = 0
+ single_nr_pass = 0
+ stats.each do |stat, value|
+ fail_count += 1 if stat.match(/\.fail$/i)
+ pass_count += 1 if stat.match(/\.pass$/i)
+ single_nr_fail = value if stat.match(/\.nr_fail$/i)
+ single_nr_pass = value if stat.match(/\.nr_pass$/i)
+ end
+ fail_count = single_nr_fail.zero? ? fail_count : single_nr_fail
+ pass_count = single_nr_pass.zero? ? pass_count : single_nr_pass
+ [fail_count, pass_count, fail_count + pass_count]
+end
+
+def count_stats(job_list)
+ nr_fail = 0
+ nr_pass = 0
+ all = 0
+ job_list.each do |job|
+ next unless job['_source']['stats']
+
+ fail_count, pass_count, all_count = single_count(job['_source']['stats'])
+ nr_fail += fail_count
+ nr_pass += pass_count
+ all += all_count
+ end
+ { 'nr_fail' => nr_fail, 'nr_pass' => nr_pass, 'all' => all }
+end
+
+def get_jobs_stats_count(dimension, must, size, from)
+ dimension_list = get_dimension_list(dimension)
+ stats_count = {}
+ dimension_list.each do |dim|
+ job_list = query_dimension(dimension[0], dim, must, size, from)
+ stats_count[dim] = count_stats(job_list)
+ end
+ stats_count.to_json
+end
+
+def get_stats_by_dimension(conditions, dimension, must, size, from)
+ must += build_multi_field_subquery_body(conditions)
+ count_query = { query: { bool: { must: must } } }
+ total = es_count(count_query)
+ return {} if total < 1
+
+ get_jobs_stats_count(dimension, must, size, from)
+end
+
+def get_jobs_stats(params)
+ dimension, conditions = get_dimension_conditions(params)
+ must = get_es_must(params)
+ get_stats_by_dimension(conditions, dimension, must, 1000, 0)
+end
+
+def group_jobs_stats(params)
+ begin
+ body = get_jobs_stats(params)
+ rescue StandardError => e
+ warn e.message
+ return [500, headers.merge('Access-Control-Allow-Origin' => '*'), 'group jobs table error']
+ end
+ [200, headers.merge('Access-Control-Allow-Origin' => '*'), body]
+end
--
2.23.0