From 5798f6722e593474a8c87927586d5b557daaaa4c Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 12 Aug 2022 09:57:13 -0400 Subject: [PATCH] Introduce and use get(Milli)SecondsFromUnixEpochInjectable Signed-off-by: Sebastian Malton --- .../metrics.api/request-metrics.injectable.ts | 6 +++--- ...t-milliseconds-from-unix-epoch.injectable.ts | 13 +++++++++++++ .../get-seconds-from-unix-epoch.injectable.ts | 17 +++++++++++++++++ .../to-helm-release.injectable.ts | 4 +++- ...-namespaces-forbidden-handler.injectable.tsx | 7 ++++--- 5 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 src/common/utils/date/get-milliseconds-from-unix-epoch.injectable.ts create mode 100644 src/common/utils/date/get-seconds-from-unix-epoch.injectable.ts diff --git a/src/common/k8s-api/endpoints/metrics.api/request-metrics.injectable.ts b/src/common/k8s-api/endpoints/metrics.api/request-metrics.injectable.ts index 08141890a9..946d1e3dda 100644 --- a/src/common/k8s-api/endpoints/metrics.api/request-metrics.injectable.ts +++ b/src/common/k8s-api/endpoints/metrics.api/request-metrics.injectable.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import moment from "moment"; +import getSecondsFromUnixEpochInjectable from "../../../utils/date/get-seconds-from-unix-epoch.injectable"; import { apiBaseInjectionToken } from "../../api-base"; import type { MetricData } from "../metrics.api"; @@ -47,14 +47,14 @@ const requestMetricsInjectable = getInjectable({ id: "request-metrics", instantiate: (di) => { const apiBase = di.inject(apiBaseInjectionToken); + const getSecondsFromUnixEpoch = di.inject(getSecondsFromUnixEpochInjectable); return (async (query: object, params: RequestMetricsParams = {}) => { const { range = 3600, step = 60, namespace } = params; let { start, end } = params; if (!start && !end) { - const timeNow = Date.now() / 1000; - const now = moment.unix(timeNow).startOf("minute").unix(); // round date to minutes + const now = getSecondsFromUnixEpoch(); start = now - range; end = now; diff --git a/src/common/utils/date/get-milliseconds-from-unix-epoch.injectable.ts b/src/common/utils/date/get-milliseconds-from-unix-epoch.injectable.ts new file mode 100644 index 0000000000..4179e14276 --- /dev/null +++ b/src/common/utils/date/get-milliseconds-from-unix-epoch.injectable.ts @@ -0,0 +1,13 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; + +const getMillisecondsFromUnixEpochInjectable = getInjectable({ + id: "get-current-time", + instantiate: () => () => Date.now(), + causesSideEffects: true, +}); + +export default getMillisecondsFromUnixEpochInjectable; diff --git a/src/common/utils/date/get-seconds-from-unix-epoch.injectable.ts b/src/common/utils/date/get-seconds-from-unix-epoch.injectable.ts new file mode 100644 index 0000000000..35a077c3f2 --- /dev/null +++ b/src/common/utils/date/get-seconds-from-unix-epoch.injectable.ts @@ -0,0 +1,17 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable } from "@ogre-tools/injectable"; +import getMillisecondsFromUnixEpochInjectable from "./get-milliseconds-from-unix-epoch.injectable"; + +const getSecondsFromUnixEpochInjectable = getInjectable({ + id: "get-seconds-from-unix-epoch", + instantiate: (di) => { + const getMilisecondsFromUnixEpoch = di.inject(getMillisecondsFromUnixEpochInjectable); + + return () => Math.floor(getMilisecondsFromUnixEpoch() / 1000); + }, +}); + +export default getSecondsFromUnixEpochInjectable; diff --git a/src/renderer/components/+helm-releases/to-helm-release.injectable.ts b/src/renderer/components/+helm-releases/to-helm-release.injectable.ts index ea10b2be7d..c47b58c538 100644 --- a/src/renderer/components/+helm-releases/to-helm-release.injectable.ts +++ b/src/renderer/components/+helm-releases/to-helm-release.injectable.ts @@ -7,6 +7,7 @@ import { capitalize } from "lodash"; import { when } from "mobx"; import helmChartVersionsInjectable from "../+helm-charts/helm-charts/versions.injectable"; import type { HelmRelease, HelmReleaseDto } from "../../../common/k8s-api/endpoints/helm-releases.api"; +import getMillisecondsFromUnixEpochInjectable from "../../../common/utils/date/get-milliseconds-from-unix-epoch.injectable"; import { formatDuration } from "../../utils"; export type ToHelmRelease = (release: HelmReleaseDto) => HelmRelease; @@ -15,6 +16,7 @@ const toHelmReleaseInjectable = getInjectable({ id: "to-helm-release", instantiate: (di): ToHelmRelease => { const helmChartVersions = (release: HelmRelease) => di.inject(helmChartVersionsInjectable, release); + const getMillisecondsFromUnixEpoch = di.inject(getMillisecondsFromUnixEpochInjectable); return (release) => ({ ...release, @@ -60,7 +62,7 @@ const toHelmReleaseInjectable = getInjectable({ getUpdated(humanize = true, compact = true) { const updated = this.updated.replace(/\s\w*$/, ""); // 2019-11-26 10:58:09 +0300 MSK -> 2019-11-26 10:58:09 +0300 to pass into Date() const updatedDate = new Date(updated).getTime(); - const diff = Date.now() - updatedDate; + const diff = getMillisecondsFromUnixEpoch() - updatedDate; if (humanize) { return formatDuration(diff, compact); diff --git a/src/renderer/ipc/list-namespaces-forbidden-handler.injectable.tsx b/src/renderer/ipc/list-namespaces-forbidden-handler.injectable.tsx index 22434df1b6..9129a014db 100644 --- a/src/renderer/ipc/list-namespaces-forbidden-handler.injectable.tsx +++ b/src/renderer/ipc/list-namespaces-forbidden-handler.injectable.tsx @@ -11,6 +11,7 @@ import { Button } from "../components/button"; import type { IpcRendererEvent } from "electron"; import React from "react"; import notificationsStoreInjectable from "../components/notifications/notifications-store.injectable"; +import getMillisecondsFromUnixEpochInjectable from "../../common/utils/date/get-milliseconds-from-unix-epoch.injectable"; const listNamespacesForbiddenHandlerInjectable = getInjectable({ id: "list-namespaces-forbidden-handler", @@ -18,7 +19,7 @@ const listNamespacesForbiddenHandlerInjectable = getInjectable({ instantiate: (di) => { const navigateToEntitySettings = di.inject(navigateToEntitySettingsInjectable); const notificationsStore = di.inject(notificationsStoreInjectable); - + const getMillisecondsFromUnixEpoch = di.inject(getMillisecondsFromUnixEpochInjectable); const notificationLastDisplayedAt = new Map(); const intervalBetweenNotifications = 1000 * 60; // 60s @@ -27,7 +28,7 @@ const listNamespacesForbiddenHandlerInjectable = getInjectable({ ...[clusterId]: ListNamespaceForbiddenArgs ): void => { const lastDisplayedAt = notificationLastDisplayedAt.get(clusterId); - const now = Date.now(); + const now = getMillisecondsFromUnixEpoch(); if ( typeof lastDisplayedAt !== "number" || @@ -74,7 +75,7 @@ const listNamespacesForbiddenHandlerInjectable = getInjectable({ * Set the time when the notification is closed as well so that there is at * least a minute between closing the notification as seeing it again */ - onClose: () => notificationLastDisplayedAt.set(clusterId, Date.now()), + onClose: () => notificationLastDisplayedAt.set(clusterId, getMillisecondsFromUnixEpoch()), }, ); };