Skip to content

Commit 17d7ee9

Browse files
committed
Add uptime value in Camel App Detail tab
1 parent e781599 commit 17d7ee9

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

src/components/camel-app-details/CamelAppStatusPod.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { podGVK } from '../../const';
1414
import { ResourceLink, ResourceStatus } from '@openshift-console/dynamic-plugin-sdk';
1515
import { useTranslation } from 'react-i18next';
1616
import Status from '@openshift-console/dynamic-plugin-sdk/lib/app/components/status/Status';
17+
import { formatDuration } from '../../date-utils';
1718

1819
type CamelAppStatusPodProps = {
1920
obj: CamelAppKind;
@@ -23,6 +24,9 @@ type CamelAppStatusPodProps = {
2324
const CamelAppStatusPod: React.FC<CamelAppStatusPodProps> = ({ obj: camelInt, pod: camelPod }) => {
2425
const { t } = useTranslation('plugin__camel-openshift-console-plugin');
2526

27+
// Golang time.Time is in nanoseconds
28+
const durationFull = formatDuration(Number(camelPod.uptime) / 1000000, { omitSuffix: false });
29+
2630
return (
2731
<>
2832
<Card>
@@ -48,6 +52,10 @@ const CamelAppStatusPod: React.FC<CamelAppStatusPodProps> = ({ obj: camelInt, po
4852
<DescriptionListTerm>{t('Internal IP')}:</DescriptionListTerm>
4953
<DescriptionListDescription>{camelPod.internalIp}</DescriptionListDescription>
5054
</DescriptionListGroup>
55+
<DescriptionListGroup>
56+
<DescriptionListTerm>{t('Uptime')}:</DescriptionListTerm>
57+
<DescriptionListDescription>{durationFull}</DescriptionListDescription>
58+
</DescriptionListGroup>
5159
<DescriptionListGroup>
5260
<DescriptionListTerm>{t('Runtime')}:</DescriptionListTerm>
5361
<DescriptionListDescription>

src/date-utils.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { useTranslation } from 'react-i18next';
2+
3+
4+
export const LAST_LANGUAGE_LOCAL_STORAGE_KEY = 'bridge/last-language';
5+
6+
export const getLastLanguage = (): string =>
7+
localStorage.getItem(LAST_LANGUAGE_LOCAL_STORAGE_KEY) ?? navigator.language;
8+
9+
export type Duration = {
10+
days: number;
11+
hours: number;
12+
minutes: number;
13+
seconds: number;
14+
};
15+
16+
export const relativeTimeFormatter = (langArg: string) =>
17+
Intl.RelativeTimeFormat ? new Intl.RelativeTimeFormat(langArg) : null;
18+
19+
export const dateTimeFormatter = (langArg: string) =>
20+
new Intl.DateTimeFormat(langArg, {
21+
month: 'short',
22+
day: 'numeric',
23+
hour: 'numeric',
24+
minute: 'numeric',
25+
year: 'numeric',
26+
});
27+
28+
function getDuration(ms: number): Duration {
29+
let seconds = Math.floor(ms / 1000);
30+
let minutes = Math.floor(seconds / 60);
31+
seconds %= 60;
32+
let hours = Math.floor(minutes / 60);
33+
minutes %= 60;
34+
const days = Math.floor(hours / 24);
35+
hours %= 24;
36+
return { days, hours, minutes, seconds };
37+
}
38+
39+
export const formatDuration = (ms: number, options?) => {
40+
const { t } = useTranslation('plugin__camel-openshift-console-plugin');
41+
const langArg = getLastLanguage();
42+
const duration = getDuration(ms);
43+
44+
// Check for null. If dateTime is null, it returns incorrect date Jan 1 1970.
45+
if (!duration) {
46+
return '-';
47+
}
48+
49+
const d = new Date(ms);
50+
const justNow = t('Just now');
51+
52+
// If the event occurred less than one minute in the future, assume it's clock drift and show "Just now."
53+
if (!options?.omitSuffix && ms < 60000 && ms > -60000) {
54+
return justNow;
55+
}
56+
57+
// Do not attempt to handle other dates in the future.
58+
if (ms < 0) {
59+
return '-';
60+
}
61+
62+
const { days, hours, minutes } = getDuration(ms);
63+
64+
if (options?.omitSuffix) {
65+
if (days) {
66+
return t('{{count}} day', { count: days });
67+
}
68+
if (hours) {
69+
return t('{{count}} hour', { count: hours });
70+
}
71+
return t('{{count}} minute', { count: minutes });
72+
}
73+
74+
// Fallback to normal date/time formatting if Intl.RelativeTimeFormat is not
75+
// available. This is the case for older Safari versions.
76+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat#browser_compatibility
77+
if (!relativeTimeFormatter(langArg)) {
78+
return dateTimeFormatter(langArg).format(d);
79+
}
80+
81+
if (!days && !hours && !minutes) {
82+
return justNow;
83+
}
84+
85+
if (days) {
86+
return relativeTimeFormatter(langArg).format(-days, 'day');
87+
}
88+
89+
if (hours) {
90+
return relativeTimeFormatter(langArg).format(-hours, 'hour');
91+
}
92+
93+
return relativeTimeFormatter(langArg).format(-minutes, 'minute');
94+
};

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export type CamelAppKind = K8sResourceKind & {
1111
export type CamelAppStatusPod = {
1212
name: string;
1313
internalIp: string;
14+
/** Format: date-time - in nanoseconds */
15+
uptime: string;
1416
observe: CamelAppObservability;
1517
ready: boolean;
1618
runtime: CamelAppRuntime;

0 commit comments

Comments
 (0)