File tree Expand file tree Collapse file tree 4 files changed +37
-15
lines changed Expand file tree Collapse file tree 4 files changed +37
-15
lines changed Original file line number Diff line number Diff 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
1715import { ElasticsearchQuery , Logs , LogsSortDirection } from '../types' ;
1816
1917import { LogContextUI } from './components/LogContextUI' ;
18+ import { createContextTimeRange } from './utils' ;
2019
2120export 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-
4229export class LogContextProvider {
4330 datasource : BaseQuickwitDataSource ;
4431 contextQuery : string | null ;
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import { DatasourceContext } from "@/components/QueryEditor/ElasticsearchQueryCo
1111import { BaseQuickwitDataSource } from "@/datasource/base" ;
1212import { useDatasourceFields } from "@/datasource/utils" ;
1313import { 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 = [
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments