@abyss-project/reflex 5.11.0 → 5.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/.eslintrc.js +32 -32
  2. package/.prettierrc +5 -5
  3. package/README.md +15 -15
  4. package/dist/types/interface/api/responses/service-version.response.d.ts +1 -0
  5. package/jest.config.ts +11 -11
  6. package/package.json +15 -10
  7. package/src/api/alert-rule.api.test.ts +72 -72
  8. package/src/api/alert-rule.api.ts +57 -57
  9. package/src/api/application-insights.api.test.ts +42 -42
  10. package/src/api/application-insights.api.ts +38 -38
  11. package/src/api/application-run-settings.api.test.ts +51 -51
  12. package/src/api/application-run-settings.api.ts +30 -30
  13. package/src/api/function-insights.api.test.ts +27 -27
  14. package/src/api/function-insights.api.ts +18 -18
  15. package/src/api/function-run.api.ts +38 -38
  16. package/src/api/function-settings.api.ts +74 -74
  17. package/src/api/function-test.api.ts +34 -34
  18. package/src/api/function.api.ts +62 -62
  19. package/src/api/health.api.ts +10 -10
  20. package/src/api/route-stats.api.test.ts +45 -45
  21. package/src/api/route-stats.api.ts +36 -36
  22. package/src/api/service-configuration.api.ts +16 -16
  23. package/src/api/service-deployment.api.ts +79 -79
  24. package/src/api/service-discord-event.api.test.ts +106 -106
  25. package/src/api/service-discord-event.api.ts +78 -78
  26. package/src/api/service-discord.api.test.ts +197 -197
  27. package/src/api/service-discord.api.ts +241 -241
  28. package/src/api/service-insights.api.test.ts +36 -36
  29. package/src/api/service-insights.api.ts +42 -42
  30. package/src/api/service-route.api.ts +72 -72
  31. package/src/api/service-task.api.test.ts +140 -140
  32. package/src/api/service-task.api.ts +141 -141
  33. package/src/api/service-version.api.ts +54 -54
  34. package/src/api/service.api.test.ts +215 -215
  35. package/src/api/service.api.ts +55 -55
  36. package/src/discord/discord-event-filter-cases.ts +261 -261
  37. package/src/discord/discord-event-filters.test.ts +91 -91
  38. package/src/discord/discord-event-filters.ts +95 -95
  39. package/src/discord/discord-event-intents.test.ts +112 -112
  40. package/src/discord/discord-event-intents.ts +55 -55
  41. package/src/discord/index.ts +3 -3
  42. package/src/index.test.ts +87 -87
  43. package/src/insights/index.ts +1 -1
  44. package/src/insights/percentiles.test.ts +75 -75
  45. package/src/insights/percentiles.ts +37 -37
  46. package/src/types/enum/access-grant-status.enum.ts +12 -12
  47. package/src/types/enum/access-kind.enum.ts +7 -7
  48. package/src/types/enum/bot-event-block-reason.enum.ts +7 -7
  49. package/src/types/enum/configuration-read-as.enum.ts +5 -5
  50. package/src/types/enum/discord-activity-type.enum.ts +9 -9
  51. package/src/types/enum/discord-channel-type.enum.ts +11 -11
  52. package/src/types/enum/discord-component-kind.enum.ts +7 -7
  53. package/src/types/enum/discord-event-drop-reason.enum.ts +13 -13
  54. package/src/types/enum/discord-event-filter-name.enum.ts +11 -11
  55. package/src/types/enum/discord-event-status.enum.ts +13 -13
  56. package/src/types/enum/discord-event-type.enum.ts +7 -7
  57. package/src/types/enum/discord-gateway-failure-code.enum.ts +11 -11
  58. package/src/types/enum/discord-gateway-state.enum.ts +11 -11
  59. package/src/types/enum/discord-host-action.enum.ts +35 -35
  60. package/src/types/enum/discord-locale.enum.ts +35 -35
  61. package/src/types/enum/discord-option-type.enum.ts +12 -12
  62. package/src/types/enum/discord-presence-status.enum.ts +8 -8
  63. package/src/types/enum/discord-sync-status.enum.ts +6 -6
  64. package/src/types/enum/discord-token-state.enum.ts +11 -11
  65. package/src/types/enum/execution-mode.enum.ts +9 -9
  66. package/src/types/enum/execution-profile.enum.ts +20 -20
  67. package/src/types/enum/execution-settings-source.enum.ts +9 -9
  68. package/src/types/enum/health-status.enum.ts +4 -4
  69. package/src/types/enum/insights-period.enum.ts +8 -8
  70. package/src/types/enum/log-cap-mode.enum.ts +6 -6
  71. package/src/types/enum/log-drop-cause.enum.ts +8 -8
  72. package/src/types/enum/reflex-alert-metric.enum.ts +5 -5
  73. package/src/types/enum/reflex-alert-notification-state.enum.ts +6 -6
  74. package/src/types/enum/reflex-alert-scope.enum.ts +6 -6
  75. package/src/types/enum/reflex-error-code.enum.ts +43 -43
  76. package/src/types/enum/route-method.enum.ts +9 -9
  77. package/src/types/enum/run-log-level.enum.ts +6 -6
  78. package/src/types/enum/run-mode.enum.ts +11 -11
  79. package/src/types/enum/run-status.enum.ts +11 -11
  80. package/src/types/enum/run-step-status.enum.ts +7 -7
  81. package/src/types/enum/schedule-sync-status.enum.ts +6 -6
  82. package/src/types/enum/scheduled-run-failure-code.enum.ts +12 -12
  83. package/src/types/enum/scheduled-run-origin.enum.ts +5 -5
  84. package/src/types/enum/scheduled-run-status.enum.ts +17 -17
  85. package/src/types/enum/service-deployment-action.enum.ts +11 -11
  86. package/src/types/enum/service-deployment-status.enum.ts +5 -5
  87. package/src/types/enum/service-type.enum.ts +9 -9
  88. package/src/types/enum/service-version-change-kind.enum.ts +6 -6
  89. package/src/types/enum/trace-effect-type.enum.ts +12 -12
  90. package/src/types/enum/trigger-access-mode.enum.ts +12 -12
  91. package/src/types/enum/trigger-kind.enum.ts +8 -8
  92. package/src/types/enum/vault-value-source.enum.ts +12 -12
  93. package/src/types/enum/vault-visibility.enum.ts +5 -5
  94. package/src/types/index.ts +2 -2
  95. package/src/types/interface/api/index.ts +3 -3
  96. package/src/types/interface/api/response.ts +4 -4
  97. package/src/types/interface/api/responses/service-version.response.ts +6 -0
  98. package/src/types/interface/health.response.ts +7 -7
  99. package/src/types/interface/index.ts +3 -3
  100. package/tsconfig.check.json +15 -15
  101. package/tsconfig.json +35 -35
