Skip to content

Commit

Permalink
fix(trends): improvements (#129)
Browse files Browse the repository at this point in the history
* fix(trends): improvments

* fix config
  • Loading branch information
Julien Bouquillon authored Feb 1, 2022
1 parent 73d454f commit 02ca7b3
Show file tree
Hide file tree
Showing 3 changed files with 4,643 additions and 12,080 deletions.
34 changes: 23 additions & 11 deletions report/src/trends/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function generateTrends(gitPath, latestReport, maxDaysHistory = 30) {
const repo = await Repository.open(gitPath);

// get history for the report file
core.info(`Open GIT history for report.json`);
core.info(`Open GIT history for ${gitPath}/report.json`);
const history = await gitHistory(`${gitPath}/report.json`);

// ensure git history is sorted from latest to oldest commit
Expand Down Expand Up @@ -86,17 +86,29 @@ async function generateTrends(gitPath, latestReport, maxDaysHistory = 30) {
urlsHistory[url] = {};
}
summary &&
Object.keys(summary).forEach((
/** @type {keyof UrlReportSummary}*/ key
) => {
if (!urlsHistory[url][key]) {
urlsHistory[url][key] = [];
Object.keys(summary).forEach(
(/** @type {keyof UrlReportSummary}*/ key) => {
if (!urlsHistory[url][key]) {
urlsHistory[url][key] = [];
}
if (summary[key] !== undefined) {
let isSame = false;
// check if different than previous value
if (urlsHistory[url][key].length) {
if (
urlsHistory[url][key][urlsHistory[url][key].length - 1]
.value === summary[key]
) {
isSame = true;
}
}
if (!isSame) {
/** @ts-expect-error */
urlsHistory[url][key].push({ date, value: summary[key] });
}
}
}
if (summary[key] !== undefined) {
/** @ts-expect-error */
urlsHistory[url][key].push({ date, value: summary[key] });
}
});
);
});
});
return urlsHistory;
Expand Down
41 changes: 28 additions & 13 deletions report/www/src/components/Trends.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ const getChanges = (urlTrends: UrlMetricsHistoryValues): ChangeSet => {
Object.keys(urlTrends)
.filter((key) => key in metricsDefinitions)
.forEach((key) => {
const values = urlTrends[key].map(({ date, value }) => value);

const uniqueValues = uniqify(values);
if (uniqueValues.length > 1) {
changes[key] = uniqueValues;
const values = urlTrends[key].map(({ date, value }) => value) as any[];
if (values.length > 1) {
// keep only first and last
const firstValue = values[0];
const lastValue = values[values.length - 1];
const treshold = metricsDefinitions[key].treshold;
const isValid = treshold
? Math.abs(firstValue - lastValue) > (treshold || 0)
: true;
if (isValid && firstValue !== lastValue) {
changes[key] = [firstValue, lastValue];
}
}
});
return changes;
Expand All @@ -29,24 +36,32 @@ const metricsDefinitions = {
codescanGrade: { title: "Codescan grade" },
dependabotGrade: { title: "Dependabot grade" },
httpGrade: { title: "HTTP observatory" },
lighthouse_performance: { title: "Performance" },
lighthouse_seo: { title: "Lighthouse SEO" },
lighthouse_pwa: { title: "Lighthouse PWA" },
lighthouse_performance: { treshold: 0.1, title: "Lighthouse Performance" },
lighthouse_seo: { treshold: 0.1, title: "Lighthouse SEO" },
lighthouse_pwa: { treshold: 0.1, title: "Lighthouse PWA" },
lighthouse_accessibility: { title: "Lighthouse accessibility" },
"lighthouse_best-practices": { title: "Lighthouse best practices" },
"lighthouse_best-practices": {
treshold: 0.1,
title: "Lighthouse best practices",
},
nmapGrade: { title: "NMAP grade" },
nmapOpenPortsGrade: { title: "NMAP open ports grade" },
trackersCount: { title: "Trackers count", reverse: true },
cookiesCount: { title: "Cookies count", reverse: true },
uptime: { title: "uptime" },
apdex: { title: "apDex" },
} as Record<any, { title: string; reverse?: boolean }>;
uptime: { treshold: 1, title: "uptime" },
apdex: { treshold: 0.05, title: "apDex" },
"declaration-a11y": { title: "Déclaration a11y" },
trivyGrade: { title: "Trivy grade" },
} as Record<
keyof UrlReportSummary,
{ title: string; reverse?: boolean; treshold?: number }
>;

const getTrend = (metric: SummaryKey, values: any[]) => {
const metricDefinition = metricsDefinitions[metric];
const firstValue = values[0];
const lastValue = values[values.length - 1];
if (metric.match(/Grade$/)) {
if (metric.match(/Grade$/) || metric === "declaration-a11y") {
return letterGradeValue(lastValue) - letterGradeValue(firstValue);
}
if (metricDefinition.reverse) {
Expand Down
Loading

0 comments on commit 02ca7b3

Please sign in to comment.