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:
parent
ce614461ce
commit
5798f6722e
@ -3,7 +3,7 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
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 { apiBaseInjectionToken } from "../../api-base";
|
||||||
import type { MetricData } from "../metrics.api";
|
import type { MetricData } from "../metrics.api";
|
||||||
|
|
||||||
@ -47,14 +47,14 @@ const requestMetricsInjectable = getInjectable({
|
|||||||
id: "request-metrics",
|
id: "request-metrics",
|
||||||
instantiate: (di) => {
|
instantiate: (di) => {
|
||||||
const apiBase = di.inject(apiBaseInjectionToken);
|
const apiBase = di.inject(apiBaseInjectionToken);
|
||||||
|
const getSecondsFromUnixEpoch = di.inject(getSecondsFromUnixEpochInjectable);
|
||||||
|
|
||||||
return (async (query: object, params: RequestMetricsParams = {}) => {
|
return (async (query: object, params: RequestMetricsParams = {}) => {
|
||||||
const { range = 3600, step = 60, namespace } = params;
|
const { range = 3600, step = 60, namespace } = params;
|
||||||
let { start, end } = params;
|
let { start, end } = params;
|
||||||
|
|
||||||
if (!start && !end) {
|
if (!start && !end) {
|
||||||
const timeNow = Date.now() / 1000;
|
const now = getSecondsFromUnixEpoch();
|
||||||
const now = moment.unix(timeNow).startOf("minute").unix(); // round date to minutes
|
|
||||||
|
|
||||||
start = now - range;
|
start = now - range;
|
||||||
end = now;
|
end = now;
|
||||||
|
|||||||
@ -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;
|
||||||
@ -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;
|
||||||
@ -7,6 +7,7 @@ import { capitalize } from "lodash";
|
|||||||
import { when } from "mobx";
|
import { when } from "mobx";
|
||||||
import helmChartVersionsInjectable from "../+helm-charts/helm-charts/versions.injectable";
|
import helmChartVersionsInjectable from "../+helm-charts/helm-charts/versions.injectable";
|
||||||
import type { HelmRelease, HelmReleaseDto } from "../../../common/k8s-api/endpoints/helm-releases.api";
|
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";
|
import { formatDuration } from "../../utils";
|
||||||
|
|
||||||
export type ToHelmRelease = (release: HelmReleaseDto) => HelmRelease;
|
export type ToHelmRelease = (release: HelmReleaseDto) => HelmRelease;
|
||||||
@ -15,6 +16,7 @@ const toHelmReleaseInjectable = getInjectable({
|
|||||||
id: "to-helm-release",
|
id: "to-helm-release",
|
||||||
instantiate: (di): ToHelmRelease => {
|
instantiate: (di): ToHelmRelease => {
|
||||||
const helmChartVersions = (release: HelmRelease) => di.inject(helmChartVersionsInjectable, release);
|
const helmChartVersions = (release: HelmRelease) => di.inject(helmChartVersionsInjectable, release);
|
||||||
|
const getMillisecondsFromUnixEpoch = di.inject(getMillisecondsFromUnixEpochInjectable);
|
||||||
|
|
||||||
return (release) => ({
|
return (release) => ({
|
||||||
...release,
|
...release,
|
||||||
@ -60,7 +62,7 @@ const toHelmReleaseInjectable = getInjectable({
|
|||||||
getUpdated(humanize = true, compact = true) {
|
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 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 updatedDate = new Date(updated).getTime();
|
||||||
const diff = Date.now() - updatedDate;
|
const diff = getMillisecondsFromUnixEpoch() - updatedDate;
|
||||||
|
|
||||||
if (humanize) {
|
if (humanize) {
|
||||||
return formatDuration(diff, compact);
|
return formatDuration(diff, compact);
|
||||||
|
|||||||
@ -11,6 +11,7 @@ import { Button } from "../components/button";
|
|||||||
import type { IpcRendererEvent } from "electron";
|
import type { IpcRendererEvent } from "electron";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import notificationsStoreInjectable from "../components/notifications/notifications-store.injectable";
|
import notificationsStoreInjectable from "../components/notifications/notifications-store.injectable";
|
||||||
|
import getMillisecondsFromUnixEpochInjectable from "../../common/utils/date/get-milliseconds-from-unix-epoch.injectable";
|
||||||
|
|
||||||
const listNamespacesForbiddenHandlerInjectable = getInjectable({
|
const listNamespacesForbiddenHandlerInjectable = getInjectable({
|
||||||
id: "list-namespaces-forbidden-handler",
|
id: "list-namespaces-forbidden-handler",
|
||||||
@ -18,7 +19,7 @@ const listNamespacesForbiddenHandlerInjectable = getInjectable({
|
|||||||
instantiate: (di) => {
|
instantiate: (di) => {
|
||||||
const navigateToEntitySettings = di.inject(navigateToEntitySettingsInjectable);
|
const navigateToEntitySettings = di.inject(navigateToEntitySettingsInjectable);
|
||||||
const notificationsStore = di.inject(notificationsStoreInjectable);
|
const notificationsStore = di.inject(notificationsStoreInjectable);
|
||||||
|
const getMillisecondsFromUnixEpoch = di.inject(getMillisecondsFromUnixEpochInjectable);
|
||||||
const notificationLastDisplayedAt = new Map<string, number>();
|
const notificationLastDisplayedAt = new Map<string, number>();
|
||||||
const intervalBetweenNotifications = 1000 * 60; // 60s
|
const intervalBetweenNotifications = 1000 * 60; // 60s
|
||||||
|
|
||||||
@ -27,7 +28,7 @@ const listNamespacesForbiddenHandlerInjectable = getInjectable({
|
|||||||
...[clusterId]: ListNamespaceForbiddenArgs
|
...[clusterId]: ListNamespaceForbiddenArgs
|
||||||
): void => {
|
): void => {
|
||||||
const lastDisplayedAt = notificationLastDisplayedAt.get(clusterId);
|
const lastDisplayedAt = notificationLastDisplayedAt.get(clusterId);
|
||||||
const now = Date.now();
|
const now = getMillisecondsFromUnixEpoch();
|
||||||
|
|
||||||
if (
|
if (
|
||||||
typeof lastDisplayedAt !== "number" ||
|
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
|
* 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
|
* least a minute between closing the notification as seeing it again
|
||||||
*/
|
*/
|
||||||
onClose: () => notificationLastDisplayedAt.set(clusterId, Date.now()),
|
onClose: () => notificationLastDisplayedAt.set(clusterId, getMillisecondsFromUnixEpoch()),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user