Skip to content

Commit 8afe6fb

Browse files
authored
Merge pull request #123 from quickwit-oss/ddelemeny/fix-context-range
Fix context range precision gap
2 parents 9a35aae + a8d8cab commit 8afe6fb

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

src/LogContext/LogContextProvider.ts

+1-14
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import {
88
DataFrame,
99
DataQueryError,
1010
DataQueryRequest,
11-
dateTime,
1211
LogRowModel,
1312
rangeUtil,
14-
TimeRange,
1513
} from '@grafana/data';
1614

1715
import { ElasticsearchQuery, Logs, LogsSortDirection} from '../types';
1816

1917
import { LogContextUI } from './components/LogContextUI';
18+
import { createContextTimeRange } from './utils';
2019

2120
export interface LogRowContextOptions {
2221
direction?: LogRowContextQueryDirection;
@@ -27,18 +26,6 @@ export enum LogRowContextQueryDirection {
2726
Forward = 'FORWARD',
2827
}
2928

30-
export function createContextTimeRange(rowTimeEpochMs: number, direction?: LogRowContextQueryDirection): TimeRange {
31-
const offset = 7;
32-
const timeFrom = dateTime(rowTimeEpochMs)
33-
const timeTo = dateTime(rowTimeEpochMs)
34-
35-
const timeRange = {
36-
from: (direction === LogRowContextQueryDirection.Forward) ? timeFrom.utc() : timeFrom.subtract(offset, 'hours').utc(),
37-
to: (direction === LogRowContextQueryDirection.Backward) ? timeTo.utc() : timeTo.add(offset, 'hours').utc(),
38-
}
39-
return { ...timeRange, raw:timeRange }
40-
}
41-
4229
export class LogContextProvider {
4330
datasource: BaseQuickwitDataSource;
4431
contextQuery: string | null;

src/LogContext/components/LogContextUI.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { DatasourceContext } from "@/components/QueryEditor/ElasticsearchQueryCo
1111
import { BaseQuickwitDataSource } from "@/datasource/base";
1212
import { useDatasourceFields } from "@/datasource/utils";
1313
import { Field, FieldContingency, Filter } from "../types";
14-
import { createContextTimeRange } from "LogContext/LogContextProvider";
14+
import { createContextTimeRange } from 'LogContext/utils';
1515

1616
// TODO : define sensible defaults here
1717
// const excludedFields = [

src/LogContext/utils.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { LogRowContextQueryDirection } from "./LogContextProvider";
2+
import { createContextTimeRange } from "./utils";
3+
4+
5+
describe('Test LogContextProvider/utils:createContextTimeRange', () => {
6+
7+
it('Should produce a range overlapping target', () => {
8+
const targetTimestampMicros = 1714062468704123
9+
const targetTimestampMillis = 1714062468704
10+
const range = createContextTimeRange(targetTimestampMillis, LogRowContextQueryDirection.Backward)
11+
12+
expect(range.from.toDate().getTime() * 1000).toBeLessThanOrEqual(targetTimestampMicros);
13+
expect(range.to.toDate().getTime() * 1000).toBeGreaterThanOrEqual(targetTimestampMicros);
14+
});
15+
});

src/LogContext/utils.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { dateTime, TimeRange } from "@grafana/data";
2+
import { LogRowContextQueryDirection } from './LogContextProvider';
3+
4+
5+
export function createContextTimeRange(rowTimeEpochMs: number, direction?: LogRowContextQueryDirection): TimeRange {
6+
const offset = 7;
7+
let timeFrom = dateTime(rowTimeEpochMs);
8+
let timeTo = dateTime(rowTimeEpochMs);
9+
10+
if (direction === LogRowContextQueryDirection.Backward) {
11+
// Add 1 to avoid missing results due to precision gap
12+
timeTo = dateTime(rowTimeEpochMs + 1);
13+
}
14+
15+
const timeRange = {
16+
from: (direction === LogRowContextQueryDirection.Forward) ? timeFrom.utc() : timeFrom.subtract(offset, 'hours').utc(),
17+
to: (direction === LogRowContextQueryDirection.Backward) ? timeTo.utc() : timeTo.add(offset, 'hours').utc(),
18+
};
19+
return { ...timeRange, raw: timeRange };
20+
}

0 commit comments

Comments
 (0)