-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRapLPDiagnostic.ts
More file actions
174 lines (169 loc) · 6.59 KB
/
RapLPDiagnostic.ts
File metadata and controls
174 lines (169 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// SPDX-FileCopyrightText: 2025 Digg - Agency for Digital Government
//
// SPDX-License-Identifier: EUPL-1.2
import { RapLPCustomSpectralDiagnostic } from './RapLPCustomSpectralDiagnostic.js';
import { RuleExecutionLog, RuleExecutionContext } from './RuleExecutionContext.js';
class RapLPDiagnostic {
private _ruleSets: DiagnosticRuleinfoSet = {
//--- Diagnostic information
notApplicableRules: [],
executedUniqueRules: [],
executedUniqueRulesWithError: [],
};
public get diagnosticInformation(): DiagnosticRuleinfoSet {
return this._ruleSets;
}
constructor(private context: RuleExecutionContext) {}
processRuleExecutionInformation(
raplpCustomResult: RapLPCustomSpectralDiagnostic[],
rules: Record<string, any>,
instanceCategoryMap: Map<string, any>,
): void {
this.processRuleExecutionLog(this.context.ruleExecutionLogDictionary, raplpCustomResult, instanceCategoryMap,rules);
}
private processRuleExecutionLog(
log: RuleExecutionLog,
spectralResults: RapLPCustomSpectralDiagnostic[],
instanceCategoryMap: Map<string, any>,
rules: Record<string, any>,
) {
let executedRuleIds = new Set<string>(); // Set to track executed rule IDs
let executedRuleIdsWithError = new Set<string>(); // Set to track executed rule IDs with error
let ruleIdsNotApplicable = new Set<string>(); // Set to track rules that are not applicable, that is the (Δ) between the two above sets
const ruleIdToMessage = new Map<string, string>();
for (const key in log) {
const rules = log[key];
const { moduleName, className } = rules[0]; // Get module and class name from the first entry
//console.log(`Rule execution status for ${moduleName}:${className}:`);
rules.forEach((rule) => {
const { customProperties, severity, passed, targetVal } = rule;
const status = passed ? 'PASSED' : 'FAILED';
const severityText = severity.toUpperCase();
// Check if rule is found in Spectral results
const spectralResult = spectralResults.find((result) => {
return result.område === customProperties.område && result.id === customProperties.id;
});
if (spectralResult) {
//We have a match, that means there is an error
if (executedRuleIdsWithError != undefined && executedRuleIdsWithError.size >= 0) {
if (!executedRuleIdsWithError.has(customProperties.id)) {
this._ruleSets.executedUniqueRulesWithError.push({
id: customProperties.id, // Store some more diagnostic info (Duplicate NOT OK)
område: customProperties.område,
krav: rules[key]?.message ?? '',
});
}
}
executedRuleIdsWithError.add(customProperties.id); // Store current ID of rule with error
} else {
//We dont have a match, that means there is not an error and 'only' a 'tracked' rule execution
if (executedRuleIds != undefined && executedRuleIds.size >= 0) {
}
if (!executedRuleIds.has(customProperties.id)) {
this._ruleSets.executedUniqueRules.push({
id: customProperties.id, // Store some more diagnostic info (Duplicate OK)
område: customProperties.område,
krav:rules[key]?.message ?? '',
});
}
executedRuleIds.add(customProperties.id); // Store current ID of rule with NO error
}
});
}
ruleIdsNotApplicable = new Set([...executedRuleIds, ...executedRuleIdsWithError]);
for (const key of instanceCategoryMap.keys()) {
const customProperties = instanceCategoryMap.get(key).customProperties;
const exists = this._ruleSets.notApplicableRules.some((rule) => {
return rule.id === customProperties.id && rule.område === customProperties.område;
});
if (!ruleIdsNotApplicable.has(customProperties.id) && !exists) {
// If not present, store the id and område in the not applicableRules
this._ruleSets.notApplicableRules.push({ id: customProperties.id, område: customProperties.område, krav: rules[key]?.message ?? ''}); // Rules
}
}
}
processDiagnosticInformation(): DiagnosticReport[] {
const allReports: DiagnosticReport[] = [];
// Populate the diagnostic reports and add them to the array
if (this.diagnosticInformation.executedUniqueRules && this.diagnosticInformation.executedUniqueRules.length > 0) {
allReports.push(
this.populateDiagnosticRuleInformation(
this.diagnosticInformation.executedUniqueRules,
'OK',
'N/A',
'N/A',
'Godkända regler - RAP-LP',
),
);
}
if (
this.diagnosticInformation.executedUniqueRulesWithError &&
this.diagnosticInformation.executedUniqueRulesWithError.length > 0
) {
allReports.push(
this.populateDiagnosticRuleInformation(
this.diagnosticInformation.executedUniqueRulesWithError,
'EJ OK',
'N/A',
'N/A',
'Ej Godkända regler - RAP-LP',
),
);
}
if (this.diagnosticInformation.notApplicableRules && this.diagnosticInformation.notApplicableRules.length > 0) {
allReports.push(
this.populateDiagnosticRuleInformation(
this.diagnosticInformation.notApplicableRules,
'N/A',
'N/A',
'N/A',
'Ej tillämpade regler - RAP-LP',
),
);
}
return allReports;
}
private populateDiagnosticRuleInformation(
rules: DiagnosticRuleInfo[],
status: string,
area: string,
identificationNumber: string,
notering: string,
): DiagnosticReport {
// Map each rule to a PopulatedDiagnosticRuleInfo object
const populatedRules: PopulatedDiagnosticRuleInfo[] = rules.map((rule) => ({
...rule,
status,
//add other fields here as well,
}));
// Construct the diagnostic report for current DiagnosticRuleInfo[]
const report: DiagnosticReport = {
Notering: notering,
regler: populatedRules,
};
return report;
}
}
export { RapLPDiagnostic };
interface DiagnosticRuleinfoSet {
//--- Diagnostic information
notApplicableRules: DiagnosticRuleInfo[];
//--- Unique information
executedUniqueRules: DiagnosticRuleInfo[];
executedUniqueRulesWithError: DiagnosticRuleInfo[];
}
interface DiagnosticRuleInfo {
id: string;
område: string;
krav: string;
}
interface PopulatedDiagnosticRuleInfo extends DiagnosticRuleInfo {
status: string;
}
interface NoteringField {
Notering: string;
}
export interface DiagnosticReport {
Notering: string;
regler: PopulatedDiagnosticRuleInfo[];
}