diff --git a/public/app/features/logs/components/log-context/LogRowContextModal.tsx b/public/app/features/logs/components/log-context/LogRowContextModal.tsx index bcfec8fe92b..411b725c2fc 100644 --- a/public/app/features/logs/components/log-context/LogRowContextModal.tsx +++ b/public/app/features/logs/components/log-context/LogRowContextModal.tsx @@ -12,7 +12,8 @@ import { LogsDedupStrategy, LogsSortOrder, SelectableValue, - rangeUtil, + dateTime, + TimeRange, } from '@grafana/data'; import { config, reportInteraction } from '@grafana/runtime'; import { DataQuery, TimeZone } from '@grafana/schema'; @@ -23,6 +24,7 @@ import { splitOpen } from 'app/features/explore/state/main'; import { SETTINGS_KEYS } from 'app/features/explore/utils/logs'; import { useDispatch } from 'app/types'; +import { sortLogRows } from '../../utils'; import { LogRows } from '../LogRows'; import { LoadMoreOptions, LogContextButtons } from './LogContextButtons'; @@ -154,16 +156,26 @@ export const LogRowContextModal: React.FunctionComponent { const { before, after } = context; - const allRows = [...before, row, ...after].sort((a, b) => a.timeEpochMs - b.timeEpochMs); - const first = allRows[0]; - const last = allRows[allRows.length - 1]; - return rangeUtil.convertRawToRange( - { - from: first.timeUtc, - to: last.timeUtc, + const allRows = sortLogRows([...before, row, ...after], LogsSortOrder.Ascending); + const fromMs = allRows[0].timeEpochMs; + let toMs = allRows[allRows.length - 1].timeEpochMs; + // In case we have a lot of logs and from and to have same millisecond + // we add 1 millisecond to toMs to make sure we have a range + if (fromMs === toMs) { + toMs += 1; + } + const from = dateTime(fromMs); + const to = dateTime(toMs); + + const range: TimeRange = { + from, + to, + raw: { + from, + to, }, - 'utc' - ); + }; + return range; }, [context, row]); const onChangeLimitOption = (option: SelectableValue) => {