Skip to content

Commit

Permalink
HPCC-32820 ECL Watch v9 logs view timestamp formatting
Browse files Browse the repository at this point in the history
support conditionally formatting the timestamp returned by the logging
engine (loki returns nanoseconds rather than unixtime or a Date)

Signed-off-by: Jeremy Clements <[email protected]>
  • Loading branch information
jeclrsg committed Oct 17, 2024
1 parent 986b454 commit 492caf4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
14 changes: 12 additions & 2 deletions esp/src/src-react/components/Logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CommandBar, ContextualMenuItemType, ICommandBarItemProps } from "@fluen
import { GetLogsExRequest, LogaccessService, LogType, TargetAudience, WsLogaccess } from "@hpcc-js/comms";
import { Level, scopedLogger } from "@hpcc-js/util";
import nlsHPCC from "src/nlsHPCC";
import { logColor, removeAllExcept, wuidToDate, wuidToTime } from "src/Utility";
import { formatDateString, logColor, removeAllExcept, timestampToDate, wuidToDate, wuidToTime } from "src/Utility";
import { useLogAccessInfo } from "../hooks/platform";
import { HolyGrail } from "../layouts/HolyGrail";
import { pushParams } from "../util/history";
Expand Down Expand Up @@ -132,7 +132,17 @@ export const Logs: React.FunctionComponent<LogsProps> = ({
}
});
const retVal = {
timestamp: { label: nlsHPCC.TimeStamp, width: 140, sortable: false, },
timestamp: {
label: nlsHPCC.TimeStamp, width: 140, sortable: false,
formatter: ts => {
if (ts) {
if (ts.indexOf(":") < 0) {
return timestampToDate(ts).toISOString();
}
return formatDateString(ts);
}
},
},
message: { label: nlsHPCC.Message, width: 600, sortable: false, },
components: { label: nlsHPCC.ContainerName, width: 150, sortable: false },
audience: { label: nlsHPCC.Audience, width: 60, sortable: false, },
Expand Down
23 changes: 23 additions & 0 deletions esp/src/src/Utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,29 @@ export function format(labelTpl, obj) {
.join("\n")
;
}

const TEN_TRILLION = 10000000000000;
export function nanosToMillis(timestamp: number): number {
if (timestamp > TEN_TRILLION) {
return Math.round(timestamp / 1000000);
} else {
return timestamp;
}
}

export function timestampToDate(timestamp: number): Date {
const millis = nanosToMillis(timestamp);
return new Date(millis);
}

export function formatDateString(dateStr: string): string {
const matches = dateStr.match(/([0-9]{4}(?:-[0-9]{1,2})+)([T\s])((?:[0-9]{1,2}:)+[0-9]{1,2}\.[0-9]{1,3})(Z*)/);
if (matches) {
return `${matches[1]}T${matches[3]}${matches[4] ? matches[4] : "Z"}`;
}
return dateStr;
}

const theme = getTheme();
const { semanticColors } = theme;

Expand Down

0 comments on commit 492caf4

Please sign in to comment.