@@ -1,55 +1,55 @@
1
- import { DiscordEventType } from '../types/enum';
2
- import type { IReflexDiscordEvent } from '../types/interface/models';
3
-
4
- /** The Gateway intents the Discord events need, as Discord numbers their bits. */
5
- export enum DiscordGatewayIntent {
6
- /** Member arrival and departure. Privileged. */
7
- GUILD_MEMBERS = 1 << 1,
8
- GUILD_MESSAGES = 1 << 9,
9
- GUILD_MESSAGE_REACTIONS = 1 << 10,
10
- /** The content of a message. Privileged. */
11
- MESSAGE_CONTENT = 1 << 15,
12
- }
13
-
14
- /** The intents Discord asks the bot owner to enable in the portal (and to have verified past 100 servers). */
15
- export const PRIVILEGED_DISCORD_GATEWAY_INTENTS: readonly DiscordGatewayIntent[] = [
16
- DiscordGatewayIntent.GUILD_MEMBERS,
17
- DiscordGatewayIntent.MESSAGE_CONTENT,
18
- ];
19
-
20
- /** The intent each event type needs by itself. `GUILDS` is never asked: the four events do not need its large packets. */
21
- export const DISCORD_EVENT_BASE_INTENT: Record<DiscordEventType, DiscordGatewayIntent> = {
22
- [DiscordEventType.GUILD_MEMBER_ADD]: DiscordGatewayIntent.GUILD_MEMBERS,
23
- [DiscordEventType.GUILD_MEMBER_REMOVE]: DiscordGatewayIntent.GUILD_MEMBERS,
24
- [DiscordEventType.MESSAGE_CREATE]: DiscordGatewayIntent.GUILD_MESSAGES,
25
- [DiscordEventType.MESSAGE_REACTION_ADD]: DiscordGatewayIntent.GUILD_MESSAGE_REACTIONS,
26
- };
27
-
28
- type IntentSource = Pick<IReflexDiscordEvent, 'eventType' | 'filters' | 'includeContent'>;
29
-
30
- /** True when the trigger reads the content of a message: it asks for it, or filters on a prefix. */
31
- const needsMessageContent = (event: IntentSource): boolean =>
32
- event.eventType === DiscordEventType.MESSAGE_CREATE &&
33
- (event.includeContent || (event.filters.contentPrefix ?? '') !== '');
34
-
35
- /** The intents one trigger needs. */
36
- const intentListOf = (event: IntentSource): DiscordGatewayIntent[] => {
37
- const intents = [DISCORD_EVENT_BASE_INTENT[event.eventType]];
38
- if (needsMessageContent(event)) {
39
- intents.push(DiscordGatewayIntent.MESSAGE_CONTENT);
40
- }
41
- return intents;
42
- };
43
-
44
- /** The `intents` of IDENTIFY for the triggers of a version: the union of their bits, 0 without a trigger. */
45
- export const intentsOf = (events: readonly IntentSource[]): number =>
46
- events.reduce(
47
- (bits, event) => intentListOf(event).reduce((acc, intent) => acc | intent, bits),
48
- 0,
49
- );
50
-
51
- /** The privileged intents the triggers need, in bit order, without duplicates. */
52
- export const privilegedIntentsOf = (events: readonly IntentSource[]): DiscordGatewayIntent[] => {
53
- const bits = intentsOf(events);
54
- return PRIVILEGED_DISCORD_GATEWAY_INTENTS.filter((intent) => (bits & intent) !== 0);
55
- };
1
+ import { DiscordEventType } from '../types/enum';
2
+ import type { IReflexDiscordEvent } from '../types/interface/models';
3
+
4
+ /** The Gateway intents the Discord events need, as Discord numbers their bits. */
5
+ export enum DiscordGatewayIntent {
6
+ /** Member arrival and departure. Privileged. */
7
+ GUILD_MEMBERS = 1 << 1,
8
+ GUILD_MESSAGES = 1 << 9,
9
+ GUILD_MESSAGE_REACTIONS = 1 << 10,
10
+ /** The content of a message. Privileged. */
11
+ MESSAGE_CONTENT = 1 << 15,
12
+ }
13
+
14
+ /** The intents Discord asks the bot owner to enable in the portal (and to have verified past 100 servers). */
15
+ export const PRIVILEGED_DISCORD_GATEWAY_INTENTS: readonly DiscordGatewayIntent[] = [
16
+ DiscordGatewayIntent.GUILD_MEMBERS,
17
+ DiscordGatewayIntent.MESSAGE_CONTENT,
18
+ ];
19
+
20
+ /** The intent each event type needs by itself. `GUILDS` is never asked: the four events do not need its large packets. */
21
+ export const DISCORD_EVENT_BASE_INTENT: Record<DiscordEventType, DiscordGatewayIntent> = {
22
+ [DiscordEventType.GUILD_MEMBER_ADD]: DiscordGatewayIntent.GUILD_MEMBERS,
23
+ [DiscordEventType.GUILD_MEMBER_REMOVE]: DiscordGatewayIntent.GUILD_MEMBERS,
24
+ [DiscordEventType.MESSAGE_CREATE]: DiscordGatewayIntent.GUILD_MESSAGES,
25
+ [DiscordEventType.MESSAGE_REACTION_ADD]: DiscordGatewayIntent.GUILD_MESSAGE_REACTIONS,
26
+ };
27
+
28
+ type IntentSource = Pick<IReflexDiscordEvent, 'eventType' | 'filters' | 'includeContent'>;
29
+
30
+ /** True when the trigger reads the content of a message: it asks for it, or filters on a prefix. */
31
+ const needsMessageContent = (event: IntentSource): boolean =>
32
+ event.eventType === DiscordEventType.MESSAGE_CREATE &&
33
+ (event.includeContent || (event.filters.contentPrefix ?? '') !== '');
34
+
35
+ /** The intents one trigger needs. */
36
+ const intentListOf = (event: IntentSource): DiscordGatewayIntent[] => {
37
+ const intents = [DISCORD_EVENT_BASE_INTENT[event.eventType]];
38
+ if (needsMessageContent(event)) {
39
+ intents.push(DiscordGatewayIntent.MESSAGE_CONTENT);
40
+ }
41
+ return intents;
42
+ };
43
+
44
+ /** The `intents` of IDENTIFY for the triggers of a version: the union of their bits, 0 without a trigger. */
45
+ export const intentsOf = (events: readonly IntentSource[]): number =>
46
+ events.reduce(
47
+ (bits, event) => intentListOf(event).reduce((acc, intent) => acc | intent, bits),
48
+ 0,
49
+ );
50
+
51
+ /** The privileged intents the triggers need, in bit order, without duplicates. */
52
+ export const privilegedIntentsOf = (events: readonly IntentSource[]): DiscordGatewayIntent[] => {
53
+ const bits = intentsOf(events);
54
+ return PRIVILEGED_DISCORD_GATEWAY_INTENTS.filter((intent) => (bits & intent) !== 0);
55
+ };
@@ -1,3 +1,3 @@
1
- export * from './discord-event-intents';
2
- export * from './discord-event-filters';
3
- export * from './discord-event-filter-cases';
1
+ export * from './discord-event-intents';
2
+ export * from './discord-event-filters';
3
+ export * from './discord-event-filter-cases';
package/src/index.test.ts CHANGED
@@ -1,87 +1,87 @@
1
- import { AxiosAdapter, AxiosError, InternalAxiosRequestConfig } from 'axios';
2
- import {
3
- AbyssReflexCore,
4
- HealthStatus,
5
- IHealthResponse,
6
- intentsOf,
7
- matchesEventFilters,
8
- privilegedIntentsOf,
9
- } from '.';
10
-
11
- const answer =
12
- (status: number, data: IHealthResponse): AxiosAdapter =>
13
- async (config: InternalAxiosRequestConfig) => {
14
- const response = { data, status, statusText: '', headers: {}, config };
15
- if (status >= 400) {
16
- throw new AxiosError('failed', 'ERR_BAD_RESPONSE', config, null, response);
17
- }
18
- return response;
19
- };
20
-
21
- describe('AbyssReflexCore', () => {
22
- it('points at the production API until setConfig says otherwise', () => {
23
- expect(AbyssReflexCore.getConfig().baseURL).toBe('https://reflex-api.abyss-project.fr/');
24
- });
25
-
26
- it('setConfig changes the base URL of the axios instance', () => {
27
- AbyssReflexCore.setConfig({ baseURL: 'http://localhost:10141/' });
28
- expect(AbyssReflexCore.axios.defaults.baseURL).toBe('http://localhost:10141/');
29
- expect(AbyssReflexCore.getConfig().baseURL).toBe('http://localhost:10141/');
30
- });
31
-
32
- it('health() calls GET /health on the configured URL and returns the typed body', async () => {
33
- const adapter = jest.fn(answer(200, { database: HealthStatus.UP, redis: HealthStatus.UP }));
34
- AbyssReflexCore.setConfig({ baseURL: 'http://reflex.test/' });
35
- AbyssReflexCore.axios.defaults.adapter = adapter;
36
-
37
- const body = await AbyssReflexCore.sdk.health();
38
-
39
- expect(body).toEqual({ database: 'up', redis: 'up' });
40
- const config = adapter.mock.calls[0][0];
41
- expect(config.method).toBe('get');
42
- expect(config.url).toBe('health');
43
- expect(config.baseURL).toBe('http://reflex.test/');
44
- });
45
-
46
- it('health() rejects on a 503 and the error carries the component that is down', async () => {
47
- AbyssReflexCore.axios.defaults.adapter = answer(503, {
48
- database: HealthStatus.DOWN,
49
- redis: HealthStatus.UP,
50
- });
51
-
52
- const error = await AbyssReflexCore.sdk.health().catch((e: AxiosError<IHealthResponse>) => e);
53
-
54
- expect((error as AxiosError<IHealthResponse>).response?.status).toBe(503);
55
- expect((error as AxiosError<IHealthResponse>).response?.data.database).toBe(HealthStatus.DOWN);
56
- });
57
- });
58
-
59
- describe('AbyssReflexCore facade of the Discord events', () => {
60
- it('exposes the pure event helpers of the SDK', () => {
61
- expect(intentsOf([])).toBe(0);
62
- expect(typeof matchesEventFilters).toBe('function');
63
- expect(typeof privilegedIntentsOf).toBe('function');
64
- });
65
-
66
- it('exposes the event, insights and bot limit routes under short names', () => {
67
- const { service, function: fn } = AbyssReflexCore.sdk;
68
- expect(Object.keys(service.discord.event)).toEqual(['list', 'create', 'update', 'delete']);
69
- expect(Object.keys(service.discord.eventLog)).toEqual(['list']);
70
- expect(Object.keys(service.insights)).toEqual(['get', 'events']);
71
- expect(Object.keys(fn.insights)).toEqual(['get']);
72
- expect(service.discord.connection.setAllocation).toBeInstanceOf(Function);
73
- expect(service.discord.connection.setBotSettings).toBeInstanceOf(Function);
74
- });
75
- });
76
-
77
- describe('AbyssReflexCore facade of the run understanding routes', () => {
78
- it('exposes the application, route statistics and log cap routes under short names', () => {
79
- const { application, service, function: fn } = AbyssReflexCore.sdk;
80
- expect(Object.keys(application)).toEqual(['runSettings', 'insights', 'alertRule']);
81
- expect(Object.keys(application.runSettings)).toEqual(['get', 'set']);
82
- expect(Object.keys(application.insights)).toEqual(['get', 'calendar']);
83
- expect(Object.keys(application.alertRule)).toEqual(['list', 'create', 'update', 'delete']);
84
- expect(Object.keys(service.routeStats)).toEqual(['list', 'insights']);
85
- expect(Object.keys(fn.logCap)).toEqual(['set']);
86
- });
87
- });
1
+ import { AxiosAdapter, AxiosError, InternalAxiosRequestConfig } from 'axios';
2
+ import {
3
+ AbyssReflexCore,
4
+ HealthStatus,
5
+ IHealthResponse,
6
+ intentsOf,
7
+ matchesEventFilters,
8
+ privilegedIntentsOf,
9
+ } from '.';
10
+
11
+ const answer =
12
+ (status: number, data: IHealthResponse): AxiosAdapter =>
13
+ async (config: InternalAxiosRequestConfig) => {
14
+ const response = { data, status, statusText: '', headers: {}, config };
15
+ if (status >= 400) {
16
+ throw new AxiosError('failed', 'ERR_BAD_RESPONSE', config, null, response);
17
+ }
18
+ return response;
19
+ };
20
+
21
+ describe('AbyssReflexCore', () => {
22
+ it('points at the production API until setConfig says otherwise', () => {
23
+ expect(AbyssReflexCore.getConfig().baseURL).toBe('https://reflex-api.abyss-project.fr/');
24
+ });
25
+
26
+ it('setConfig changes the base URL of the axios instance', () => {
27
+ AbyssReflexCore.setConfig({ baseURL: 'http://localhost:10141/' });
28
+ expect(AbyssReflexCore.axios.defaults.baseURL).toBe('http://localhost:10141/');
29
+ expect(AbyssReflexCore.getConfig().baseURL).toBe('http://localhost:10141/');
30
+ });
31
+
32
+ it('health() calls GET /health on the configured URL and returns the typed body', async () => {
33
+ const adapter = jest.fn(answer(200, { database: HealthStatus.UP, redis: HealthStatus.UP }));
34
+ AbyssReflexCore.setConfig({ baseURL: 'http://reflex.test/' });
35
+ AbyssReflexCore.axios.defaults.adapter = adapter;
36
+
37
+ const body = await AbyssReflexCore.sdk.health();
38
+
39
+ expect(body).toEqual({ database: 'up', redis: 'up' });
40
+ const config = adapter.mock.calls[0][0];
41
+ expect(config.method).toBe('get');
42
+ expect(config.url).toBe('health');
43
+ expect(config.baseURL).toBe('http://reflex.test/');
44
+ });
45
+
46
+ it('health() rejects on a 503 and the error carries the component that is down', async () => {
47
+ AbyssReflexCore.axios.defaults.adapter = answer(503, {
48
+ database: HealthStatus.DOWN,
49
+ redis: HealthStatus.UP,
50
+ });
51
+
52
+ const error = await AbyssReflexCore.sdk.health().catch((e: AxiosError<IHealthResponse>) => e);
53
+
54
+ expect((error as AxiosError<IHealthResponse>).response?.status).toBe(503);
55
+ expect((error as AxiosError<IHealthResponse>).response?.data.database).toBe(HealthStatus.DOWN);
56
+ });
57
+ });
58
+
59
+ describe('AbyssReflexCore facade of the Discord events', () => {
60
+ it('exposes the pure event helpers of the SDK', () => {
61
+ expect(intentsOf([])).toBe(0);
62
+ expect(typeof matchesEventFilters).toBe('function');
63
+ expect(typeof privilegedIntentsOf).toBe('function');
64
+ });
65
+
66
+ it('exposes the event, insights and bot limit routes under short names', () => {
67
+ const { service, function: fn } = AbyssReflexCore.sdk;
68
+ expect(Object.keys(service.discord.event)).toEqual(['list', 'create', 'update', 'delete']);
69
+ expect(Object.keys(service.discord.eventLog)).toEqual(['list']);
70
+ expect(Object.keys(service.insights)).toEqual(['get', 'events']);
71
+ expect(Object.keys(fn.insights)).toEqual(['get']);
72
+ expect(service.discord.connection.setAllocation).toBeInstanceOf(Function);
73
+ expect(service.discord.connection.setBotSettings).toBeInstanceOf(Function);
74
+ });
75
+ });
76
+
77
+ describe('AbyssReflexCore facade of the run understanding routes', () => {
78
+ it('exposes the application, route statistics and log cap routes under short names', () => {
79
+ const { application, service, function: fn } = AbyssReflexCore.sdk;
80
+ expect(Object.keys(application)).toEqual(['runSettings', 'insights', 'alertRule']);
81
+ expect(Object.keys(application.runSettings)).toEqual(['get', 'set']);
82
+ expect(Object.keys(application.insights)).toEqual(['get', 'calendar']);
83
+ expect(Object.keys(application.alertRule)).toEqual(['list', 'create', 'update', 'delete']);
84
+ expect(Object.keys(service.routeStats)).toEqual(['list', 'insights']);
85
+ expect(Object.keys(fn.logCap)).toEqual(['set']);
86
+ });
87
+ });
@@ -1 +1 @@
1
- export * from './percentiles';
1
+ export * from './percentiles';
@@ -1,75 +1,75 @@
1
- import { DURATION_HISTOGRAM_BOUNDS_MS } from '../types';
2
- import { percentileOfHistogram } from './percentiles';
3
-
4
- const emptyHistogram = (): number[] =>
5
- new Array<number>(DURATION_HISTOGRAM_BOUNDS_MS.length + 1).fill(0);
6
-
7
- describe('percentileOfHistogram', () => {
8
- it('reads p50, p95 and p99 at the upper bound of the bucket holding them', () => {
9
- const histogram = emptyHistogram();
10
- histogram[2] = 50; // <= 50 ms
11
- histogram[3] = 40; // <= 100 ms
12
- histogram[6] = 9; // <= 500 ms
13
- histogram[10] = 1; // <= 2500 ms
14
-
15
- expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.5)).toEqual({
16
- upperBoundMs: 50,
17
- open: false,
18
- });
19
- expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.95)).toEqual({
20
- upperBoundMs: 500,
21
- open: false,
22
- });
23
- expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.99)).toEqual({
24
- upperBoundMs: 500,
25
- open: false,
26
- });
27
- expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 1)).toEqual({
28
- upperBoundMs: 2500,
29
- open: false,
30
- });
31
- });
32
-
33
- it('answers the last bound with open set when the percentile falls in the open bucket', () => {
34
- const histogram = emptyHistogram();
35
- histogram[0] = 90;
36
- histogram[DURATION_HISTOGRAM_BOUNDS_MS.length] = 10;
37
-
38
- expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.5)).toEqual({
39
- upperBoundMs: 10,
40
- open: false,
41
- });
42
- expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.95)).toEqual({
43
- upperBoundMs: 30_000,
44
- open: true,
45
- });
46
- });
47
-
48
- it('answers null for an empty histogram', () => {
49
- expect(percentileOfHistogram(emptyHistogram(), DURATION_HISTOGRAM_BOUNDS_MS, 0.95)).toBeNull();
50
- });
51
-
52
- it('answers the bucket of a single run for every quantile', () => {
53
- const histogram = emptyHistogram();
54
- histogram[4] = 1;
55
-
56
- expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.5)).toEqual({
57
- upperBoundMs: 200,
58
- open: false,
59
- });
60
- expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.99)).toEqual({
61
- upperBoundMs: 200,
62
- open: false,
63
- });
64
- });
65
-
66
- it('rejects a histogram that does not match the bounds and a quantile out of range', () => {
67
- expect(() => percentileOfHistogram([1, 2], DURATION_HISTOGRAM_BOUNDS_MS, 0.5)).toThrow();
68
- expect(() =>
69
- percentileOfHistogram(emptyHistogram(), DURATION_HISTOGRAM_BOUNDS_MS, 0),
70
- ).toThrow();
71
- expect(() =>
72
- percentileOfHistogram(emptyHistogram(), DURATION_HISTOGRAM_BOUNDS_MS, 1.5),
73
- ).toThrow();
74
- });
75
- });
1
+ import { DURATION_HISTOGRAM_BOUNDS_MS } from '../types';
2
+ import { percentileOfHistogram } from './percentiles';
3
+
4
+ const emptyHistogram = (): number[] =>
5
+ new Array<number>(DURATION_HISTOGRAM_BOUNDS_MS.length + 1).fill(0);
6
+
7
+ describe('percentileOfHistogram', () => {
8
+ it('reads p50, p95 and p99 at the upper bound of the bucket holding them', () => {
9
+ const histogram = emptyHistogram();
10
+ histogram[2] = 50; // <= 50 ms
11
+ histogram[3] = 40; // <= 100 ms
12
+ histogram[6] = 9; // <= 500 ms
13
+ histogram[10] = 1; // <= 2500 ms
14
+
15
+ expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.5)).toEqual({
16
+ upperBoundMs: 50,
17
+ open: false,
18
+ });
19
+ expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.95)).toEqual({
20
+ upperBoundMs: 500,
21
+ open: false,
22
+ });
23
+ expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.99)).toEqual({
24
+ upperBoundMs: 500,
25
+ open: false,
26
+ });
27
+ expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 1)).toEqual({
28
+ upperBoundMs: 2500,
29
+ open: false,
30
+ });
31
+ });
32
+
33
+ it('answers the last bound with open set when the percentile falls in the open bucket', () => {
34
+ const histogram = emptyHistogram();
35
+ histogram[0] = 90;
36
+ histogram[DURATION_HISTOGRAM_BOUNDS_MS.length] = 10;
37
+
38
+ expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.5)).toEqual({
39
+ upperBoundMs: 10,
40
+ open: false,
41
+ });
42
+ expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.95)).toEqual({
43
+ upperBoundMs: 30_000,
44
+ open: true,
45
+ });
46
+ });
47
+
48
+ it('answers null for an empty histogram', () => {
49
+ expect(percentileOfHistogram(emptyHistogram(), DURATION_HISTOGRAM_BOUNDS_MS, 0.95)).toBeNull();
50
+ });
51
+
52
+ it('answers the bucket of a single run for every quantile', () => {
53
+ const histogram = emptyHistogram();
54
+ histogram[4] = 1;
55
+
56
+ expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.5)).toEqual({
57
+ upperBoundMs: 200,
58
+ open: false,
59
+ });
60
+ expect(percentileOfHistogram(histogram, DURATION_HISTOGRAM_BOUNDS_MS, 0.99)).toEqual({
61
+ upperBoundMs: 200,
62
+ open: false,
63
+ });
64
+ });
65
+
66
+ it('rejects a histogram that does not match the bounds and a quantile out of range', () => {
67
+ expect(() => percentileOfHistogram([1, 2], DURATION_HISTOGRAM_BOUNDS_MS, 0.5)).toThrow();
68
+ expect(() =>
69
+ percentileOfHistogram(emptyHistogram(), DURATION_HISTOGRAM_BOUNDS_MS, 0),
70
+ ).toThrow();
71
+ expect(() =>
72
+ percentileOfHistogram(emptyHistogram(), DURATION_HISTOGRAM_BOUNDS_MS, 1.5),
73
+ ).toThrow();
74
+ });
75
+ });
@@ -1,37 +1,37 @@
1
- import type { IReflexDurationPercentile } from '../types';
2
-
3
- /**
4
- * Reads a percentile on a duration histogram: the upper bound of the bucket holding the `q` quantile (the
5
- * smallest bucket whose cumulative count reaches `ceil(q * total)`). The histogram has one bucket per bound plus
6
- * an open one; a percentile in the open bucket answers the last bound with `open` true. Null when the histogram
7
- * holds no run: the denominator is its own sum, never the number of runs.
8
- */
9
- export const percentileOfHistogram = (
10
- histogram: readonly number[],
11
- bounds: readonly number[],
12
- q: number,
13
- ): IReflexDurationPercentile | null => {
14
- if (bounds.length === 0 || histogram.length !== bounds.length + 1) {
15
- throw new Error(
16
- `A histogram needs one bucket per bound plus an open one: got ${histogram.length} buckets for ${bounds.length} bounds`,
17
- );
18
- }
19
- if (!(q > 0 && q <= 1)) {
20
- throw new Error(`A quantile is in ]0, 1]: got ${q}`);
21
- }
22
- const total = histogram.reduce((sum, count) => sum + count, 0);
23
- if (total === 0) {
24
- return null;
25
- }
26
- const rank = Math.max(1, Math.ceil(q * total));
27
- let cumulative = 0;
28
- for (let index = 0; index < histogram.length; index += 1) {
29
- cumulative += histogram[index];
30
- if (cumulative >= rank) {
31
- return index < bounds.length
32
- ? { upperBoundMs: bounds[index], open: false }
33
- : { upperBoundMs: bounds[bounds.length - 1], open: true };
34
- }
35
- }
36
- return { upperBoundMs: bounds[bounds.length - 1], open: true };
37
- };
1
+ import type { IReflexDurationPercentile } from '../types';
2
+
3
+ /**
4
+ * Reads a percentile on a duration histogram: the upper bound of the bucket holding the `q` quantile (the
5
+ * smallest bucket whose cumulative count reaches `ceil(q * total)`). The histogram has one bucket per bound plus
6
+ * an open one; a percentile in the open bucket answers the last bound with `open` true. Null when the histogram
7
+ * holds no run: the denominator is its own sum, never the number of runs.
8
+ */
9
+ export const percentileOfHistogram = (
10
+ histogram: readonly number[],
11
+ bounds: readonly number[],
12
+ q: number,
13
+ ): IReflexDurationPercentile | null => {
14
+ if (bounds.length === 0 || histogram.length !== bounds.length + 1) {
15
+ throw new Error(
16
+ `A histogram needs one bucket per bound plus an open one: got ${histogram.length} buckets for ${bounds.length} bounds`,
17
+ );
18
+ }
19
+ if (!(q > 0 && q <= 1)) {
20
+ throw new Error(`A quantile is in ]0, 1]: got ${q}`);
21
+ }
22
+ const total = histogram.reduce((sum, count) => sum + count, 0);
23
+ if (total === 0) {
24
+ return null;
25
+ }
26
+ const rank = Math.max(1, Math.ceil(q * total));
27
+ let cumulative = 0;
28
+ for (let index = 0; index < histogram.length; index += 1) {
29
+ cumulative += histogram[index];
30
+ if (cumulative >= rank) {
31
+ return index < bounds.length
32
+ ? { upperBoundMs: bounds[index], open: false }
33
+ : { upperBoundMs: bounds[bounds.length - 1], open: true };
34
+ }
35
+ }
36
+ return { upperBoundMs: bounds[bounds.length - 1], open: true };
37
+ };
@@ -1,12 +1,12 @@
1
- /**
2
- * State of an access granted to a function.
3
- * - ACTIVE: the grantor still holds the right the access needs.
4
- * - SUSPENDED: a publication found the grantor without that right. Publishing is refused
5
- * (`GRANT_SUSPENDED`) until the access is revoked and granted again; what is already promoted keeps running.
6
- * - REVOKED: withdrawn on purpose; the row stays as the journal.
7
- */
8
- export enum AccessGrantStatus {
9
- ACTIVE = 'ACTIVE',
10
- SUSPENDED = 'SUSPENDED',
11
- REVOKED = 'REVOKED',
12
- }
1
+ /**
2
+ * State of an access granted to a function.
3
+ * - ACTIVE: the grantor still holds the right the access needs.
4
+ * - SUSPENDED: a publication found the grantor without that right. Publishing is refused
5
+ * (`GRANT_SUSPENDED`) until the access is revoked and granted again; what is already promoted keeps running.
6
+ * - REVOKED: withdrawn on purpose; the row stays as the journal.
7
+ */
8
+ export enum AccessGrantStatus {
9
+ ACTIVE = 'ACTIVE',
10
+ SUSPENDED = 'SUSPENDED',
11
+ REVOKED = 'REVOKED',
12
+ }
@@ -1,7 +1,7 @@
1
- /** The kinds of access a definition declares and a member grants (the format of @abyss-project/runtime). */
2
- export enum ReflexAccessKind {
3
- /** Outbound HTTP to a list of host names (exact, or `*.domain` for any sub-domain). */
4
- HTTP = 'HTTP',
5
- /** One action of the host catalog (`store.get`, `store.set`, `store.increment`, `secret.get`, `http.respond`). */
6
- HOST_ACTION = 'HOST_ACTION',
7
- }
1
+ /** The kinds of access a definition declares and a member grants (the format of @abyss-project/runtime). */
2
+ export enum ReflexAccessKind {
3
+ /** Outbound HTTP to a list of host names (exact, or `*.domain` for any sub-domain). */
4
+ HTTP = 'HTTP',
5
+ /** One action of the host catalog (`store.get`, `store.set`, `store.increment`, `secret.get`, `http.respond`). */
6
+ HOST_ACTION = 'HOST_ACTION',
7
+ }
@@ -1,7 +1,7 @@
1
- /** Why the events of a bot are rejected at the Gateway. */
2
- export enum BotEventBlockReason {
3
- /** The monthly spend cap of the bot is reached. */
4
- MONTHLY_CAP = 'MONTHLY_CAP',
5
- /** The credits of the organization are spent and the bot has no overage. */
6
- CREDITS_EXHAUSTED = 'CREDITS_EXHAUSTED',
7
- }
1
+ /** Why the events of a bot are rejected at the Gateway. */
2
+ export enum BotEventBlockReason {
3
+ /** The monthly spend cap of the bot is reached. */
4
+ MONTHLY_CAP = 'MONTHLY_CAP',
5
+ /** The credits of the organization are spent and the bot has no overage. */
6
+ CREDITS_EXHAUSTED = 'CREDITS_EXHAUSTED',
7
+ }
@@ -1,5 +1,5 @@
1
- /** How a function reads a vault key: `config.NAME` (a VISIBLE entry) or `secrets.NAME` (a SECRET entry). */
2
- export enum ConfigurationReadAs {
3
- CONFIG = 'config',
4
- SECRETS = 'secrets',
5
- }
1
+ /** How a function reads a vault key: `config.NAME` (a VISIBLE entry) or `secrets.NAME` (a SECRET entry). */
2
+ export enum ConfigurationReadAs {
3
+ CONFIG = 'config',
4
+ SECRETS = 'secrets',
5
+ }
@@ -1,9 +1,9 @@
1
- /** What the activity of a Discord bot says it does ("Playing", "Listening to", "Watching", "Competing in", or a free text). */
2
- export enum DiscordActivityType {
3
- PLAYING = 'PLAYING',
4
- LISTENING = 'LISTENING',
5
- WATCHING = 'WATCHING',
6
- COMPETING = 'COMPETING',
7
- /** The text alone, shown as it is written. */
8
- CUSTOM = 'CUSTOM',
9
- }
1
+ /** What the activity of a Discord bot says it does ("Playing", "Listening to", "Watching", "Competing in", or a free text). */
2
+ export enum DiscordActivityType {
3
+ PLAYING = 'PLAYING',
4
+ LISTENING = 'LISTENING',
5
+ WATCHING = 'WATCHING',
6
+ COMPETING = 'COMPETING',
7
+ /** The text alone, shown as it is written. */
8
+ CUSTOM = 'CUSTOM',
9
+ }
@@ -1,11 +1,11 @@
1
- /**
2
- * Type of a Discord channel a CHANNEL option may be limited to (Discord's names; the API sends their
3
- * numbers). Same set as the runtime's `DiscordChannelType`, which a parity test keeps equal.
4
- */
5
- export enum DiscordChannelType {
6
- TEXT = 'TEXT',
7
- VOICE = 'VOICE',
8
- CATEGORY = 'CATEGORY',
9
- ANNOUNCEMENT = 'ANNOUNCEMENT',
10
- FORUM = 'FORUM',
11
- }
1
+ /**
2
+ * Type of a Discord channel a CHANNEL option may be limited to (Discord's names; the API sends their
3
+ * numbers). Same set as the runtime's `DiscordChannelType`, which a parity test keeps equal.
4
+ */
5
+ export enum DiscordChannelType {
6
+ TEXT = 'TEXT',
7
+ VOICE = 'VOICE',
8
+ CATEGORY = 'CATEGORY',
9
+ ANNOUNCEMENT = 'ANNOUNCEMENT',
10
+ FORUM = 'FORUM',
11
+ }