1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Introduce and use get(Milli)SecondsFromUnixEpochInjectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-08-12 09:57:13 -04:00
parent ce614461ce
commit 5798f6722e
5 changed files with 40 additions and 7 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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<string, number>();
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()),
},
);
};