Commit 09a26fee authored by Pierre Dittgen's avatar Pierre Dittgen
Browse files

Implements compute_badge function

parent 44551ce0
......@@ -237,3 +237,58 @@ class Validator:
report['date'] = datetime.now(timezone.utc).isoformat()
return report
def compute_badge(report, config) -> dict:
"""Compute badge from report statistics and badge configuration."""
# Gets stats from report
stats = report['tables'][0]['error-stats']
# And total number of cells
column_count = len(report['tables'][0]['headers'])
row_count = report['tables'][0]['row-count']
cell_total_number = column_count * row_count
# No errors
if stats['count'] == 0:
return {
"structure": 'OK',
"body": 'OK',
"ratio": 0.0,
}
# Structure part
structure_status = None
if stats['structure-errors']['count'] == 0:
structure_status = 'OK'
else:
cbc = stats['structure-errors']['count-by-code']
if len(cbc) == 1 and 'invalid-column-delimiter' in cbc:
structure_status = 'WARN'
else:
# structure_status = 'KO'
return {
"structure": 'KO'
}
# body part
value_errors = stats['value-errors']
if value_errors['count'] == 0:
return {
"structure": structure_status,
"body": 'OK',
"ratio": 0.0
}
# Computes error ratio
weight_dict = config['body']['errors-weight']
ratio = sum([nb * weight_dict.get(err, 1.0) for err, nb in value_errors['count-by-code'].items()]) \
/ cell_total_number
body_status = 'WARN' if ratio < config['body']['acceptability-threshold'] else 'KO'
return {
"structure": structure_status,
"body": body_status,
"ratio": ratio
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment