From a5c5491ff50c693bcbb6bf1d0b8065d3ceedfe9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Fri, 16 Jun 2023 11:46:13 +0200 Subject: [PATCH] introduce durationToMilliseconds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/hip-mugs-visit.md | 5 +++ .changeset/purple-cheetahs-grow.md | 6 ++++ .../httpRouter/createLifecycleMiddleware.ts | 25 ++----------- .../src/sources/RemoteConfigSource.ts | 29 ++++----------- packages/types/api-report.md | 3 ++ packages/types/src/index.ts | 2 +- packages/types/src/time.ts | 35 +++++++++++++++++++ 7 files changed, 58 insertions(+), 47 deletions(-) create mode 100644 .changeset/hip-mugs-visit.md create mode 100644 .changeset/purple-cheetahs-grow.md diff --git a/.changeset/hip-mugs-visit.md b/.changeset/hip-mugs-visit.md new file mode 100644 index 0000000000..c2029bd374 --- /dev/null +++ b/.changeset/hip-mugs-visit.md @@ -0,0 +1,5 @@ +--- +'@backstage/types': patch +--- + +Added a `durationToMilliseconds` function to help with the conversion to a single duration number diff --git a/.changeset/purple-cheetahs-grow.md b/.changeset/purple-cheetahs-grow.md new file mode 100644 index 0000000000..3eb629e4d7 --- /dev/null +++ b/.changeset/purple-cheetahs-grow.md @@ -0,0 +1,6 @@ +--- +'@backstage/backend-app-api': patch +'@backstage/config-loader': patch +--- + +Use `durationToMilliseconds` from `@backstage/types` instead of our own diff --git a/packages/backend-app-api/src/services/implementations/httpRouter/createLifecycleMiddleware.ts b/packages/backend-app-api/src/services/implementations/httpRouter/createLifecycleMiddleware.ts index 958ae33e99..3f8ee3ea69 100644 --- a/packages/backend-app-api/src/services/implementations/httpRouter/createLifecycleMiddleware.ts +++ b/packages/backend-app-api/src/services/implementations/httpRouter/createLifecycleMiddleware.ts @@ -16,32 +16,11 @@ import { LifecycleService } from '@backstage/backend-plugin-api'; import { ServiceUnavailableError } from '@backstage/errors'; -import { HumanDuration } from '@backstage/types'; +import { HumanDuration, durationToMilliseconds } from '@backstage/types'; import { RequestHandler } from 'express'; export const DEFAULT_TIMEOUT = { seconds: 5 }; -function durationToMs(duration: HumanDuration): number { - const { - years = 0, - months = 0, - weeks = 0, - days = 0, - hours = 0, - minutes = 0, - seconds = 0, - milliseconds = 0, - } = duration; - - const totalDays = years * 365 + months * 30 + weeks * 7 + days; - const totalHours = totalDays * 24 + hours; - const totalMinutes = totalHours * 60 + minutes; - const totalSeconds = totalMinutes * 60 + seconds; - const totalMilliseconds = totalSeconds * 1000 + milliseconds; - - return totalMilliseconds; -} - /** * Options for {@link createLifecycleMiddleware}. * @public @@ -102,7 +81,7 @@ export function createLifecycleMiddleware( waiting.clear(); }); - const timeoutMs = durationToMs(startupRequestPauseTimeout); + const timeoutMs = durationToMilliseconds(startupRequestPauseTimeout); return (_req, _res, next) => { if (state === 'up') { diff --git a/packages/config-loader/src/sources/RemoteConfigSource.ts b/packages/config-loader/src/sources/RemoteConfigSource.ts index 3a8f6bf812..10a64ee635 100644 --- a/packages/config-loader/src/sources/RemoteConfigSource.ts +++ b/packages/config-loader/src/sources/RemoteConfigSource.ts @@ -15,7 +15,11 @@ */ import { ResponseError } from '@backstage/errors'; -import { HumanDuration, JsonObject } from '@backstage/types'; +import { + HumanDuration, + JsonObject, + durationToMilliseconds, +} from '@backstage/types'; import isEqual from 'lodash/isEqual'; import fetch from 'node-fetch'; import yaml from 'yaml'; @@ -29,27 +33,6 @@ import { const DEFAULT_RELOAD_INTERVAL = { seconds: 60 }; -function durationToMs(duration: HumanDuration): number { - const { - years = 0, - months = 0, - weeks = 0, - days = 0, - hours = 0, - minutes = 0, - seconds = 0, - milliseconds = 0, - } = duration; - - const totalDays = years * 365 + months * 30 + weeks * 7 + days; - const totalHours = totalDays * 24 + hours; - const totalMinutes = totalHours * 60 + minutes; - const totalSeconds = totalMinutes * 60 + seconds; - const totalMilliseconds = totalSeconds * 1000 + milliseconds; - - return totalMilliseconds; -} - /** * Options for {@link RemoteConfigSource.create}. * @@ -104,7 +87,7 @@ export class RemoteConfigSource implements ConfigSource { private constructor(options: RemoteConfigSourceOptions) { this.#url = options.url; - this.#reloadIntervalMs = durationToMs( + this.#reloadIntervalMs = durationToMilliseconds( options.reloadInterval ?? DEFAULT_RELOAD_INTERVAL, ); this.#transformer = createConfigTransformer({ diff --git a/packages/types/api-report.md b/packages/types/api-report.md index 4b43f5daa6..9f33bef012 100644 --- a/packages/types/api-report.md +++ b/packages/types/api-report.md @@ -3,6 +3,9 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts +// @public +export function durationToMilliseconds(duration: HumanDuration): number; + // @public export type HumanDuration = { years?: number; diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 0b5c8689b3..29ba114037 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -22,4 +22,4 @@ export type { JsonArray, JsonObject, JsonPrimitive, JsonValue } from './json'; export type { Observable, Observer, Subscription } from './observable'; -export type { HumanDuration } from './time'; +export { type HumanDuration, durationToMilliseconds } from './time'; diff --git a/packages/types/src/time.ts b/packages/types/src/time.ts index 7ab8288cea..7b87e11e71 100644 --- a/packages/types/src/time.ts +++ b/packages/types/src/time.ts @@ -29,3 +29,38 @@ export type HumanDuration = { seconds?: number; milliseconds?: number; }; + +/** + * Converts a {@link HumanDuration} to milliseconds. + * + * @public + * @remarks + * + * Note that this conversion by definition is an approximation in the absence of + * an anchor to a point in time and time zone, as the number of milliseconds is + * not constant for many units. So the conversion assumes 365-day years, 30-day + * months, and fixed 24-hour days. + * + * @param duration - A human friendly duration object. + * @returns The number of approximate milliseconds that the duration represents. + */ +export function durationToMilliseconds(duration: HumanDuration): number { + const { + years = 0, + months = 0, + weeks = 0, + days = 0, + hours = 0, + minutes = 0, + seconds = 0, + milliseconds = 0, + } = duration; + + const totalDays = years * 365 + months * 30 + weeks * 7 + days; + const totalHours = totalDays * 24 + hours; + const totalMinutes = totalHours * 60 + minutes; + const totalSeconds = totalMinutes * 60 + seconds; + const totalMilliseconds = totalSeconds * 1000 + milliseconds; + + return totalMilliseconds; +}