-
Notifications
You must be signed in to change notification settings - Fork 248
/
metrics.js
134 lines (122 loc) · 3.84 KB
/
metrics.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
const { EOL } = require("os");
const chalk = require("chalk");
const utl = require("./utl");
const DECIMAL_BASE = 10;
const METRIC_WIDTH = 5;
const COMPONENT_HEADER = "name";
function getHeader(pMaxNameWidth) {
return `${COMPONENT_HEADER.padEnd(pMaxNameWidth)} ${"N".padStart(
METRIC_WIDTH + 1
)} ${"Ca".padStart(METRIC_WIDTH + 1)} ${"Ce".padStart(
METRIC_WIDTH + 1
)} ${"I (%)".padStart(METRIC_WIDTH + 1)}`;
}
function getDemarcationLine(pMaxNameWidth) {
return `${"-".repeat(pMaxNameWidth)} ${"-".repeat(
METRIC_WIDTH + 1
)} ${"-".repeat(METRIC_WIDTH + 1)} ${"-".repeat(
METRIC_WIDTH + 1
)} ${"-".repeat(METRIC_WIDTH + 1)}`;
}
function getMetricsTable(pMetrics, pMaxNameWidth) {
return pMetrics.map(
({
name,
moduleCount,
afferentCouplings,
efferentCouplings,
instability,
}) =>
`${name.padEnd(pMaxNameWidth, " ")} ${moduleCount
.toString(DECIMAL_BASE)
.padStart(METRIC_WIDTH)} ${afferentCouplings
.toString(DECIMAL_BASE)
.padStart(METRIC_WIDTH)} ${efferentCouplings
.toString(DECIMAL_BASE)
.padStart(METRIC_WIDTH)} ${utl
.formatPercentage(instability)
.padStart(METRIC_WIDTH)}`
);
}
function metricifyModule({ source, dependents, dependencies, instability }) {
return {
name: source,
moduleCount: 1,
afferentCouplings: dependents.length,
efferentCouplings: dependencies.length,
instability,
};
}
function metricsAreCalculable(pModule) {
return Object.prototype.hasOwnProperty.call(pModule, "instability");
}
function orderByNumber(pAttributeName) {
// eslint-disable-next-line security/detect-object-injection
return (pLeft, pRight) => pRight[pAttributeName] - pLeft[pAttributeName];
}
function orderByString(pAttributeName) {
return (pLeft, pRight) =>
// eslint-disable-next-line security/detect-object-injection
pLeft[pAttributeName].localeCompare(pRight[pAttributeName]);
}
function componentIsCalculable(pComponent) {
return (
Number.isInteger(pComponent.moduleCount) && pComponent.moduleCount > -1
);
}
function transformMetricsToTable(
{ modules, folders },
{ orderBy, hideFolders, hideModules }
) {
// TODO: should probably use a table module for this (i.e. text-table)
// to simplify this code
let lComponents = [];
lComponents = lComponents.concat(hideFolders ? [] : folders);
lComponents = lComponents.concat(
hideModules ? [] : modules.filter(metricsAreCalculable).map(metricifyModule)
);
const lMaxNameWidth = lComponents
.map(({ name }) => name.length)
.concat(COMPONENT_HEADER.length)
.sort((pLeft, pRight) => pLeft - pRight)
.pop();
return [chalk.bold(getHeader(lMaxNameWidth))]
.concat(getDemarcationLine(lMaxNameWidth))
.concat(
getMetricsTable(
lComponents
.filter(componentIsCalculable)
.sort(orderByString("name"))
.sort(orderByNumber(orderBy || "instability")),
lMaxNameWidth
)
)
.join(EOL)
.concat(EOL);
}
/**
* returns stability metrics of modules & folders in an ascii table
*
* Potential future features:
* - additional output formats (csv?, html?)
*
* @param {import('../..').ICruiseResult} pCruiseResult -
* @param {import("../../types/reporter-options").IMetricsReporterOptions} pReporterOptions
* @return {import('../..').IReporterOutput} -
*/
module.exports = function metrics(pCruiseResult, pReporterOptions) {
const lReporterOptions = pReporterOptions || {};
if (pCruiseResult.folders) {
return {
output: transformMetricsToTable(pCruiseResult, lReporterOptions),
exitCode: 0,
};
} else {
return {
output:
`${EOL}ERROR: The cruise result didn't contain any metrics - re-running the cruise with${EOL}` +
` the '--metrics' command line option should fix that.${EOL}${EOL}`,
exitCode: 1,
};
}
};