These matches can be more strict, like
/stderr/i => /.stderr./ /fail/i => /.fail$/
- def set_job_summary(stats, job)
- summary_result = ''
- if stats.keys.any? { |stat| stat.match(/warn/i) }
job['summary.any_warning'] = 1
summary_result = 'warning'
- end
- if stats.keys.any? { |stat| stat.match(/stderr/i) }
job['summary.any_stderr'] = 1
summary_result = 'stderr'
- end
- if stats.keys.any? { |stat| stat.match(/error|nr_fail=0/i) }
nr_fail=0?
job['summary.any_error'] = 1
summary_result = 'error'
- end
- if stats.keys.any? { |stat| stat.match(/fail/i) } &&
stats.keys.all? { |stat| !stat.match(/nr_fail=0/i) }
Remove nr_fail test here. What if the script does not account nr_fail? What if there is another nr_error?
Thanks, Fengguang
job['summary.any_fail'] = 1
summary_result = 'fail'
- end
- return unless summary_result.empty?
- @refine_jobs << job['id']
- @fields.each do |field|
value = job[field]
if value
value = job['id'] + '.' + value if field == 'job_state'
@result[field] << value
end
- job['summary.success'] = 1
- end
next unless job['stats']
- # set jobs summary fields information in place
- def set_jobs_summary
- @jobs.each do |job|
stats = job['stats']
next unless stats
@result[field] << job['stats'][field] if job['stats'][field]
end endset_job_summary(stats, job)
- def deal_jobs
- stats_count = Hash.new(0)
- stats_jobs = {}
- def get_all_metrics(jobs)
- metrics = []
- jobs.each do |job|
stats = job['stats']
next unless stats
- @jobs.each do |job_id, job|
level = deal_stats(job)
add_result_fields(job, level)
stat_key = @stats_level[level]
stat_jobs_key = stat_key + '_jobs'
stats_count[stat_key] += 1
stats_jobs[stat_jobs_key] ||= []
stats_jobs[stat_jobs_key] << job_id
endmetrics.concat(stats.keys)
- metrics.uniq!
- end
- @stats['stats.count'].merge!(stats_count)
- @stats['stats.count'].merge!(stats_jobs)
- def initialize_result_hash(metrics)
- result = {
'kvcount' => {},
'raw.id' => {},
'sum.stats' => {},
'raw.stats' => {},
'avg.stats' => {},
'max.stats' => {},
'min.stats' => {}
- }
- metrics.each { |metric| result['raw.stats'][metric] = [] }
- result end
- def deal_stats(job, level = 0)
- return 1 unless job['stats']
- def set_default_value(result, stats, metrics)
- left_metrics = metrics - stats.keys
- left_metrics.each { |metric| result['raw.stats'][metric] << nil }
- job['stats'].each do |key, value|
match_stats_filter(key, value, job['id'])
calculate_stat(key, value)
level = get_stat_level(key, level)
- stats.each do |key, value|
endresult['raw.stats'][key] << value
return level end
def match_stats_filter(key, value, job_id)
@stats_filter.each do |filter|
next unless key.include?(filter)
- def kvcount(result, job)
- KEYWORD.each do |keyword|
next unless job[keyword]
key = job_id + '.' + key
@stats_filter_result[key] = value
break
result['kvcount']["#{keyword}=#{job[keyword]}"] ||= 0
result['kvcount']["#{keyword}=#{job[keyword]}"] += 1
result['raw.id']["[#{keyword}=#{job[keyword]}]"] ||= []
end endresult['raw.id']["[#{keyword}=#{job[keyword]}]"] << job['id']
- def calculate_stat(key, value)
- if function_stat?(key)
return unless @fields.include?('stats.sum')
@stats['stats.sum'][key] += value
- else
return unless @fields.include?('stats.avg')
@stats['stats.avg'][key] = (@stats['stats.avg'][key] + value) / 2
- def stats_count(result)
- result['raw.stats'].each do |key, value|
if function_stat?(key)
result['sum.stats'][key] = value.compact.size
else
result['avg.stats'][key] = value.compact.sum / value.compact.size.to_f
result['max.stats'][key] = value.compact.max
result['min.stats'][key] = value.compact.min
end endend
- def get_stat_level(stat, level)
- return level if level >= 3
- return 3 if stat.match(/error|fail/i)
- return 2 if stat.match(/warn/i)
- def query_jobs_state(jobs)
- metrics = get_all_metrics(jobs)
- result = initialize_result_hash(metrics)
- jobs.each do |job|
stats = job['stats']
next unless stats
set_default_value(result, stats, metrics)
kvcount(result, job)
- end
- return 0
- stats_count(result)
- result end
- def output
- result = {
'stats.count' => @stats['stats.count']
- }
- def output_yaml(prefix, result)
- result.each do |key, value|
if prefix.empty?
prefix_key = "#{key}"
else
prefix_key = "#{prefix}.#{key}"
end
- @stats.each do |key, value|
result[key] = value if @fields.include?(key)
if value.is_a? Hash
output_yaml(prefix_key, value)
else
puts "#{prefix_key}: #{value.to_json}"
endend
- end
- @result['stats_filter_result'] = @stats_filter_result unless @stats_filter.empty?
- @result.merge!(result)
- puts JSON.pretty_generate(@result)
- def output
- @result = query_jobs_state(@jobs)
- @result['kvcount'] = @result['kvcount'].sort.to_h
- @result['raw.id'] = @result['raw.id'].sort.to_h
- output_yaml('', @result) end
end
2.23.0