@antelopejs/dms-api 0.1.6 → 0.2.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.
- package/README.md +99 -8
- package/dist/automation/get-api-health.js +4 -3
- package/dist/automation/get-api-health.js.map +1 -1
- package/dist/automation/route-unhealthy.js +2 -2
- package/dist/automation/route-unhealthy.js.map +1 -1
- package/dist/automation/scope.d.ts +0 -1
- package/dist/automation/scope.js +0 -7
- package/dist/automation/scope.js.map +1 -1
- package/dist/config.d.ts +4 -0
- package/dist/config.js +15 -9
- package/dist/config.js.map +1 -1
- package/dist/db/models/request_log.model.d.ts +37 -2
- package/dist/db/models/request_log.model.js +130 -35
- package/dist/db/models/request_log.model.js.map +1 -1
- package/dist/db/models/request_log.search.d.ts +5 -0
- package/dist/db/models/request_log.search.js +35 -0
- package/dist/db/models/request_log.search.js.map +1 -0
- package/dist/db/models/request_log.status.d.ts +5 -0
- package/dist/db/models/request_log.status.js +9 -0
- package/dist/db/models/request_log.status.js.map +1 -0
- package/dist/db/models/routes.model.d.ts +1 -0
- package/dist/db/models/routes.model.js +5 -1
- package/dist/db/models/routes.model.js.map +1 -1
- package/dist/db/models/routes_statistics.model.js +1 -1
- package/dist/db/models/routes_statistics.model.js.map +1 -1
- package/dist/db/tables/api_settings.table.d.ts +1 -0
- package/dist/db/tables/api_settings.table.js +4 -0
- package/dist/db/tables/api_settings.table.js.map +1 -1
- package/dist/db/tables/request_log.table.d.ts +18 -3
- package/dist/db/tables/request_log.table.js +34 -1
- package/dist/db/tables/request_log.table.js.map +1 -1
- package/dist/db/tables/routes.table.d.ts +1 -2
- package/dist/db/tables/routes.table.js.map +1 -1
- package/dist/db/tables/routes_statistics.table.d.ts +4 -3
- package/dist/db/tables/routes_statistics.table.js +5 -1
- package/dist/db/tables/routes_statistics.table.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/memo.d.ts +5 -0
- package/dist/memo.js +36 -0
- package/dist/memo.js.map +1 -0
- package/dist/pages/blocks.d.ts +3 -0
- package/dist/pages/blocks.js +31 -0
- package/dist/pages/blocks.js.map +1 -0
- package/dist/pages/displays.d.ts +7 -0
- package/dist/pages/displays.js +23 -0
- package/dist/pages/displays.js.map +1 -0
- package/dist/pages/index.d.ts +5 -2
- package/dist/pages/index.js +5 -2
- package/dist/pages/index.js.map +1 -1
- package/dist/pages/logs.d.ts +18 -0
- package/dist/pages/logs.js +112 -0
- package/dist/pages/logs.js.map +1 -0
- package/dist/pages/module.d.ts +3 -0
- package/dist/pages/module.js +102 -6
- package/dist/pages/module.js.map +1 -1
- package/dist/pages/overview.d.ts +14 -0
- package/dist/pages/overview.js +215 -0
- package/dist/pages/overview.js.map +1 -0
- package/dist/pages/period.d.ts +9 -0
- package/dist/pages/period.js +24 -0
- package/dist/pages/period.js.map +1 -0
- package/dist/pages/request-table.d.ts +11 -0
- package/dist/pages/request-table.js +187 -0
- package/dist/pages/request-table.js.map +1 -0
- package/dist/pages/routes.d.ts +15 -0
- package/dist/pages/routes.js +269 -0
- package/dist/pages/routes.js.map +1 -0
- package/dist/pages/scope-chip.d.ts +9 -0
- package/dist/pages/scope-chip.js +28 -0
- package/dist/pages/scope-chip.js.map +1 -0
- package/dist/pages/{logs/page.d.ts → settings.d.ts} +3 -3
- package/dist/pages/settings.js +218 -0
- package/dist/pages/settings.js.map +1 -0
- package/dist/pages/toolbar.d.ts +9 -0
- package/dist/pages/toolbar.js +33 -0
- package/dist/pages/toolbar.js.map +1 -0
- package/dist/realtime.d.ts +2 -0
- package/dist/realtime.js +21 -0
- package/dist/realtime.js.map +1 -0
- package/dist/routes/index.d.ts +2 -2
- package/dist/routes/index.js +2 -2
- package/dist/routes/index.js.map +1 -1
- package/dist/routes/logs.d.ts +173 -2
- package/dist/routes/logs.js +177 -86
- package/dist/routes/logs.js.map +1 -1
- package/dist/routes/overview.d.ts +27 -0
- package/dist/routes/overview.js +131 -0
- package/dist/routes/overview.js.map +1 -0
- package/dist/routes/routes.d.ts +92 -28
- package/dist/routes/routes.js +165 -97
- package/dist/routes/routes.js.map +1 -1
- package/dist/routes/settings.d.ts +16 -11
- package/dist/routes/settings.js +62 -54
- package/dist/routes/settings.js.map +1 -1
- package/dist/routes/tester.d.ts +7 -0
- package/dist/routes/tester.js +34 -0
- package/dist/routes/tester.js.map +1 -0
- package/dist/services/activity.d.ts +49 -0
- package/dist/services/activity.js +207 -0
- package/dist/services/activity.js.map +1 -0
- package/dist/services/aggregate-cache.d.ts +3 -0
- package/dist/services/aggregate-cache.js +16 -0
- package/dist/services/aggregate-cache.js.map +1 -0
- package/dist/services/api-origin.d.ts +1 -0
- package/dist/services/api-origin.js +9 -0
- package/dist/services/api-origin.js.map +1 -0
- package/dist/services/attention.d.ts +45 -0
- package/dist/services/attention.js +203 -0
- package/dist/services/attention.js.map +1 -0
- package/dist/services/composed-text.d.ts +6 -0
- package/dist/services/composed-text.js +23 -0
- package/dist/services/composed-text.js.map +1 -0
- package/dist/services/health-rules.d.ts +49 -0
- package/dist/services/health-rules.js +233 -0
- package/dist/services/health-rules.js.map +1 -0
- package/dist/services/index.d.ts +1 -3
- package/dist/services/index.js +3 -3
- package/dist/services/index.js.map +1 -1
- package/dist/services/introspection/decorators.d.ts +14 -0
- package/dist/services/introspection/decorators.js +192 -0
- package/dist/services/introspection/decorators.js.map +1 -0
- package/dist/services/introspection/errors.js +4 -4
- package/dist/services/introspection/errors.js.map +1 -1
- package/dist/services/introspection/index.d.ts +6 -8
- package/dist/services/introspection/index.js +96 -32
- package/dist/services/introspection/index.js.map +1 -1
- package/dist/services/introspection/probe.d.ts +2 -1
- package/dist/services/introspection/probe.js +11 -0
- package/dist/services/introspection/probe.js.map +1 -1
- package/dist/services/links.d.ts +25 -0
- package/dist/services/links.js +72 -0
- package/dist/services/links.js.map +1 -0
- package/dist/services/live-feed.d.ts +6 -0
- package/dist/services/live-feed.js +18 -0
- package/dist/services/live-feed.js.map +1 -0
- package/dist/services/live.d.ts +16 -0
- package/dist/services/live.js +32 -0
- package/dist/services/live.js.map +1 -0
- package/dist/services/overview.d.ts +122 -0
- package/dist/services/overview.js +324 -0
- package/dist/services/overview.js.map +1 -0
- package/dist/services/period.d.ts +0 -9
- package/dist/services/period.js +5 -52
- package/dist/services/period.js.map +1 -1
- package/dist/services/request-log/access.d.ts +5 -0
- package/dist/services/request-log/access.js +21 -0
- package/dist/services/request-log/access.js.map +1 -0
- package/dist/services/request-log/capture.d.ts +30 -0
- package/dist/services/request-log/capture.js +216 -39
- package/dist/services/request-log/capture.js.map +1 -1
- package/dist/services/request-log/list.d.ts +36 -0
- package/dist/services/request-log/list.js +201 -0
- package/dist/services/request-log/list.js.map +1 -0
- package/dist/services/request-log/read.d.ts +15 -2
- package/dist/services/request-log/read.js +47 -4
- package/dist/services/request-log/read.js.map +1 -1
- package/dist/services/request-log/redaction.d.ts +16 -3
- package/dist/services/request-log/redaction.js +149 -27
- package/dist/services/request-log/redaction.js.map +1 -1
- package/dist/services/request-log/util.d.ts +10 -3
- package/dist/services/request-log/util.js +39 -22
- package/dist/services/request-log/util.js.map +1 -1
- package/dist/services/route-catalog.d.ts +62 -0
- package/dist/services/route-catalog.js +163 -0
- package/dist/services/route-catalog.js.map +1 -0
- package/dist/services/route-contract.d.ts +66 -0
- package/dist/services/route-contract.js +266 -0
- package/dist/services/route-contract.js.map +1 -0
- package/dist/services/route-health.d.ts +35 -0
- package/dist/services/route-health.js +179 -0
- package/dist/services/route-health.js.map +1 -0
- package/dist/services/route-statistics.d.ts +68 -0
- package/dist/services/route-statistics.js +277 -0
- package/dist/services/route-statistics.js.map +1 -0
- package/dist/services/routes-sync.d.ts +7 -3
- package/dist/services/routes-sync.js +96 -20
- package/dist/services/routes-sync.js.map +1 -1
- package/dist/services/scope.d.ts +0 -1
- package/dist/services/scope.js +4 -17
- package/dist/services/scope.js.map +1 -1
- package/dist/services/settings-form.d.ts +131 -0
- package/dist/services/settings-form.js +210 -0
- package/dist/services/settings-form.js.map +1 -0
- package/dist/services/settings.d.ts +2 -0
- package/dist/services/settings.js +5 -0
- package/dist/services/settings.js.map +1 -1
- package/dist/services/statistics-aggregate.d.ts +6 -41
- package/dist/services/statistics-aggregate.js +55 -280
- package/dist/services/statistics-aggregate.js.map +1 -1
- package/dist/services/statistics-utils.d.ts +7 -12
- package/dist/services/statistics-utils.js +36 -51
- package/dist/services/statistics-utils.js.map +1 -1
- package/dist/services/statistics-write.js +64 -22
- package/dist/services/statistics-write.js.map +1 -1
- package/dist/services/tester-source.d.ts +11 -0
- package/dist/services/tester-source.js +48 -0
- package/dist/services/tester-source.js.map +1 -0
- package/dist/services/traffic-samples.d.ts +18 -0
- package/dist/services/traffic-samples.js +134 -0
- package/dist/services/traffic-samples.js.map +1 -0
- package/dist/services/traffic.d.ts +43 -0
- package/dist/services/traffic.js +270 -0
- package/dist/services/traffic.js.map +1 -0
- package/dist/services/window.d.ts +48 -0
- package/dist/services/window.js +179 -0
- package/dist/services/window.js.map +1 -0
- package/dist/types/constants.d.ts +11 -20
- package/dist/types/constants.js +15 -25
- package/dist/types/constants.js.map +1 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.js +0 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/statistics.d.ts +5 -3
- package/dist/types/statistics.js +5 -0
- package/dist/types/statistics.js.map +1 -1
- package/frontend-vue/app/components/blocks/BetaBadge.vue +20 -0
- package/frontend-vue/app/components/blocks/HealthHero.vue +708 -0
- package/frontend-vue/app/components/blocks/LiveTraffic.vue +356 -0
- package/frontend-vue/app/components/blocks/LogsEmptyState.vue +228 -0
- package/frontend-vue/app/components/blocks/LogsFilters.vue +279 -0
- package/frontend-vue/app/components/blocks/RequestDetail.vue +722 -0
- package/frontend-vue/app/components/blocks/RetentionInput.vue +227 -0
- package/frontend-vue/app/components/blocks/RouteDocumentation.vue +611 -0
- package/frontend-vue/app/components/blocks/RouteHeader.vue +320 -0
- package/frontend-vue/app/components/blocks/RouteTester.vue +1879 -0
- package/frontend-vue/app/components/blocks/RouteTree.vue +676 -0
- package/frontend-vue/app/components/blocks/ScopeChip.vue +89 -0
- package/frontend-vue/app/components/blocks/SettingNumberInput.vue +65 -0
- package/frontend-vue/app/components/blocks/SplitLayout.vue +41 -0
- package/frontend-vue/app/components/internal/ApiBody.vue +129 -0
- package/frontend-vue/app/components/internal/ApiJsonTree.vue +82 -0
- package/frontend-vue/app/components/internal/ApiKeyValues.vue +41 -0
- package/frontend-vue/app/components/internal/ApiMethodBadge.vue +19 -19
- package/frontend-vue/app/components/internal/ApiPath.vue +33 -0
- package/frontend-vue/app/components/internal/ApiStatusBadge.vue +20 -15
- package/frontend-vue/app/components/internal/ApiTesterBadge.vue +17 -0
- package/frontend-vue/app/composables/useApiOrigin.ts +19 -0
- package/frontend-vue/app/composables/useApiPeriod.ts +19 -0
- package/frontend-vue/app/composables/useApiResource.ts +92 -0
- package/frontend-vue/app/composables/useDateFormat.ts +23 -0
- package/frontend-vue/app/composables/useLiveMissedErrors.ts +33 -0
- package/frontend-vue/app/composables/useRouteDetail.ts +253 -0
- package/frontend-vue/app/composables/useRouteSelection.ts +43 -0
- package/frontend-vue/app/config/shortcuts-registry.ts +30 -0
- package/frontend-vue/app/plugins/displays.ts +92 -0
- package/frontend-vue/app/utils/format.ts +281 -0
- package/frontend-vue/app/utils/http.ts +127 -0
- package/frontend-vue/app/utils/logsQuery.ts +261 -0
- package/frontend-vue/app/utils/minuteHistogram.ts +116 -0
- package/frontend-vue/app/utils/requestDrawer.ts +257 -0
- package/frontend-vue/app/utils/routeExplorer.ts +206 -0
- package/frontend-vue/app/utils/search.ts +45 -0
- package/frontend-vue/app/utils/settingsForm.ts +119 -0
- package/frontend-vue/app/utils/snippets.ts +501 -0
- package/frontend-vue/app/utils/testerHistory.ts +354 -0
- package/frontend-vue/app/utils/testerRequest.ts +632 -11
- package/frontend-vue/dms.frontend.build.ts +7 -0
- package/frontend-vue/dms.frontend.ts +10 -3
- package/frontend-vue/i18n/locales/api-en-GB.json +866 -0
- package/frontend-vue/i18n/locales/api-fr-FR.json +866 -0
- package/frontend-vue/package.json +4 -5
- package/frontend-vue/pnpm-lock.yaml +97 -3375
- package/frontend-vue/tests/format.test.ts +180 -0
- package/frontend-vue/tests/http.test.ts +61 -0
- package/frontend-vue/tests/logsQuery.test.ts +255 -0
- package/frontend-vue/tests/minuteHistogram.test.ts +94 -0
- package/frontend-vue/tests/registration.test.ts +24 -7
- package/frontend-vue/tests/requestDrawer.test.ts +179 -0
- package/frontend-vue/tests/routeExplorer.test.ts +206 -0
- package/frontend-vue/tests/settingsForm.test.ts +155 -0
- package/frontend-vue/tests/snippets.test.ts +285 -0
- package/frontend-vue/tests/testerHistory.test.ts +295 -0
- package/frontend-vue/tests/testerRequest.test.ts +471 -76
- package/frontend-vue/tests/tooltipTriggers.test.ts +51 -0
- package/package.json +8 -8
- package/dist/middlewares/index.d.ts +0 -1
- package/dist/middlewares/index.js +0 -18
- package/dist/middlewares/index.js.map +0 -1
- package/dist/middlewares/request-log-capture.d.ts +0 -1
- package/dist/middlewares/request-log-capture.js +0 -6
- package/dist/middlewares/request-log-capture.js.map +0 -1
- package/dist/pages/all.d.ts +0 -4
- package/dist/pages/all.js +0 -21
- package/dist/pages/all.js.map +0 -1
- package/dist/pages/logs/page.js +0 -29
- package/dist/pages/logs/page.js.map +0 -1
- package/dist/pages/routes/page.d.ts +0 -9
- package/dist/pages/routes/page.js +0 -29
- package/dist/pages/routes/page.js.map +0 -1
- package/dist/pages/settings/form.d.ts +0 -3
- package/dist/pages/settings/form.js +0 -80
- package/dist/pages/settings/form.js.map +0 -1
- package/dist/pages/settings/page.d.ts +0 -9
- package/dist/pages/settings/page.js +0 -26
- package/dist/pages/settings/page.js.map +0 -1
- package/dist/pages/summary/page.d.ts +0 -9
- package/dist/pages/summary/page.js +0 -29
- package/dist/pages/summary/page.js.map +0 -1
- package/dist/routes/monitoring.d.ts +0 -17
- package/dist/routes/monitoring.js +0 -101
- package/dist/routes/monitoring.js.map +0 -1
- package/dist/routes/summary.d.ts +0 -32
- package/dist/routes/summary.js +0 -181
- package/dist/routes/summary.js.map +0 -1
- package/dist/services/request-log/index.d.ts +0 -3
- package/dist/services/request-log/index.js +0 -12
- package/dist/services/request-log/index.js.map +0 -1
- package/dist/services/statistics-read.d.ts +0 -4
- package/dist/services/statistics-read.js +0 -48
- package/dist/services/statistics-read.js.map +0 -1
- package/dist/services/tree.d.ts +0 -10
- package/dist/services/tree.js +0 -128
- package/dist/services/tree.js.map +0 -1
- package/dist/types/charts.d.ts +0 -9
- package/dist/types/charts.js +0 -3
- package/dist/types/charts.js.map +0 -1
- package/frontend-vue/app/components/ConfigurationView.vue +0 -187
- package/frontend-vue/app/components/DocumentationView.vue +0 -224
- package/frontend-vue/app/components/LogDetailDrawer.vue +0 -178
- package/frontend-vue/app/components/LogsView.vue +0 -347
- package/frontend-vue/app/components/RouteDetailTabs.vue +0 -179
- package/frontend-vue/app/components/RouteTree.vue +0 -196
- package/frontend-vue/app/components/RoutesView.vue +0 -79
- package/frontend-vue/app/components/StatsView.vue +0 -268
- package/frontend-vue/app/components/SummaryView.vue +0 -657
- package/frontend-vue/app/components/TesterPanel.vue +0 -499
- package/frontend-vue/app/components/internal/PeriodSelect.vue +0 -58
- package/frontend-vue/app/components/internal/RouteTreeNode.vue +0 -168
- package/frontend-vue/app/components/internal/StatusBreakdownBars.vue +0 -162
- package/frontend-vue/app/components/internal/StatusDonut.vue +0 -84
- package/frontend-vue/app/components/internal/UnitAreaChart.vue +0 -73
- package/frontend-vue/app/composables/useApiIntrospection.ts +0 -350
- package/frontend-vue/app/composables/useLogDetailDrawer.ts +0 -50
- package/frontend-vue/app/composables/useSelectedPeriod.ts +0 -107
- package/frontend-vue/app/composables/useStatsPeriods.ts +0 -139
- package/frontend-vue/app/custom-layouts/.gitkeep +0 -0
- package/frontend-vue/app/custom-pages/.gitkeep +0 -0
- package/frontend-vue/app/utils/httpMethod.ts +0 -49
- package/frontend-vue/app/utils/statusClass.ts +0 -49
- package/frontend-vue/i18n/locales/en-GB.json +0 -240
- package/frontend-vue/i18n/locales/fr-FR.json +0 -240
- package/frontend-vue/public/.gitkeep +0 -0
- package/frontend-vue/server/.gitkeep +0 -0
- package/frontend-vue/tests/periods.test.ts +0 -145
- package/frontend-vue/tests/persistence.test.ts +0 -45
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
const MS_PER_SECOND = 1000
|
|
2
|
+
const MS_PER_MINUTE = 60_000
|
|
3
|
+
const MS_PER_DAY = 86_400_000
|
|
4
|
+
const BYTES_PER_KB = 1024
|
|
5
|
+
const SECONDS_DISPLAY_FROM_MS = 10_000
|
|
6
|
+
const PERCENT = 100
|
|
7
|
+
const DECIMAL_DISPLAY_BELOW = 10
|
|
8
|
+
const SECONDS_PER_MINUTE = 60
|
|
9
|
+
const MINUTES_PER_HOUR = 60
|
|
10
|
+
const HOURS_PER_DAY = 24
|
|
11
|
+
const DAYS_PER_WEEK = 7
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Makes the `Intl.DateTimeFormat` of a set of options. The blocks pass the
|
|
15
|
+
* DMS's `useRegionalFormat({ locale: "format" }).dateTimeFormat`
|
|
16
|
+
* (`useDateFormat`), so every date and time follows the user's Language &
|
|
17
|
+
* region preferences: time zone, clock and date format.
|
|
18
|
+
*/
|
|
19
|
+
export type DateFormat = (
|
|
20
|
+
options: Intl.DateTimeFormatOptions,
|
|
21
|
+
) => Intl.DateTimeFormat
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* `make`, each format made once per set of options: a table writes the time
|
|
25
|
+
* of each of its rows (2,000 after a few "Load more"), and making a format
|
|
26
|
+
* costs far more than using one (about 0.15 ms a row). The options are
|
|
27
|
+
* constants, so they key the cache as they are.
|
|
28
|
+
*/
|
|
29
|
+
export function cachedDateFormat(make: DateFormat): DateFormat {
|
|
30
|
+
const formats = new WeakMap<Intl.DateTimeFormatOptions, Intl.DateTimeFormat>()
|
|
31
|
+
return (options) => {
|
|
32
|
+
let format = formats.get(options)
|
|
33
|
+
if (!format) {
|
|
34
|
+
format = make(options)
|
|
35
|
+
formats.set(options, format)
|
|
36
|
+
}
|
|
37
|
+
return format
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const TIME: Intl.DateTimeFormatOptions = { hour: '2-digit', minute: '2-digit' }
|
|
42
|
+
const CLOCK_TIME: Intl.DateTimeFormatOptions = { ...TIME, second: '2-digit' }
|
|
43
|
+
const CLOCK_TIME_MS: Intl.DateTimeFormatOptions = {
|
|
44
|
+
...CLOCK_TIME,
|
|
45
|
+
fractionalSecondDigits: 3,
|
|
46
|
+
}
|
|
47
|
+
const SHORT_DAY: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric' }
|
|
48
|
+
const SHORT_DAY_YEAR: Intl.DateTimeFormatOptions = {
|
|
49
|
+
...SHORT_DAY,
|
|
50
|
+
year: 'numeric',
|
|
51
|
+
}
|
|
52
|
+
const WEEKDAY: Intl.DateTimeFormatOptions = { weekday: 'short' }
|
|
53
|
+
const FULL_DATE_TIME: Intl.DateTimeFormatOptions = {
|
|
54
|
+
dateStyle: 'full',
|
|
55
|
+
timeStyle: 'short',
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const dayPartFormats = new Map<string, Intl.DateTimeFormat>()
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The calendar day a moment falls on in the time zone `format` writes in
|
|
62
|
+
* (the user's), as a day count: two moments share a day when their numbers
|
|
63
|
+
* are equal.
|
|
64
|
+
*/
|
|
65
|
+
function dayNumber(date: Date, format: DateFormat): number {
|
|
66
|
+
const { timeZone } = format(TIME).resolvedOptions()
|
|
67
|
+
let parts = dayPartFormats.get(timeZone)
|
|
68
|
+
if (!parts) {
|
|
69
|
+
parts = new Intl.DateTimeFormat('en-US', {
|
|
70
|
+
timeZone,
|
|
71
|
+
year: 'numeric',
|
|
72
|
+
month: 'numeric',
|
|
73
|
+
day: 'numeric',
|
|
74
|
+
})
|
|
75
|
+
dayPartFormats.set(timeZone, parts)
|
|
76
|
+
}
|
|
77
|
+
const values = parts.formatToParts(date)
|
|
78
|
+
const read = (type: Intl.DateTimeFormatPartTypes) =>
|
|
79
|
+
Number(values.find((part) => part.type === type)?.value ?? 0)
|
|
80
|
+
return Date.UTC(read('year'), read('month') - 1, read('day')) / MS_PER_DAY
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Whole calendar days from `date` to `now`, in the user's time zone. */
|
|
84
|
+
export function daysBefore(date: Date, now: Date, format: DateFormat): number {
|
|
85
|
+
return dayNumber(now, format) - dayNumber(date, format)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The year a moment falls in, in the user's time zone. */
|
|
89
|
+
function yearOf(date: Date, format: DateFormat): number {
|
|
90
|
+
return new Date(dayNumber(date, format) * MS_PER_DAY).getUTCFullYear()
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** "yesterday", "hier": the day before, in the language `format` writes. */
|
|
94
|
+
function yesterday(format: DateFormat): string {
|
|
95
|
+
const { locale } = format(TIME).resolvedOptions()
|
|
96
|
+
return new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }).format(
|
|
97
|
+
-1,
|
|
98
|
+
'day',
|
|
99
|
+
)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const numberFormats = new Map<string, Intl.NumberFormat>()
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The locale's number format, made once: a table draws a count and a
|
|
106
|
+
* duration in each of its thousands of cells, and making a format costs
|
|
107
|
+
* far more than using one.
|
|
108
|
+
*/
|
|
109
|
+
function numberFormat(
|
|
110
|
+
locale?: string,
|
|
111
|
+
options?: Intl.NumberFormatOptions,
|
|
112
|
+
): Intl.NumberFormat {
|
|
113
|
+
const key = `${locale ?? ''}|${options ? JSON.stringify(options) : ''}`
|
|
114
|
+
let format = numberFormats.get(key)
|
|
115
|
+
if (!format) {
|
|
116
|
+
format = new Intl.NumberFormat(locale, options)
|
|
117
|
+
numberFormats.set(key, format)
|
|
118
|
+
}
|
|
119
|
+
return format
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A count, grouped the way the locale writes it. */
|
|
123
|
+
export function formatCount(value: number, locale?: string): string {
|
|
124
|
+
return numberFormat(locale).format(Math.round(value))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A duration in ms, as "48 ms", "1,840 ms" or "12.4 s"; "< 1 ms" under a
|
|
129
|
+
* millisecond, which "0 ms" would read as no time at all.
|
|
130
|
+
*/
|
|
131
|
+
export function formatMs(value: number, locale?: string): string {
|
|
132
|
+
if (value > 0 && value < 1) return `< ${formatCount(1, locale)} ms`
|
|
133
|
+
if (value >= SECONDS_DISPLAY_FROM_MS) {
|
|
134
|
+
return `${numberFormat(locale, { maximumFractionDigits: 1 }).format(value / MS_PER_SECOND)} s`
|
|
135
|
+
}
|
|
136
|
+
return `${formatCount(value, locale)} ms`
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A duration threshold (the slow threshold of the settings), the one way
|
|
141
|
+
* every page writes it: "800 ms" under a second, "1 s", "1.5 s" from one.
|
|
142
|
+
*/
|
|
143
|
+
export function formatThreshold(ms: number, locale?: string): string {
|
|
144
|
+
if (ms < MS_PER_SECOND) return `${formatCount(ms, locale)} ms`
|
|
145
|
+
return `${new Intl.NumberFormat(locale, { maximumFractionDigits: 2 }).format(ms / MS_PER_SECOND)} s`
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* A share in percent (0–100), written the way the locale writes a percentage
|
|
150
|
+
* ("11.4%" in English, "11,4 %" in French): one decimal, two under 1 % so a
|
|
151
|
+
* small rate does not read as zero.
|
|
152
|
+
*/
|
|
153
|
+
export function formatPercent(value: number, locale?: string): string {
|
|
154
|
+
const digits = value > 0 && value < 1 ? 2 : 1
|
|
155
|
+
return new Intl.NumberFormat(locale, {
|
|
156
|
+
style: 'percent',
|
|
157
|
+
maximumFractionDigits: digits,
|
|
158
|
+
}).format(value / PERCENT)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** A count that may have a decimal, such as a rate per minute: "4.2", "1,250". */
|
|
162
|
+
export function formatDecimal(value: number, locale?: string): string {
|
|
163
|
+
return new Intl.NumberFormat(locale, {
|
|
164
|
+
maximumFractionDigits: Math.abs(value) < DECIMAL_DISPLAY_BELOW ? 1 : 0,
|
|
165
|
+
}).format(value)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function formatBytes(bytes: number, locale?: string): string {
|
|
169
|
+
if (bytes < BYTES_PER_KB) return `${formatCount(bytes, locale)} B`
|
|
170
|
+
return `${new Intl.NumberFormat(locale, { maximumFractionDigits: 1 }).format(bytes / BYTES_PER_KB)} KB`
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The time of a request: "14:31:58.412" today, "28 Sept, 14:31:58" before.
|
|
175
|
+
*/
|
|
176
|
+
export function formatClock(
|
|
177
|
+
value: string | number | Date | undefined,
|
|
178
|
+
format: DateFormat,
|
|
179
|
+
milliseconds = false,
|
|
180
|
+
now: Date = new Date(),
|
|
181
|
+
): string {
|
|
182
|
+
if (value === undefined || value === null) return ''
|
|
183
|
+
const date = new Date(value)
|
|
184
|
+
if (Number.isNaN(date.getTime())) return ''
|
|
185
|
+
const time = format(milliseconds ? CLOCK_TIME_MS : CLOCK_TIME).format(date)
|
|
186
|
+
if (daysBefore(date, now, format) === 0) return time
|
|
187
|
+
return `${format(SHORT_DAY).format(date)}, ${time}`
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* A moment named from today: "17:33" today, "yesterday 17:33", "Mon 17:33"
|
|
192
|
+
* within the week, "3 Oct, 17:33" before, with the year when it is not this
|
|
193
|
+
* one. A bare "9 Oct, 17:33" beside a time of today reads as if it came after.
|
|
194
|
+
*/
|
|
195
|
+
export function formatRelativeDay(
|
|
196
|
+
value: string | number | Date | undefined | null,
|
|
197
|
+
format: DateFormat,
|
|
198
|
+
now: Date = new Date(),
|
|
199
|
+
): string {
|
|
200
|
+
if (value === undefined || value === null) return ''
|
|
201
|
+
const date = new Date(value)
|
|
202
|
+
if (Number.isNaN(date.getTime())) return ''
|
|
203
|
+
const time = format(TIME).format(date)
|
|
204
|
+
const days = daysBefore(date, now, format)
|
|
205
|
+
if (days === 0) return time
|
|
206
|
+
if (days === 1) return `${yesterday(format)} ${time}`
|
|
207
|
+
if (days > 1 && days < DAYS_PER_WEEK) {
|
|
208
|
+
return `${format(WEEKDAY).format(date)} ${time}`
|
|
209
|
+
}
|
|
210
|
+
const sameYear = yearOf(date, format) === yearOf(now, format)
|
|
211
|
+
const day = format(sameYear ? SHORT_DAY : SHORT_DAY_YEAR).format(date)
|
|
212
|
+
return `${day}, ${time}`
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** The whole date and time, for a tooltip: "Friday 9 October 2026 at 17:33". */
|
|
216
|
+
export function formatFullDateTime(
|
|
217
|
+
value: string | number | Date | undefined | null,
|
|
218
|
+
format: DateFormat,
|
|
219
|
+
): string {
|
|
220
|
+
if (value === undefined || value === null) return ''
|
|
221
|
+
const date = new Date(value)
|
|
222
|
+
if (Number.isNaN(date.getTime())) return ''
|
|
223
|
+
return format(FULL_DATE_TIME).format(date)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export type DurationUnit = 's' | 'min' | 'h' | 'd'
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* A length of time in its two largest units, rounded down: "45 s",
|
|
230
|
+
* "38 min", "2 h 5 min", "3 d 4 h". The caller words each part.
|
|
231
|
+
*/
|
|
232
|
+
export function durationParts(
|
|
233
|
+
ms: number,
|
|
234
|
+
): Array<{ amount: number; unit: DurationUnit }> {
|
|
235
|
+
const seconds = Math.max(0, Math.floor(ms / MS_PER_SECOND))
|
|
236
|
+
if (seconds < SECONDS_PER_MINUTE) return [{ amount: seconds, unit: 's' }]
|
|
237
|
+
const minutes = Math.floor(seconds / SECONDS_PER_MINUTE)
|
|
238
|
+
if (minutes < MINUTES_PER_HOUR) return [{ amount: minutes, unit: 'min' }]
|
|
239
|
+
const hours = Math.floor(minutes / MINUTES_PER_HOUR)
|
|
240
|
+
const parts: Array<{ amount: number; unit: DurationUnit }> =
|
|
241
|
+
hours < HOURS_PER_DAY
|
|
242
|
+
? [
|
|
243
|
+
{ amount: hours, unit: 'h' },
|
|
244
|
+
{ amount: minutes % MINUTES_PER_HOUR, unit: 'min' },
|
|
245
|
+
]
|
|
246
|
+
: [
|
|
247
|
+
{ amount: Math.floor(hours / HOURS_PER_DAY), unit: 'd' },
|
|
248
|
+
{ amount: hours % HOURS_PER_DAY, unit: 'h' },
|
|
249
|
+
]
|
|
250
|
+
return parts.filter((part, index) => index === 0 || part.amount > 0)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The length of a window in one unit, the way a card names it: "15 min",
|
|
255
|
+
* "24 h", "7 d". Hours up to two days, so the last day reads "24 h".
|
|
256
|
+
*/
|
|
257
|
+
export function spanPart(ms: number): { amount: number; unit: DurationUnit } {
|
|
258
|
+
const minutes = Math.round(ms / MS_PER_MINUTE)
|
|
259
|
+
if (minutes < MINUTES_PER_HOUR) return { amount: minutes, unit: 'min' }
|
|
260
|
+
const hours = Math.round(minutes / MINUTES_PER_HOUR)
|
|
261
|
+
if (hours <= 2 * HOURS_PER_DAY) return { amount: hours, unit: 'h' }
|
|
262
|
+
return { amount: Math.round(hours / HOURS_PER_DAY), unit: 'd' }
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Pretty JSON when the text parses, the text itself otherwise. */
|
|
266
|
+
export function prettyBody(text: string | undefined | null): {
|
|
267
|
+
text: string
|
|
268
|
+
json: boolean
|
|
269
|
+
} {
|
|
270
|
+
if (!text) return { text: '', json: false }
|
|
271
|
+
try {
|
|
272
|
+
return { text: JSON.stringify(JSON.parse(text), null, 2), json: true }
|
|
273
|
+
} catch {
|
|
274
|
+
return { text, json: false }
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** UTF-8 size of a text, in bytes. */
|
|
279
|
+
export function byteLength(text: string): number {
|
|
280
|
+
return new TextEncoder().encode(text).length
|
|
281
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/** Tones of the DMS design system a badge can take. */
|
|
2
|
+
export type ApiTone =
|
|
3
|
+
| 'neutral'
|
|
4
|
+
| 'primary'
|
|
5
|
+
| 'success'
|
|
6
|
+
| 'warning'
|
|
7
|
+
| 'error'
|
|
8
|
+
| 'info'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* One colour per method, the same in every screen and theme: reads cyan,
|
|
12
|
+
* creates green, changes amber, deletes red.
|
|
13
|
+
*/
|
|
14
|
+
const METHOD_TONES: Record<string, ApiTone> = {
|
|
15
|
+
GET: 'info',
|
|
16
|
+
POST: 'success',
|
|
17
|
+
PUT: 'warning',
|
|
18
|
+
PATCH: 'warning',
|
|
19
|
+
DELETE: 'error',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function methodTone(method: string | undefined): ApiTone {
|
|
23
|
+
return METHOD_TONES[(method ?? '').toUpperCase()] ?? 'neutral'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Methods that change data: the tester warns before sending them. */
|
|
27
|
+
const WRITING_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE'])
|
|
28
|
+
|
|
29
|
+
export function isWritingMethod(method: string | undefined): boolean {
|
|
30
|
+
return WRITING_METHODS.has((method ?? '').toUpperCase())
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Methods that carry a request body. */
|
|
34
|
+
export function hasRequestBody(method: string | undefined): boolean {
|
|
35
|
+
return isWritingMethod(method)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Methods a body may go with: the tester's proxy refuses one on GET and HEAD. */
|
|
39
|
+
export function acceptsRequestBody(method: string | undefined): boolean {
|
|
40
|
+
return !['GET', 'HEAD'].includes((method ?? '').toUpperCase())
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Methods the tester starts with a body: DELETE gets one only on demand. */
|
|
44
|
+
const BODY_METHODS = new Set(['POST', 'PUT', 'PATCH'])
|
|
45
|
+
|
|
46
|
+
export function startsWithRequestBody(method: string | undefined): boolean {
|
|
47
|
+
return BODY_METHODS.has((method ?? '').toUpperCase())
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type StatusClass = '2xx' | '3xx' | '4xx' | '5xx'
|
|
51
|
+
|
|
52
|
+
export function statusClassOf(status: number): StatusClass {
|
|
53
|
+
if (status >= 500) return '5xx'
|
|
54
|
+
if (status >= 400) return '4xx'
|
|
55
|
+
if (status >= 300) return '3xx'
|
|
56
|
+
return '2xx'
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const STATUS_CLASS_TONES: Record<StatusClass, ApiTone> = {
|
|
60
|
+
'2xx': 'success',
|
|
61
|
+
'3xx': 'info',
|
|
62
|
+
'4xx': 'warning',
|
|
63
|
+
'5xx': 'error',
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function statusTone(status: number | string | undefined): ApiTone {
|
|
67
|
+
const code = Number(status)
|
|
68
|
+
if (!Number.isFinite(code) || code <= 0) return 'neutral'
|
|
69
|
+
return STATUS_CLASS_TONES[statusClassOf(code)]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const STATUS_TEXTS: Record<number, string> = {
|
|
73
|
+
200: 'OK',
|
|
74
|
+
201: 'Created',
|
|
75
|
+
202: 'Accepted',
|
|
76
|
+
204: 'No Content',
|
|
77
|
+
301: 'Moved Permanently',
|
|
78
|
+
302: 'Found',
|
|
79
|
+
304: 'Not Modified',
|
|
80
|
+
400: 'Bad Request',
|
|
81
|
+
401: 'Unauthorized',
|
|
82
|
+
403: 'Forbidden',
|
|
83
|
+
404: 'Not Found',
|
|
84
|
+
405: 'Method Not Allowed',
|
|
85
|
+
409: 'Conflict',
|
|
86
|
+
410: 'Gone',
|
|
87
|
+
413: 'Payload Too Large',
|
|
88
|
+
415: 'Unsupported Media Type',
|
|
89
|
+
422: 'Unprocessable Entity',
|
|
90
|
+
429: 'Too Many Requests',
|
|
91
|
+
500: 'Internal Server Error',
|
|
92
|
+
502: 'Bad Gateway',
|
|
93
|
+
503: 'Service Unavailable',
|
|
94
|
+
504: 'Gateway Timeout',
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The reason phrase of a status code, when it is a common one. */
|
|
98
|
+
export function statusText(status: number): string {
|
|
99
|
+
return STATUS_TEXTS[status] ?? ''
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Segments of a route path, each flagged when it is a `:param`. */
|
|
103
|
+
export interface PathSegment {
|
|
104
|
+
text: string
|
|
105
|
+
param: boolean
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function pathSegments(path: string): PathSegment[] {
|
|
109
|
+
return path
|
|
110
|
+
.split(/(:[A-Za-z_][\w]*)/)
|
|
111
|
+
.filter(Boolean)
|
|
112
|
+
.map((text) => ({ text, param: /^:[A-Za-z_]/.test(text) }))
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** `:id` names of a route path. */
|
|
116
|
+
export function pathParams(path: string): string[] {
|
|
117
|
+
return pathSegments(path)
|
|
118
|
+
.filter((segment) => segment.param)
|
|
119
|
+
.map((segment) => segment.text.slice(1))
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function parseRouteRef(
|
|
123
|
+
ref: string | null | undefined,
|
|
124
|
+
): { method: string; path: string } | null {
|
|
125
|
+
const match = ref?.trim().match(/^([A-Za-z]+)\s+(\/\S*)$/)
|
|
126
|
+
return match ? { method: match[1].toUpperCase(), path: match[2] } : null
|
|
127
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
// The request logs page keeps its filters in its URL.
|
|
2
|
+
//
|
|
3
|
+
// `?method=`, `?slow=`, `?route=`, `?path=`, `?error=` and an incident's
|
|
4
|
+
// `?since=` / `?until=` are the request table's arrival filters
|
|
5
|
+
// (`queryParamFilters`, `src/pages/logs.ts`): the table reads them from the
|
|
6
|
+
// URL whatever wrote them (a link of another page, the filters bar), counts
|
|
7
|
+
// its tabs with them, and follows them as they change. So the page has one
|
|
8
|
+
// filter state, which a reload, Back and a shared link all keep. `?from=`
|
|
9
|
+
// names the page a link came from, for the band that says so.
|
|
10
|
+
//
|
|
11
|
+
// The period is the toolbar's DMS `PeriodSelector`'s, which the table
|
|
12
|
+
// follows through its period scope and the blocks read with
|
|
13
|
+
// `usePeriodScope` (`useApiPeriod`): its "All time" is every request kept.
|
|
14
|
+
// An incident's range narrows that period without replacing it, so the
|
|
15
|
+
// page stays on the period the user chose. A change to the period is
|
|
16
|
+
// written in its URL key, which the selector follows.
|
|
17
|
+
|
|
18
|
+
import { parseRouteRef } from './http'
|
|
19
|
+
|
|
20
|
+
type Query = Record<string, unknown>
|
|
21
|
+
|
|
22
|
+
/** What the filters read of the page's period, `null` until it is known. */
|
|
23
|
+
export type LogsPeriod = Pick<PeriodState, 'preset' | 'range'> | null
|
|
24
|
+
|
|
25
|
+
/** The selector's URL key: `<selector id>.period` (`PERIOD_SCOPE`). */
|
|
26
|
+
const PERIOD_KEY = 'api.period'
|
|
27
|
+
|
|
28
|
+
/** The selector's own custom range: no preset the band can name. */
|
|
29
|
+
const CUSTOM_RANGE = 'custom'
|
|
30
|
+
|
|
31
|
+
/** The DMS "All time": every request kept, which the request logs offer. */
|
|
32
|
+
const ALL_TIME = 'all'
|
|
33
|
+
|
|
34
|
+
/** Methods the filters bar offers. */
|
|
35
|
+
export const LOG_METHODS = [
|
|
36
|
+
'GET',
|
|
37
|
+
'POST',
|
|
38
|
+
'PUT',
|
|
39
|
+
'PATCH',
|
|
40
|
+
'DELETE',
|
|
41
|
+
'HEAD',
|
|
42
|
+
'OPTIONS',
|
|
43
|
+
] as const
|
|
44
|
+
|
|
45
|
+
/** Filters only a link sets: the bar shows them as chips, each removable. */
|
|
46
|
+
export const LINK_FILTER_KEYS = ['route', 'path', 'error'] as const
|
|
47
|
+
export type LinkFilterKey = (typeof LINK_FILTER_KEYS)[number]
|
|
48
|
+
|
|
49
|
+
/** Pages a link to the request logs may name as its origin (`?from=`). */
|
|
50
|
+
export const LINK_ORIGINS = ['overview', 'routes'] as const
|
|
51
|
+
export type LinkOrigin = (typeof LINK_ORIGINS)[number]
|
|
52
|
+
|
|
53
|
+
/** An incident's range, `[from, to)`, which a link narrows the rows to. */
|
|
54
|
+
interface IncidentRange {
|
|
55
|
+
from: Date
|
|
56
|
+
to: Date
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The filters of the request logs, as the URL holds them. */
|
|
60
|
+
export interface LogsFilters {
|
|
61
|
+
method?: string
|
|
62
|
+
slow: boolean
|
|
63
|
+
route?: string
|
|
64
|
+
/** A path exactly as requested: one resource of a route. */
|
|
65
|
+
path?: string
|
|
66
|
+
error?: string
|
|
67
|
+
incident?: IncidentRange
|
|
68
|
+
from?: LinkOrigin
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** A change to the filters: `null` or `false` removes one. */
|
|
72
|
+
export interface LogsFiltersPatch {
|
|
73
|
+
/** Every request kept: the period's "All time". */
|
|
74
|
+
allTime?: true
|
|
75
|
+
method?: string | null
|
|
76
|
+
slow?: boolean
|
|
77
|
+
route?: null
|
|
78
|
+
path?: null
|
|
79
|
+
error?: null
|
|
80
|
+
/** Removes the incident's range. */
|
|
81
|
+
incident?: null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function single(value: unknown): string | undefined {
|
|
85
|
+
const first = Array.isArray(value) ? value[0] : value
|
|
86
|
+
return typeof first === 'string' && first !== '' ? first : undefined
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function oneOf<T extends string>(
|
|
90
|
+
values: readonly T[],
|
|
91
|
+
value: string | undefined,
|
|
92
|
+
): T | undefined {
|
|
93
|
+
return values.find((candidate) => candidate === value)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function date(value: string | undefined): Date | undefined {
|
|
97
|
+
if (!value) return undefined
|
|
98
|
+
const parsed = new Date(value)
|
|
99
|
+
return Number.isNaN(parsed.getTime()) ? undefined : parsed
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** `?since=` / `?until=`, when both read as dates. */
|
|
103
|
+
function readIncident(query: Query): IncidentRange | undefined {
|
|
104
|
+
const from = date(single(query.since))
|
|
105
|
+
const to = date(single(query.until))
|
|
106
|
+
return from && to ? { from, to } : undefined
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The filters a URL query holds; a value the table would not apply is left out. */
|
|
110
|
+
export function readLogsFilters(query: Query): LogsFilters {
|
|
111
|
+
return {
|
|
112
|
+
method: oneOf(LOG_METHODS, single(query.method)),
|
|
113
|
+
// The table sends the value as it is, and the server reads "true" only.
|
|
114
|
+
slow: single(query.slow) === 'true',
|
|
115
|
+
route: single(query.route),
|
|
116
|
+
path: single(query.path),
|
|
117
|
+
error: single(query.error),
|
|
118
|
+
incident: readIncident(query),
|
|
119
|
+
from: oneOf(LINK_ORIGINS, single(query.from)),
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Whether the period is every request kept. */
|
|
124
|
+
export function isAllTime(period: LogsPeriod): boolean {
|
|
125
|
+
return period?.preset === ALL_TIME
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The preset the period follows, neither "All time" nor a custom range. */
|
|
129
|
+
export function windowPreset(period: LogsPeriod): string | undefined {
|
|
130
|
+
return period && !isAllTime(period) && period.preset !== CUSTOM_RANGE
|
|
131
|
+
? period.preset
|
|
132
|
+
: undefined
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The period a link to the request logs carries, for their selector to open
|
|
137
|
+
* on: the preset the period follows, if any.
|
|
138
|
+
*/
|
|
139
|
+
export function linkedPeriod(period: LogsPeriod): Record<string, string> {
|
|
140
|
+
const preset = windowPreset(period)
|
|
141
|
+
return preset ? { [PERIOD_KEY]: preset } : {}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Whether a link's filters narrow the table: a route, a path, an error, an
|
|
146
|
+
* incident's range.
|
|
147
|
+
*/
|
|
148
|
+
function hasLinkFilters(filters: LogsFilters): boolean {
|
|
149
|
+
return LINK_FILTER_KEYS.some((key) => !!filters[key]) || !!filters.incident
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Whether a `?route=` can name a route at all (`garbage` cannot). */
|
|
153
|
+
export function isRouteRef(route: string): boolean {
|
|
154
|
+
return parseRouteRef(route) !== null
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** The method a route reference fixes: `POST /webhooks` → `POST`. */
|
|
158
|
+
export function routeMethod(route: string | undefined): string | undefined {
|
|
159
|
+
const method = route?.trim().split(/\s+/)[0]?.toUpperCase()
|
|
160
|
+
return oneOf(LOG_METHODS, method)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* `query` with `patch` applied, every other key kept (the table's tab, the
|
|
165
|
+
* open request, the period). "All time", or removing the last linked
|
|
166
|
+
* filter, forgets the page the link came from: the band would name an
|
|
167
|
+
* origin the filters no longer come from.
|
|
168
|
+
*/
|
|
169
|
+
export function withLogsFilters(
|
|
170
|
+
query: Query,
|
|
171
|
+
patch: LogsFiltersPatch,
|
|
172
|
+
): Record<string, string> {
|
|
173
|
+
const next: Record<string, string> = {}
|
|
174
|
+
for (const [key, value] of Object.entries(query)) {
|
|
175
|
+
const text = single(value)
|
|
176
|
+
if (text !== undefined) next[key] = text
|
|
177
|
+
}
|
|
178
|
+
const set = (key: string, value: string | null | undefined) => {
|
|
179
|
+
if (value) next[key] = value
|
|
180
|
+
else delete next[key]
|
|
181
|
+
}
|
|
182
|
+
if (patch.allTime) set(PERIOD_KEY, ALL_TIME)
|
|
183
|
+
if ('method' in patch) set('method', patch.method)
|
|
184
|
+
if ('slow' in patch) set('slow', patch.slow ? 'true' : null)
|
|
185
|
+
if ('route' in patch) set('route', null)
|
|
186
|
+
if ('path' in patch) set('path', null)
|
|
187
|
+
if ('error' in patch) set('error', null)
|
|
188
|
+
if ('incident' in patch) {
|
|
189
|
+
set('since', null)
|
|
190
|
+
set('until', null)
|
|
191
|
+
}
|
|
192
|
+
const linked = LINK_FILTER_KEYS.some((key) => !!next[key]) || !!next.since
|
|
193
|
+
if (patch.allTime || !linked) delete next.from
|
|
194
|
+
return next
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The filters' part of the URL, cleared: method, slowness and links, an
|
|
199
|
+
* incident's range among them. The period stays the selector's.
|
|
200
|
+
*/
|
|
201
|
+
export function clearedLogsFilters(query: Query): Record<string, string> {
|
|
202
|
+
return withLogsFilters(query, {
|
|
203
|
+
method: null,
|
|
204
|
+
slow: false,
|
|
205
|
+
route: null,
|
|
206
|
+
path: null,
|
|
207
|
+
error: null,
|
|
208
|
+
incident: null,
|
|
209
|
+
})
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Whether the URL narrows the request table besides its period: a method,
|
|
214
|
+
* the slow requests, a link's filters.
|
|
215
|
+
*/
|
|
216
|
+
export function hasLogsFilters(filters: LogsFilters): boolean {
|
|
217
|
+
return !!filters.method || filters.slow || hasLinkFilters(filters)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Why the request table is empty, to word its empty state. */
|
|
221
|
+
export type LogsEmptyCause =
|
|
222
|
+
| 'error'
|
|
223
|
+
| 'search'
|
|
224
|
+
| 'link'
|
|
225
|
+
| 'filters'
|
|
226
|
+
| 'first-run'
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* What the empty table owes its emptiness to, the most specific first: a
|
|
230
|
+
* failed list, the search, a link's filters, the other filters, else nothing
|
|
231
|
+
* captured yet. The DMS only knows its own search and filters: the URL's
|
|
232
|
+
* filters are read here.
|
|
233
|
+
*/
|
|
234
|
+
export function logsEmptyCause(
|
|
235
|
+
state: string | undefined,
|
|
236
|
+
search: string | undefined,
|
|
237
|
+
filters: LogsFilters,
|
|
238
|
+
period: LogsPeriod,
|
|
239
|
+
): LogsEmptyCause {
|
|
240
|
+
if (state === 'error') return 'error'
|
|
241
|
+
if (search?.trim()) return 'search'
|
|
242
|
+
if (hasLinkFilters(filters)) return 'link'
|
|
243
|
+
// A period frames the rows unless it is "All time".
|
|
244
|
+
if (hasLogsFilters(filters) || !isAllTime(period)) return 'filters'
|
|
245
|
+
return 'first-run'
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const BYTES_PER_KB = 1024
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* The body size limit in the unit that reads best: `none` when bodies are
|
|
252
|
+
* not captured, bytes under 1 KB (a 300 B limit is not "0 KB").
|
|
253
|
+
*/
|
|
254
|
+
export function bodyLimit(bytes: number): {
|
|
255
|
+
unit: 'none' | 'b' | 'kb'
|
|
256
|
+
amount: number
|
|
257
|
+
} {
|
|
258
|
+
if (bytes <= 0) return { unit: 'none', amount: 0 }
|
|
259
|
+
if (bytes < BYTES_PER_KB) return { unit: 'b', amount: Math.round(bytes) }
|
|
260
|
+
return { unit: 'kb', amount: Math.round((bytes / BYTES_PER_KB) * 10) / 10 }
|
|
261
|
+
}
|