Skip to content

Commit 3d695cb

Browse files
authored
fix(scorecard): Return threshold definition on metric calculation error (#1487)
* Return threshold definition on metric calc error Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Fix double negative Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> * Update error message Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com> --------- Signed-off-by: Dominika Zemanovicova <dzemanov@redhat.com>
1 parent 694f69c commit 3d695cb

5 files changed

Lines changed: 45 additions & 30 deletions

File tree

workspaces/scorecard/plugins/scorecard-backend/src/service/CatalogMetricService.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,27 @@ describe('CatalogMetricService', () => {
193193
...jiraMetricResult,
194194
status: 'error' as const,
195195
error: 'Error: Jira API failure',
196-
result: undefined,
196+
result: {
197+
thresholdResult: {
198+
definition: {
199+
rules: [
200+
{
201+
expression: '==true',
202+
key: 'success',
203+
},
204+
{
205+
expression: '==false',
206+
key: 'error',
207+
},
208+
],
209+
},
210+
evaluation: undefined,
211+
status: 'error',
212+
error: 'Unable to evaluate thresholds, metric value is missing',
213+
},
214+
timestamp: '2024-01-15T10:30:00.000Z',
215+
value: undefined,
216+
},
197217
};
198218
mockCatalogApi.getEntityByRef.mockResolvedValue(mockEntity);
199219

workspaces/scorecard/plugins/scorecard-backend/src/service/CatalogMetricService.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,6 @@ export class CatalogMetricService {
101101
const provider = this.registry.getProvider(providerId);
102102
const metric = authorizedMetricsToCalculate[index];
103103

104-
if (error || value === undefined) {
105-
return {
106-
id: providerId,
107-
status: 'error',
108-
metadata: {
109-
title: metric.title,
110-
description: metric.description,
111-
type: metric.type,
112-
history: metric.history,
113-
},
114-
error: error
115-
? stringifyError(error)
116-
: stringifyError(new Error(`Metric value is 'undefined'`)),
117-
};
118-
}
119-
120104
let thresholds: ThresholdConfig | undefined;
121105
let evaluation: string | undefined;
122106
let thresholdError: string | undefined;
@@ -126,24 +110,35 @@ export class CatalogMetricService {
126110
provider,
127111
metric.type,
128112
);
129-
evaluation = this.thresholdEvaluator.getFirstMatchingThreshold(
130-
value,
131-
metric.type,
132-
thresholds,
133-
);
113+
if (value === undefined) {
114+
thresholdError =
115+
'Unable to evaluate thresholds, metric value is missing';
116+
} else {
117+
evaluation = this.thresholdEvaluator.getFirstMatchingThreshold(
118+
value,
119+
metric.type,
120+
thresholds,
121+
);
122+
}
134123
} catch (e) {
135124
thresholdError = stringifyError(e);
136125
}
137126

127+
const isMetricCalcError = error || value === undefined;
138128
return {
139129
id: metric.id,
140-
status: 'success',
130+
status: isMetricCalcError ? 'error' : 'success',
141131
metadata: {
142132
title: metric.title,
143133
description: metric.description,
144134
type: metric.type,
145135
history: metric.history,
146136
},
137+
...(isMetricCalcError && {
138+
error: error
139+
? stringifyError(error)
140+
: stringifyError(new Error(`Metric value is 'undefined'`)),
141+
}),
147142
result: {
148143
value,
149144
timestamp: new Date().toISOString(),

workspaces/scorecard/plugins/scorecard-common/report.api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export type MetricResult = {
2727
type: MetricType;
2828
history?: boolean;
2929
};
30-
result?: {
31-
value: MetricValue;
30+
result: {
31+
value?: MetricValue;
3232
timestamp: string;
3333
thresholdResult: ThresholdResult;
3434
};

workspaces/scorecard/plugins/scorecard-common/src/types/Metric.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ export type MetricResult = {
5353
type: MetricType;
5454
history?: boolean;
5555
};
56-
result?: {
57-
value: MetricValue;
56+
result: {
57+
value?: MetricValue;
5858
timestamp: string;
5959
thresholdResult: ThresholdResult;
6060
};

workspaces/scorecard/plugins/scorecard/src/components/Scorecard/EntityScorecardContent.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const EntityScorecardContent = () => {
6262
}
6363

6464
const statusConfig = getStatusConfig(
65-
metric.result?.thresholdResult?.evaluation,
65+
metric.result.thresholdResult?.evaluation,
6666
);
6767

6868
return (
@@ -73,8 +73,8 @@ export const EntityScorecardContent = () => {
7373
loading={false}
7474
statusColor={statusConfig.color}
7575
StatusIcon={statusConfig.icon}
76-
value={metric.result?.value}
77-
thresholds={metric.result?.thresholdResult}
76+
value={metric.result.value}
77+
thresholds={metric.result.thresholdResult}
7878
/>
7979
);
8080
})}

0 commit comments

Comments
 (0)