Skip to content

Commit

Permalink
Fix context range precision gap
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelemeny committed Apr 25, 2024
1 parent 9a35aae commit a8d8cab
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
15 changes: 1 addition & 14 deletions src/LogContext/LogContextProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import {
DataFrame,
DataQueryError,
DataQueryRequest,
dateTime,
LogRowModel,
rangeUtil,
TimeRange,
} from '@grafana/data';

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

import { LogContextUI } from './components/LogContextUI';
import { createContextTimeRange } from './utils';

export interface LogRowContextOptions {
direction?: LogRowContextQueryDirection;
Expand All @@ -27,18 +26,6 @@ export enum LogRowContextQueryDirection {
Forward = 'FORWARD',
}

export function createContextTimeRange(rowTimeEpochMs: number, direction?: LogRowContextQueryDirection): TimeRange {
const offset = 7;
const timeFrom = dateTime(rowTimeEpochMs)
const timeTo = dateTime(rowTimeEpochMs)

const timeRange = {
from: (direction === LogRowContextQueryDirection.Forward) ? timeFrom.utc() : timeFrom.subtract(offset, 'hours').utc(),
to: (direction === LogRowContextQueryDirection.Backward) ? timeTo.utc() : timeTo.add(offset, 'hours').utc(),
}
return { ...timeRange, raw:timeRange }
}

export class LogContextProvider {
datasource: BaseQuickwitDataSource;
contextQuery: string | null;
Expand Down
2 changes: 1 addition & 1 deletion src/LogContext/components/LogContextUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DatasourceContext } from "@/components/QueryEditor/ElasticsearchQueryCo
import { BaseQuickwitDataSource } from "@/datasource/base";
import { useDatasourceFields } from "@/datasource/utils";
import { Field, FieldContingency, Filter } from "../types";
import { createContextTimeRange } from "LogContext/LogContextProvider";
import { createContextTimeRange } from 'LogContext/utils';

// TODO : define sensible defaults here
// const excludedFields = [
Expand Down
15 changes: 15 additions & 0 deletions src/LogContext/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { LogRowContextQueryDirection } from "./LogContextProvider";
import { createContextTimeRange } from "./utils";


describe('Test LogContextProvider/utils:createContextTimeRange', () => {

it('Should produce a range overlapping target', () => {
const targetTimestampMicros = 1714062468704123
const targetTimestampMillis = 1714062468704
const range = createContextTimeRange(targetTimestampMillis, LogRowContextQueryDirection.Backward)

expect(range.from.toDate().getTime() * 1000).toBeLessThanOrEqual(targetTimestampMicros);
expect(range.to.toDate().getTime() * 1000).toBeGreaterThanOrEqual(targetTimestampMicros);
});
});
20 changes: 20 additions & 0 deletions src/LogContext/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { dateTime, TimeRange } from "@grafana/data";
import { LogRowContextQueryDirection } from './LogContextProvider';


export function createContextTimeRange(rowTimeEpochMs: number, direction?: LogRowContextQueryDirection): TimeRange {
const offset = 7;
let timeFrom = dateTime(rowTimeEpochMs);
let timeTo = dateTime(rowTimeEpochMs);

if (direction === LogRowContextQueryDirection.Backward) {
// Add 1 to avoid missing results due to precision gap
timeTo = dateTime(rowTimeEpochMs + 1);
}

const timeRange = {
from: (direction === LogRowContextQueryDirection.Forward) ? timeFrom.utc() : timeFrom.subtract(offset, 'hours').utc(),
to: (direction === LogRowContextQueryDirection.Backward) ? timeTo.utc() : timeTo.add(offset, 'hours').utc(),
};
return { ...timeRange, raw: timeRange };
}

0 comments on commit a8d8cab

Please sign in to comment.