Skip to content

Commit 72d6c14

Browse files
committed
feat(core): implement categories, groups and audits comparisons
1 parent 4e87cd5 commit 72d6c14

File tree

3 files changed

+520
-0
lines changed

3 files changed

+520
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import {
2+
AuditDiff,
3+
AuditReport,
4+
AuditResult,
5+
CategoryDiff,
6+
CategoryResult,
7+
GroupDiff,
8+
GroupResult,
9+
PluginMeta,
10+
ReportsDiff,
11+
} from '@code-pushup/models';
12+
import {
13+
Diff,
14+
ScoredCategoryConfig,
15+
ScoredGroup,
16+
ScoredReport,
17+
comparePairs,
18+
listAuditsFromAllPlugins,
19+
listGroupsFromAllPlugins,
20+
matchArrayItemsByKey,
21+
} from '@code-pushup/utils';
22+
23+
export type ReportsToCompare = Diff<ScoredReport>;
24+
25+
export function compareCategories(
26+
reports: ReportsToCompare,
27+
): ReportsDiff['categories'] {
28+
const { pairs, added, removed } = matchArrayItemsByKey({
29+
before: reports.before.categories,
30+
after: reports.after.categories,
31+
key: 'slug',
32+
});
33+
const { changed, unchanged } = comparePairs(
34+
pairs,
35+
({ before, after }) => before.score === after.score,
36+
);
37+
38+
return {
39+
changed: changed.map(categoryPairToDiff),
40+
unchanged: unchanged.map(categoryToResult),
41+
added: added.map(categoryToResult),
42+
removed: removed.map(categoryToResult),
43+
};
44+
}
45+
46+
export function compareGroups(
47+
reports: ReportsToCompare,
48+
): ReportsDiff['groups'] {
49+
const { pairs, added, removed } = matchArrayItemsByKey({
50+
before: listGroupsFromAllPlugins(reports.before),
51+
after: listGroupsFromAllPlugins(reports.after),
52+
key: ({ plugin, group }) => `${plugin.slug}/${group.slug}`,
53+
});
54+
const { changed, unchanged } = comparePairs(
55+
pairs,
56+
({ before, after }) => before.group.score === after.group.score,
57+
);
58+
59+
return {
60+
changed: changed.map(pluginGroupPairToDiff),
61+
unchanged: unchanged.map(pluginGroupToResult),
62+
added: added.map(pluginGroupToResult),
63+
removed: removed.map(pluginGroupToResult),
64+
};
65+
}
66+
67+
export function compareAudits(
68+
reports: ReportsToCompare,
69+
): ReportsDiff['audits'] {
70+
const { pairs, added, removed } = matchArrayItemsByKey({
71+
before: listAuditsFromAllPlugins(reports.before),
72+
after: listAuditsFromAllPlugins(reports.after),
73+
key: ({ plugin, audit }) => `${plugin.slug}/${audit.slug}`,
74+
});
75+
const { changed, unchanged } = comparePairs(
76+
pairs,
77+
({ before, after }) =>
78+
before.audit.value === after.audit.value &&
79+
before.audit.score === after.audit.score,
80+
);
81+
82+
return {
83+
changed: changed.map(pluginAuditPairToDiff),
84+
unchanged: unchanged.map(pluginAuditToResult),
85+
added: added.map(pluginAuditToResult),
86+
removed: removed.map(pluginAuditToResult),
87+
};
88+
}
89+
90+
function categoryToResult(category: ScoredCategoryConfig): CategoryResult {
91+
return {
92+
slug: category.slug,
93+
title: category.title,
94+
score: category.score,
95+
};
96+
}
97+
98+
function categoryPairToDiff({
99+
before,
100+
after,
101+
}: Diff<ScoredCategoryConfig>): CategoryDiff {
102+
return {
103+
slug: after.slug,
104+
title: after.title,
105+
scores: {
106+
before: before.score,
107+
after: after.score,
108+
diff: after.score - before.score,
109+
},
110+
};
111+
}
112+
113+
type PluginGroup = {
114+
group: ScoredGroup;
115+
plugin: PluginMeta;
116+
};
117+
118+
function pluginGroupToResult({ group, plugin }: PluginGroup): GroupResult {
119+
return {
120+
slug: group.slug,
121+
title: group.title,
122+
plugin: {
123+
slug: plugin.slug,
124+
title: plugin.title,
125+
},
126+
score: group.score,
127+
};
128+
}
129+
130+
function pluginGroupPairToDiff({
131+
before,
132+
after,
133+
}: Diff<PluginGroup>): GroupDiff {
134+
return {
135+
slug: after.group.slug,
136+
title: after.group.title,
137+
plugin: {
138+
slug: after.plugin.slug,
139+
title: after.plugin.title,
140+
},
141+
scores: {
142+
before: before.group.score,
143+
after: after.group.score,
144+
diff: after.group.score - before.group.score,
145+
},
146+
};
147+
}
148+
149+
type PluginAudit = {
150+
audit: AuditReport;
151+
plugin: PluginMeta;
152+
};
153+
154+
function pluginAuditToResult({ audit, plugin }: PluginAudit): AuditResult {
155+
return {
156+
slug: audit.slug,
157+
title: audit.title,
158+
plugin: {
159+
slug: plugin.slug,
160+
title: plugin.title,
161+
},
162+
score: audit.score,
163+
value: audit.value,
164+
displayValue: audit.displayValue,
165+
};
166+
}
167+
168+
function pluginAuditPairToDiff({
169+
before,
170+
after,
171+
}: Diff<PluginAudit>): AuditDiff {
172+
return {
173+
slug: after.audit.slug,
174+
title: after.audit.title,
175+
plugin: {
176+
slug: after.plugin.slug,
177+
title: after.plugin.title,
178+
},
179+
scores: {
180+
before: before.audit.score,
181+
after: after.audit.score,
182+
diff: after.audit.score - before.audit.score,
183+
},
184+
values: {
185+
before: before.audit.value,
186+
after: after.audit.value,
187+
diff: after.audit.value - before.audit.value,
188+
},
189+
displayValues: {
190+
before: before.audit.displayValue,
191+
after: after.audit.displayValue,
192+
},
193+
};
194+
}

0 commit comments

Comments
 (0)