Skip to content

Commit 40d72e7

Browse files
committed
FI-1635 fix: add separate resources group to API statistics
1 parent 51e3d22 commit 40d72e7

13 files changed

Lines changed: 119 additions & 66 deletions

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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

1213
/**
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+
};

src/utils/apiStatistics/addRequests.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import {assertValueIsDefined} from '../asserts';
22
import {getKeys, setReadonlyProperty} from '../object';
33

4-
import type {ApiStatistics, RequestStatistics, StatisticsUnit} from '../../types/internal';
4+
import {addRequestStatistics} from './addRequestStatistics';
5+
6+
import type {ApiStatistics, RequestStatistics} from '../../types/internal';
57

68
/**
7-
* Add additional requests to total API statistics requests.
9+
* Adds additional requests to total API statistics requests.
810
* @internal
911
*/
1012
export const addRequests = (
@@ -32,23 +34,7 @@ export const addRequests = (
3234
setReadonlyProperty(targetByMethod, method, targetByStatusCode);
3335
}
3436

35-
for (const statusCode of getKeys(sourceByStatusCode)) {
36-
const sourceUnit: StatisticsUnit | undefined = sourceByStatusCode[statusCode];
37-
38-
assertValueIsDefined(sourceUnit, 'sourceUnit is defined', {method, statusCode, url});
39-
40-
let targetUnit = targetByStatusCode[statusCode];
41-
42-
// eslint-disable-next-line max-depth
43-
if (targetUnit === undefined) {
44-
targetUnit = {count: 0, duration: 0, size: 0};
45-
setReadonlyProperty(targetByStatusCode, statusCode, targetUnit);
46-
}
47-
48-
setReadonlyProperty(targetUnit, 'count', targetUnit.count + sourceUnit.count);
49-
setReadonlyProperty(targetUnit, 'duration', targetUnit.duration + sourceUnit.duration);
50-
setReadonlyProperty(targetUnit, 'size', targetUnit.size + sourceUnit.size);
51-
}
37+
addRequestStatistics(targetByStatusCode, sourceByStatusCode);
5238
}
5339
}
5440
};
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 {addRequestStatistics} from './addRequestStatistics';
5+
6+
import type {ApiStatistics} from '../../types/internal';
7+
8+
/**
9+
* Adds additional resources to total API statistics resources.
10+
* @internal
11+
*/
12+
export const addResources = (
13+
targetByUrl: ApiStatistics['resources'],
14+
sourceByUrl: ApiStatistics['resources'],
15+
): void => {
16+
for (const url of getKeys(sourceByUrl)) {
17+
const sourceByStatusCode = sourceByUrl[url];
18+
19+
assertValueIsDefined(sourceByStatusCode, 'sourceByStatusCode is defined', {
20+
sourceByUrl,
21+
url,
22+
});
23+
24+
let targetByStatusCode = targetByUrl[url];
25+
26+
targetByStatusCode ??= setReadonlyProperty(targetByUrl, url, Object.create(null) as {});
27+
28+
addRequestStatistics(targetByStatusCode, sourceByStatusCode);
29+
}
30+
};

src/utils/apiStatistics/addResponseToApiStatistics.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {getHeaderValue} from '../headers';
55
import {addApiStatistics} from './addApiStatistics';
66
import {getUrlTemplate} from './getUrlTemplate';
77

8-
import type {ApiStatistics, ResponseWithRequest} from '../../types/internal';
8+
import type {ApiStatistics, RequestStatistics, ResponseWithRequest} from '../../types/internal';
99

1010
/**
11-
* Add single `ResponseWithRequest` to API statistics.
11+
* Adds single `ResponseWithRequest` to API statistics.
1212
* @internal
1313
*/
1414
export const addResponseToApiStatistics = (responseWithRequest: ResponseWithRequest): void => {
@@ -19,13 +19,14 @@ export const addResponseToApiStatistics = (responseWithRequest: ResponseWithRequ
1919
statusCode,
2020
} = responseWithRequest;
2121

22+
const {hasExtension, urlTemplate} = getUrlTemplate(url);
23+
const isResource = hasExtension && method === 'GET';
2224
const size = Number(getHeaderValue(responseWithRequest.responseHeaders, 'content-length')) || 0;
23-
const urlTemplate = getUrlTemplate(url);
25+
const requestStatistics: RequestStatistics = {[statusCode]: {count: 1, duration, size}};
2426

25-
const additionalApiStatistics: ApiStatistics = {
26-
pages: {},
27-
requests: {[urlTemplate]: {[method]: {[statusCode]: {count: 1, duration, size}}}},
28-
};
27+
const additionalApiStatistics: ApiStatistics = isResource
28+
? {pages: {}, requests: {}, resources: {[urlTemplate]: requestStatistics}}
29+
: {pages: {}, requests: {[urlTemplate]: {[method]: requestStatistics}}, resources: {}};
2930

3031
addApiStatistics(apiStatistics, additionalApiStatistics);
3132
};

src/utils/apiStatistics/getBorderCount.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ const zCodePoint = 122;
99
const ZCodePoint = 90;
1010
const zeroCodePoint = 48;
1111

12+
type Return = Readonly<{
13+
borderCount: number;
14+
letterCount: number;
15+
}>;
16+
1217
/**
1318
* Returns count of border (letter or non-letter) inside string.
1419
* @internal
1520
*/
1621
// eslint-disable-next-line complexity
17-
export const getBorderCount = (value: string): number => {
22+
export const getBorderCount = (value: string): Return => {
1823
let borderCount = -1;
24+
let letterCount = 0;
1925
let mode: '.' | '0' | 'A' | 'a' | undefined;
2026

2127
for (const char of value) {
@@ -37,6 +43,7 @@ export const getBorderCount = (value: string): number => {
3743
borderCount += 1;
3844
}
3945

46+
letterCount += 1;
4047
mode = 'A';
4148
break;
4249

@@ -45,6 +52,7 @@ export const getBorderCount = (value: string): number => {
4552
borderCount += 1;
4653
}
4754

55+
letterCount += 1;
4856
mode = 'a';
4957
break;
5058

@@ -57,5 +65,5 @@ export const getBorderCount = (value: string): number => {
5765
}
5866
}
5967

60-
return borderCount;
68+
return {borderCount, letterCount};
6169
};

0 commit comments

Comments
 (0)