@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,116 @@
|
|
|
1
|
+
// The live histogram above the request logs: one bar per minute of the last
|
|
2
|
+
// hour, stacked by status class, on a scale a single spike cannot flatten.
|
|
3
|
+
|
|
4
|
+
const MS_PER_MINUTE = 60_000
|
|
5
|
+
|
|
6
|
+
/** One minute of the histogram, as `/api/monitoring/logs/live` sends it. */
|
|
7
|
+
export interface MinuteBucket {
|
|
8
|
+
start: number
|
|
9
|
+
/** Every answer below 400: 2xx and 3xx. */
|
|
10
|
+
success: number
|
|
11
|
+
clientErrors: number
|
|
12
|
+
serverErrors: number
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A minute as the histogram draws it. */
|
|
16
|
+
export interface MinuteBar extends MinuteBucket {
|
|
17
|
+
total: number
|
|
18
|
+
/** The minute still running: its count is partial. */
|
|
19
|
+
current: boolean
|
|
20
|
+
/** Taller than the scale: drawn cut, with a mark. */
|
|
21
|
+
clipped: boolean
|
|
22
|
+
/** Heights of the stacked segments, in % of the plot, bottom to top. */
|
|
23
|
+
heights: { success: number; clientErrors: number; serverErrors: number }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A peak this many times the usual busy minute no longer sets the scale. */
|
|
27
|
+
const PEAK_FACTOR = 3
|
|
28
|
+
/** The scale then stops at this many times the usual busy minute. */
|
|
29
|
+
const CEILING_FACTOR = 2
|
|
30
|
+
/** The usual busy minute: this quantile of the minutes that saw traffic. */
|
|
31
|
+
const TYPICAL_QUANTILE = 0.9
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The count the top of the plot stands for: the busiest minute, unless it
|
|
35
|
+
* is a spike far above the usual traffic, which would crush every other bar
|
|
36
|
+
* to a pixel. The scale then stops at twice the usual busy minute and the
|
|
37
|
+
* spike is drawn cut. Empty minutes do not count: a quiet hour with a few
|
|
38
|
+
* calls keeps them readable.
|
|
39
|
+
*/
|
|
40
|
+
export function scaleCeiling(totals: number[]): number {
|
|
41
|
+
const busy = totals.filter((total) => total > 0).sort((a, b) => a - b)
|
|
42
|
+
if (busy.length === 0) return 0
|
|
43
|
+
const peak = busy[busy.length - 1]
|
|
44
|
+
const typical = busy[Math.floor((busy.length - 1) * TYPICAL_QUANTILE)]
|
|
45
|
+
return peak > typical * PEAK_FACTOR
|
|
46
|
+
? Math.ceil(typical * CEILING_FACTOR)
|
|
47
|
+
: peak
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The minute `at` falls in, as the start of its bucket. */
|
|
51
|
+
export function minuteOf(at: number): number {
|
|
52
|
+
return Math.floor(at / MS_PER_MINUTE) * MS_PER_MINUTE
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The bars of `buckets` on their scale. The minute holding `refreshedAt` is
|
|
57
|
+
* the one still running. An empty minute has no segment: nothing is drawn.
|
|
58
|
+
*/
|
|
59
|
+
export function minuteBars(
|
|
60
|
+
buckets: MinuteBucket[],
|
|
61
|
+
refreshedAt?: string | number,
|
|
62
|
+
): { bars: MinuteBar[]; ceiling: number; clipped: boolean } {
|
|
63
|
+
const totals = buckets.map(
|
|
64
|
+
(bucket) => bucket.success + bucket.clientErrors + bucket.serverErrors,
|
|
65
|
+
)
|
|
66
|
+
const ceiling = scaleCeiling(totals)
|
|
67
|
+
const at = refreshedAt === undefined ? NaN : new Date(refreshedAt).getTime()
|
|
68
|
+
const currentMinute = Number.isNaN(at) ? undefined : minuteOf(at)
|
|
69
|
+
const bars = buckets.map((bucket, index) => {
|
|
70
|
+
const total = totals[index]
|
|
71
|
+
const shown = Math.min(total, ceiling)
|
|
72
|
+
// Each segment keeps its share of the bar when the bar is cut.
|
|
73
|
+
const percent = (value: number) =>
|
|
74
|
+
total === 0 ? 0 : ((value / total) * shown * 100) / ceiling
|
|
75
|
+
return {
|
|
76
|
+
...bucket,
|
|
77
|
+
total,
|
|
78
|
+
current: bucket.start === currentMinute,
|
|
79
|
+
clipped: total > ceiling,
|
|
80
|
+
heights: {
|
|
81
|
+
success: percent(bucket.success),
|
|
82
|
+
clientErrors: percent(bucket.clientErrors),
|
|
83
|
+
serverErrors: percent(bucket.serverErrors),
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
return { bars, ceiling, clipped: bars.some((bar) => bar.clipped) }
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The busiest minute, for the chart's text alternative. */
|
|
91
|
+
export function peakBar(bars: MinuteBar[]): MinuteBar | undefined {
|
|
92
|
+
return bars.reduce<MinuteBar | undefined>(
|
|
93
|
+
(peak, bar) =>
|
|
94
|
+
bar.total > 0 && (!peak || bar.total > peak.total) ? bar : peak,
|
|
95
|
+
undefined,
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The minutes of the time axis: the first, the middle and the last. */
|
|
100
|
+
export function axisTicks(bars: MinuteBar[]): number[] {
|
|
101
|
+
if (bars.length === 0) return []
|
|
102
|
+
const last = bars.length - 1
|
|
103
|
+
return [...new Set([0, Math.floor(last / 2), last])]
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* What the histogram's live indicator says: following the page's live mode,
|
|
108
|
+
* paused with it, or reconnecting since the last read failed (from the first
|
|
109
|
+
* failure).
|
|
110
|
+
*/
|
|
111
|
+
export type LiveStatus = 'live' | 'paused' | 'reconnecting'
|
|
112
|
+
|
|
113
|
+
export function liveStatus(live: boolean, failures: number): LiveStatus {
|
|
114
|
+
if (!live) return 'paused'
|
|
115
|
+
return failures > 0 ? 'reconnecting' : 'live'
|
|
116
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
// Reading a captured request, for the request tables' cells and the side
|
|
2
|
+
// panel a row opens (`RequestDetail`): its URL, its time, its duration
|
|
3
|
+
// against the slow threshold, its bodies and the lists it leads to.
|
|
4
|
+
|
|
5
|
+
import { daysBefore, type DateFormat } from './format'
|
|
6
|
+
|
|
7
|
+
const CLOCK_TIME: Intl.DateTimeFormatOptions = {
|
|
8
|
+
hour: '2-digit',
|
|
9
|
+
minute: '2-digit',
|
|
10
|
+
second: '2-digit',
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const CLOCK_TIME_MS: Intl.DateTimeFormatOptions = {
|
|
14
|
+
...CLOCK_TIME,
|
|
15
|
+
fractionalSecondDigits: 3,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const SHORT_DAY: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric' }
|
|
19
|
+
|
|
20
|
+
const FULL_DATE_TIME: Intl.DateTimeFormatOptions = {
|
|
21
|
+
weekday: 'short',
|
|
22
|
+
year: 'numeric',
|
|
23
|
+
month: 'short',
|
|
24
|
+
day: 'numeric',
|
|
25
|
+
hour: '2-digit',
|
|
26
|
+
minute: '2-digit',
|
|
27
|
+
second: '2-digit',
|
|
28
|
+
fractionalSecondDigits: 3,
|
|
29
|
+
timeZoneName: 'short',
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const relativeFormats = new Map<string, Intl.RelativeTimeFormat>()
|
|
33
|
+
|
|
34
|
+
function relativeFormat(locale: string): Intl.RelativeTimeFormat {
|
|
35
|
+
let format = relativeFormats.get(locale)
|
|
36
|
+
if (!format) {
|
|
37
|
+
format = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' })
|
|
38
|
+
relativeFormats.set(locale, format)
|
|
39
|
+
}
|
|
40
|
+
return format
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** "Today", "Yesterday" in the language `format` writes, else null. */
|
|
44
|
+
function relativeDay(date: Date, now: Date, format: DateFormat): string | null {
|
|
45
|
+
const days = daysBefore(date, now, format)
|
|
46
|
+
if (days !== 0 && days !== 1) return null
|
|
47
|
+
const { locale } = format(CLOCK_TIME).resolvedOptions()
|
|
48
|
+
const text = relativeFormat(locale).format(-days, 'day')
|
|
49
|
+
return text.charAt(0).toLocaleUpperCase(locale) + text.slice(1)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The time of a request in a table: "17:02:28.412" today, "Yesterday,
|
|
54
|
+
* 23:42:10.120" the day before, "8 Oct, 12:00:00.000" earlier.
|
|
55
|
+
*/
|
|
56
|
+
export function requestClock(
|
|
57
|
+
value: string | number | Date,
|
|
58
|
+
format: DateFormat,
|
|
59
|
+
milliseconds = false,
|
|
60
|
+
now: Date = new Date(),
|
|
61
|
+
): string {
|
|
62
|
+
const date = new Date(value)
|
|
63
|
+
if (Number.isNaN(date.getTime())) return ''
|
|
64
|
+
const time = format(milliseconds ? CLOCK_TIME_MS : CLOCK_TIME).format(date)
|
|
65
|
+
if (daysBefore(date, now, format) === 0) return time
|
|
66
|
+
const day = relativeDay(date, now, format) ?? format(SHORT_DAY).format(date)
|
|
67
|
+
return `${day}, ${time}`
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A moment in full, with its time zone: "Fri, 10 Oct 2026, 17:02:28.412 CEST". */
|
|
71
|
+
export function fullDateTime(
|
|
72
|
+
value: string | number | Date,
|
|
73
|
+
format: DateFormat,
|
|
74
|
+
): string {
|
|
75
|
+
const date = new Date(value)
|
|
76
|
+
if (Number.isNaN(date.getTime())) return ''
|
|
77
|
+
return format(FULL_DATE_TIME).format(date)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The URL a request was sent to: `origin`, its path and its query. */
|
|
81
|
+
export function requestUrl(
|
|
82
|
+
origin: string,
|
|
83
|
+
rawPath: string,
|
|
84
|
+
query: Record<string, string | string[]> | undefined,
|
|
85
|
+
): string {
|
|
86
|
+
const search = new URLSearchParams()
|
|
87
|
+
for (const [key, entry] of Object.entries(query ?? {})) {
|
|
88
|
+
for (const item of Array.isArray(entry) ? entry : [entry])
|
|
89
|
+
search.append(key, item)
|
|
90
|
+
}
|
|
91
|
+
const text = search.toString()
|
|
92
|
+
return `${origin}${rawPath}${text ? `?${text}` : ''}`
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface DurationScale {
|
|
96
|
+
/** Where the duration ends on the bar, in % of its length. */
|
|
97
|
+
share: number
|
|
98
|
+
/** Where the slow threshold sits on the bar, in %. */
|
|
99
|
+
threshold: number
|
|
100
|
+
/** The duration over the threshold: 6.9 for six times and more. */
|
|
101
|
+
ratio: number
|
|
102
|
+
slow: boolean
|
|
103
|
+
/** The value the bar ends at, in ms. */
|
|
104
|
+
max: number
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A duration against the slow threshold, on a bar long enough for both: up
|
|
109
|
+
* to twice the threshold, or to the duration when it is longer, so a request
|
|
110
|
+
* seven times too slow does not look like one just over it.
|
|
111
|
+
*/
|
|
112
|
+
export function durationScale(
|
|
113
|
+
durationMs: number,
|
|
114
|
+
thresholdMs: number,
|
|
115
|
+
): DurationScale {
|
|
116
|
+
const limit = Math.max(thresholdMs, 0)
|
|
117
|
+
const max = Math.max(limit * 2, durationMs, 1)
|
|
118
|
+
return {
|
|
119
|
+
share: (Math.max(durationMs, 0) / max) * 100,
|
|
120
|
+
threshold: (limit / max) * 100,
|
|
121
|
+
ratio: limit > 0 ? durationMs / limit : 0,
|
|
122
|
+
slow: limit > 0 && durationMs >= limit,
|
|
123
|
+
max,
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* A duration over the slow threshold, to one decimal: "2.2", "0.4"; "< 0.1"
|
|
129
|
+
* for a request far under it, which "0" would read as no time at all.
|
|
130
|
+
*/
|
|
131
|
+
export function thresholdRatio(ratio: number, locale?: string): string {
|
|
132
|
+
const format = new Intl.NumberFormat(locale, { maximumFractionDigits: 1 })
|
|
133
|
+
return ratio < 0.05 ? `< ${format.format(0.1)}` : format.format(ratio)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Why a body was not stored, as the API names it (`requestBodyOmitted`). */
|
|
137
|
+
export const BODY_OMISSIONS = [
|
|
138
|
+
'disabled',
|
|
139
|
+
'too-large',
|
|
140
|
+
'not-json',
|
|
141
|
+
'malformed',
|
|
142
|
+
'empty',
|
|
143
|
+
'not-read',
|
|
144
|
+
] as const
|
|
145
|
+
export type BodyOmission = (typeof BODY_OMISSIONS)[number]
|
|
146
|
+
|
|
147
|
+
export function bodyOmission(value: unknown): BodyOmission | null {
|
|
148
|
+
return BODY_OMISSIONS.find((reason) => reason === value) ?? null
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The size a body had before it was cut: the size the capture recorded, else
|
|
153
|
+
* the `content-length` it was sent with. Undefined for a body kept whole:
|
|
154
|
+
* masking its secrets changed its length, not what it holds, and "249 B of
|
|
155
|
+
* 260 B" would read as a cut. Undefined too when neither says more than what
|
|
156
|
+
* was kept.
|
|
157
|
+
*/
|
|
158
|
+
export function originalBodySize(
|
|
159
|
+
kept: number,
|
|
160
|
+
truncated: boolean | undefined,
|
|
161
|
+
recorded: number | null | undefined,
|
|
162
|
+
headers: Record<string, string> | undefined,
|
|
163
|
+
): number | undefined {
|
|
164
|
+
if (!truncated) return undefined
|
|
165
|
+
const declared = Number(
|
|
166
|
+
Object.entries(headers ?? {}).find(
|
|
167
|
+
([name]) => name.toLowerCase() === 'content-length',
|
|
168
|
+
)?.[1],
|
|
169
|
+
)
|
|
170
|
+
const size =
|
|
171
|
+
typeof recorded === 'number' && Number.isFinite(recorded)
|
|
172
|
+
? recorded
|
|
173
|
+
: declared
|
|
174
|
+
return Number.isFinite(size) && size > kept ? size : undefined
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** What the capture masked in a request, section by section (`redactions`). */
|
|
178
|
+
export interface Redactions {
|
|
179
|
+
query: number
|
|
180
|
+
requestBody: number
|
|
181
|
+
responseBody: number
|
|
182
|
+
/** Allow-listed headers left out: their name looks like a secret. */
|
|
183
|
+
headers: string[]
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Nothing masked: what the panel shows before a request is read. */
|
|
187
|
+
export const NO_REDACTIONS: Redactions = {
|
|
188
|
+
query: 0,
|
|
189
|
+
requestBody: 0,
|
|
190
|
+
responseBody: 0,
|
|
191
|
+
headers: [],
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Whether a URL could name a captured request: a link may hold anything. */
|
|
195
|
+
export function isRequestId(id: string): boolean {
|
|
196
|
+
return /^[\w.:-]{1,128}$/.test(id)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** `href` with `key` set to `value`, kept relative when it was. */
|
|
200
|
+
export function withParam(href: string, key: string, value: string): string {
|
|
201
|
+
const url = new URL(href, 'http://console.local')
|
|
202
|
+
url.searchParams.set(key, value)
|
|
203
|
+
return `${url.pathname}?${url.searchParams}`
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** How the route of a request behaves, read with it (`routeLatency`). */
|
|
207
|
+
export interface RouteLatency {
|
|
208
|
+
/** The days it is read over, up to now; the tester's calls left out. */
|
|
209
|
+
windowDays: number
|
|
210
|
+
/** Median of the route's measured durations, in ms. */
|
|
211
|
+
p50: number
|
|
212
|
+
/** 95th percentile of the route's measured durations, in ms. */
|
|
213
|
+
p95: number
|
|
214
|
+
/** The route's requests at or over the slow threshold. */
|
|
215
|
+
slow: number
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** A list of requests related to the one open, in the request logs. */
|
|
219
|
+
export interface RequestPivot {
|
|
220
|
+
kind: 'same_route' | 'same_path' | 'slow_route'
|
|
221
|
+
to: string
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* The lists a request leads to, each holding requests. A request no route
|
|
226
|
+
* matched leads to its path: the route list would be empty, unmatched
|
|
227
|
+
* requests have no route. Its slow requests are offered once the route has
|
|
228
|
+
* some, or, without its figures, when the request itself is one.
|
|
229
|
+
*/
|
|
230
|
+
export function requestPivots(log: {
|
|
231
|
+
rawPath: string
|
|
232
|
+
routeRegistered: boolean
|
|
233
|
+
responseTimeMs: number | null
|
|
234
|
+
slowThresholdMs: number
|
|
235
|
+
routeLatency?: RouteLatency | null
|
|
236
|
+
links: { sameRoute: string; samePath?: string | null }
|
|
237
|
+
}): RequestPivot[] {
|
|
238
|
+
if (!log.routeRegistered)
|
|
239
|
+
return log.links.samePath
|
|
240
|
+
? [{ kind: 'same_path', to: log.links.samePath }]
|
|
241
|
+
: []
|
|
242
|
+
const pivots: RequestPivot[] = [
|
|
243
|
+
{ kind: 'same_route', to: log.links.sameRoute },
|
|
244
|
+
]
|
|
245
|
+
if (log.links.samePath)
|
|
246
|
+
pivots.push({ kind: 'same_path', to: log.links.samePath })
|
|
247
|
+
const hasSlow = log.routeLatency
|
|
248
|
+
? log.routeLatency.slow > 0
|
|
249
|
+
: log.responseTimeMs !== null &&
|
|
250
|
+
durationScale(log.responseTimeMs, log.slowThresholdMs).slow
|
|
251
|
+
if (hasSlow)
|
|
252
|
+
pivots.push({
|
|
253
|
+
kind: 'slow_route',
|
|
254
|
+
to: withParam(log.links.sameRoute, 'slow', 'true'),
|
|
255
|
+
})
|
|
256
|
+
return pivots
|
|
257
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Routes page's pure logic: how the list groups, orders and walks its
|
|
3
|
+
* routes, and how the page names a period in its links. Kept out of the
|
|
4
|
+
* components so it can be tested without a DMS around it.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** A route's health, in the words of the API verdict (`route-health.ts`). */
|
|
8
|
+
export type RouteState =
|
|
9
|
+
| 'failing'
|
|
10
|
+
| 'slow'
|
|
11
|
+
| 'client-errors'
|
|
12
|
+
| 'recovered'
|
|
13
|
+
| 'healthy'
|
|
14
|
+
| 'idle'
|
|
15
|
+
|
|
16
|
+
/** What the list needs of a route row of `/routes/list`. */
|
|
17
|
+
export interface ExplorerRoute {
|
|
18
|
+
ref: string
|
|
19
|
+
method: string
|
|
20
|
+
path: string
|
|
21
|
+
folder: string
|
|
22
|
+
requests: number
|
|
23
|
+
state: RouteState
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface RouteFolder<T extends ExplorerRoute = ExplorerRoute> {
|
|
27
|
+
path: string
|
|
28
|
+
routes: T[]
|
|
29
|
+
worst: RouteState
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Worst first, for the list and its folders: a route still slow outranks one
|
|
34
|
+
* that has recovered. The Overview orders its Needs attention its own way
|
|
35
|
+
* (recovered right after failing), over the same routes.
|
|
36
|
+
*/
|
|
37
|
+
export const STATE_RANK: Record<RouteState, number> = {
|
|
38
|
+
failing: 0,
|
|
39
|
+
slow: 1,
|
|
40
|
+
'client-errors': 2,
|
|
41
|
+
recovered: 3,
|
|
42
|
+
healthy: 4,
|
|
43
|
+
idle: 5,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Failing, slow, answering many 4xx or recovered from an incident a moment
|
|
48
|
+
* ago: the routes the Overview's "Needs attention" lists.
|
|
49
|
+
*/
|
|
50
|
+
export function needsAttention(route: ExplorerRoute): boolean {
|
|
51
|
+
return STATE_RANK[route.state] < STATE_RANK.healthy
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** `/auth/2fa` → `/auth`; a top-level folder is its own parent. */
|
|
55
|
+
function parentFolder(folder: string): string {
|
|
56
|
+
const first = folder.split('/').filter(Boolean)[0]
|
|
57
|
+
return first ? `/${first}` : '/'
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The folder each route is listed under. A folder holding a single route
|
|
62
|
+
* folds into its first segment (`/auth/2fa` joins `/auth`), so a large API
|
|
63
|
+
* does not read as dozens of one-line folders. Decided on every route, not
|
|
64
|
+
* on the filtered ones, so filtering never moves a route to another folder.
|
|
65
|
+
*/
|
|
66
|
+
export function listedFolders(routes: ExplorerRoute[]): Map<string, string> {
|
|
67
|
+
const sizes = new Map<string, number>()
|
|
68
|
+
for (const route of routes) {
|
|
69
|
+
sizes.set(route.folder, (sizes.get(route.folder) ?? 0) + 1)
|
|
70
|
+
}
|
|
71
|
+
return new Map(
|
|
72
|
+
routes.map((route) => [
|
|
73
|
+
route.ref,
|
|
74
|
+
sizes.get(route.folder) === 1 ? parentFolder(route.folder) : route.folder,
|
|
75
|
+
]),
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function worstOf(routes: ExplorerRoute[]): RouteState {
|
|
80
|
+
return routes.reduce<RouteState>(
|
|
81
|
+
(worst, route) =>
|
|
82
|
+
STATE_RANK[route.state] < STATE_RANK[worst] ? route.state : worst,
|
|
83
|
+
'idle',
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** `routes` grouped under their listed folder, folders and routes by path. */
|
|
88
|
+
export function groupRoutes<T extends ExplorerRoute>(
|
|
89
|
+
routes: T[],
|
|
90
|
+
folders: Map<string, string>,
|
|
91
|
+
): RouteFolder<T>[] {
|
|
92
|
+
const groups = new Map<string, T[]>()
|
|
93
|
+
for (const route of routes) {
|
|
94
|
+
const folder = folders.get(route.ref) ?? route.folder
|
|
95
|
+
const group = groups.get(folder) ?? []
|
|
96
|
+
group.push(route)
|
|
97
|
+
groups.set(folder, group)
|
|
98
|
+
}
|
|
99
|
+
return [...groups]
|
|
100
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
101
|
+
.map(([path, members]) => ({
|
|
102
|
+
path,
|
|
103
|
+
routes: [...members].sort(
|
|
104
|
+
(a, b) =>
|
|
105
|
+
a.path.localeCompare(b.path) || a.method.localeCompare(b.method),
|
|
106
|
+
),
|
|
107
|
+
worst: worstOf(members),
|
|
108
|
+
}))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Worst state first, then the route serving the most traffic. */
|
|
112
|
+
export function bySeverity<T extends ExplorerRoute>(routes: T[]): T[] {
|
|
113
|
+
return [...routes].sort(
|
|
114
|
+
(a, b) =>
|
|
115
|
+
STATE_RANK[a.state] - STATE_RANK[b.state] || b.requests - a.requests,
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The route a step of the keyboard lands on, counted from the last route
|
|
121
|
+
* asked for rather than from the one on screen: pressing ↓ three times walks
|
|
122
|
+
* three routes even while the first one is still loading.
|
|
123
|
+
*/
|
|
124
|
+
export function stepFrom<T extends ExplorerRoute>(
|
|
125
|
+
items: T[],
|
|
126
|
+
current: string | null,
|
|
127
|
+
step: number,
|
|
128
|
+
): T | undefined {
|
|
129
|
+
if (items.length === 0) return undefined
|
|
130
|
+
const index = items.findIndex((route) => route.ref === current)
|
|
131
|
+
if (index === -1) return step < 0 ? items.at(-1) : items[0]
|
|
132
|
+
return items[Math.min(items.length - 1, Math.max(0, index + step))]
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The route a page opened without one shows: the last one looked at, if it
|
|
137
|
+
* is still listed, else the one needing a look first.
|
|
138
|
+
*/
|
|
139
|
+
export function initialRoute<T extends ExplorerRoute>(
|
|
140
|
+
routes: T[],
|
|
141
|
+
last: string | null,
|
|
142
|
+
): T | undefined {
|
|
143
|
+
return (
|
|
144
|
+
(last ? routes.find((route) => route.ref === last) : undefined) ??
|
|
145
|
+
bySeverity(routes)[0]
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The messages listed under a status of the documentation, each with its own
|
|
151
|
+
* count, and how many distinct ones are left out: the status's volume is the
|
|
152
|
+
* status's, never its first message's.
|
|
153
|
+
*/
|
|
154
|
+
export function listedMessages(
|
|
155
|
+
observed: {
|
|
156
|
+
messages: Array<{ message: string; count: number }>
|
|
157
|
+
/** Distinct messages in all, the listed ones included. */
|
|
158
|
+
messageCount: number
|
|
159
|
+
},
|
|
160
|
+
limit: number,
|
|
161
|
+
): { messages: Array<{ message: string; count: number }>; more: number } {
|
|
162
|
+
const messages = observed.messages.slice(0, limit)
|
|
163
|
+
return {
|
|
164
|
+
messages,
|
|
165
|
+
more: Math.max(0, observed.messageCount - messages.length),
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** An id safe for `aria-activedescendant`: a route ref holds spaces and `/`. */
|
|
170
|
+
export function optionId(index: number): string {
|
|
171
|
+
return `api-route-option-${index}`
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const MINUTES_PER_HOUR = 60
|
|
175
|
+
const MINUTES_PER_DAY = 1440
|
|
176
|
+
|
|
177
|
+
/** A health window in minutes, as its unit and amount: 15 → min 15, 1440 → h 24. */
|
|
178
|
+
export function windowParts(minutes: number): {
|
|
179
|
+
unit: 'min' | 'h' | 'd'
|
|
180
|
+
amount: number
|
|
181
|
+
} {
|
|
182
|
+
if (minutes >= MINUTES_PER_DAY * 2 && minutes % MINUTES_PER_DAY === 0)
|
|
183
|
+
return { unit: 'd', amount: minutes / MINUTES_PER_DAY }
|
|
184
|
+
if (minutes >= MINUTES_PER_HOUR && minutes % MINUTES_PER_HOUR === 0)
|
|
185
|
+
return { unit: 'h', amount: minutes / MINUTES_PER_HOUR }
|
|
186
|
+
return { unit: 'min', amount: minutes }
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const LAST_ROUTE_KEY = 'dms-api:routes:last'
|
|
190
|
+
|
|
191
|
+
/** The route last opened in this tab, if the browser lets us keep it. */
|
|
192
|
+
export function lastRoute(): string | null {
|
|
193
|
+
try {
|
|
194
|
+
return window.sessionStorage.getItem(LAST_ROUTE_KEY)
|
|
195
|
+
} catch {
|
|
196
|
+
return null
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function rememberRoute(ref: string): void {
|
|
201
|
+
try {
|
|
202
|
+
window.sessionStorage.setItem(LAST_ROUTE_KEY, ref)
|
|
203
|
+
} catch {
|
|
204
|
+
// Storage blocked: the page falls back to the route needing a look.
|
|
205
|
+
}
|
|
206
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const MAX_SUGGESTION_DISTANCE_RATIO = 0.4
|
|
2
|
+
|
|
3
|
+
/** Edit distance between two strings (insertions, deletions, substitutions). */
|
|
4
|
+
export function editDistance(a: string, b: string): number {
|
|
5
|
+
const previous = Array.from({ length: b.length + 1 }, (_, index) => index)
|
|
6
|
+
for (let i = 1; i <= a.length; i++) {
|
|
7
|
+
let diagonal = previous[0]
|
|
8
|
+
previous[0] = i
|
|
9
|
+
for (let j = 1; j <= b.length; j++) {
|
|
10
|
+
const above = previous[j]
|
|
11
|
+
previous[j] = Math.min(
|
|
12
|
+
previous[j] + 1,
|
|
13
|
+
previous[j - 1] + 1,
|
|
14
|
+
diagonal + (a[i - 1] === b[j - 1] ? 0 : 1),
|
|
15
|
+
)
|
|
16
|
+
diagonal = above
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return previous[b.length]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The candidate a mistyped search most likely meant: compared segment by
|
|
24
|
+
* segment, so "refnd" finds `/api/orders/:id/refund`. Null when nothing is
|
|
25
|
+
* close enough to be a typo.
|
|
26
|
+
*/
|
|
27
|
+
export function closestMatch(
|
|
28
|
+
search: string,
|
|
29
|
+
candidates: string[],
|
|
30
|
+
): string | null {
|
|
31
|
+
const needle = search.toLowerCase()
|
|
32
|
+
let best: { candidate: string; distance: number } | null = null
|
|
33
|
+
for (const candidate of candidates) {
|
|
34
|
+
const parts = [candidate, ...candidate.split('/')].filter(Boolean)
|
|
35
|
+
for (const part of parts) {
|
|
36
|
+
const distance = editDistance(needle, part.toLowerCase())
|
|
37
|
+
if (!best || distance < best.distance) best = { candidate, distance }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (!best) return null
|
|
41
|
+
return best.distance <=
|
|
42
|
+
Math.max(1, Math.round(needle.length * MAX_SUGGESTION_DISTANCE_RATIO))
|
|
43
|
+
? best.candidate
|
|
44
|
+
: null
|
|
45
|
+
}
|