Skip to content

Commit 05d37af

Browse files
committed
FI-1635 feat: collect API statistics for requests
refactor: add typed `getKeys` instead of `Object.keys`
1 parent 52c7c25 commit 05d37af

62 files changed

Lines changed: 355 additions & 91 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ Modules in the dependency graph should only import the modules above them:
1414
7. `configurator`
1515
8. `utils/getHash`
1616
9. `generators`
17-
10. `utils/viewport`
18-
11. `utils/parse`
19-
12. `utils/distanceBetweenSelectors`
20-
13. `utils/getDurationWithUnits`
21-
14. `utils/setReadonlyProperty`
22-
15. `utils/paths`
23-
16. `utils/valueToString`
24-
17. `utils/error`
25-
18. `utils/asserts`
17+
10. `utils/headers`
18+
11. `utils/viewport`
19+
12. `utils/parse`
20+
13. `utils/distanceBetweenSelectors`
21+
14. `utils/getDurationWithUnits`
22+
15. `utils/valueToString`
23+
16. `utils/error`
24+
17. `utils/asserts`
25+
18. `utils/object`
2626
19. `utils/uiMode`
2727
20. `utils/runLabel`
2828
21. `utils/clone`
@@ -44,10 +44,11 @@ Modules in the dependency graph should only import the modules above them:
4444
37. `utils/pack`
4545
38. `useContext`
4646
39. `context`
47-
40. `utils/selectors`
48-
41. `selectors`
49-
42. `utils/log`
50-
43. `utils/waitForEvents`
51-
44. `utils/expect`
52-
45. `expect`
53-
46. ...
47+
40. `utils/apiStatistics`
48+
41. `utils/selectors`
49+
42. `selectors`
50+
43. `utils/log`
51+
44. `utils/waitForEvents`
52+
45. `utils/expect`
53+
46. `expect`
54+
47. ...

src/actions/mock/mockApiRoute.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {assertValueIsDefined} from '../../utils/asserts';
66
import {setCustomInspectOnFunction} from '../../utils/fn';
77
import {log} from '../../utils/log';
88
import {getRequestsFilter, getSetResponse} from '../../utils/mockApiRoute';
9-
import {setReadonlyProperty} from '../../utils/setReadonlyProperty';
9+
import {setReadonlyProperty} from '../../utils/object';
1010

1111
import type {
1212
ApiMockFunction,

src/actions/mock/mockWebSocketRoute.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {assertValueIsDefined} from '../../utils/asserts';
66
import {setCustomInspectOnFunction} from '../../utils/fn';
77
import {log} from '../../utils/log';
88
import {getRequestsFilter, getSetResponse} from '../../utils/mockWebSocketRoute';
9-
import {setReadonlyProperty} from '../../utils/setReadonlyProperty';
9+
import {setReadonlyProperty} from '../../utils/object';
1010

1111
import type {
1212
WebSocketMockFunction,

src/actions/setHeadersAndNavigateToUrl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {AsyncLocalStorage} from 'node:async_hooks';
22

33
import {LogEventType} from '../constants/internal';
44
import {getPlaywrightPage} from '../useContext';
5+
import {applyHeadersMapper} from '../utils/headers';
56
import {log} from '../utils/log';
6-
import {applyHeadersMapper} from '../utils/requestHooks';
77

88
import {navigateToUrl} from './navigateToUrl';
99

src/actions/waitFor/waitForAllRequestsComplete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {E2edError} from '../../utils/error';
66
import {setCustomInspectOnFunction} from '../../utils/fn';
77
import {getDurationWithUnits} from '../../utils/getDurationWithUnits';
88
import {log} from '../../utils/log';
9+
import {setReadonlyProperty} from '../../utils/object';
910
import {getPromiseWithResolveAndReject} from '../../utils/promise';
10-
import {setReadonlyProperty} from '../../utils/setReadonlyProperty';
1111
import {
1212
getInitialIdsForAllRequestsCompletePredicate,
1313
getUrlsByRequestHookContextIds,

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {assertValueIsTrue} from './utils/asserts';
2121
import {assertUserlandPack} from './utils/config/assertUserlandPack';
2222
import {getPathToPack} from './utils/environment';
2323
import {setCustomInspectOnFunction} from './utils/fn';
24-
import {setReadonlyProperty} from './utils/setReadonlyProperty';
24+
import {setReadonlyProperty} from './utils/object';
2525
import {isUiMode} from './utils/uiMode';
2626
import {isLocalRun} from './configurator';
2727

src/constants/internal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export {CREATE_PAGE_TOKEN} from './pages';
4343
export {
4444
ABSOLUTE_PATH_TO_INSTALLED_E2ED_DIRECTORY,
4545
ABSOLUTE_PATH_TO_PROJECT_ROOT_DIRECTORY,
46+
API_STATISTICS_PATH,
4647
AUTOTESTS_DIRECTORY_PATH,
4748
COMPILED_USERLAND_CONFIG_DIRECTORY,
4849
CONFIG_PATH,

src/constants/paths.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,20 @@ export const TESTS_DIRECTORY_PATH = join(
7676
) as DirectoryPathFromRoot;
7777

7878
/**
79-
* Relative (from root) path to tmp directory.
79+
* Relative (from root) path to temporary directory.
8080
* @internal
8181
*/
8282
export const TMP_DIRECTORY_PATH = join(REPORTS_DIRECTORY_PATH, 'tmp') as DirectoryPathFromRoot;
8383

84+
/**
85+
* Relative (from root) path to file with total API statistics of run.
86+
* @internal
87+
*/
88+
export const API_STATISTICS_PATH = join(
89+
TMP_DIRECTORY_PATH,
90+
'apiStatistics.txt',
91+
) as FilePathFromRoot;
92+
8493
/**
8594
* Relative (from root) path to directory with compiled pack configuration files.
8695
* @internal

src/context/apiStatistics.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {useContext} from '../useContext';
2+
3+
import type {ApiStatistics} from '../types/internal';
4+
5+
/**
6+
* Raw get and set internal (maybe `undefined`) API statistics.
7+
* @internal
8+
*/
9+
const [getRawApiStatistics, setRawApiStatistics] = useContext<ApiStatistics>();
10+
11+
/**
12+
* Get internal always defined API statistics.
13+
* @internal
14+
*/
15+
export const getApiStatistics = (): ApiStatistics => {
16+
const maybeApiStatistics = getRawApiStatistics();
17+
18+
if (maybeApiStatistics !== undefined) {
19+
return maybeApiStatistics;
20+
}
21+
22+
const apiStatistics: ApiStatistics = {requests: Object.create(null) as {}};
23+
24+
setRawApiStatistics(apiStatistics);
25+
26+
return apiStatistics;
27+
};

src/types/apiStatistics.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type {Method, StatusCode, Url} from './http';
2+
3+
/**
4+
* API statistics of `e2ed` run.
5+
*/
6+
export type ApiStatistics = Readonly<{
7+
requests: Readonly<Record<Url, Readonly<Partial<Record<Method, RequestStatistics>>>>>;
8+
}>;
9+
10+
/**
11+
* Statistics of one API request (by `origin`).
12+
*/
13+
export type RequestStatistics = Readonly<Partial<Record<StatusCode, StatisticsUnit>>>;
14+
15+
/**
16+
* Unit of summary statistics for some exact conditions.
17+
*/
18+
export type StatisticsUnit = Readonly<{count: number; duration: number; size: number}>;

0 commit comments

Comments
 (0)