-
Notifications
You must be signed in to change notification settings - Fork 1
/
rescore.js
143 lines (135 loc) · 5.27 KB
/
rescore.js
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
/*
© 2024 CVS Health and/or one of its affiliates. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
rescore.js
Rescores and returns a scored report.
Arguments:
0. scoring function.
1. scored report.
2. restriction type (tools or issues).
3. array of IDs of tools or issues to be included.
*/
// ########## IMPORTS
// Module to perform common operations.
const {getNowStamp} = require('./procs/util');
// ########## FUNCTIONS
// Rescores a report.
exports.rescore = (scorer, report, restrictionType, includedIDs) => {
const result = {
success: true
};
// If tools are restricted:
const {acts, id, score} = report;
if (restrictionType === 'tools') {
// If all the tools included by the restriction are in the report:
const reportToolIDs = new Set(acts.filter(act => act.type === 'test').map(act => act.which));
if (includedIDs.every(
includedID => acts.some(act => act.type === 'test' && act.which === includedID)
)) {
// Delete the acts that are tests of other tools.
report.acts = acts.filter(act => act.type !== 'test' || includedIDs.includes(act.which));
}
// Otherwise, i.e. if any tool included by the restriction is not in the report:
else {
// Report this and abort rescoring.
const message = `Report includes only tools ${Array.from(reportToolIDs).join(', ')}`;
result.success = false;
result.error = message;
console.log(`ERROR: ${message}`);
}
}
// Otherwise, if issues are restricted:
else if (restrictionType === 'issues') {
// Initialize data on the violated rules of the issues included by the restriction.
const ruleData = {};
// For each issue with any rule violations:
const issueDetails = score.details.issue;
const reportIssueIDs = Object.keys(issueDetails);
reportIssueIDs.forEach(reportIssueID => {
// If the restriction includes the issue:
if (includedIDs.includes(reportIssueID)) {
// For each tool with any violated rules of the issue:
const issueToolIDs = Object.keys(issueDetails[reportIssueID].tools);
issueToolIDs.forEach(issueToolID => {
// For each violated rule of the issue of the tool:
const issueToolRuleIDs = Object.keys(issueDetails[reportIssueID].tools[issueToolID]);
issueToolRuleIDs.forEach(issueToolRuleID => {
// Add the rule to the rule data.
ruleData[issueToolID] ??= [];
ruleData[issueToolID].push(issueToolRuleID);
});
});
}
});
// For each act:
acts.forEach(act => {
// If it is a test act:
if (act.type === 'test') {
// Delete any standard instances of rules not included by the restriction.
const {data, standardResult} = act;
standardResult.instances = standardResult.instances.filter(
instance => ruleData[act.which].includes(instance.ruleID)
);
// Reinitialize the totals of the standard result.
standardResult.totals = [0, 0, 0, 0];
const {totals} = standardResult;
// If the tool of the act is Testaro:
if (act.which === 'testaro') {
// Recalculate the totals of the act.
const {ruleTotals} = data;
ruleTotals.forEach(ruleTotal => {
totals.forEach((total, index) => {
totals[index] += ruleTotal[index];
});
});
}
// Otherwise, i.e. if the tool is not Testaro:
else {
// Recalculate the totals of the act.
const {instances} = standardResult;
instances.forEach(instance => {
const {count, ordinalSeverity} = instance;
totals[ordinalSeverity] += count * ordinalSeverity;
});
}
}
});
}
// Otherwise, i.e. if neither tools nor issues are restricted:
else {
// Report this and abort rescoring.
const message = 'Neither tools nor issues are restricted';
result.success = false;
result.error = message;
console.log(`ERROR: ${message}`);
}
// Add rescoring data to the report.
report.rescore = {
originalID: id,
timeStamp: getNowStamp(),
restrictionType,
includedIDs,
result
}
// If rescoring was not aborted:
if (result.success) {
// Rescore the revised report.
scorer(report);
}
}