diff --git a/web/src/utils/dashboard/convertDataIntoUnitValue.ts b/web/src/utils/dashboard/convertDataIntoUnitValue.ts index 863410390d..eac353e6b4 100644 --- a/web/src/utils/dashboard/convertDataIntoUnitValue.ts +++ b/web/src/utils/dashboard/convertDataIntoUnitValue.ts @@ -307,34 +307,6 @@ export const isTimeStamp = (sample: any, treatAsNonTimestamp: any) => { } }; -/** - * Converts 16-digit microsecond timestamps to readable date format - * @param value - The value to convert (can be single value or array) - * @returns The converted value(s) in date format - */ -export const convert16DigitTimestamp = (value: any): any => { - // If value is an array, convert each element - if (Array.isArray(value)) { - return value.map((item: any) => convert16DigitTimestamp(item)); - } - - // If value is a string or number, convert it - if (typeof value === "string" || typeof value === "number") { - const strValue = value.toString(); - const microsecondsPattern = /^\d{16}$/; - - if (microsecondsPattern.test(strValue)) { - // Convert microseconds to milliseconds (divide by 1000) - const milliseconds = parseInt(strValue) / 1000; - const date = new Date(milliseconds); - return formatDate(date); - } - } - - // Return original value if not a 16-digit timestamp - return value; -}; - export function convertOffsetToSeconds( offset: string, endISOTimestamp: number, diff --git a/web/src/utils/dashboard/convertTableData.ts b/web/src/utils/dashboard/convertTableData.ts index 78216481ff..a94f192d3b 100644 --- a/web/src/utils/dashboard/convertTableData.ts +++ b/web/src/utils/dashboard/convertTableData.ts @@ -21,7 +21,6 @@ import { getUnitValue, isTimeSeries, isTimeStamp, - convert16DigitTimestamp, } from "./convertDataIntoUnitValue"; const applyValueMapping = (value: any, mappings: any) => { @@ -226,17 +225,20 @@ export const convertTableData = ( return valueMapping; } - // Check if the value is a 16-digit timestamp and convert it - const convertedVal = convert16DigitTimestamp(val); + // Handle 16-digit microsecond timestamps + let timestamp; + if (typeof val === "string" && /^\d{16}$/.test(val)) { + // 16-digit string represents microseconds, convert to milliseconds + timestamp = parseInt(val) / 1000; + } else if (typeof val === "string") { + // Regular ISO string, append Z if needed + timestamp = `${val}Z`; + } else { + // Number or Date object + timestamp = new Date(val)?.getTime() / 1000; + } - return formatDate( - toZonedTime( - typeof convertedVal === "string" - ? `${convertedVal}Z` - : new Date(convertedVal)?.getTime() / 1000, - store.state.timezone, - ), - ); + return formatDate(toZonedTime(timestamp, store.state.timezone)); }; } return obj; @@ -378,17 +380,20 @@ export const convertTableData = ( return valueMapping; } - // Check if the value is a 16-digit timestamp and convert it - const convertedVal = convert16DigitTimestamp(val); + // Handle 16-digit microsecond timestamps + let timestamp; + if (typeof val === "string" && /^\d{16}$/.test(val)) { + // 16-digit string represents microseconds, convert to milliseconds + timestamp = parseInt(val) / 1000; + } else if (typeof val === "string") { + // Regular ISO string, append Z if needed + timestamp = `${val}Z`; + } else { + // Number or Date object + timestamp = new Date(val)?.getTime() / 1000; + } - return formatDate( - toZonedTime( - typeof convertedVal === "string" - ? `${convertedVal}Z` - : new Date(convertedVal)?.getTime() / 1000, - store.state.timezone, - ), - ); + return formatDate(toZonedTime(timestamp, store.state.timezone)); }; } @@ -402,14 +407,22 @@ export const convertTableData = ( (acc: any, curr: any, reduceIndex: any) => { const value = searchQueryData[0][reduceIndex][it.alias] ?? ""; acc[curr] = histogramFields.includes(it.alias) - ? formatDate( - toZonedTime( - typeof convert16DigitTimestamp(value) === "string" - ? `${convert16DigitTimestamp(value)}Z` - : new Date(convert16DigitTimestamp(value))?.getTime() / 1000, - store.state.timezone, - ), - ) + ? (() => { + // Handle 16-digit microsecond timestamps + let timestamp; + if (typeof value === "string" && /^\d{16}$/.test(value)) { + // 16-digit string represents microseconds, convert to milliseconds + timestamp = parseInt(value) / 1000; + } else if (typeof value === "string") { + // Regular ISO string, append Z if needed + timestamp = `${value}Z`; + } else { + // Number or Date object + timestamp = new Date(value)?.getTime() / 1000; + } + + return formatDate(toZonedTime(timestamp, store.state.timezone)); + })() : value; return acc; },