Skip to content

Commit da6d4f8

Browse files
authored
Merge pull request #114 from joomcode/fix/add-resources-to-api-statistics
feat: add API statistics to HTML report
2 parents 51e3d22 + 8b26198 commit da6d4f8

34 files changed

Lines changed: 384 additions & 163 deletions

package-lock.json

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"@playwright/test": "1.51.0",
2929
"create-locator": "0.0.27",
3030
"get-modules-graph": "0.0.11",
31-
"globby": "11.1.0"
31+
"globby": "11.1.0",
32+
"sort-json-keys": "1.0.3"
3233
},
3334
"devDependencies": {
3435
"@playwright/browser-chromium": "1.51.0",

src/context/apiStatistics.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const getApiStatistics = (): ApiStatistics => {
2222
const apiStatistics: ApiStatistics = {
2323
pages: Object.create(null) as {},
2424
requests: Object.create(null) as {},
25+
resources: Object.create(null) as {},
2526
};
2627

2728
setRawApiStatistics(apiStatistics);

src/types/apiStatistics.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@ import type {Method, StatusCode, Url} from './http';
77
export type ApiStatistics = Readonly<{
88
pages: Readonly<Record<PageName, Readonly<Record<Url, PageStatistics>>>>;
99
requests: Readonly<Record<Url, Readonly<Partial<Record<Method, RequestStatistics>>>>>;
10+
resources: Readonly<Record<Url, RequestStatistics>>;
1011
}>;
1112

13+
/**
14+
* Hash string of menu buttons for showing parts of `ApiStatistics` in HTML report.
15+
* @internal
16+
*/
17+
export type ApiStatisticsReportHash = `api-statistics-${keyof ApiStatistics}`;
18+
1219
/**
1320
* Page name (as name of page class).
1421
*/

src/types/internal.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export type {
99
RequestStatistics,
1010
StatisticsUnit,
1111
} from './apiStatistics';
12+
/** @internal */
13+
export type {ApiStatisticsReportHash} from './apiStatistics';
1214
export type {Brand, IsBrand} from './brand';
1315
export type {Expect, IsEqual, IsReadonlyKey} from './checks';
1416
export type {Class} from './class';

src/types/report.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export type Retry = Readonly<{
116116
*/
117117
export type RetryButtonProps = Readonly<{
118118
disabled: boolean;
119+
name: string;
119120
retry: number;
120121
selected: boolean;
121122
}>;
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
import {addPages} from './addPages';
22
import {addRequests} from './addRequests';
3+
import {addResources} from './addResources';
34

45
import type {ApiStatistics} from '../../types/internal';
56

67
/**
7-
* Add additional API statistics to total API statistics.
8+
* Adds additional API statistics to total API statistics.
89
* @internal
910
*/
1011
export const addApiStatistics = (target: ApiStatistics, source: ApiStatistics): void => {
11-
const sourceByPageName = source.pages;
12-
const targetByPageName = target.pages;
13-
14-
addPages(targetByPageName, sourceByPageName);
15-
16-
const sourceByUrl = source.requests;
17-
const targetByUrl = target.requests;
18-
19-
addRequests(targetByUrl, sourceByUrl);
12+
addPages(target.pages, source.pages);
13+
addRequests(target.requests, source.requests);
14+
addResources(target.resources, source.resources);
2015
};

src/utils/apiStatistics/addPageToApiStatistics.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ type Options = Readonly<{
1212
}>;
1313

1414
/**
15-
* Add single page to API statistics.
15+
* Adds single page to API statistics.
1616
* @internal
1717
*/
1818
export const addPageToApiStatistics = ({duration, pageName, url}: Options): void => {
1919
const apiStatistics = getApiStatistics();
20-
const urlTemplate = getUrlTemplate(url);
20+
const {urlTemplate} = getUrlTemplate(url);
2121

2222
const additionalApiStatistics: ApiStatistics = {
2323
pages: {[pageName]: {[urlTemplate]: {count: 1, duration}}},
2424
requests: {},
25+
resources: {},
2526
};
2627

2728
addApiStatistics(apiStatistics, additionalApiStatistics);

src/utils/apiStatistics/addPages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {getKeys, setReadonlyProperty} from '../object';
44
import type {ApiStatistics, PageStatistics} from '../../types/internal';
55

66
/**
7-
* Add additional pages to total API statistics pages.
7+
* Adds additional pages to total API statistics pages.
88
* @internal
99
*/
1010
export const addPages = (
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {assertValueIsDefined} from '../asserts';
2+
import {getKeys, setReadonlyProperty} from '../object';
3+
4+
import type {RequestStatistics, StatisticsUnit} from '../../types/internal';
5+
6+
/**
7+
* Adds additional requests statistics to total API resource statistics.
8+
* @internal
9+
*/
10+
export const addRequestStatistics = (
11+
targetByStatusCode: RequestStatistics,
12+
sourceByStatusCode: RequestStatistics,
13+
): void => {
14+
for (const statusCode of getKeys(sourceByStatusCode)) {
15+
const sourceUnit: StatisticsUnit | undefined = sourceByStatusCode[statusCode];
16+
17+
assertValueIsDefined(sourceUnit, 'sourceUnit is defined', {statusCode});
18+
19+
let targetUnit = targetByStatusCode[statusCode];
20+
21+
if (targetUnit === undefined) {
22+
targetUnit = {count: 0, duration: 0, size: 0};
23+
setReadonlyProperty(targetByStatusCode, statusCode, targetUnit);
24+
}
25+
26+
setReadonlyProperty(targetUnit, 'count', targetUnit.count + sourceUnit.count);
27+
setReadonlyProperty(targetUnit, 'duration', targetUnit.duration + sourceUnit.duration);
28+
setReadonlyProperty(targetUnit, 'size', targetUnit.size + sourceUnit.size);
29+
}
30+
};

0 commit comments

Comments
 (0)