File tree 4 files changed +37
-15
lines changed
4 files changed +37
-15
lines changed Original file line number Diff line number Diff line change @@ -8,15 +8,14 @@ import {
8
8
DataFrame ,
9
9
DataQueryError ,
10
10
DataQueryRequest ,
11
- dateTime ,
12
11
LogRowModel ,
13
12
rangeUtil ,
14
- TimeRange ,
15
13
} from '@grafana/data' ;
16
14
17
15
import { ElasticsearchQuery , Logs , LogsSortDirection } from '../types' ;
18
16
19
17
import { LogContextUI } from './components/LogContextUI' ;
18
+ import { createContextTimeRange } from './utils' ;
20
19
21
20
export interface LogRowContextOptions {
22
21
direction ?: LogRowContextQueryDirection ;
@@ -27,18 +26,6 @@ export enum LogRowContextQueryDirection {
27
26
Forward = 'FORWARD' ,
28
27
}
29
28
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
-
42
29
export class LogContextProvider {
43
30
datasource : BaseQuickwitDataSource ;
44
31
contextQuery : string | null ;
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import { DatasourceContext } from "@/components/QueryEditor/ElasticsearchQueryCo
11
11
import { BaseQuickwitDataSource } from "@/datasource/base" ;
12
12
import { useDatasourceFields } from "@/datasource/utils" ;
13
13
import { Field , FieldContingency , Filter } from "../types" ;
14
- import { createContextTimeRange } from " LogContext/LogContextProvider" ;
14
+ import { createContextTimeRange } from ' LogContext/utils' ;
15
15
16
16
// TODO : define sensible defaults here
17
17
// 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